IAR For AVR Timer Overflow Interrupt (Usage Summary)

Publisher:AlisallllLatest update time:2016-10-06 Source: eefocusKeywords:IAR  AVR  timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
       Regarding overflow interrupt, no matter which microcontroller it is, it will continue to accumulate, causing its register to overflow and trigger an interrupt, and then jump to the interrupt function to execute the interrupt service program. Setting the initial value of the timer can deepen the understanding of the working principle of the timer.

       ATMega16 has two types of timers, 8-bit and 16-bit. When they overflow is fixed, that is, an interrupt will be generated when the maximum value of their counting range is reached. The maximum counting range of the 8-bit timer is 0~256 (2 to the 8th power), which means it will generate an interrupt when the count reaches 256. The maximum counting range of the 16-bit timer is 0~65536 (2 to the 16th power), which means it will generate an interrupt when the count reaches 65536.

     What we call the initial counting value is to set where the timer starts counting. Take an 8-bit timer as an example: the initial value is 100, so the timer starts to accumulate from 100, accumulates 156 times, and generates an interrupt after adding to 256. This is the time consumed in the middle and the instruction cycle is the time we want to set; for example: the initial value is 200, so the timer starts to accumulate from 200, accumulates 56 times, and generates an interrupt after adding to 256. It can be seen that the first timing will only be interrupted after accumulating 156 times, while the second time will be interrupted as long as it accumulates 56 times. Obviously, the time set for the first time is longer than the second time.

      Timers can not only set time, but we often use timers when we need precise timing. We can calculate how long it will take for the initial value we set to enter the interrupt.

The following are the registers needed when setting the 8-bit timer:

      

  IAR For AVR Timer Overflow Interrupt (Usage Summary) - wanghengzhi@126 - Code Beans
  IAR For AVR Timer Overflow Interrupt (Usage Summary) - wanghengzhi@126 - Code Beans
 

 

 

  IAR For AVR Timer Overflow Interrupt (Usage Summary) - wanghengzhi@126 - Code Beans
  IAR For AVR Timer Overflow Interrupt (Usage Summary) - wanghengzhi@126 - Code Beans


Experimental platform: ATMega16    

Crystal: 11.0592 MHz

Calculation of initial value:

                     1, 11059200 / 1024 = 10800 Set the frequency to 1024 times, and get how many accumulations are needed per second

                     2. 10800 / 100 = 108. How many accumulations are needed to get a timing of 10ms?

                     3, 256 - 108 = 148. Subtract the time to be accumulated from the maximum value of the calculation range to get the initial value, that is, where to start accumulating so that the time is 10ms when overflow occurs.        

                     4, 148 <==> 0x94 Get the hexadecimal value and assign it to TCNT0

Experimental code: Timing 10ms

#include
unsigned char flag = 0;
void timer_init(void)
{
TCCR0 = 0x05; //1024 division

TCNT0 = 0x94; //Assign initial value to count
  
TIMSK_TOIE0 = 1; //Enable
SREG_I = 1; //Open total interrupt
}
#pragma vector = TIMER0_OVF_vect
__interrupt void time0_normal(void)
{
    TCNT0 = 0x94; //Re-assign initial value
    flag++;
}
void main(void)
{
    timer_init();
    DDRB_Bit0 = 1;
    while(1)
    {
      if(flag == 100) //10ms repeated 100 times, that is 1 second
      {
        PORTB_Bit0 = ~PORTB_Bit0; //Make LED flash
        flag = 0;
      }
    }
}
      

//++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++//

Experimental platform: ATMega16

Crystal: 11.0592

16-bit timer initial value setting:

         1, 11059200 / 256 = 43200 Set the frequency division to 256 times to get the number of accumulations required per second

         2. 65536 - 43200 = 22336. Subtract the time to be accumulated from the maximum value of the calculation range to get the initial value, that is, where to start accumulating so that the time is 1s when overflow occurs.

         3, 22336 <==> 0x57 0x40 Get the hexadecimal value and assign it to TCNT1H and TCNT1L

Experimental code: Timing 1s

#include
unsigned char flag = 0;
void timer_init(void)
{
TCCR1B = 0x04;

TCNT1H = 0x57; 
TCNT1L = 0x40;

TIMSK_TOIE1 = 1;
SREG_I = 1;
}
#pragma vector = TIMER1_OVF_vect
__interrupt void time1_normal(void )
{
TCNT1H = 0x57; 
TCNT1L = 0x40;

    flag++;
}
void main(void)
{
    timer_init();
    DDRB_Bit0 = 1;
    while(1)
    {
      if(flag == 1)
      {
        PORTB_Bit0 = ~PORTB_Bit0;
        flag = 0;
      }
    }
}

Keywords:IAR  AVR  timer Reference address:IAR For AVR Timer Overflow Interrupt (Usage Summary)

Previous article:IAR For AVR Serial Port Interrupt Receive
Next article:IAR For AVR Precision Delay

Recommended ReadingLatest update time:2024-11-16 18:09

AVR MCU ISP (In-System Programming) and Fuse Repair Method (DebugWIRE)
Parallel programming, the earliest programming method, has the most powerful functions, but requires connecting more pins and usually requires a high voltage of 12V~24V. To distinguish between them, it is called high voltage parallel programming below.  ISP (In System Programmability) is in-system programming, also k
[Microcontroller]
Building your own AVR RTOS (1) - Function execution
Since 2003, the enthusiasm for learning and applying RTOS of microcontrollers has been growing stronger and stronger. In 2003, before leaving campus, during the SARS months, I bought Shao Beibei's "UCOSII" at the back door of South China Normal University. I read it several times, but without the experimental equipment
[Microcontroller]
AVR automotive microcontroller ISP considerations
The isp made a brief summary. Through online programming (the chip is programmed by continuously resetting the chip while the high voltage is converted), the MCU's flash, eeprom, fuse bits, encryption bits, etc. can be modified; the download line supports AVR microcontrollers with a clock above 8kHz and a voltage betw
[Microcontroller]
AVR automotive microcontroller ISP considerations
R&S new version of field strength and power calculation
The Field Strength & Power Estimator iPhone app designed by Rohde & Schwarz (R&S) estimates the power flux density, the transmitted power of the electric and magnetic fields, and the gain of the transmitting antenna and the associated frequency. In addition, the gain of the receiving antenna can be used to calc
[Mobile phone portable]
AVR M16 MCU NRF2401 Wireless Communication Development Kit
The microcontroller source program is as follows: //ICC-AVR application builder :  // Target : M16 // Crystal: 8.0000Mhz #include iom16v.h #include macros.h //------------------------------------------------------------------------------ //--------------------------------NRF24L01 interface definition---------------
[Microcontroller]
OnePlus 10R may be equipped with MediaTek's Dimensity 9000 chipset
      In the future development strategy announced at the end of last year, OnePlus stated that its digital series will continue to follow the high-end flagship route; the R series models (including OnePlus 9R and OnePlus 9RT) are mainly aimed at regional markets; the Nord and Nord CE series focus on the mid-range mar
[Mobile phone portable]
Summary and analysis of common problems in AVR microcontroller C programming
I have been using AVR microcontrollers to do projects these days. This time, I am learning in depth while doing it, and I try to use the knowledge that I have ignored before, such as pointers, file inclusion, conditional compilation, variable scope, etc. I have learned a lot and realized again that there is no useless
[Microcontroller]
Summary and analysis of common problems in AVR microcontroller C programming
DS1302 Operation Program of AVR Microcontroller
Become a prodigal son //mega16 7.3728MHz quartz crystal iccavr6.31a //Related definitions: #define uint unsigned int #define uchar unsigned char #define DS1302_RST 0 //pc0 #define DS1302_SDA 1 //pc1 #define DS1302_SCLK 6 //pc6 //ds1302 related //DS1302_RST=1 #define Set_DS1302
[Microcontroller]
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号