原创 转载: STM8的C语言编程(6)--8位定时器应用之二

2011-8-21 06:35 2171 4 4 分类: MCU/ 嵌入式

今天进行的实验依然是用定时器4,只不过改成了用中断方式来实现,由定时器4的中断服务程序来驱动LED的闪烁。.0pt;background:white;word-break:break-all">实现中断方式的关键点有几个,第一个关键点就是要打开定时器4的中断允许位,在定时器4IER寄存器中有定义。第二个关键点,就是打开CPU的全局中断允许位,在汇编语言中,就是执行RIM指令,在C语言中,用下列语句实现:

_asm("rim");

3个关键点就是中断服务程序的框架或写法,中断服务程序的写法如下:

@far @interrupt void TIM4_UPD_OVF_IRQHandler (void)

{

    // 下面是中断服务程序的实体

}                            

4个关键点就是要设置中断向量,即将中断服务程序的入口填写到中断向量表中,如下所示,将IRQ23对应的中断服务程序的入口填写成TIM4_UPD_OVF_IRQHandler

struct interrupt_vector const _vectab[] = {

      {0x82, (interrupt_handler_t)_stext}, /* reset */

      {0x82, NonHandledInterrupt}, /* trap  */

      {0x82, NonHandledInterrupt}, /* irq0  */

      {0x82, NonHandledInterrupt}, /* irq1  */

      {0x82, NonHandledInterrupt}, /* irq2  */

      {0x82, NonHandledInterrupt}, /* irq3  */

      {0x82, NonHandledInterrupt}, /* irq4  */

      {0x82, NonHandledInterrupt}, /* irq5  */

      {0x82, NonHandledInterrupt}, /* irq6  */

      {0x82, NonHandledInterrupt}, /* irq7  */

      {0x82, NonHandledInterrupt}, /* irq8  */

      {0x82, NonHandledInterrupt}, /* irq9  */

      {0x82, NonHandledInterrupt}, /* irq10 */

      {0x82, NonHandledInterrupt}, /* irq11 */

      {0x82, NonHandledInterrupt}, /* irq12 */

      {0x82, NonHandledInterrupt}, /* irq13 */

      {0x82, NonHandledInterrupt}, /* irq14 */

      {0x82, NonHandledInterrupt}, /* irq15 */

      {0x82, NonHandledInterrupt}, /* irq16 */

      {0x82, NonHandledInterrupt}, /* irq17 */

      {0x82, NonHandledInterrupt}, /* irq18 */

      {0x82, NonHandledInterrupt}, /* irq19 */

      {0x82, NonHandledInterrupt}, /* irq20 */

      {0x82, NonHandledInterrupt}, /* irq21 */

      {0x82, NonHandledInterrupt}, /* irq22 */

   {0x82, TIM4_UPD_OVF_IRQHandler},/* irq23 */

      {0x82, NonHandledInterrupt}, /* irq24 */

      {0x82, NonHandledInterrupt}, /* irq25 */

      {0x82, NonHandledInterrupt}, /* irq26 */

      {0x82, NonHandledInterrupt}, /* irq27 */

      {0x82, NonHandledInterrupt}, /* irq28 */

      {0x82, NonHandledInterrupt}, /* irq29 */

};

解决了以上4个关键点,我们就能很轻松地用C语言实现中断服务了。

同样还是利用ST的开发工具,生成一个C程序的框架,然后修改其中的main.c,修改后的代码如下。另外还要修改stm8_interrupt_vector.c

编译通过后,下载到开发板,运行程序,可以看到LED在闪烁,或者用示波器可以在LED引脚上看到方波。

 

  修改后的main.c如下:

//  程序描述:通过初始化定时器4,以中断方式驱动LED闪烁

//            LED接在MCUGPIOPD3

 

#include "STM8S207C_S.h"

 

main()

{

  // 首先初始化GPIO

  PD_DDR = 0x08;

  PD_CR1 = 0x08;              // PD3设置成推挽输出

  PD_CR2 = 0x00;

 

  // 然后初始化定时器4                              

  TIM4_IER = 0x00;            // 禁止中断

  TIM4_EGR = 0x01;            // 允许产生更新事件

  TIM4_PSCR = 0x07;           // 计数器时钟=主时钟/128=2MHZ/128

                                                            // 相当于计数器周期为64uS

              TIM4_ARR = 255;             // 设定重装载时的寄存器值,255是最大值

  TIM4_CNTR = 255;            // 设定计数器的初值

                              // 定时周期=(ARR+1)*64=16320uS

  TIM4_CR1 = 0x01;            // b0 = 1,允许计数器工作

                              // b1 = 0,允许更新

                              // 设置控制器,启动定时器

  TIM4_IER = 0x01;            // 允许更新中断

  _asm("rim");                // 允许CPU全局中断

 

  while(1)                    // 进入无限循环

  {

  }                             

}

 

// 函数功能:定时器4的更新中断服务程序

// 输入参数:无

// 输出参数:无

// 值:无

@far @interrupt void TIM4_UPD_OVF_IRQHandler (void)

{

    TIM4_SR1 = 0x00;          // 清除更新标志

    PD_ODR = PD_ODR ^ 0x08;   // LED驱动信号取反

                              //LED闪烁频率=2MHZ/128/255/2=30.63

}                                          

  修改后的stm8_interrupt_vector.c如下:

/*    BASIC INTERRUPT VECTOR TABLE FOR STM8 devices

 *   Copyright (c) 2007 STMicroelectronics

 */

 

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

 

struct interrupt_vector {

       unsigned char interrupt_instruction;

       interrupt_handler_t interrupt_handler;

};

 

@far @interrupt void NonHandledInterrupt (void)

{

       /* in order to detect unexpected events during development,

          it is recommended to set a breakpoint on the following instruction

       */

       return;

}

 

extern void _stext();     /* startup routine */

extern @far @interrupt void TIM4_UPD_OVF_IRQHandler (void);

 

struct interrupt_vector const _vectab[] = {

       {0x82, (interrupt_handler_t)_stext}, /* reset */

       {0x82, NonHandledInterrupt}, /* trap  */

       {0x82, NonHandledInterrupt}, /* irq0  */

       {0x82, NonHandledInterrupt}, /* irq1  */

       {0x82, NonHandledInterrupt}, /* irq2  */

       {0x82, NonHandledInterrupt}, /* irq3  */

       {0x82, NonHandledInterrupt}, /* irq4  */

       {0x82, NonHandledInterrupt}, /* irq5  */

       {0x82, NonHandledInterrupt}, /* irq6  */

       {0x82, NonHandledInterrupt}, /* irq7  */

       {0x82, NonHandledInterrupt}, /* irq8  */

       {0x82, NonHandledInterrupt}, /* irq9  */

       {0x82, NonHandledInterrupt}, /* irq10 */

       {0x82, NonHandledInterrupt}, /* irq11 */

       {0x82, NonHandledInterrupt}, /* irq12 */

       {0x82, NonHandledInterrupt}, /* irq13 */

       {0x82, NonHandledInterrupt}, /* irq14 */

       {0x82, NonHandledInterrupt}, /* irq15 */

       {0x82, NonHandledInterrupt}, /* irq16 */

       {0x82, NonHandledInterrupt}, /* irq17 */

       {0x82, NonHandledInterrupt}, /* irq18 */

       {0x82, NonHandledInterrupt}, /* irq19 */

       {0x82, NonHandledInterrupt}, /* irq20 */

       {0x82, NonHandledInterrupt}, /* irq21 */

       {0x82, NonHandledInterrupt}, /* irq22 */

    {0x82, TIM4_UPD_OVF_IRQHandler},/* irq23 */

       {0x82, NonHandledInterrupt}, /* irq24 */

       {0x82, NonHandledInterrupt}, /* irq25 */

       {0x82, NonHandledInterrupt}, /* irq26 */

       {0x82, NonHandledInterrupt}, /* irq27 */

       {0x82, NonHandledInterrupt}, /* irq28 */

       {0x82, NonHandledInterrupt}, /* irq29 */

};

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /1 下一条