参照3.0库写的例程为何只能进入TIM_IT_Update而不能进入TIM_IT_CC1
右边对应3.0库代码:
void Tim2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 5000;// 1us * 50 000 = 50ms;TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 35;// 36mhz /36 = 1mhz, =1us TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;// TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);// TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM2, 256, TIM_PSCReloadMode_Immediate);// TIM_PrescalerConfig(TIM2, 4, TIM_PSCReloadMode_Immediate);
/* Output Compare Timing Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;// TIM_OCInitStructure.TIM_Pulse = CCR1_Val; __IO uint16_t CCR1_Val = 49152;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_Update, ENABLE);// TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);我没有使用cc2,cc3,cc4
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
中断处理
void TIM2_IRQHandler(void)
{
uint8_t b;
b=10;
b=b+1;
#ifdef _DEBUG_
printf("TIM2_IRQHnadler Success!\n");
#endif
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
#ifdef _DEBUG_
printf("TIM2_IRQHnadler_Update Success!\n");
#endif
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
#ifdef _DEBUG_
printf("TIM2_IRQHnadler_CC1 Success!\n");
#endif
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
TIM_Cmd(TIM2, DISABLE);
}
}
keil4.12 调试结果
ex_timebase.JPG
(80.05 KB)
下载次数:0
2010-7-27 12:18
void displayWeek(UCHAR week)
{
UCHAR i;
week--; //因为weekTable数组是从0开始的,故减去1
writeCmd(0x80 + 0x0d); //初始化地址指针
writeCmd(0x0c); //不显示光标
for(i = 0; i < 3; i++)
writeData(weekTable[week][i]);
}
/*函数功能:刷新显示屏幕上任意两个数字
*参 数:类型:unsinged char所要显示二个数字的数值
*/
void displayTime(UCHAR first, UCHAR second)
{
writeCmd(0x0c); //不显示光标
writeData(first + 48); //转成数字相对应的ASCII值
writeData(second + 48);
}
/*函数功能:更新时间变量及刷新屏幕时间显示
*参数 :所要显示的时间变量的地址,displayOn为0时仅更新时间变量
*/
void display(UCHAR *seconds, UCHAR *minute, UCHAR *hour, UCHAR *day, UCHAR *week, UCHAR *month, bit displayOn)
{
timeUpdate(seconds, minute, 59, 0);
timeUpdate(minute, hour, 59, 0);
timeUpdate(hour, day, 23, 0);
if(*day == 0) //每个月的第一天再往后减一,即回到上个月的最后一天
{
if(*month == 1) //如果一月份的第一天
{
year--; //回到去年的最后一天
*day = 31;
*month = 12;
}
else //除一月份外每个月的第一天
{
if(leapYear) //把日改为上个月的最后一天
*day = monthTable[1][*month - 2];
else
*day = monthTable[0][*month - 2];
(*month)--; //月减一
}
}
leapYear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
processDayMon(day, month); //每月的最后一天再往前加一天
if(*month == 13) //每年的最后一个月再往前加一个月
{
*month = 1;
year++;
}
else if(*month == 0) //每年的第一个月再往后减一个月
{
*month = 12;
year--;
}
//timeUpdate(month, &year, 12, 1);
leapYear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
if((*week = getWeek(*day, *month, year)) == 0)
*week = 7;
if(displayOn)
{
writeCmd(0x80 + 0x4a); //地址指针移到秒的位置
displayTime(*seconds / 10, *seconds % 10); //更新秒的显示
writeCmd(0x80 + 0x47); //地址指针移到分钟的位置
displayTime(*minute / 10, *minute % 10); //更新分钟的显示
writeCmd(0x80 + 0x44); //地址指针移到小时的位置
displayTime(*hour / 10, *hour % 10);
writeCmd(0x80 + 0x0a); //地址指针移到日的位置
displayTime(*day / 10, *day % 10);
displayWeek(*week); //更新星期
writeCmd(0x80 + 0x07); //地址指针移到月的位置
displayTime(*month / 10, *month % 10);
if(year % 100 == 0) //当年的前两位发生改变时,刷新年的前两位
{
writeCmd(0x80 + 0x02); //地址指针移到年的高二位
displayTime(year % 1000, year / 100 % 10);
}
writeCmd(0x80 + 0x04); //地址指针移到年的低二位
displayTime(year % 100 / 10, year % 10);
}
}
/*函数功能:当相应的时间单位超出范围时对其进行更新处理
*参数 : value:所要判断的时间变量的地址,nextValue:该时间变量的下一级单位时间的地址,
largest:*value对应的时间单位的所能显示的最大值,如月份为12,分钟为59
smallest:*value对应的时间单位的所能显示的最小值,如月份为1,分钟为0
*/
/*小时分钟秒这些我是用UCHAR类型来表示的,但年我是用unsigned int类型来表示的
*这样子对于年怎么实现参数的传递呢?
*我用timeUpdate(&month, (char*)year,)
*或者把参数原型改成 timeUpdate(UCHAR *value, UINT *value,,,);
*然后把month强制转成UINT *型..timeUpdta((UINT*)&month, &year...);
编译通过,但运行结果是错的. */
void timeUpdate(UCHAR *value, UCHAR *nextValue, UCHAR largest, UCHAR smallest)
{
largest++; //让相应的时间单位所能显示的最大值加1
smallest--;
if(*value == largest) //当对应的时间单位超出所能显示的最大值时
{
*value = smallest; //低一级时间单位回到最小值
(*nextValue)++; //高一级时间单位加1
}
else if(*value == smallest) //对应的时间单位的最小值再往下减一时
{
*value = largest; //低一级时间单位回到最大值
(*nextValue)--; //高一级时间单位减1
}
}
/*函数功能:算出某年某月某日是星期几
*返 回值 :返回0,代表星期天,其余返回值代表星期几
*/
UCHAR getWeek(UCHAR day, UCHAR month, UINT year)
{
UINT temp, yearTemp;
UCHAR i;
temp = 0; //必须初始化temp的值,否则出错了
yearTemp = year - 1;
for(i = 0; i < month - 1; i++)
if(leapYear)
temp += monthTable[1][i];
else
temp += monthTable[0][i];
temp += yearTemp + yearTemp / 4 - yearTemp / 100 + yearTemp / 400 + day;
return (temp % 7);
}
/*函数功能:每个月的最后一天再往前加一天,对月日进行处理
*参 数:所要处理的日、月的地址
*返回 值:如果是每个月的最后一天,返回值为1,否则为0
*/
bit processDayMon(UCHAR *day, UCHAR *month)
{
if(leapYear && monthTable[1][*month - 1] < *day || monthTable[0][*month - 1] < *day)
{
*day = 1;
(*month)++;
return 1;
}
return 0;
}
/*函数功能:刷新计时器时间变量及其屏幕显示
*参数 :相对应的时间的地址
*/
void displayCounter(UCHAR *minute, UCHAR *seconds, UCHAR *tenMs)
{
if(*tenMs == 100)
{
(*seconds)++;
*tenMs = 0;
}
if(*seconds == 60)
{
(*minute)++;
*seconds = 0;
}
if(*minute == 60)
*minute = 0;
writeCmd(0x80 + 0x44);
displayTime(*minute / 10, *minute % 10);
writeData(':');
displayTime(*seconds / 10, *seconds % 10);
writeData(':');
displayTime(*tenMs / 10, *tenMs % 10);
}
/*函数功能:恢复时钟界面 */
void recoverClock()
{
writeCmd(0x01); //清屏
writeCmd(0x80 + 0x06);
writeData('-');
writeCmd(0x80 + 0x09);
writeData('-');
writeCmd(0x80 + 0x02);
displayTime(year / 1000, year / 100 % 10);
writeCmd(0x80 + 0x46);
writeData(':');
writeCmd(0x80 + 0x49);
writeData(':');
}复制代码