PIC16F877A TIMER1定时操作

/**********************
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
***********************/
#include <pic.h>
#include "../head/config.h"

__CONFIG(HS&WDTDIS&LVPDIS&PWRTEN);

void Delayms(uint16 dly);

void main(void)
{
    T1CKPS1 = 1;
    T1CKPS0 = 1;
    TMR1CS = 0;
    TMR1L = (65536 - 37500)%256;
    TMR1H = (65536 - 37500)/256;
    GIE = 1;
    PEIE = 1;
    TMR1IE = 1;
    TMR1ON = 1;   
    PORTD = 0x00;
    TRISD = 0x00;
    while(1){}
}

void interrupt ISR(void)
{
    TMR1L = (65536 - 37500)%256;
    TMR1H = (65536 - 37500)/256;
    if(TMR1IF&&TMR1IE)
    {
        TMR1IF = 0;
        PORTD = ~PORTD;
    }   
}

 

/**********************
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
***********************/
#include <pic.h>
#include "../head/config.h"

__CONFIG(HS&WDTDIS&LVPDIS&PWRTEN);

void Delayms(uint16 dly);

void main(void)
{
	T1CKPS1 = 1;
	T1CKPS0 = 1;
	
	TMR1CS = 0;
	
	TMR1L = (65536 - 37500)%256;
	TMR1H = (65536 - 37500)/256;
	
	GIE = 1;
	PEIE = 1;
	TMR1IE = 1;
	
	TMR1ON = 1;	
	
	PORTD = 0x00;
	TRISD = 0x00;
	
	while(1){}
}

void interrupt ISR(void)
{
	TMR1L = (65536 - 37500)%256;
	TMR1H = (65536 - 37500)/256;
	if(TMR1IF&&TMR1IE)
	{
		TMR1IF = 0;
		PORTD = ~PORTD;
	}	
}

转载于:https://www.cnblogs.com/hnrainll/archive/2010/12/28/1919141.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值