?? timer.c
字號(hào):
#include <pic.h>
#include "..\inc\include.h"
/***********************************************************************
** Fosc = 8MHz, CLKO = Fosc/4 = 2MHz
**
***********************************************************************/
volatile uchar Cnt10us; //延時(shí)10us定時(shí)器計(jì)數(shù)器
volatile uchar Cnt1ms; //延時(shí)1ms定時(shí)器計(jì)數(shù)器
void Timer_Init(void)
{
//注意:OPTION復(fù)位值為0xFF
//CLKO = Fosc/4 = 1MHz
//---Timer0---
OPTION &= ~7; //PS<2:0> = 0
//OPTION |= 3; //PS<2:0> = 3,TMR0 rate = 1:16,
//OPTION |= 2; //PS<2:0> = 2,TMR0 rate = 1:8,
//OPTION |= 1; //PS<2:0> = 1,TMR0 rate = 1:4,
OPTION |= 0; //PS<2:0> = 0,TMR0 rate = 1:2,
T0CS = 0; //clock source slecet internal
PSA = 0; //Prescaler is assigned to the Timer0 module
//TMR0 = (256-250); // 4us*250=1000us(1ms)
//TMR0 = (256-1); // 4us*250=1000us(1ms)
TMR0IE = 0; //Disable Timer0 interrupt
//---Timer1---
T1CKPS1 = 0; //prescale value 1:1
T1CKPS0 = 0;
TMR1CS = 0; //Clock Source Select,0=Internal clock (FOSC/4)
TMR1IF = 0; //Clear interrupt flag
TMR1IE = 0; //Disable Timer1 interrupt
TMR1ON = 0; //Disable Timer1
//---Timer2---
}
/***********************************************************************
** 10us延時(shí)
***********************************************************************/
void Delay10us(uchar t)
{
if(t==0) return;
Cnt10us = t;
TMR0 = (256-5); //(0.5us*2)*5 = 5us
TMR0IF = 0;
TMR0IE = 1;
while(Cnt10us);
TMR0IE = 0;
}
void Timer0_ISR(void)
{
TMR0IF = 0;
if(Cnt10us){
Cnt10us--;
}
if(Cnt10us){
TMR0 = (256-5); //(0.5us*2)*5 = 5us
}
}
/***********************************************************************
** 10ms延時(shí)
***********************************************************************/
void Delay1ms(uchar t)
{
if(t==0) return;
Cnt1ms = t;
TMR1H = (65536-2000)/256; //0.5us*2000 = 1000us(1ms)
TMR1L = (65536-2000)%256;
TMR1IF = 0; //Clear interrupt flag
TMR1IE = 1; //Enable Timer1 interrupt
TMR1ON = 1; //Enable Timer1
while(Cnt1ms);
TMR1ON = 0; //Disable Timer1
TMR1IE = 0; //Disable Timer1 interrupt
}
void Timer1_ISR(void)
{
TMR1IF = 0;
if(Cnt1ms){
Cnt1ms--;
}
if(Cnt1ms){
TMR1H = (65536-2000)/256; //0.5us*20000 = 10000us(10ms)
TMR1L = (65536-2000)%256;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -