Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233443
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: 嵌入式

2016-08-01 15:22:51

1、定时器概述
    STM8S提供三种类型的 TIM 定时器:高级控制型(TIM1)、通用型(TIM2/TIM3/TIM5)和基本型定
时器(TIM4/TIM6)。它们虽有不同功能但都基于共同的架构。此共同的架构使得采用各个定时器
来设计应用变得非常容易与方便(相同的寄存器映射,相同的基本功能)。

2、现在来介绍高级定时器1的配置和应用。
代码如下:
main.c

点击(此处)折叠或打开

  1. /* MAIN.C file
  2.  *
  3.  * Copyright (c) 2002-2005 STMicroelectronics
  4.  */
  5. #include "stm8s.h"
  6. #include "stm8s003f3p.h"
  7. #include "stm8s_conf.h"

  8. void GPIOInit(void);
  9. void Time1_Init(void);


  10. void main(void)
  11. {
  12.     
  13.     CLK_HSECmd(DISABLE); //关闭外部高速振荡器
  14.     CLK_HSICmd(ENABLE);     //启用内部高速时钟
  15.     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //设置 内部 时钟16M 为主时钟(默认是8分配2MHz)
  16.     GPIOInit();
  17.     Time1_Init();
  18.     while (1){
  19.         
  20.     }
  21. }

  22. void GPIOInit(void)
  23. {
  24.         GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
  25.         GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
  26.         
  27. }

  28. void Time1_Init(void)
  29. {
  30.     TIM1_DeInit();                                                                //恢复为默认值
  31.     TIM1_TimeBaseInit(0x0000, TIM1_COUNTERMODE_UP, 0x3e80, 0x00); //不分频16MHz 计数值16000 1ms中断一次
  32.     TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE);    //手册里有说明,不设置的话就不会产生中断
  33.     TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);                //配置更新中断
  34.     TIM1_Cmd(ENABLE);                                                            //使能定时器
  35.     enableInterrupts();                                                        //开启总中断
  36. }
  37.     
  38.     
  39. @far @interrupt void Timer4_interrupt_handler_t(void) //1ms定时中断
  40. {
  41.         static unsigned int count = 0;
  42.         static unsigned char falg = 0;
  43.     
  44.     TIM1_ClearITPendingBit(TIM1_IT_UPDATE); //TIM1_SR = 0,清除中断标志    
  45.         
  46.         if(count<1000){
  47.                 
  48.                 GPIO_WriteHigh(GPIOC, GPIO_PIN_4);    
  49.                 GPIO_WriteLow(GPIOC, GPIO_PIN_3);
  50.         }
  51.         else if(count>=1000)
  52.         {
  53.             
  54.             GPIO_WriteHigh(GPIOC, GPIO_PIN_3);
  55.             GPIO_WriteLow(GPIOC, GPIO_PIN_4);
  56.         }
  57.     
  58.         if(count >=2000)count =0;
  59.         count++;
  60. }


然后还有一个很重要的地方,就是在stm8_interrupt_vector.c中注册中断服务函数。

stm8_interrupt_vector.c

点击(此处)折叠或打开

  1. /*    BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
  2.  *    Copyright (c) 2007 STMicroelectronics
  3.  */

  4. typedef void @far (*interrupt_handler_t)(void);

  5. struct interrupt_vector {
  6.     unsigned char interrupt_instruction;
  7.     interrupt_handler_t interrupt_handler;
  8. };

  9. @far @interrupt void NonHandledInterrupt (void)
  10. {
  11.     /* in order to detect unexpected events during development,
  12.      it is recommended to set a breakpoint on the following instruction
  13.     */
  14.     return;
  15. }

  16. extern void _stext(); /* startup routine */
  17. extern @far @interrupt void Timer4_interrupt_handler_t(void);
  18. struct interrupt_vector const _vectab[] = {
  19.     {0x82, (interrupt_handler_t)_stext}, /* reset */
  20.     {0x82, NonHandledInterrupt}, /* trap */
  21.     {0x82, NonHandledInterrupt}, /* TLI */
  22.     {0x82, NonHandledInterrupt}, /* AWU */
  23.     {0x82, NonHandledInterrupt}, /* CLK */
  24.     {0x82, NonHandledInterrupt}, /* EXTI0 Port A */
  25.     {0x82, NonHandledInterrupt}, /* EXTI1 Port B */
  26.     {0x82, NonHandledInterrupt}, /* EXTI2 Port C */
  27.     {0x82, NonHandledInterrupt}, /* EXTI3 Port D */
  28.     {0x82, NonHandledInterrupt}, /* EXTI4 Port E */
  29.     {0x82, NonHandledInterrupt}, /* CAN RX */
  30.     {0x82, NonHandledInterrupt}, /* CAN TX */
  31.     {0x82, NonHandledInterrupt}, /* SPI (End of transfer) */
  32.     {0x82, (interrupt_handler_t)Timer4_interrupt_handler_t}, /* TIM1 update/ overflow/ underflow/trigger/ break */
  33.     {0x82, NonHandledInterrupt}, /* TIM1 capture/ compare */
  34.     {0x82, NonHandledInterrupt}, /* TIM2 update/ overflow */
  35.     {0x82, NonHandledInterrupt}, /* TIM2 capture/ compare */
  36.     {0x82, NonHandledInterrupt}, /* TIM3 update/ overflow */
  37.     {0x82, NonHandledInterrupt}, /* TIM3 capture/ compare */
  38.     {0x82, NonHandledInterrupt}, /* UART1 Tx complete */
  39.     {0x82, NonHandledInterrupt}, /* UART1 Receive register DATA FULL */
  40.     {0x82, NonHandledInterrupt}, /* I2C interrupt */
  41.     {0x82, NonHandledInterrupt}, /* UART2/3 Tx complete */
  42.     {0x82, NonHandledInterrupt}, /* UART2/3 Receive register DATA FULL */
  43.     {0x82, NonHandledInterrupt}, /* ADC1 end of conversion/ analog watchdog interrupt */
  44.     {0x82, NonHandledInterrupt}, /* TIM4 update/ overflow */
  45.     {0x82, NonHandledInterrupt}, /* Flash EOP/WR_PG_DIS */
  46.     {0x82, NonHandledInterrupt}, /* irq25 */
  47.     {0x82, NonHandledInterrupt}, /* irq26 */
  48.     {0x82, NonHandledInterrupt}, /* irq27 */
  49.     {0x82, NonHandledInterrupt}, /* irq28 */
  50.     {0x82, NonHandledInterrupt}, /* irq29 */
  51. };

通过上述的简单设置就可以使用高级定时器1了。

现象是一个LED以1s的间隔在亮灭间转换。
阅读(6622) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

Nguhyw2016-08-01 15:23:35

爱莉清