stm32 使用定时器捕获功能 测脉冲并在12864上显示
文件内包含所有源码
0error 0warning
开发平台为stm32f107 根据自己需要更改端口
1、该例程为GPIO例程。
2、使用说明
(1)工程文件路径:例程目录\GPIO\MDK-ARM\Project.uvproj。
(2)请使用MDK 4.0以上版本打开,MDK版本过低会导致无法识别工程。
(3)下载调试工具为ULINK。
(4)HEX文件下载到板子后,LED灯闪烁,表明例程运行正确。
3、注意事项
请务必在下载、调试、运行过程中,保持板子上电、ULINK连接并插在电脑上。
单片机源程序如下:
- /****************************************Copyright (c)****************************************************
- **--------------File Info---------------------------------------------------------------------------------
- ** Descriptions: The GPIO LED application function
- **
- **--------------------------------------------------------------------------------------------------------
- ** Created by: AVRman
- ** Version: v1.0
- ** Descriptions: The original version
- **
- **--------------------------------------------------------------------------------------------------------
- ** Modified by:
- ** Modified date:
- ** Version:
- ** Descriptions:
- **
- *********************************************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "LCD_12864.H"
- #include "stm32f10x_it.h"
- #include <stdio.h>
- uint8_t i = 0;
- /* Private functions ---------------------------------------------------------*/
- void GPIO_Configuration(void);
- void TIM_Configuration(void);
- void RCC_Configuration(void);
- void NVIC_Configuration(void);
- /*******************************************************************************
- * Function Name : Delay
- * Description : Delay Time
- * Input : - nCount: Delay Time
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- void Delay (uint32_t nCount)
- {
- for(; nCount != 0; nCount--);
- }
- void SysClock_Init(void)
- {
- ErrorStatus HSEStartUpStatus;
- RCC_DeInit();
- RCC_HSEConfig(RCC_HSE_ON);
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS){
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- FLASH_SetLatency(FLASH_Latency_2);
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- RCC_PCLK2Config(RCC_HCLK_Div1);
- RCC_PCLK1Config(RCC_HCLK_Div2);
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- RCC_PLLCmd(ENABLE);
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
- ;
- }
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- while(RCC_GetSYSCLKSource() != 0x08){
- ;
- }
- }
- }
- /*******************************************************************************
- * Function Name :
- * Description : Programme
- * Input : None
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- int main(void)
- {
- char str[20];
- SysClock_Init(); // 初始化系统时钟 72MHZ
- GPIO_Configuration();
- NVIC_Configuration();
- RCC_Configuration();
- TIM_Configuration();
- /* Infinite loop */
- lcd_init(); //初始化LCD12864
- while (1){
- if(CH2_finish)
- { //多放几个空格,不留痕迹
- sprintf( str, "%d ", (tmp16_CH2) ); //计数值
- lcd_char(0x90,str);
- sprintf( str, "%.1f Hz. ", (8000000.0/tmp16_CH2) ); //保留一位小数
- lcd_char(0x88,str);
- CH2_finish = 0;
- i++;
- Delay(0X01FFFF);
- }
- lcd_char(0x80, "脉冲波捕捉测试");
- i++;
- Delay(0X01FFFF);
- }
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : Configure GPIO Pin
- * Input : None
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /**
- * LED1 -> PC6 , LED2 -> PC7,LED1 -> PC8 , LED2 -> PC9
- */ //管脚配置
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE , ENABLE); //要有这个
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- //no 12864 pin
- //TIM4对应 4路捕捉脚PB6~PB9
- //信号发生器用有夹子的一组才有效
- //GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //管脚重映射
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- }
- void TIM_Configuration(void)
- {
-
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // TIM4 时基
- ///TIM4
- TIM_DeInit(TIM4);
- /*TIM_Prescaler = (720-1);大于2hz小于1khz比较准,计数值<=85 :10us记一次数
- TIM_Prescaler = (18-1): 大于900hz 小于40Khz : 1/4us记一次数
- TIM_Prescaler = (9-1): 大于900hz 小于50Khz :1/8us记一次数
- 计数值小于100都不怎么准了
- */
- //为自动重装值,与普通单片机一样,满值基本不溢出65535
- TIM_TimeBaseStructure.TIM_Period = 0xffff;
- //预分频值, 使TIMx_CLK=100KHz ,系统时钟运行于72M时720分频,定时器运行于100KHZ,即10us每分度
- TIM_TimeBaseStructure.TIM_Prescaler = (9-1);
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //输入时钟不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- //ch2
- // TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP; //输入捕捉方式
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;// //输入通道
- 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 = 0x0; //捕捉输入不滤波
- TIM_ICInit(TIM4, &TIM_ICInitStructure);
-
- //TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //选择IC2为始终触发源
- //TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
- //TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发
- //ch2
- //TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;//| TIM_Channel_2; //输入通道
- // 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 = 0x0; //捕捉输入不滤波
- // TIM_ICInit(TIM4, &TIM_ICInitStructure);
- //
- // TIM_ICInitStructure.TIM_Channel = TIM_Channel_3 ;//TIM_Channel_2; //输入通道
- // //ch3
- // 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 = 0x0; //捕捉输入不滤波
- // TIM_ICInit(TIM4, &TIM_ICInitStructure);
- //
- // TIM_ICInitStructure.TIM_Channel = TIM_Channel_4 ;//| TIM_Channel_2; //输入通道
- // //ch4
- // TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //捕捉上升沿
- //
- // TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //捕捉中断
- //
- // TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //捕捉不分频
- //
- // TIM_ICInitStructure.TIM_ICFilter = 0xf; //捕捉输入不滤波
- // TIM_ICInit(TIM4, &TIM_ICInitStructure);
- // /* TIM enable counter */
- TIM_Cmd(TIM4, ENABLE);
- /* Enable the CC2 Interrupt Request */
- // TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
- TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
- // TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
- // TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE); //第四路有问题
- }
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
- |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
- |RCC_APB2Periph_GPIOD |RCC_APB2Periph_GPIOE
- | RCC_APB2Periph_AFIO , ENABLE );
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE );
-
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置优先级分组:先占优先级0位,从优先级4位
- /* Enable the TIM4 global Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /*********************************************************************************************************
- END FILE
- *********************************************************************************************************/
复制代码
所有资料51hei提供下载:
定时器捕捉 测脉冲 测转速12864显示.rar
(585.26 KB, 下载次数: 102)
|