tawny
Newbie
Hello everyone,
I'm doing a project that is almost finished, I just have one small problem. Days have gone by and I can't seem to solve it. I'm using uvision from keil to program and an AT89S51 (11,0592MHz crystal).
I have 2 leds, one green (P2_2), and one red (P2_3). I'm using a UART connection to ask the user for a code and read his answer, if the code is right the green led should turn on for 5 seconds and if it's wrong the red led should be on for 2 seconds. The problem is the leds do switch on when they should, but they never go off after that.
I'm using Timer 0 for this and I wrote the following code:
I'm doing a project that is almost finished, I just have one small problem. Days have gone by and I can't seem to solve it. I'm using uvision from keil to program and an AT89S51 (11,0592MHz crystal).
I have 2 leds, one green (P2_2), and one red (P2_3). I'm using a UART connection to ask the user for a code and read his answer, if the code is right the green led should turn on for 5 seconds and if it's wrong the red led should be on for 2 seconds. The problem is the leds do switch on when they should, but they never go off after that.
I'm using Timer 0 for this and I wrote the following code:
C:
unsigned int aux1=0;
unsigned int aux2=0;
void counter()
{
TMOD=0x01; //Timer0 mode 1
TH0=0x4C; //for 50ms
TL0=0X00;
TR0=1; //start timer
}
// Configuration for the Interrupt of Timer0
void configInterrupt(){
EA=1; //activate interrupts on 8051
ET0=1; //activate interrupt of Timer0
}
void interruptTimer0Routine() interrupt 1{
TH0=0x4C; //everytime there's overflow, we reload the value
TL0=0X00;
aux1++;
aux2++;
}
void main(){
counter();
configInterrupt();
P2_2=0;
P2_3=0;
while(1){
if(answerIsRight){
P2_2=1; //turn on green led
if(aux1==100){ //overflow 100 times 50ms*100=5s
P2_2=0; //turn off green led
aux1=0;
}
}
else{
P2_3=1; //turn on red led
if(aux2==40){ //overflow 40 times 50ms*40=2s
P2_3=0; //turn off red led
aux2=0;
}
}
}
}
Last edited: