Hardware Reference
In-Depth Information
{
unsigned char count;
DDRB 5 0xFF;
/* configure Port b for output */
count 5 0;
PTB 5 count;
while(1) {
delayby1ms(200);
count 11 ;
PTB 5 count;
}
}
void delayby1ms(int k)
{
int ix;
TSCR1 5 0x90;
/* enable TCNT and fast timer flag clear */
TSCR2 5 0x03;
/* disable timer interrupt, set prescaler to 8*/
TIOS | 5 OC0;
/* enable OC0 */
TC0 5 TCNT 1 3000;
for(ix 5 0; ix < k; ix 11 ) {
while(!(TFLG1 & C0F));
TC0 15 3000;
}
TIOS & 5 , OC0;
/* disable OC0 */
}
Example 8.6
Generate a sequence of pulses using an OC function. Write a program to generate a number
of pulses with the specified high interval duration (12 ms) and low interval duration (8 ms). Use
the interrupt-driven approach so that the CPU can perform other operations.
Solution: Let the number of pulses to be generated, the high interval duration, low interval
duration, the flag to select DelayHi or DelayLo, and number of OC0 operations to be performed
be NN , DelayHi , DelayLo , HiorLo , and pcnt, respectively.
The algorithm for generating a sequence of pulses with the specified duration of high inter-
val and low interval is as follows:
Step 1:
Pull the PT0 pin high quickly using the OC0 operation.
Step 2
Change the OC0 pin action to toggle. Start the next OC0 operation with delay equal to
DelayHi .
Step 3
pcnt R 2 * NN 2 1. HiorLo R 0.
Step 4
Enable OC0 interrupt.
 
Search WWH ::




Custom Search