如果用STM8S的一个IO口检测一个脉冲电平,用在电话机上面,比方说是GPIO3用来检测脉冲电平变化后,MCU将接通电话的GPIO1置高,接通电话。当挂断电话后,GPIO3脉冲再次变化,GPIO1置低,电话挂断。那我的做法是用TIM1的中断检测脉冲,但是当脉冲再次变化时我该怎么做?timer1.c
#include "timer1.h"
#include <stdarg.h>
#include <stdio.h>
static u32 TimingDelay;
//timer1_capture define
u32 TIM1ClockFreq = 25;//15Hz
u32 LSIClockFreq = 0;
u16 ICValue1 =0, ICValue2 =0;
//标志位放在TIM1中断中
volatile u8 pulse_change_flag = 0;//脉冲变化标志位,1:接通 2:挂断 0:重置
//
void Tim1_Init(void)
{
TIM1_TimeBaseInit(16,TIM1_COUNTERMODE_UP,1000,0);//16分频,向上计数,计数完了触发中断,如果想
//再 1MS 触发中断,则计数器应该计数 1000 次,1MHZ/1000=1KHZ,正好就是 1ms。
TIM1_ARRPreloadConfig(ENABLE);//使能自动重装
TIM1_ITConfig(TIM1_IT_UPDATE , ENABLE);//数据更新中断
TIM1_Cmd(ENABLE);//开定时器
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void delay_ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/*
*brief:timer1_capture输入捕获
*/
void timer1_capture(void)
{
/* Capture only every 8 events!!! */
/* Enable capture of TI1 */
/*
TIM1_Channel = TIM1_CHANNEL_1
TIM1_ICPolarity = TIM1_ICPOLARITY_FALLING
TIM1_ICSelection = TIM1_ICSELECTION_DIRECTTI
TIM1_ICPrescaler = TIM1_ICPSC_DIV8
TIM1_ICFilter = 0x0
*/
//IC:Input Capture,下降沿优先
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0x0);
/* Enable CC1 interrupt */
TIM1_ITConfig(TIM1_IT_CC1, ENABLE); //TIMER1 中断配置
//TIM1 IT 里面用脉冲变化的标志位
pulse_change_flag = 1;//接通
//
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
/* Clear CC1 Flag*/
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Connect LSI to CCO pin PC4作为输出 */
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
CLK_CCOConfig(CLK_OUTPUT_LSI);//时钟参数配置
CLK_CCOCmd(ENABLE);//Configure Clock Output(CCO)使能
/* wait a capture on CC1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on cc1 */ //Configure Clock 1
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();//Input Capture
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Compute LSI clock frequency */
LSIClockFreq = (8 * TIM1ClockFreq) / (ICValue2 - ICValue1);
} |