思路:
用IO 口PC6 检测市电220V输入(硬件已整形为方波);
我的思路.
220V市电50HZ=20ms
Tim8系统时钟:72MHZ
已经将IO系统时钟及TIM8时钟设置好,且PC6配置为浮空输入
/* TIM8 channel 1 pin (PC.06) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM8设置如下.:
void tim8ch1_config(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_DeInit(TIM8);
TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP; //配置为输入捕获模式
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //选择通道1
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //输入上升沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // 通道方向选择
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //每次检测到捕获输入就触发一次捕获
TIM_ICInitStructure.TIM_ICFilter = 0x4; //
TIM_ICInit(TIM8, &TIM_ICInitStructure);
TIM_PrescalerConfig(TIM8, 71, TIM_PSCReloadMode_Immediate);//72M/72=1M
/* Select the TIM8 Input Trigger: TI2FP2 【输入触发源选择】*/
TIM_SelectInputTrigger(TIM8,TIM_TS_TI2FP2); //参考TIM结构图选择滤波后的TI2输入 寄存器SMCR
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM8,TIM_SlaveMode_Reset); //复位模式-选中的触发输入(TRGI)的上升沿初始化计数器,并且产生一个更新线号
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM8,TIM_MasterSlaveMode_Enable); //启动定时器的被动触发.
TIM_ITConfig(TIM8, TIM_IT_CC1, ENABLE); //打开中断
TIM_Cmd(TIM8, ENABLE); //启动TIM8
Tim8_SetupNVIC();
}
中断配置:
void Tim8_SetupNVIC(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断里面做的事情:
void TIM8_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM8, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM8, TIM_IT_CC1);
if(HW_flag==0 )
{
IC1[HW_NUM]=TIM8->CCR1;
if ( IC1[HW_NUM] != 0)
{
// IC2[HW_NUM]=TIM8->CCR2;
IC1[HW_NUM]=TIM8->CCR1;
}
}
HW_NUM++;
if(HW_NUM>=14)
{
HW_NUM=0;
HW_flag=1;
}
}
}
结果:
IC1[0]=687;IC1[1]=0;IC1[2]=0;IC1[3]=0;IC1[4]=0;IC1[5]=0;IC1[6]=0;IC1[7]=0;IC1[8]=0;IC1[9]=0;IC1[10]=0;IC1[11]=0;IC1[12]=0;IC1[13]=0;
怎么不对呢.郁闷两天了,快要崩溃了..
用户547841 2010-3-10 17:27