Các anh ơi xem giúp em cái code này,em viết để điều khiển led 7 đoạn hiển thị 4 chứ số,khi nhấn nút thì tăng hoặc giảm 1 đơn vị,nhưng mà sao code sửa hoài k ra,xem giúp em,em cảm ơn.
//////////////////////////
#include <mega8.h>
#include <delay.h>
#define CONTROL PORTB
#define DATA PORTC
#define Button PINA
char key, pr, flag, i, j;
unsigned int number;
char temp[4];
// MA LED 7 DOAN CATHODE CHUNG
char seven_seg_code[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6 F};
// HAM TACH CAC KY SO TRONG CHU SO CAN HIEN THI
void Num2Digit(unsigned int in_number)
{
temp[3] = in_number/1000;
temp[2] = (in_number%1000)/100;
temp[1] = ((in_number%1000)%100)/10;
temp[0] = ((in_number%1000)%100)%10;
return;
}
// HAM QUET LED 7 DOAN
void Seven_Seg_Scan()
{
for (i=0;i<=3;i++){
// TRA CUU KY SO CHUYEN THANH MA LED
DATA=seven_seg_code[temp[i]];
CONTROL=~0x08>>i;
delay_ms(1);
CONTROL=0xFF;
}
}
//HAM DOC PHIM - TRA VE VI TRI PHIM DUOC AN - MUC TAC DONG PHIM LA THAP
char Button_Read()
{
if(Button(&PINA,0,1,0))
return 1;
if(Button(&PINA,1,1,0))
return 2;
return 0;
}
//HAM DOC PHIM VA CHONG DOI
char Button_Read_Debounce()
{
char temp;
temp=Button_Read();
if(temp==0)
{
if(pr==1)
{
pr=0;
return;
}
else
{
flag=1;
return;
}
}
else
{
delay_ms(5);
temp=Button_Read();
if(temp==0) return;
else
{
pr=1;
return temp;
}
}
}
// Declare your global variables here
void main(void)
{
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
Num2Digit(number);
Seven_Seg_Scan();
key= Button_Read_Debounce();
if((pr==1)&&(flag==1)){
flag=0;
switch(key){
case 1:number++;
// GIOI HAN GIA TRI TRONG 4 CHU SO
if(number==10000) number=0;
break;
case 2:number--;
// GIOI HAN GIA TRI TRONG 4 CHU SO
if(number>=9999) number=9999;
break;
}
}
};
}
///////////////////////////////////////////
//////////////////////////
#include <mega8.h>
#include <delay.h>
#define CONTROL PORTB
#define DATA PORTC
#define Button PINA
char key, pr, flag, i, j;
unsigned int number;
char temp[4];
// MA LED 7 DOAN CATHODE CHUNG
char seven_seg_code[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6 F};
// HAM TACH CAC KY SO TRONG CHU SO CAN HIEN THI
void Num2Digit(unsigned int in_number)
{
temp[3] = in_number/1000;
temp[2] = (in_number%1000)/100;
temp[1] = ((in_number%1000)%100)/10;
temp[0] = ((in_number%1000)%100)%10;
return;
}
// HAM QUET LED 7 DOAN
void Seven_Seg_Scan()
{
for (i=0;i<=3;i++){
// TRA CUU KY SO CHUYEN THANH MA LED
DATA=seven_seg_code[temp[i]];
CONTROL=~0x08>>i;
delay_ms(1);
CONTROL=0xFF;
}
}
//HAM DOC PHIM - TRA VE VI TRI PHIM DUOC AN - MUC TAC DONG PHIM LA THAP
char Button_Read()
{
if(Button(&PINA,0,1,0))
return 1;
if(Button(&PINA,1,1,0))
return 2;
return 0;
}
//HAM DOC PHIM VA CHONG DOI
char Button_Read_Debounce()
{
char temp;
temp=Button_Read();
if(temp==0)
{
if(pr==1)
{
pr=0;
return;
}
else
{
flag=1;
return;
}
}
else
{
delay_ms(5);
temp=Button_Read();
if(temp==0) return;
else
{
pr=1;
return temp;
}
}
}
// Declare your global variables here
void main(void)
{
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
Num2Digit(number);
Seven_Seg_Scan();
key= Button_Read_Debounce();
if((pr==1)&&(flag==1)){
flag=0;
switch(key){
case 1:number++;
// GIOI HAN GIA TRI TRONG 4 CHU SO
if(number==10000) number=0;
break;
case 2:number--;
// GIOI HAN GIA TRI TRONG 4 CHU SO
if(number>=9999) number=9999;
break;
}
}
};
}
///////////////////////////////////////////
Comment