你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

TIM5定时器点亮LED

[复制链接]
在路上11111111 发布时间:2017-3-9 21:06
  1. #include "stm32f10x.h"
  2. #include "stdio.h"
  3. uint8_t flag = 0;
  4. void STM32_LED_Configuration(void)
  5. {
  6.         GPIO_InitTypeDef  GPIO_InitStructure;
  7.        
  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
  9.        
  10.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  11.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
  13.         GPIO_Init(GPIOD,&GPIO_InitStructure);
  14.        
  15.         GPIOD->BSRR = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
  16. }
  17. void NVIC_TIM5Configuration(void)
  18. {
  19.         NVIC_InitTypeDef  NVIC_InitStructure;
  20.        
  21.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  22.         NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
  23.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  24.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  25.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  26.   NVIC_Init(&NVIC_InitStructure);
  27. }

  28. void TIM5_Init(void)
  29. {
  30.         TIM_TimeBaseInitTypeDef  TIM_Time_BaseInitStructure;
  31.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);//打开TIM5的时钟;
  32.        
  33.         TIM_Time_BaseInitStructure.TIM_Prescaler = (100 - 1);//预分频器寄存器1ms
  34.         //这个是你要设置的预分频器的数值;
  35.         //就是将你的频率分成多少比如说你要将其分频成36000HZ的话就是预分频的数值设置成72000000/36000;
  36.         //这个值就是用来和计数值相互合作的,可以减少一个数值的使用大小的范围;
  37.         //16位可编程(可以实时修改)预分频器,计数器时钟频率的分频系数为1~65536之间的任意
  38.        
  39.         TIM_Time_BaseInitStructure.TIM_Period = (720 - 1);//自动装载寄存器
  40.         //这个是你要自动重装载的值;
  41.         //只要这两个数是72M的整数倍即可(72M是APB1没有进行分频的情况下的数值,如果分频 的话
  42.         //需要的就是*2,
  43.         TIM_Time_BaseInitStructure.TIM_ClockDivision = 0;//这个就是一个时钟分割;现阶段没怎么用的到
  44.        
  45.         TIM_Time_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Down;
  46.         //计数的方式;
  47.         //向下计数;
  48.        
  49.         TIM_TimeBaseInit(TIM5,&TIM_Time_BaseInitStructure);
  50.         //初始化TIM5
  51.        
  52.         TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
  53.        
  54.        
  55.         TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
  56.         //这个是使能中断;
  57.         TIM_Cmd(TIM5,ENABLE);
  58.         //使能TIM5
  59. }
  60. void TIM5_IRQHandler(void)
  61. {
  62.         if(TIM_GetITStatus(TIM5,TIM_IT_Update) != RESET)
  63.         {
  64.     TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
  65.                 flag = 1;
  66.     GPIOD->ODR ^= GPIO_Pin_7;
  67.         }
  68. }       
  69. void STM32_USART_Config()
  70. {
  71.         USART_InitTypeDef USART_InitStructure;
  72.         GPIO_InitTypeDef  GPIO_InitStructure;
  73.        
  74.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
  75.         GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
  76.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
  77.        
  78.        
  79.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  80.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  81.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  82.         GPIO_Init(GPIOD,&GPIO_InitStructure);
  83.        
  84.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  85.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  86.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  87.         GPIO_Init(GPIOD,&GPIO_InitStructure);
  88.        
  89.         USART_InitStructure.USART_BaudRate = 115200;
  90.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  91.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  92.         USART_InitStructure.USART_Parity = USART_Parity_No;
  93.         USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
  94.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  95.         USART_Init(USART2,&USART_InitStructure);
  96.        
  97.         USART_Cmd(USART2,ENABLE);
  98. }

  99. #ifndef MicroLIB
  100. //#pragma import(__use_no_semihosting)             //没有实现fgetc时需要声明该参数   
  101. /* 标准库需要的支持函数 使用printf()调试打印不需要实现该函数 */               
  102. struct __FILE
  103. {
  104.         int handle;
  105.     /* Whatever you require here. If the only file you are using is */   
  106.     /* standard output using printf() for debugging, no file handling */   
  107.     /* is required. */
  108. };

  109. FILE __stdout;      
  110. //定义_sys_exit()以避免使用半主机模式   
  111. _sys_exit(int x)
  112. {
  113.         x = x;
  114. }
  115. /* 重定义fputc函数 如果使用MicroLIB只需要重定义fputc函数即可 */  
  116. int fputc(int ch, FILE *f)
  117. {
  118.     /* Place your implementation of fputc here */

  119.          /* e.g. write a character to the USART */
  120.     USART_SendData(USART2, (uint8_t) ch);

  121.     /* Loop until the end of transmission */
  122.     while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
  123.     {}

  124.     return ch;
  125. }
  126. /*
  127. 可以直接使用putchar
  128. 不需要再定义 int putchar(int ch),因为stdio.h中有如下定义
  129. #define putchar(c) putc(c, stdout)
  130. */

  131. int ferror(FILE *f) {  
  132.     /* Your implementation of ferror */  
  133.     return EOF;  
  134. }
  135. #endif

  136. FILE __stdin;

  137. int fgetc(FILE *fp)
  138. {
  139.         int ch = 0;
  140.        
  141.     while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
  142.     {
  143.     }

  144.     ch = (int)USART2->DR & 0xFF;
  145.        
  146.     putchar(ch); //回显
  147.        
  148.         return ch;
  149. }

  150. int main()
  151. {
  152.         uint8_t  Count =0;
  153.         uint32_t PwmValue = 0;
  154.         uint32_t DirValue = 0;
  155.         STM32_LED_Configuration();
  156.   NVIC_TIM5Configuration();
  157.         TIM5_Init();
  158.         STM32_USART_Config();
  159.        
  160.         while(1)
  161.         {

  162.                 if(flag == 1)
  163.                 {
  164.                         flag = 0;
  165.                         printf("\r\n   TIMI的实验,第%d个周期发送数据 \r\n",Count++);
  166.                 }
  167.                
  168.         }
  169. }


  170. <img src="https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=390661&size=300x300&key=254b7fc2b1c5f5b3&nocache=yes&type=fixnone" aid="attachimg_390661" alt="" border="0">

  171. 在下有个两个问题想问一下:
  172. 1:为什么我设置的是1ms去点亮一下,为什么测试的是500HZ;
  173. 2:我在改定时器的数值大小的时候,为什么我改到频率的数值好几M以后,还是显示300KHz;
  174. 希望大家帮我解决一下
复制代码


为啥是500HZ不是1000HZ

为啥是500HZ不是1000HZ

评分

参与人数 1 ST金币 +10 收起 理由
zero99 + 10

查看全部评分

收藏 1 评论2 发布时间:2017-3-9 21:06

举报

2个回答
zhao.zhao 回答时间:2017-3-9 21:28:12
1、中断1ms高电平1ms取反为低电平,方波一个周期=2ms,就是500HZ;
2、第二个我是猜想的,频率高了,系统频繁中断,实际上这些都是需要频繁执行执行压栈出栈指令的,因此时间都耗在这里了,所以频率高不了;解决的办法可以用定时器做50%输出的PWM波,频率肯定可以提高,但最高到多少我也没试过。
在路上11111111 回答时间:2017-3-9 21:34:37
zhao.zhao 发表于 2017-3-9 21:28
1、中断1ms高电平1ms取反为低电平,方波一个周期=2ms,就是500HZ;
2、第二个我是猜想的,频率高了,系统频 ...

懂了,谢谢,嘿嘿

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版