//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;
}
}
}
上一篇:STM8S专题之GPIO输入输出
下一篇:基于STM8S的无感方波BLDC控制
推荐阅读最新更新时间:2024-11-04 20:53
设计资源 培训 开发板 精华推荐
- 【物联网】鸿蒙训练营_323380
- 使用 Analog Devices 的 LTC2908IDDB-C1 的参考设计
- 单片机自动清洁机器人设计(电路图+原理图+流程图+源程序)
- AD420-EB,使用 AD420 的评估板,16 位串行输入,4-20mA、0-20mA 数模转换器
- 黄淮学院创客空间立创杯电子设计大赛-尹本行-1002434A
- 使用 Infineon Technologies AG 的 OM7620ST 的参考设计
- USBI2CCLICK,基于 MCP2221 USB 2.0 至 I2C/UART 协议转换器的评估套件
- LT8410EDC-1 演示板,在 3V 输入电压下为 0.5mA (min) 具有输出断开连接的超低功率升压转换器
- AM2G-1203SH30Z 3.3V 2瓦DC-DC转换器的典型应用
- AM2S-0505SZ 5V 2瓦直流转直流转换器的典型应用