本帖最后由 skygz 于 2016-3-24 21:50 编辑
芯片:STM8L152 (血糖仪里拆的)
捕捉用 红外反射式模块, 在一个9025散热风扇在一个扇叶上贴反光纸. 模块的开关电平D0接口接到 STM8L152的 PB2(TIM2_CH2)上
对捕捉的中断得到的数据不明白如何使用...
问
读取到的ReadValue1, ReadValue2值 是什么来的, 脉冲值, 中断值 ? 还是什么的?
还有Capture这个值 计算出来的 又叫什么呢?
还有 计算 频率 Frequency = 1000000 / Capture; 是否这样的弄的? 取 TIM2分频的值1Mhz来算????
最终问这些 捕捉到数据可以计算转速吗? 要怎么计算? 数学我最烂了....
不知弄得对不对..
风扇低速时数据好乱, 加快风扇转速后, 数据会稳定
低速时的数据
ReadValue1:00019054|ReadValue2:00017572|Capture:00018518|Frequency:00000054|Speed:00001620
ReadValue1:00004072|ReadValue2:00018307|Capture:00014235|Frequency:00000070|Speed:00002100
ReadValue1:00006453|ReadValue2:00014892|Capture:00008439|Frequency:00000118|Speed:00003540
ReadValue1:00015578|ReadValue2:00001430|Capture:00005852|Frequency:00000170|Speed:00005100
ReadValue1:00018180|ReadValue2:00010053|Capture:00011873|Frequency:00000084|Speed:00002520
ReadValue1:00006643|ReadValue2:00008935|Capture:00002292|Frequency:00000436|Speed:00013080
ReadValue1:00018627|ReadValue2:00014139|Capture:00015512|Frequency:00000064|Speed:00001920
ReadValue1:00012705|ReadValue2:00002941|Capture:00010236|Frequency:00000097|Speed:00002880
ReadValue1:00008089|ReadValue2:00014290|Capture:00006201|Frequency:00000161|Speed:00004800
ReadValue1:00004561|ReadValue2:00007632|Capture:00003071|Frequency:00000325|Speed:00009720
ReadValue1:00001721|ReadValue2:00002314|Capture:00000593|Frequency:00001686|Speed:00050580
ReadValue1:00019566|ReadValue2:00018175|Capture:00018609|Frequency:00000053|Speed:00001560
ReadValue1:00017728|ReadValue2:00014697|Capture:00016969|Frequency:00000058|Speed:00001740
ReadValue1:00016237|ReadValue2:00011823|Capture:00015586|Frequency:00000064|Speed:00001920
高速时的数据稳定
ReadValue1:00014965|ReadValue2:00009932|Capture:00014967|Frequency:00000066|Speed:00001980
ReadValue1:00014945|ReadValue2:00009913|Capture:00014968|Frequency:00000066|Speed:00001980
ReadValue1:00014950|ReadValue2:00009927|Capture:00014977|Frequency:00000066|Speed:00001980
ReadValue1:00014958|ReadValue2:00009938|Capture:00014980|Frequency:00000066|Speed:00001980
ReadValue1:00014965|ReadValue2:00009940|Capture:00014975|Frequency:00000066|Speed:00001980
ReadValue1:00014965|ReadValue2:00009936|Capture:00014971|Frequency:00000066|Speed:00001980
ReadValue1:00014949|ReadValue2:00009931|Capture:00014982|Frequency:00000066|Speed:00001980
ReadValue1:00014988|ReadValue2:00009982|Capture:00014994|Frequency:00000066|Speed:00001980
ReadValue1:00014989|ReadValue2:00009988|Capture:00014999|Frequency:00000066|Speed:00001980
ReadValue1:00014976|ReadValue2:00009967|Capture:00014991|Frequency:00000066|Speed:00001980
ReadValue1:00014984|ReadValue2:00009986|Capture:00015002|Frequency:00000066|Speed:00001980
ReadValue1:00014998|ReadValue2:00010000|Capture:00015002|Frequency:00000066|Speed:00001980
ReadValue1:00014988|ReadValue2:00009998|Capture:00015010|Frequency:00000066|Speed:00001980
中断里的代码
INTERRUPT_HANDLER(TIM2_CC_USART2_RX_IRQHandler,20)
{
if(TIM2_GetFlagStatus(TIM2_FLAG_CC2))
{
TIM2_ClearFlag(TIM2_FLAG_CC2);//清空标志位
//TIM2_ClearITPendingBit(TIM2_IT_CC2);
if(IC3ReadValue1 == 0x00)
{//第一次捕获中断来临
IC3ReadValue1 = TIM2_GetCapture2();//得到第一次捕捉计数值
}
else
{
//第二次捕获中断来临
IC3ReadValue2 = TIM2_GetCapture2();
TIM2->CNTRH = 0x00;//清零计数器高8位
TIM2->CNTRL = 0x00;//清零计数器低8位
TIM2->IER |= 0x02;//CC2IE=1,使能捕获/比较2中断
TIM2->CR2 |= 0x01;//使能定时/计数器
if(IC3ReadValue2 > IC3ReadValue1)
Capture = (IC3ReadValue2 - IC3ReadValue1);
else
//Capture = 0xFFFF + IC3ReadValue2 - IC3ReadValue1;
Capture=20000 + IC3ReadValue2 - IC3ReadValue1; //防止计数到末端的时候 重新计数
Frequency = (uint32_t) (1000000 / Capture);
Speed = Frequency / 2 *60; //速度值肯定不对, 不知公式是如何的?
//USART_SendString("Freq:",6);
//Send_ADC_Value(CLK_GetClockFreq());
//USART_SendByte('|');
USART_SendString("ReadValue1:",11);
Send_ADC_Value(IC3ReadValue1);
USART_SendByte('|');
USART_SendString("ReadValue2:",11);
Send_ADC_Value(IC3ReadValue2);
USART_SendByte('|');
USART_SendString("Capture:",8);
Send_ADC_Value(Capture);
USART_SendByte('|');
USART_SendString("Frequency:",10);
Send_ADC_Value(Frequency);
USART_SendByte('|');
USART_SendString("Speed:",6);
Send_ADC_Value(Speed);
USART_SendByte('\n');
IC3ReadValue1 = 0x00;
//CaptureNumber = 1;
}
}
}
初始化部份
void TIM2_Config(void)
{
GPIO_Init(GPIOB, GPIO_Pin_2, GPIO_Mode_In_PU_No_IT);
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);
/*
初始化
TIM2_Channel_2:管脚接到PB2上,PB2为TIM2_CH2
TIM2_ICPolarity_Rising:捕获上升沿
TIM2_ICSelection_DirectTI:输入捕获为直接输入
TIM2_ICPSC_DIV1:输入1分频
*/
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_Prescaler_16,TIM2_CounterMode_Up, 20000); //16分频=1MHZ, 20000时间2S
TIM2_ICInit(TIM2_Channel_2, TIM2_ICPolarity_Rising, TIM2_ICSelection_DirectTI, TIM2_ICPSC_DIV1, 0x00);
TIM2_ClearFlag(TIM2_FLAG_CC2);//清TIM2_CH2标志位
TIM2_ITConfig(TIM2_IT_CC2, ENABLE);//开启TIM2_CH2中断。
TIM2_CCxCmd(TIM2_Channel_2, ENABLE);//使能输入捕获通道1 引脚PB2
TIM2_Cmd(ENABLE);//使能定时器(TIM2整个模块)
}
void main( void )
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
USART_ClockInit(USART2,USART_Clock_Enable,USART_CPOL_Low,USART_CPHA_2Edge,USART_LastBit_Disable);
CLK_PeripheralClockConfig(CLK_Peripheral_USART2, ENABLE);
USART_DeInit(USART2);
USART_Init(USART2, (u32)115200, USART_WordLength_8b, USART_StopBits_1, \
USART_Parity_No , USART_Mode_Rx|USART_Mode_Tx);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_HalfDuplexCmd(USART2,ENABLE);
USART_Cmd(USART2, ENABLE );
TIM2_Config();
enableInterrupts();
while (1)
{}
}
|
|