PIC16F877A|PIC16F877A TIMER1计数操作
/**********************
Title:PIC16F877A TIMER1计数操作
Author:hnrain
Date:2010-12-28
使用前置分频器
T1CKPS1T1CKPS1
001分频TMR1时钟为晶振时钟/(4*1)
012分频TMR1时钟为晶振时钟/(4*2)
104分频TMR1时钟为晶振时钟/(4*4)
118分频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
bit3T1OSCEN: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);
【PIC16F877A|PIC16F877A TIMER1计数操作】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;
}
}
/**********************
Title:PIC16F877A TIMER1计数操作
Author:hnrain
Date:2010-12-28使用前置分频器
T1CKPS1T1CKPS1
001分频TMR1时钟为晶振时钟/(4*1)
012分频TMR1时钟为晶振时钟/(4*2)
104分频TMR1时钟为晶振时钟/(4*4)
118分频TMR1时钟为晶振时钟/(4*8)TMR1是16位宽度的TMR1由2个8位的可读写的寄存器TMR1H和TMR1L组成。TMR1有专门的启停控制位TMR1ON,通过软件可以任意启动或暂停TMR1计数功能。T1CON:TIMER1 CONTROL REGISTERbit7-6 unimplemented :Read as ‘0’bit5-4 T1CKPS1:T1CKPS0:Timer1 input Clock Prescale Select bits11=1:8 prescale value10=1:4 prescale value01=1:2 prescale value00=1:1 prescale valuebit3T1OSCEN:Timer1 Oscillator Enable Control bit1 = Oscillator is enable0 = Oscillator is shut-offbit2 T1SYNC:Timer1 External Clock Input Synchronization Control bitwhen TMR1CS = 11= Do not synchronize external clock input0= Synchronize external clock inputwhen TMR1CS = 0This bit is ignored .Timer1 uses the internal clock when TMR1CS = 0.bit1 TMR1CS:Timer1 Clock Source Select bit1 = External clock from pin RC0/T1OSO/T1CKI0 = Internal clockbit0 TMR1ON:Timer1 on bit1 = enables timer10 = 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;
}
}
转载于:https://www.cnblogs.com/hnrainll/archive/2010/12/28/1919168.html
推荐阅读
- 十二种排序(冒泡、插入、归并、快速排序等包含希尔和计数排序)
- 【golang】leetcode初级-Fizz|【golang】leetcode初级-Fizz Buzz&计数质数
- [Golang]力扣Leetcode—初级算法—数学—计数质数(厄拉多塞筛法)
- 【组合计数】ARC061F Card Game for Three
- 学习笔记|Burnside引理和polay计数学习笔记
- Codeforces Round #277 (Div. 2)D(树形DP计数类)
- #|JQuery计数器+延时+从隐藏域取值赋值+页面加载自动运行
- 【hdu5304】生成树计数—基尔霍夫矩阵 DP
- 数学|【SRM 565 UnknownTree】计数 分类讨论
- 51单片机的定时器/计数器概念