We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Does anybody know how to set 8051 timer to sleep for a few second and then wake up by itself?
With a normal crystal speed, any 8051 timer will expire in much less than a few seconds. However, you can set up a timer ISR that will periodically decrement a counter after checking that it is not already zero. To make an idle loop, the main program will simply set the counter to the required value and then go into the idle mode. The CPU will come out of the idle mode when there is an interrupt (the first thing to be executed will be the ISR). The idle loop must next check whether the counter is zero; if it is, drop out of the idle loop; if it is not, loop round and go back into idle mode.
Take a look at the following: http://www.keil.com/download/docs/8051fx_idle.zip.asp Jon
in line 27 of the code 8051fx_idle.zip, what does TMOD &= ~0x0F; /* 16-bit, no prescale mode */ mean?
The statement clears the low four bits of the TMOD register, while leaving the upper four bits unchanged. The comment would indicate that this is intended to put timer 0 into 16-bit timer mode, without any prescaling. You could check the manual for the chip to see what clearing those bits really does on this part. According to the documentation for my variant, setting the low two bits of TMOD to 0 makes Timer 0 a 13-bit timer (or 8-bit timer with a 5-bit prescaler, if you prefer). Perhaps this code is correct for an 8051fx, or perhaps the comment is wrong. You'll have to check the documentation to be sure. (And try an experiment yourself really to be certain!)
Well, it probably helps to show the whole section of code so that the comment really makes sense.
/*------------------------------------------------ Setup TIMER0 to generate a regular interupt. ------------------------------------------------*/ TR0 = 0; /* Stop Timer 0 */ TMOD &= ~0x0F; /* 16-bit, no prescale mode */ TMOD |= 0x01; TL0 = 0; /* Set T0 */ TH0 = 0; PT0 = 0; /* Low Priority Interrupt */ ET0 = 1; /* Enable Timer0 Interrupt */ TR0 = 1; /* Start Timer 0 */ EA = 1; /* Enable Global Interrupts */