简体   繁体   中英

microcontroller TMR0 timer counter interrupt

I am programming the microcontroller PIC16F676 SPI interface with MCP2515. It will set a flag in every 224ms, and timercounter will increase from 0*F8 to 0*FF then overflow to set this flag. Therefore, 32ms * 07H = 224ms. The question is how to let the timer interrupt every 32ms,WHERE this 32ms comes from.

//Timer interrupts every 32ms and set a flag every 224ms (32ms * 07H = 224ms)

//Initial value = FFH - 07H = F8H


if(T0IF)     //TMR0 overflow interrupt flag bit

{

  TimerCounter++;

  if(!TimerCounter)  //if rolled over, set flag. Main will handle the rest.

  {

     TimerCounter = 0*F8;

     gSampleFlag = 1;

  }

  T0IF = 0; //reset for next flag

}

The 32 ms period of the timer is determined by the configuration of the timer, which is not included in your code excerpt (ie, it may be done elsewhere in your code). Read section 4.0 of the PIC16F630/676 datasheet , which explains the TIMER0 Module.

Timer0 is configured as follows:

  • T0CS is either:
    • cleared to select the internal clock source (Timer mode) for Timer0,
    • or set to select the external clock source (Counter mode).
  • T0IE is set to enable the Timer0 interrupt.
  • The prescaler may be used in conjunction with the internal clock source to adjust the tick rate of Timer0
    • PSA is cleared to assign the prescaler to Timer0.
    • PS2:PS0 are set to select the prescaler rate.

So either the external clock source or the internal clock source and prescaler determine the tick rate of Timer0.

32ms is the time period of clock source which your timer counter is counting for 0x07 times.

Your timer unit is synchronised with common clock source which is derived from either external crystal or internal oscillator. During clock configuration you need to decide what should be peripheral bus clock through which your timer unit is interfaced. While configuring timer unit you can further divide this peripheral clock to less frequency using prescaler to increase the range of period.

Now suppose your peripheral bus clock frequency is 1MHz and your prescaler is 1, your counter will increment or decrement every 1us and for 0x07 count, it will generate only 7us of period. In your example you need to set prescaler 32000 (if it allowed to set) as reference clock for counting so that 1 count means 32ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM