×
单片机 > 单片机程序设计 > 详情

PIC16F877A的timer

发布时间:2020-06-08 发布时间:
|
  1. ----------------------------------------------------------timer 0----------------------------------------------------------

     

    TMR0为8位宽,有一个可选的预分频器,用于通用目的,可用于定时和计数。

    TMR1为16位宽,附带一个可编程的预分频器和一个可选的低频时基振荡器,适合与CPP模块配合使用来实现输入捕扣或输出比较功能,也可于定时和计数。

    TMR2为8位宽,附带一个配合使用来实现PWM脉冲宽度调制信号的产生,只能用于定时。

     

    TMR0用作定时器时,定时器时钟=系统时钟/4; 
    写TMR0时,会产生2个周期的的延时,如果不使用前置分频器,可通过设置 
    初值进行补偿。 
    例如 需要定时200个时钟周期时,TMR0 = 256 -200 + 2= 58

     

    OPTION_REG:

    bit5 T0CS:TMR0 Clock Source Select bit

           1 = Transition on T0CKI pin

           0 = Internal instruction cycle clock(CLKO)

    bit 4 T0SE:TMR0 Source Edge Select bit

           1 = Increment on high-to-low transition on T0CKI pin

           0 = Increment on low-to-high transition on T0CKI pin

    bit 3 PSA:Prescaler Assignment bit

          1 = Prescaler is assigned to the WDT

          0 = Prescaler is assigned to the Timer0 module

    bit2-0 PS2:PS0:Prescaler Rate Select bits

     

    任何时候对TMR0寄存器进行一次写操作后,其计数功能将被禁止2个指令周期。

    给大家一个意见:如果想用TMR0实现精确的定时,一旦用了预分频器后就不要对TMR0做任何写操作。

     

    注意:在中断服务程序中查询TMR0中断时,既要查询T0IE位,还要查询T0IF位。

    uint16 count = 0; 
    void main(void) 

        PSA = 1;//prescaler is assigned to the WDT 
        T0CS  = 0;//Internal instruction cycle clock

        TMR0 = 58;//timer 200 cycle clock

        GIE = 1;//global interrupt enable 
        T0IE = 1;//TMR0 interrupt enable

        PORTD = 0x00; 
        TRISD = 0x00;

        while(1) 
        {} 
    }

    void interrupt ISR(void) 

        if(T0IE && T0IF) 
        { 
            T0IF = 0; 
            count++; 
            if(count ==15530)  // 大于1s左右,led闪 烁一次。 
            { 
            count = 0; 
            PORTD = ~PORTD; 
            } 
            TMR0 = TMR0 + 58; 
        } 
    }

     ---------------------------------------------timer1--------------------------------------

  2. Title:PIC16F877A TIMER1计数操作 
    Author:hnrain 
    Date:2010-12-28

    使用前置分频器 
    T1CKPS1  T1CKPS1   
       0        0        1  分频  TMR1时钟为晶振时钟/(4*1) 
       0        1        2  分频  TMR1时钟为晶振时钟/(4*2) 
       1        0        4  分频  TMR1时钟为晶振时钟/(4*4) 
       1        1        8  分频  TMR1时钟为晶振时钟/(4*8) 
       TMR1是16位宽度的TMR1由2个8位的可读写的寄存器TMR1H和TMR1L组成。

    TMR1有专门的启停控制位TMR1ON,通过软件可以任意启动或暂停TMR1计数功能。

    T1CON:TIMER1 CONTROL REGISTER

    bit7-6 unimplemented :Read as ‘0’

    bit5-4 T1CKPS1:T1CKPS0:Timer1 input Clock Prescale Select bits

              11=1:8 prescale value

              10=1:4 prescale value

              01=1:2 prescale value

              00=1:1 prescale value

    bit3   T1OSCEN:Timer1 Oscillator Enable Control bit

             1 = Oscillator is enable

             0 = Oscillator is shut-off

    bit2 T1SYNC:Timer1 External Clock Input Synchronization Control bit

            when TMR1CS = 1

            1= Do not synchronize external clock input

            0= Synchronize external clock input

            when TMR1CS = 0

            This bit is ignored .Timer1 uses the internal clock when TMR1CS = 0.

    bit1 TMR1CS:Timer1 Clock Source Select bit

           1 = External clock from pin RC0/T1OSO/T1CKI

           0 = Internal clock

    bit0 TMR1ON:Timer1 on bit

         1 = enables timer1

         0 = stops timer1 
    说明:作用在TMR1的计数状态,计数信号从RC0/T1CKI输入, 
    当来一个上升沿时,采集到一个有效的信号,计数到TMR1L,TMR1H中。 
    当计满时就会产生中断信号。 
    ***********************/ 
    #include  
    #include "../head/config.h"

    __CONFIG(HS&WDTDIS&LVPDIS&PWRTEN);

    void main(void) 

        T1CKPS0 = 0; 
        T1CKPS1 = 0;//不分频

        TMR1L = (65536 - 1)%256;//TMR1L,TMR1H赋初值 
        TMR1H = (65536 - 1)/256; 
        T1SYNC = 1;//TMR1异步计数器 
        TMR1CS = 1; 
        GIE = 1;//打开全局中断 
        PEIE = 1;//打开外部中断 
        TMR1IE = 1;//TMR1中断打开 
        TMR1ON = 1; 
        PORTD = 0x00; 
        TRISD = 0x00; 
        while(1){} 
    }

    void interrupt ISR(void) 

        TMR1L = (65536 - 1)%256;//重新赋值 
        TMR1H = (65536 - 1)/256; 
        if(TMR1IE && TMR1IF) 
        { 
            TMR1IF = 0; 
            PORTD = ~PORTD; 
        }    
    }

  3. /*
  4. 使用MPLAB X IDE V1.85 , XC8 compiler,PICKit 3, target的外部晶振12MHz,power 使用kit3提供5V
  5. Timer1 的time计时功能
  6. */
  7. #include  
  8.  
  9. #pragma config FOSC = HS        //
  10. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
  11. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  12. #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
  13. #pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
  14. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  15. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  16. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
  17. #define uchar unsigned char  
  18. #define uint unsigned int  
  19.  
  20. void delay(uint x)  //1ms  
  21. {  
  22.     uint y,z;  
  23.     for(y=x;y>0;y--)  
  24.         for(z=25;z>0;z--);  
  25. }  
  26. void init(void)  
  27. {  
  28.     T1CON=0x01;//使能定时器1,时钟为外部晶振四分频  
  29.     PIE1|=0x01;//允许中断  
  30.     INTCON|=0xc0;//开全局中断和第一外设中断  GIE =1,PIE1 = 1;
  31.     TMR1H=15536/256;//50ms初值  
  32.     TMR1L=15536%256;      
  33. }  
  34. void interrupt timer1(void)  
  35. {  
  36.     if(TMR1IE&&TMR1IF)    
  37.     {  
  38.         TMR1H=(65535-50000)/256;  
  39.         TMR1L=(65535-50000)%256;  
  40.         TMR1IF=0;//中断标志位清零  
  41. // ISR 相应的中断处理   
  42.     }  
  43. }  
  44.   
  45. void main(void)  
  46. {     
  47.     init();  
  48.     while(1);  
--------------------------------------------Timer2-------------------------------------------

TMR2的宽度与TMR0一样的也是8位,一般伴随着CCP模块和PWM功能一起出现。

 8位宽度的TMR2定时器有一个前置预分频器和后置预分频器,同时还有一个周期控制寄存器与它配合一起实现针对单片机指令周期的计数。

 TMR2只能作为定时器使用,无法对外部输入的脉冲作计数。

 TMR2定时器与TMR0相比,最大的区别是TMR2有一个周期的控制寄存器PR2。PR2寄存器可以设定定时器的上限值。只要当TMR2的计数值和PR2的设定值相等时就会自动归0,同时产生一个中断。

 当TMR2的计数值与PR2寄存器中所设定的数值相一致后,一睛个计数脉冲,的到来应会让TMR2溢出归0,注意:一个计数溢出归0后,并不一定产即产生TMR2IF中断标志,何时产生中断标志将取决于后分频器的分频比。如果选择的后分频比为1:8,则TMR2计数溢出8次后才产生1次中断。

//前分频器 
//T2CKPS1   T2CKPS0 
//   0         0        1  分频  TMR1时钟为晶振时钟/(4*1) 
//   0         1        4  分频  TMR1时钟为晶振时钟/(4*4) 
//   1         x        16 分频  TMR1时钟为晶振时钟/(4*16) 
//后分频器 
//TOUTPS3  TOUTPS2  TOUTPS1  TOUTPS0 
//   0        0        0        0     1 分频 
//   0        0        0        1     2 分频 
//   0        0        1        0     3 分频 
//   0        0        1        1     4 分频 
//   0        1        0        0     5 分频 
//   0        1        0        1     6 分频 
//   0        1        1        0     7 分频 
//   0        1        1        1     8 分频 
//   1        0        0        0     9 分频 
//   1        0        0        1     10分频 
//   1        0        1        0     11分频 
//   1        0        1        1     12分频 
//   1        1        0        0     13分频 
//   1        1        0        1     14分频 
//   1        1        1        0     15分频 
//   1        1        1        1     16分频

 
关于PIC16F877A 的时钟问题,我还是没搞懂,调整config bits的Fsoc,只有HS可以使用,其它选项均提示:“Programming/Verify complete
The target device is not ready for debugging. Please check your configuration bit settings and program the device before proceeding.”?
Key:原因在PIC16F877A的时钟设置 根据crystal大小设定相应的configuration bits。8M~16MHz 属于HS配置,详见PIC16F877a的datasheet的145页,关于crystal的讲解。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机中断源的扩展方法