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)
2017-12-14 23:46 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|