最近想写点关于stc函数的东西,今天看了一下定时器的函数库,感觉这个函数有点问题呢!
我先把代码贴出来
定时器头文件 /*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* --- QQ: 800003751 ----------------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#ifndef __TIMER_H
#define __TIMER_H
#include "config.h"
#define Timer0 0
#define Timer1 1
#define Timer2 2
#define Timer3 3
#define Timer4 4
#define TIM_16BitAutoReload 0
#define TIM_16Bit 1
#define TIM_8BitAutoReload 2
#define TIM_16BitAutoReloadNoMask 3
#define TIM_CLOCK_1T 0
#define TIM_CLOCK_12T 1
#define TIM_CLOCK_Ext 2
typedef struct
{
u8 TIM_Mode; //工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
u8 TIM_Polity; //优先级设置 PolityHigh,PolityLow
u8 TIM_Interrupt; //中断允许 ENABLE,DISABLE
u8 TIM_ClkSource; //时钟源 TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
u8 TIM_ClkOut; //可编程时钟输出, ENABLE,DISABLE
u16 TIM_Value; //装载初值
u8 TIM_Run; //是否运行 ENABLE,DISABLE
} TIM_InitTypeDef;
u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx);
#endif
下边是官方的c文件 /* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* --- QQ: 800003751 ----------------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
/************* 功能说明 **************
本文件为STC15xxx系列的定时器初始化和中断程序,用户可以在这个文件中修改自己需要的中断程序.
******************************************/
#include "timer.h"
/********************* Timer0中断函数************************/
void timer0_int (void) interrupt TIMER0_VECTOR
{
P25 = ~P25;
}
/********************* Timer1中断函数************************/
void timer1_int (void) interrupt TIMER1_VECTOR
{
P26 = ~P26;
}
/********************* Timer2中断函数************************/
void timer2_int (void) interrupt TIMER2_VECTOR
{
P10 = ~P10;
}
//========================================================================
// 函数: 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 > Timer2) 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; //输出时钟
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; //成功
}
return 2; //错误
}
在头文件中定时器的定义已经到了timer4,但是在c中却只支持到了timer2,还有在GPIO的c文件配置中判断了参数的数值是否超出了范围,但是在
这个定时器文件中却没有判断,只判断了是不是超出了定时器2,其他的没有判断!也就是说不知道这个函数是不是最后能不能配置成功定时器,
还有就是这个最后的returm 2;这句话,到底存在着什么意义呢?希望有大神能详细的解释一下!
0
|
2个回答
|
|
|