TIM1输入捕获测量方波信号周期
程序员文章站
2022-07-13 21:11:47
...
//main.c
#include"stm8s.h"
#include"main.h"
#include"tm1638.h"
extern volatile u16 captureValue1,captureValue2;//存放俩次捕获的值
volatile u8 successFlag=0;//连续俩次成功捕获将该位置一
extern volatile u16 numberOfCapture;//捕获次数
u32 frequency=0;//频率
int main(void)
{
CLK->CKDIVR=0X00;//主时钟HSI一分频,即为16MHz
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);//PC4 COO输出引脚
GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);//TIM1_CH1通道引脚
Tim1_Init();//TIM1初始化
init_TM1638();//该模块用以将频率值显示在数码管上
rim();//开总中断
CLK_CCOConfigCLK_CCOConfig(CLK_OUTPUT_LSI);//输出LSI时钟128KHz
CLK_CCOCmd(ENABLE);//开启CCO
while(1)
{
if(successFlag==1)
{
frequency=(8*16000000UL)/(captureValue2-captureValue1);
frequency/=100;//以KHz为单位,小数点后一位
successFlag=0;
Display(frequency); //显示频率值
successFlag=0;
TIM1_CCxCmd(TIM1_CHANNEL_1,ENABLE);//开启TIM1_CH1通道的输入捕获
}
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(u8* file,u32 line)
{
while(1)
{
}
}
#endif
//main.h
#ifndef _MAIN_H
#define _MAIN_H
#include"stm8s.h"
#include"tm1638.h"
u8 qian,bai,shi,ge,wan;
extern u32 frequency;
extern unsigned char tab[];
void Tim1_Init(void)
{
TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING,TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8,0x00);
//TIM1_CH1通道,上升沿,八分频
TIM1_UpdateRequestConfig(TIM1_UPDATESOURCE_REGULAR);//更新事件中断
TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE);//产生一个更新事件,更新寄存器
TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);//使能更新中断
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);//开启捕获中断
TIM1_Cmd(ENABLE);//开TIM1
TIM1_CCxCmd(TIM1_CHANNEL_1,ENABLE);//开捕获
}
void Display(u32 frequency)
{
wan=frequency/10000;
qian=frequency%10000/1000;
bai=frequency%1000/100;
shi=frequency%100/10;
ge=frequency%10;
Write_DATA(0<<1,tab[wan]);
Write_DATA(1<<1,tab[qian]);
Write_DATA(2<<1,tab[bai]);
Write_DATA(3<<1,tab[shi]|0x80);//带小数点
Write_DATA(4<<1,tab[ge]);
}
#endif
//stm8s_it.c
#include"stm8s_it.h"
extern volatile u16 count;//用于TIM4计数
volatile u8 numberOfCapture;
volatile u16
captureValue1=0,captureValue2=0;
extern volatile u8 successFlag;
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler,11)
{
/*
In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
TIM1_ClearITPendingBit(TIM1_IT_UPDATE);//清除溢出中断标志
numberOfCapture=0;//捕获过程发生中断则重新捕获
}
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler,12)
{
/*
In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if((TIM1->SR1&TIM1_FLAG_CC1)!=0)
{
if(numberOfCapture==0)//第一次捕获
{
captureValue1=TIM1_GetCapture1();
numberOfCapture=1;
}
else//第二次捕获
{
TIM1_CCxCmd(TIM1_CHANNEL_1,DISABLE);
captureValue2=TIM1_GetCapture1();
numberOfCapture=0;
successFlag=1;
}
}
}
上一篇: k8s离线部署学习-Harbor仓库部署
下一篇: kubernetes部署-ETCD(一)
推荐阅读