找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 5526|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32定時(shí)器捕捉 測(cè)脈沖 測(cè)轉(zhuǎn)速12864顯示源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:261733 發(fā)表于 2017-12-14 20:54 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
stm32 使用定時(shí)器捕獲功能 測(cè)脈沖并在12864上顯示
文件內(nèi)包含所有源碼
0error 0warning
開發(fā)平臺(tái)為stm32f107 根據(jù)自己需要更改端口

1、該例程為GPIO例程。

2、使用說明
   (1)工程文件路徑:例程目錄\GPIO\MDK-ARM\Project.uvproj。
   (2)請(qǐng)使用MDK 4.0以上版本打開,MDK版本過低會(huì)導(dǎo)致無法識(shí)別工程。
   (3)下載調(diào)試工具為ULINK。
   (4)HEX文件下載到板子后,LED燈閃爍,表明例程運(yùn)行正確。
   
3、注意事項(xiàng)
   請(qǐng)務(wù)必在下載、調(diào)試、運(yùn)行過程中,保持板子上電、ULINK連接并插在電腦上。

單片機(jī)源程序如下:
  1. /****************************************Copyright (c)****************************************************
  2. **--------------File Info---------------------------------------------------------------------------------
  3. ** Descriptions:            The GPIO LED application function
  4. **
  5. **--------------------------------------------------------------------------------------------------------
  6. ** Created by:              AVRman
  7. ** Version:                 v1.0
  8. ** Descriptions:            The original version
  9. **
  10. **--------------------------------------------------------------------------------------------------------
  11. ** Modified by:            
  12. ** Modified date:           
  13. ** Version:                 
  14. ** Descriptions:            
  15. **
  16. *********************************************************************************************************/

  17. /* Includes ------------------------------------------------------------------*/
  18. #include "stm32f10x.h"
  19. #include "LCD_12864.H"
  20. #include "stm32f10x_it.h"
  21. #include <stdio.h>

  22. uint8_t   i = 0;
  23. /* Private functions ---------------------------------------------------------*/
  24. void GPIO_Configuration(void);
  25. void TIM_Configuration(void);
  26. void RCC_Configuration(void);
  27. void NVIC_Configuration(void);
  28. /*******************************************************************************
  29. * Function Name  : Delay
  30. * Description    : Delay Time
  31. * Input          : - nCount: Delay Time
  32. * Output         : None
  33. * Return         : None
  34. * Attention                 : None
  35. *******************************************************************************/
  36. void  Delay (uint32_t nCount)
  37. {
  38.   for(; nCount != 0; nCount--);
  39. }

  40. void SysClock_Init(void)
  41. {
  42.         ErrorStatus HSEStartUpStatus;     
  43.         RCC_DeInit();
  44.         RCC_HSEConfig(RCC_HSE_ON);
  45.         HSEStartUpStatus = RCC_WaitForHSEStartUp();
  46.         if(HSEStartUpStatus == SUCCESS){
  47.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  48.                 FLASH_SetLatency(FLASH_Latency_2);
  49.             RCC_HCLKConfig(RCC_SYSCLK_Div1);           
  50.             RCC_PCLK2Config(RCC_HCLK_Div1);         
  51.             RCC_PCLK1Config(RCC_HCLK_Div2);        
  52.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);        
  53.             RCC_PLLCmd(ENABLE);        
  54.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
  55.                     ;
  56.                 }        
  57.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);        
  58.             while(RCC_GetSYSCLKSource() != 0x08){
  59.                     ;
  60.                 }
  61.         }
  62. }
  63. /*******************************************************************************
  64. * Function Name  :
  65. * Description    :  Programme
  66. * Input          : None
  67. * Output         : None
  68. * Return         : None
  69. * Attention                 : None
  70. *******************************************************************************/
  71. int main(void)
  72. {
  73.         char        str[20];
  74.         SysClock_Init();                 // 初始化系統(tǒng)時(shí)鐘 72MHZ
  75.         GPIO_Configuration();
  76.         NVIC_Configuration();
  77.         RCC_Configuration();
  78.         TIM_Configuration();
  79.     /* Infinite loop */
  80.         lcd_init();           //初始化LCD12864
  81.     while (1){
  82.                 if(CH2_finish)
  83.                 {                                 //多放幾個(gè)空格,不留痕跡
  84.                         sprintf( str, "%d      ", (tmp16_CH2) );         //計(jì)數(shù)值
  85.                         lcd_char(0x90,str);
  86.                         sprintf( str, "%.1f Hz.      ", (8000000.0/tmp16_CH2) );         //保留一位小數(shù)
  87.                         lcd_char(0x88,str);        
  88.                         CH2_finish = 0;        
  89.                         i++;
  90.                         Delay(0X01FFFF);
  91.                 }
  92.                 lcd_char(0x80, "脈沖波捕捉測(cè)試");
  93.                 i++;
  94.                         Delay(0X01FFFF);
  95.     }
  96. }

  97. /*******************************************************************************
  98. * Function Name  : GPIO_Configuration
  99. * Description    : Configure GPIO Pin
  100. * Input          : None
  101. * Output         : None
  102. * Return         : None
  103. * Attention                 : None
  104. *******************************************************************************/
  105. void GPIO_Configuration(void)
  106. {
  107.   GPIO_InitTypeDef GPIO_InitStructure;                                                
  108.   /**
  109.   *  LED1 -> PC6 , LED2 -> PC7,LED1 -> PC8 , LED2 -> PC9
  110.   */                                                         //管腳配置
  111.   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE , ENABLE); //要有這個(gè)
  112.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;  
  113.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  114.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  115.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  116.   //no 12864 pin


  117.     //TIM4對(duì)應(yīng) 4路捕捉腳PB6~PB9
  118.         //信號(hào)發(fā)生器用有夾子的一組才有效
  119.         //GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);                //管腳重映射
  120.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;  
  121.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  122.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
  123.   GPIO_Init(GPIOB, &GPIO_InitStructure);


  124. }

  125. void TIM_Configuration(void)
  126. {
  127.    
  128.         TIM_ICInitTypeDef  TIM_ICInitStructure;
  129.          TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure; // TIM4 時(shí)基
  130. ///TIM4
  131.         TIM_DeInit(TIM4);
  132.             /*TIM_Prescaler = (720-1);大于2hz小于1khz比較準(zhǔn),計(jì)數(shù)值<=85  :10us記一次數(shù)
  133.                   TIM_Prescaler = (18-1): 大于900hz  小于40Khz                                  :        1/4us記一次數(shù)
  134.                   TIM_Prescaler = (9-1): 大于900hz   小于50Khz                                  :1/8us記一次數(shù)
  135.                   計(jì)數(shù)值小于100都不怎么準(zhǔn)了
  136.                 */         
  137.         //為自動(dòng)重裝值,與普通單片機(jī)一樣,滿值基本不溢出65535
  138.     TIM_TimeBaseStructure.TIM_Period = 0xffff;                           
  139.         //預(yù)分頻值, 使TIMx_CLK=100KHz ,系統(tǒng)時(shí)鐘運(yùn)行于72M時(shí)720分頻,定時(shí)器運(yùn)行于100KHZ,即10us每分度
  140.     TIM_TimeBaseStructure.TIM_Prescaler = (9-1);     
  141.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;       //輸入時(shí)鐘不分頻
  142.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //向上計(jì)數(shù)
  143.     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  144.                                       
  145.         //ch2
  146. //  TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP;     //輸入捕捉方式
  147.     TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//      //輸入通道
  148.         TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;     //捕捉上升沿
  149.         TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;    //捕捉中斷
  150.         TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;       //捕捉不分頻  
  151.         TIM_ICInitStructure.TIM_ICFilter = 0x0;          //捕捉輸入不濾波
  152.     TIM_ICInit(TIM4, &TIM_ICInitStructure);
  153.    
  154.   //TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //選擇IC2為始終觸發(fā)源
  155.   //TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//TIM從模式:觸發(fā)信號(hào)的上升沿重新初始化計(jì)數(shù)器和觸發(fā)寄存器的更新事件
  156.   //TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //啟動(dòng)定時(shí)器的被動(dòng)觸發(fā)
  157. //ch2
  158. //TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;//| TIM_Channel_2; //輸入通道

  159. //        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;     //捕捉上升沿
  160. //  
  161. //        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;    //捕捉中斷
  162. //  
  163. //        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;       //捕捉不分頻
  164. //  
  165. //        TIM_ICInitStructure.TIM_ICFilter = 0x0;          //捕捉輸入不濾波
  166. //  TIM_ICInit(TIM4, &TIM_ICInitStructure);
  167. //  
  168. //  TIM_ICInitStructure.TIM_Channel = TIM_Channel_3 ;//TIM_Channel_2;        //輸入通道
  169. //  //ch3
  170. //        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;     //捕捉上升沿
  171. //  
  172. //        TIM_ICInitStructure.TIM_ICSelection =         TIM_ICSelection_DirectTI;    //捕捉中斷
  173. //  
  174. //        TIM_ICInitStructure.TIM_ICPrescaler =         TIM_ICPSC_DIV1;       //捕捉不分頻
  175. //  
  176. //        TIM_ICInitStructure.TIM_ICFilter = 0x0;          //捕捉輸入不濾波
  177. //  TIM_ICInit(TIM4, &TIM_ICInitStructure);
  178. //
  179. //  TIM_ICInitStructure.TIM_Channel = TIM_Channel_4 ;//| TIM_Channel_2;        //輸入通道
  180. //          //ch4
  181. //        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;     //捕捉上升沿
  182. //  
  183. //        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;    //捕捉中斷
  184. //  
  185. //        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;       //捕捉不分頻
  186. //  
  187. //        TIM_ICInitStructure.TIM_ICFilter = 0xf;          //捕捉輸入不濾波
  188. //          TIM_ICInit(TIM4, &TIM_ICInitStructure);
  189. //  /* TIM enable counter */
  190.           TIM_Cmd(TIM4, ENABLE);
  191.   /* Enable the CC2 Interrupt Request */
  192. //         TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
  193.           TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
  194. //        TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
  195. //          TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE);        //第四路有問題
  196. }
  197. void RCC_Configuration(void)
  198. {
  199.         SystemInit();        
  200.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
  201.                            |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
  202.                            |RCC_APB2Periph_GPIOD |RCC_APB2Periph_GPIOE
  203.                                                         | RCC_APB2Periph_AFIO , ENABLE );
  204.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE );
  205.         
  206. }
  207. void NVIC_Configuration(void)
  208. {
  209.   NVIC_InitTypeDef NVIC_InitStructure;

  210.           /* Configure the NVIC Preemption Priority Bits */  
  211.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);        //設(shè)置優(yōu)先級(jí)分組:先占優(yōu)先級(jí)0位,從優(yōu)先級(jí)4位
  212.     /* Enable the TIM4 global Interrupt */
  213.         NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM4中斷
  214.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占優(yōu)先級(jí)0級(jí)
  215.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //從優(yōu)先級(jí)3級(jí)
  216.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  217.         NVIC_Init(&NVIC_InitStructure);  //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)NVIC寄存器        
  218. }
  219. #ifdef  USE_FULL_ASSERT

  220. /**
  221.   * @brief  Reports the name of the source file and the source line number
  222.   *   where the assert_param error has occurred.
  223.   * @param  file: pointer to the source file name
  224.   * @param  line: assert_param error line source number
  225.   * @retval None
  226.   */
  227. void assert_failed(uint8_t* file, uint32_t line)
  228. {
  229.   /* User can add his own implementation to report the file name and line number,
  230.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  231.   /* Infinite loop */
  232.   while (1)
  233.   {
  234.   }
  235. }
  236. #endif

  237. /*********************************************************************************************************
  238.       END FILE
  239. *********************************************************************************************************/
復(fù)制代碼

所有資料51hei提供下載:
定時(shí)器捕捉 測(cè)脈沖 測(cè)轉(zhuǎn)速12864顯示.rar (585.26 KB, 下載次數(shù): 102)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表