AVR microcontroller TC0 timer overflow routine

Publisher:MysticalGardenLatest update time:2015-10-26 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The program generation wizard configures TC0, overflow interrupt, and generates 1ms/20ms delay.

Timing formula: Time = PRE*(MAX-TCNT0+1)/F_cpu unit S, where PRE is the frequency division number

#include
#include
void port_init(void)
{
PORTA = 0xFF;
DDRA   = 0x00;
PORTB = 0xFF;
DDRB   = 0xFF;
PORTC = 0xFF; //m103 output only
DDRC   = 0x00;
PORTD = 0xFF;
DDRD   = 0x00;
}
//TIMER0 initialize - prescale:8/256
// WGM: Normal
// desired value: 1mSec/20ms
// actual value:   1.000mSec (0.0%)/19.968ms(0.16%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
//TCNT0 = 0x83; //set count T=PRE*(MAX-TCNT0+1)/F_cpu=8*(255-130)/1MHz=1ms
TCNT0 = 0xB2; //set count T=PRE*(MAX-TCNT0+1)/F_cpu=256*( 255-178+1)/1MHz=19.968ms
OCR0   = 0x7D;  // Not used in  set compare  program //TCCR0 = 0x02; //start timer  8 division TCCR0 = 0x04; //start timer  256 division } #pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF void timer0_ovf_isr(void) { //TCNT0 = 0x83; //reload counter value Reload TCNT0 to make TC0 count repeatedly from 0x83-0xff TCNT0 = 0xB2; //reload counter value Reload TCNT0 to make TC0 repeat counting from 0xB2-0xff PORTB^=BIT(7)|BIT(6)|BIT(5); //Flip PB6/PB7 ports to achieve 1ms for two LED lights Interval light off } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer0_init(); MCUCR = 0x00; GICR  = 0x00; TIMSK = 0x01; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } // void main(void) { init_devices(); //insert your functional code here.. . }   
 
 
















 










Keywords:AVR Reference address:AVR microcontroller TC0 timer overflow routine

Previous article:This is the AVR watchdog timeout
Next article:12864 LCD (AVR_Mega128) header file

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号