我用的是STM8S103F3P6 内部8M的晶振 ,步骤1hz~1KHz的频率
程序如下。
void TIME1_INIT(void)
{
PC_DDR&=~(1<<3); //PD3 输入模式
PC_CR1|= 1<<3; //PD3,上拉
PC_CR2&=~(1<<3); //PD3,不使用中断
TIM1_DeInit();
// TIM1_TimeBaseInit(800, TIM1_COUNTERMODE_DOWN, 20000, 0);
//CC3通道被配置为输入,IC3映射在TI3FP3上 11:每8个事件触发一次捕获 无滤波器,f SAMPLING =f MASTER
TIM1_ICInit( 0x00,0x01,0x0C, 0x0);//通道3 上升沿
TIM1_CCR3H=0;
TIM1_CCR3L=0;
TIM1_ITConfig(TIM1_IT_CC3,ENABLE);
TIM1_ClearFlag(TIM1_FLAG_CC3);
TIM1_Cmd(ENABLE);
// TIM1_CCMR3 = 0x0d;
// TIM1_CCER2 = 0x01;
// TIM1_IRE = 0x08;
// TIM1_CR1 = 0x01;
}
//中断
#pragma vector=TIM1_CAPCOM_CC3IF_vector
__interrupt void TIM1_CAP_COM_IRQHandler(void)
{
if(TIM1_GetITStatus(TIM1_FLAG_CC3) != RESET)
{
switch(Icp_Status)
{
case 0: ICValue1=TIM1_GetCapture3(); //第一次捕捉到上升沿
Icp_Status=1;
break;
case 1: ICValue2=TIM1_GetCapture3();
Icp_Status=2;
break;
default:
TIM1_CCxCmd( DISABLE);
TIM1_ITConfig(TIM1_IT_CC3, DISABLE);
Icp_Status=3;
break;
}
TIM1_SR1 = (uint8_t)(~(uint8_t)TIM1_IT_CC3);
TIM1_ClearFlag(TIM1_FLAG_CC3);
}
}
//主函数
while(1)
{
if( Icp_Status == 3)
{
Icp_Status = 0;
if(ICValue2 > ICValue1)ICp_Vaule = ICValue2-ICValue1;
else if(ICValue2<ICValue1) ICp_Vaule = 0xffff + ICValue2 - ICValue1;
TIM1_CCxCmd(ENABLE);
TIM1_ITConfig(TIM1_IT_CC3, ENABLE);
TIM1_CCR3H=0;
TIM1_CCR3L=0;
}
现在给125HZ 周期8ms 读到的差值是 54000.这个值和这个频率完全对不上,还有我想请问下,这个值与频率关系,应该怎么算? |