We haven't gotten over the AVR for a long time. This time, we are going to improve the school's low-level robot driver. First, we will do a few necessary small experiments.
Both AVR and ucoⅱ are good things ~~, The compact and fast running speed reminds people of the previous dual-wheeled car made by a foreigner using AVR as the CPU. This is really exciting ..
// Environment: winavr + AVR Studio
Char temp = 0;
ISR (timer1_compa_vect) // interrupt function
{
// User code here
Temp ++;
If (temp = 10)
Temp = 0;
Porta = temp;
Ddra = 0xff;
}
Void mytimer_init (void)
{
// Total disconnection
CLI ();
// Tccr1a is the output mode of the configuration pin. The default value here is 0, which is not connected. Therefore, you do not need to assign a value to tccr1a.
// T/C1 clock source is CLK/64, CLK is now 4 MHz internal crystal oscillator, so the clock is 4,000,000/64Hz = 62500Hz
Tccr1b & = ~ 0x07;
Tccr1b | = 0x03;
// Select the wgm10 ~ Wgm13
Tccr1a & = ~ 0x03;
Tccr1b & = ~ (0x03 <3); // Mode 4 CTC
Tccr1b | = 0x01 <3;
// Assign a value with an initial value of 16 digits
Tcnt1 = 0;
// Assign a value to compare the register top Initial Value of 16 bits. In the above working mode, when tcnt1 = ocr1a, tcnt1 is automatically cleared to 0
Ocr1a = 62500; // One interruption per second
// Enable the timer t/C1 overflow interrupt, and other t/C1 interruptions are blocked
Timsk & = ~ (0xf0 <2 );
Timsk | = (0x01 <4 );
Etimsk & = ~ 0x01;
// Clear the interrupt overflow flag. It will be automatically cleared after the interrupt is entered. You do not need to clear it later.
TIFR | = 1 <2;
// Start the timer
Sfior & = 0x7e;
// Total disconnection
SEI ();
}