本帖最后由 buyadong 于 2012-4-12 16:01 编辑
下面是定时器4部分代码:
TIM_BaseInitStructure.TIM_Period = 20000; /* 超时时间20ms */
TIM_BaseInitStructure.TIM_Prescaler = 71;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_Pulse = 20000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_CC1);
TIM_ITConfig(TIM4, TIM_IT_CC1, DISABLE);
TIM_Cmd(TIM4, DISABLE);
uint8_t IRSendStart(uint8_t id)
{
uint8_t err = 0;
memset((void *)&IRListSendbuf,0,sizeof(IRListFlashTypedef));
err = IRReadList(&IRListSendbuf,id); /* 查看是否成功读取到所要的数据*/
if(err > 0)
return err;
IRSendPend();
IRPWMEnable(1);
TIM_Cmd(TIM4, ENABLE);
TIM_SetCounter(TIM4,IRListSendbuf.IRPointTimeValue[0]); /* 手动清除定时器计数值 */
TIM_SetCompare1(TIM4,IRListSendbuf.IRPointTimeValue[0]+3);
TIM_ClearFlag(TIM4, TIM_FLAG_CC1);
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
return err;
}
static void IRPWMEnable(uint8_t flag)
{
TIM_SetCounter(TIM1,0); /* 手动清除定时器计数值 */
if(flag){
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
else{
TIM_CtrlPWMOutputs(TIM1,DISABLE);
}
}
void TIM4_IRQHandler(void)
{
uint16_t szValue = 0;
#if USE_OS_EN
OS_CPU_SR cpu_sr = 0u;
OS_ENTER_CRITICAL();
#endif //USE_OS_EN
szValue = TIM_GetCounter(TIM4);
if(TIM_GetITStatus(TIM4, TIM_IT_CC1) == SET){ /* 查询是否是溢出中断 */
TIM_ClearFlag(TIM4, TIM_FLAG_CC1); /* 清除中断标志 */
if(IRListSendbuf.IRPointTimeValue[1] == 0){ /* 若第二个字节为0,则说明为空*/
IRErr = IR_SEND_ERR; /* 发送错误 */
OSMboxPost(IRSendMbox,&IRErr); /* 发送邮箱 */
IRPWMEnable(0);
TIM_ITConfig(TIM4, TIM_IT_CC1, DISABLE);
IRListSendIndex = 0;
IRListSendCnt = 0;
return;
}
if(((IRListSendbuf.IRPointTimeValue[IRListSendIndex] == 0x00)&&(IRListSendIndex != 0))\
||(IRListSendIndex >= IRMAXPOINT - 2)){ /* 判断是否已经到最后 */
/* 结束发送 */
IRPWMEnable(0); /* 禁止输出 */
TIM_ITConfig(TIM4, TIM_IT_CC1, DISABLE);
IRErr = IR_SEND_COMP; /* 发送完成 */
OSMboxPost(IRSendMbox,&IRErr); /* 发送邮箱 */
IRListSendIndex = 0;
IRListSendCnt = 0;
}else{
IRListSendIndex++;
TIM_SetCompare1(TIM4,IRListSendbuf.IRPointTimeValue[IRListSendIndex]);
if((IRListSendIndex%2) != 0){ /* 奇数位为输出载波 */
IRPWMEnable(1); /* 允许输出 */
}else{ /* 偶数位为不输出载波 */
IRPWMEnable(0); /* 禁止输出 */
}
TIM_SetCounter(TIM4,0); /* 手动清除定时器计数值 */
}
}
#if USE_OS_EN
OS_EXIT_CRITICAL();
#endif //USE_OS_EN
}
|