加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
timer.c 6.71 KB
一键复制 编辑 原始数据 按行查看 历史
dk鲁晓东 提交于 2019-12-19 22:54 . 定时器驱动
#include "timer.h"
static TIM_InitTypeDef TIM;//先定义,这样在主函数中可以随时可调用
//TIM_InitTypeDef TIM1;
//TIM_InitTypeDef TIM2;
//TIM_InitTypeDef TIM3;
//TIM_InitTypeDef TIM4;
/********************* Timer0中断函数************************/
void timer0_int (void) interrupt 1
{
P00=~P00;
}
/********************* Timer1中断函数************************/
void timer1_int (void) interrupt 3
{
}
/********************* Timer2中断函数************************/
void timer2_int (void) interrupt 12
{
P00=~P00;
}
/********************* Timer3中断函数************************/
void timer3_int (void) interrupt 19
{
P00=~P00;
}
/********************* Timer4中断函数************************/
void timer4_int (void) interrupt 20
{
// static u16 count=0;
// count++;
// if(count==10)
// {P00=~P00;count=0;}
P00=~P00;
}
//========================================================================
// 函数: u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
// 描述: 定时器初始化程序.
// 参数: TIMx: 结构参数,请参考timer.h里的定义.
// 返回: 成功返回0, 空操作返回1,错误返回2.
// 版本: V1.0, 2012-10-22
//========================================================================
u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
{
if(TIM > Timer4) return 1; //空操作
if(TIM == Timer0)
{
TR0 = 0; //停止计数
if(TIMx->TIM_Interrupt == ENABLE) {ET0 = 1;} //允许中断
else ET0 = 0; //禁止中断
if(TIMx->TIM_Polity == PolityHigh) PT0 = 1; //高优先级中断
else PT0 = 0; //低优先级中断
if(TIMx->TIM_Mode > TIM_16BitAutoReloadNoMask) return 2; //错误
TMOD = (TMOD & ~0x03) | TIMx->TIM_Mode; //工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动重装, 3: 16位自动重装, 不可屏蔽中断
if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) AUXR &= ~0x80; //12T
if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) AUXR |= 0x80; //1T
if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) TMOD |= 0x04; //对外计数或分频
else TMOD &= ~0x04; //定时
if(TIMx->TIM_ClkOut == ENABLE) INT_CLKO |= 0x01; //输出时钟
else INT_CLKO &= ~0x01; //不输出时钟
TH0 = (u8)(TIMx->TIM_Value >> 8);
TL0 = (u8)TIMx->TIM_Value;
if(TIMx->TIM_Run == ENABLE) TR0 = 1; //开始运行
return 0; //成功
}
if(TIM == Timer1)
{
TR1 = 0; //停止计数
if(TIMx->TIM_Interrupt == ENABLE) ET1 = 1; //允许中断
else ET1 = 0; //禁止中断
if(TIMx->TIM_Polity == PolityHigh) PT1 = 1; //高优先级中断
else PT1 = 0; //低优先级中断
if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask) return 2; //错误
TMOD = (TMOD & ~0x30) | TIMx->TIM_Mode; //工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动重装
if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) AUXR &= ~0x40; //12T
if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) AUXR |= 0x40; //1T
if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) TMOD |= 0x40; //对外计数或分频
else TMOD &= ~0x40; //定时
if(TIMx->TIM_ClkOut == ENABLE) INT_CLKO |= 0x02; //输出时钟
else INT_CLKO &= ~0x02; //不输出时钟
TH1 = (u8)(TIMx->TIM_Value >> 8);
TL1 = (u8)TIMx->TIM_Value;
if(TIMx->TIM_Run == ENABLE) TR1 = 1; //开始运行
return 0; //成功
}
if(TIM == Timer2) //Timer2,固定为16位自动重装, 中断无优先级
{
AUXR &= ~(1<<4); //停止计数
if(TIMx->TIM_Interrupt == ENABLE) IE2 |= (1<<2); //允许中断
else IE2 &= ~(1<<2); //禁止中断
if(TIMx->TIM_ClkSource > TIM_CLOCK_Ext) return 2;
if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) AUXR &= ~(1<<2); //12T
if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) AUXR |= (1<<2); //1T
if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) AUXR |= (1<<3); //对外计数或分频
else AUXR &= ~(1<<3); //定时
if(TIMx->TIM_ClkOut == ENABLE) INT_CLKO |= 0x04; //输出时钟P30口
else INT_CLKO &= ~0x04; //不输出时钟
TH2 = (u8)(TIMx->TIM_Value >> 8);
TL2 = (u8)TIMx->TIM_Value;
if(TIMx->TIM_Run == ENABLE) AUXR |= (1<<4); //开始运行
return 0; //成功
}
if(TIM == Timer3) //Timer3,固定为16位自动重装, 中断无优先级
{
T4T3M |= ~(1<<3); //停止计数
if(TIMx->TIM_Interrupt == ENABLE) IE2 |= (1<<5); //允许中断
else IE2 &= ~(1<<5); //禁止中断
if(TIMx->TIM_ClkSource > TIM_CLOCK_Ext) return 2;//参数错
if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) T4T3M &= ~(1<<1); //12T
if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) T4T3M |= (1<<1); //1T
if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) T4T3M |= (1<<2); //对外计数或分频
else T4T3M &= ~(1<<2); //定时
if(TIMx->TIM_ClkOut == ENABLE) T4T3M |= 0x01; //输出时钟P04
else T4T3M &= ~0x01; //不输出时钟
T3H = (u8)(TIMx->TIM_Value >> 8);// 装入高8位
T3L = (u8)TIMx->TIM_Value; // 装入低8位
if(TIMx->TIM_Run == ENABLE) T4T3M |= (1<<3); //开始运行
return 0; //成功
}
if(TIM == Timer4) //Timer3,固定为16位自动重装, 中断无优先级
{
T4T3M |= ~(1<<7); //停止计数
if(TIMx->TIM_Interrupt == ENABLE) IE2 |= (1<<6); //允许中断
else IE2 &= ~(1<<6); //禁止中断
if(TIMx->TIM_ClkSource > TIM_CLOCK_Ext) return 2;//参数错
if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) T4T3M &= ~(1<<5); //12T
if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) T4T3M |= (1<<5); //1T
if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) T4T3M |=(1<<6); //对外计数P07或分频
else T4T3M &= ~(1<<6); //定时
if(TIMx->TIM_ClkOut == ENABLE) T4T3M |= 0x10; //输出时钟P06
else T4T3M &= ~0x10; //不输出时钟
T4L = (u8)TIMx->TIM_Value; // 装入低8位
T4H = (u8)(TIMx->TIM_Value >> 8);// 装入高8位
if(TIMx->TIM_Run == ENABLE) T4T3M |= (1<<7); //开始运行
return 0; //成功
}
return 2; //错误
}
//用于初始化,是一个范例
//读TIM计数器(0,1,2,3,4)的值
u16 readcount(u8 TIM)
{
u16 count;
u8 Hcount,Lcount;
if(TIM==Timer0){
Hcount=TH0;Lcount=TL0;
if(Hcount!=TH0)
{Hcount=TH0;Lcount=TL0;}
}
if(TIM==Timer1){
Hcount=TH1;Lcount=TL1;
if(Hcount!=TH1)
{Hcount=TH1;Lcount=TL1;}
}
if(TIM==Timer2){
Hcount=T2H;Lcount=T2L;
if(Hcount!=T2H)
{Hcount=T2H;Lcount=T2L;}
}
if(TIM==Timer3){
Hcount=T3H;Lcount=T3L;
if(Hcount!=T3H)
{Hcount=T3H;Lcount=T3L;}
}
count=Hcount;
count=count<<8;
count=count+Lcount;
return count;
}
void Timer_init()
{
//TIM.TIM_Mode=0;//工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
//TIM.TIM_Polity=PolityHigh; //优先级设置 PolityHigh,PolityLow
TIM.TIM_ClkSource=TIM_CLOCK_Ext; //时钟源 TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
TIM.TIM_ClkOut=ENABLE; //可编程时钟输出, ENABLE,DISABLE
TIM.TIM_Value=65534; //装载初值
TIM.TIM_Run=ENABLE; //是否运行 ENABLE,DISABLE
TIM.TIM_Interrupt=ENABLE; //中断允许 ENABLE,DISABLE
Timer_Inilize(Timer4, &TIM);//
EA=1; //
//CT4(1);//定时
//TH4=255;TL4=254;
//T4x12(0); //12T
//ET4(1);//
//EA=1; // 中断总开关
//TR4(1);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化