找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4899|回复: 0
打印 上一主题 下一主题
收起左侧

stm32定时器捕捉 测脉冲 测转速12864显示源程序

[复制链接]
跳转到指定楼层
楼主
ID:261733 发表于 2017-12-14 20:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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连接并插在电脑上。

单片机源程序如下:
  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();                 // 初始化系统时钟 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.                 {                                 //多放几个空格,不留痕迹
  84.                         sprintf( str, "%d      ", (tmp16_CH2) );         //计数值
  85.                         lcd_char(0x90,str);
  86.                         sprintf( str, "%.1f Hz.      ", (8000000.0/tmp16_CH2) );         //保留一位小数
  87.                         lcd_char(0x88,str);        
  88.                         CH2_finish = 0;        
  89.                         i++;
  90.                         Delay(0X01FFFF);
  91.                 }
  92.                 lcd_char(0x80, "脉冲波捕捉测试");
  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); //要有这个
  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对应 4路捕捉脚PB6~PB9
  118.         //信号发生器用有夹子的一组才有效
  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 时基
  130. ///TIM4
  131.         TIM_DeInit(TIM4);
  132.             /*TIM_Prescaler = (720-1);大于2hz小于1khz比较准,计数值<=85  :10us记一次数
  133.                   TIM_Prescaler = (18-1): 大于900hz  小于40Khz                                  :        1/4us记一次数
  134.                   TIM_Prescaler = (9-1): 大于900hz   小于50Khz                                  :1/8us记一次数
  135.                   计数值小于100都不怎么准了
  136.                 */         
  137.         //为自动重装值,与普通单片机一样,满值基本不溢出65535
  138.     TIM_TimeBaseStructure.TIM_Period = 0xffff;                           
  139.         //预分频值, 使TIMx_CLK=100KHz ,系统时钟运行于72M时720分频,定时器运行于100KHZ,即10us每分度
  140.     TIM_TimeBaseStructure.TIM_Prescaler = (9-1);     
  141.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;       //输入时钟不分频
  142.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //向上计数
  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为始终触发源
  155.   //TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
  156.   //TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发
  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);        //设置优先级分组:先占优先级0位,从优先级4位
  212.     /* Enable the TIM4 global Interrupt */
  213.         NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM4中断
  214.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
  215.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //从优先级3级
  216.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  217.         NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设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. *********************************************************************************************************/
复制代码

所有资料51hei提供下载:
定时器捕捉 测脉冲 测转速12864显示.rar (585.26 KB, 下载次数: 100)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表