How to Use the ATmega128 timer 1

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.