pic16f877 microcontroller using TMR0

First, the structure

1, the rising edge of the clock signal is a 8-bit wide trigger accumulation cycle count register

2, there is a dedicated external trigger signal input terminal ( the T0CKI )

3, is a storage area within the unified addressing the register file, address 01H or 101H, users can directly use software to read / write the contents of the counter

4, having a software programmable 8-bit prescaler

Second, characteristics

1, internal trigger signal, i.e., the instruction cycle time as a clock signal source, TMR0 module operates in timed manner to a fixed trigger rising edge trigger is valid. When the counter overflows, the overflow interrupt flag corresponding T0IF automatically set, overflow interrupt

2, when the external clock signal source, working in the TMR0 counting mode, the program may be arranged to trigger the trigger rising or falling edge active. When the counter overflows, the overflow interrupt can be generated.

Third, the relevant register

1, the timer / counter register the TMR0 , bit accumulate. 8

2, option register OPTION_REG

3, the interrupt control register INTCON

4, the port direction register TRAISA (RA4 as an external trigger port)

Fourth, use

1, the query method

1  void main ()
 2  { 
 . 3      unsigned int NUM;
 . 4      / * 
. 5      Prescaler 1: 256, 256-61 = 195 assignment 61 is
 . 6      * / 
. 7      the OPTION = 0x70 ; // prescaler 
. 8      the TMR0 = 61 is ; // counter / timer register assigned equal to about 50ms 
. 9          
10      the while ( . 1 )
 . 11      {
 12 is          IF ( . 1 == The T0IF) // Analyzing overflow 
13 is          {
 14              The T0IF = 0 ;// flag is cleared 
15              NUM ++ ;
 16              IF ( 20 is == NUM) // lS 
. 17              {
 18 is                  NUM = 0 ;
 . 19                  // Function 1 
20 is                  {
 21 is                      
22 is                  }
 23              }
 24          }
 25      }
 26 }

 

2, interruptus

Used to register INTCON

1  void main ()
 2  { 
 . 3      unsigned int NUM;
 . 4      / * 
. 5      Prescaler 1: 256, 256-61 = 195 assignment 61 is
 . 6      * / 
. 7      the OPTION = 0x70 ; // prescaler 
. 8      the INTCON = 0xA0 ; // open global interrupt; open timer interrupt 
. 9      the TMR0 = 61 is ; // counter / timer register assignment is approximately equal to 50ms 
10          
. 11      the while ( . 1 )
 12 is      {
 13 is          IF ( 20 is == NUM)
 14             = NUM 0 ; // count clear
 15          // Function. 1 
16          {
 . 17              
18 is          }
 . 19      }
 20 is  }
 21 is  
22 is  void interrupt Timer0 ()      // interrupt function does not need to call 
23  {
 24      // note enters the interrupt T0IF already set 
25      = The T0IF 0 ; // flag is cleared 
26 is      the TMR0 = 61 is ;
 27      NUM ++ ;
 28 }

 

Guess you like

Origin www.cnblogs.com/jnkdog/p/11141132.html