打印
[STM8]

实验分享-【STM8-SO8】04-定时器TIM4的使用

[复制链接]
370|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
STM8S001 内部有 3 个定时器 TIM1、TIM2、TIM4,其中 TIM4 是一个 8 位的定时器,架构与功能算是比较简单的一个,很适合做为时基的使用,基本用法如下:

main 回圈:
  • void main(void)
  • {
  •   /* Clock configuration -----------------------------------------*/
  •   CLK_Config();
  •   /* TIM4 configuration -----------------------------------------*/
  •   TIM4_Config();
  •   while (1)
  •   {
  •   }
  • }


[color=rgb(51, 102, 153) !important]复制代码


将系统时钟主频设定在 16Mhz:
  • static void CLK_Config(void)
  • {
  •   /* Initialization of the clock */
  •   /* Clock divider to HSI/1 */
  •   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  • }


[color=rgb(51, 102, 153) !important]复制代码


将 TIM4 中断设定在 1ms 中断一次:
  • #define TIM4_PERIOD       124


[color=rgb(51, 102, 153) !important]复制代码

  • static void TIM4_Config(void)
  • {
  •   /* Time base configuration */
  •   TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
  •   /* Clear TIM4 update flag */
  •   TIM4_ClearFlag(TIM4_FLAG_UPDATE);
  •   /* Enable update interrupt */
  •   TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  •   /* enable interrupts */
  •   enableInterrupts();
  •   /* Enable TIM4 */
  •   TIM4_Cmd(ENABLE);
  • }


[color=rgb(51, 102, 153) !important]复制代码


16Mhz / 128 = 125Khz
125Khz / 125 = 1Khz --> 1ms

TIM4 中断程序在 stm8s_it.c 这里:
  • INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  • {
  •   /* Clear Interrupt Pending bit */
  •   TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  • }


[color=rgb(51, 102, 153) !important]复制代码


前面的 LAB 有使用到按键输入的功能,对于按键输入的判断我习惯上都会搭配时基计数器的方式,在这一个程序当中我用这种方式再来实现一个按按键让 LED 翻转显示的功能。



uint8_t key_cnt;

宣告一个全局变量 key_cnt 做为按键 KEY 的状态计数,按键放开时 key_cnt 清除为 0,按键按下时每一个时基 key_cnt 就会加一,在主回圈判断 key_cnt 等于 20 时为按键初按下状态。LED1 固定以 0.5s 的周期闪烁,而按下按键 KEY 之后 LED2 翻转显示。

main 回圈:
  • void main(void)
  • {
  •   /* Clock configuration -----------------------------------------*/
  •   CLK_Config();
  •   /* TIM4 configuration -----------------------------------------*/
  •   TIM4_Config();
  •   GPIO_Init(LED1_GPIO_PORT, LED1_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
  •   GPIO_Init(LED2_GPIO_PORT, LED2_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
  •   GPIO_Init(KEY_GPIO_PORT, KEY_GPIO_PINS, GPIO_MODE_IN_PU_NO_IT);
  •   while (1)
  •   {
  •     if(key_cnt == 20)
  •     {
  •       key_cnt++;
  •       GPIO_WriteReverse(LED2_GPIO_PORT, LED2_GPIO_PINS);
  •     }
  •   }
  • }


[color=rgb(51, 102, 153) !important]复制代码


TIM4 中断程序:
  • INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  • {
  •   static uint16_t x = 0;
  •   if(++x == 500)      // 0.5s
  •   {
  •     x = 0;
  •     GPIO_WriteReverse(LED1_GPIO_PORT, LED1_GPIO_PINS);        // LED1 flash
  •   }
  •   if(GPIO_ReadInputPin(KEY_GPIO_PORT, KEY_GPIO_PINS))
  •   {
  •     key_cnt = 0;
  •   }
  •   else
  •   {
  •     if(key_cnt != 0xff)
  •       key_cnt++;
  •   }
  •   /* Clear Interrupt Pending bit */
  •   TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  • }


[color=rgb(51, 102, 153) !important]复制代码


运行结果:
本文转载于【STM8-SO8】04-定时器TIM4的使用
http://www.stmcu.org.cn/module/forum/thread-619349-1-1.html


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:每天都要开心呀

210

主题

505

帖子

4

粉丝