AVR microcontroller timer 0 overflow interrupt program

Publisher:大酉幽华1Latest update time:2015-05-11 Source: 51heiKeywords:avr Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
       February 17, 2014 22:26:32   

       Chip name: AVR (Atmel) mega16A microcontroller chip; 
       Development board: self-developed YF-A1 chip development board; (YF: is the abbreviation of my name)

        In the 53rd test, I used a logic analyzer to capture the digital pulse signal output from the IO port. The pulse signal width was 1.5~1.8 microseconds. In the signal timing of every 9~11 pulses with a width of 1.5us, there was a high pulse signal with a pulse width of 3 microseconds.
       I spent several hours and couldn't calculate the internal clock frequency of timer 0... What went wrong?
      An external 12-mega crystal oscillator was used;
      my calculation conclusion was: the internal frequency of timer 0 was 32khz, but this was obviously problematic.
      The timer clock was internally divided by 8 TCCR0=0X02; the initial value of TCNTO was 250, that is, it overflowed once every 6 clock cycles, and every time it overflowed, the PB port level was inverted once, so the reverse calculation pulse width 1.5us ÷ 6 == each divided clock time ÷ 8 = clock frequency;

/*    
    It took me more than 3 hours to write this program, and I experimented repeatedly. God will not let down those who work hard,, hey╮(╯▽╰)╭,,,,,

       Program name: Timer 0 overflow interrupt

    Overview: The timer overflows once every millisecond. If the timer overflows 500 times, that is, 0.5 seconds, the LED light will jump once. I found that if
  all IO ports are not initialized, the chip will become extremely unstable and cannot work properly. Once the timer 0 overflow flag
  TOV0 overflows, it will be automatically cleared after entering the interrupt service program, and there is no need for software clearing. I seem to understand the meaning of
  hardware clearing;

       Finish time: February 28, 2014 20:40:21;

*/


#include
#include
#include

typedef unsigned char uint8;
typedef unsigned int uint16;

volatile uint16 a=0;

void io(void)
{
  DDRA=0XFF;
  PORTA=0X00;

  DDRB=0XFF;
  PORTB=0X00;

  DDRC=0XFF;
  PORTC=0X00;

  DDRD=0XFF;
  PORTD=0X00;


}

 

int main(void)
{

   io();
   
   TIMSK=0X01; // Overflow interrupt enable;

   sei(); //Global interrupt enable;

   TCNT0=69; //Timer initial value 1 millisecond overflow;

   TCCR0=3; //64-division timer starts running!;

   PORTB=4; //8-bit LED common anode enable;
  

   while(1); //infinite loop;
 

 

}

 

SIGNAL(SIG_OVERFLOW0) //Interrupt service routine;
{
   static uint16 s=0; //Static variable;
   
   TCNT0=69; //Reset the initial value of the timer;
 
    s++;       
   if(s==502) //The PA port level changes once every 500 milliseconds!
   { s=0;
     PORTA^=0XFF;
     
}
  
  
 // TIFR=0X01;//Clear the overflow flag and compare match flag of timer 0;

}

Keywords:avr Reference address:AVR microcontroller timer 0 overflow interrupt program

Previous article:High-speed AVR realizes vibration/temperature signal acquisition and storage
Next article:AVR microcontroller 8-bit timer LED accurate 1 second flashing program

Latest Microcontroller Articles
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号