铜河 发表于 2013-2-26 12:10:47

用STM8S的定时器控制的三位流水灯程序

本帖最后由 铜河 于 2013-2-26 12:12 编辑

此程序是在STM三合一之STM8S板上三位LED流水实验,通过TIM2定时控制。
主程序如下:
/************************************
*                用定时器的STM8S三位流水灯程序                *
*        功能描述:控制PD1-3上三位LED流水        *
* 建立日期:2013年2月26日                                                *
* 程序设计:铜河                                                                                *
* 修改日期:2013年2月26日                                                *
* 工作环境:STM三合一套件之STM8S板        *
* 编 译 器:COSMIC 4.35                                                        *
************************************/
#include <stm8s105c_s.h>

unsigned char flag=0;        //定时标志

void CLK_init(void)
{
        CLK_ICKR = 0x01;                //内部高速时钟
        CLK_CKDIVR = 0x00;        //预分频1,时钟频率为16M
}

void GPIO_init(void)
{
  PD_DDR = 0x1F;                  // 配置PD端口的方向寄存器PD3输出
  PD_CR1 = 0x1F;                  // 设置PD3为推挽输出,PD7上拉输入
        PD_CR2 = 0x00;
}

void TIM_init(void)
{
        TIM2_PSCR = 0x0E;                //预分频16384,计一周期约1MS
        TIM2_ARRH = 0x01;                //500个周期计数溢出
        TIM2_ARRL = 0xF4;
        TIM2_IER = 0x01;                //更新中断使能
        TIM2_CR1 = 0x01;                //开始计数
}

void init_devices(void)
{
        _asm("sim");                                //关中断
  CLK_init();                                        //初始化时钟
  GPIO_init();                                //初始化端口
        TIM_init();                                        //初始化定时器
        _asm("rim");                                //开中断
}

void main( void )
{
        static unsigned int Count;//计数变量

        init_devices();                        //初始化器件

  while(1)
        {
                if(flag)                                        //定时时间到
                {
                        flag = 0;                                //清标志
                        switch (Count)
                        {
                                case 0:                          //亮第一灯
                                        PD_ODR = 1<<1;
                                        Count++;
                                        break;
                                
                                case 1:                          //亮第二灯
                                        PD_ODR = 1<<2;
                                        Count++;
                                        break;
                                
                                case 2:                         //亮第三灯
                                        PD_ODR = 1<<3;
                                        Count++;
                                        break;
                                
                                default:                         //复位,从头再来
                                        PD_ODR = 0x00;
                                        Count=0;
                                        break;
                        }
                }
        }
}

中断程序如下:
#include "stm8s105c_s.h"

extern unsigned char flag;                //外部变量

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;
}

//定时器2溢出中断函数
@far @interrupt void TIM2_UPD_OVF_IRQHandler (void)
{
        flag = 1;                                                                //设置标志
        TIM2_SR1 &= ~(0x01);                //清中断标志
        return;
}

extern void _stext();

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, TIM2_UPD_OVF_IRQHandler}, /* 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, NonHandledInterrupt}, /* irq23 */
        {0x82, NonHandledInterrupt}, /* irq24 */
        {0x82, NonHandledInterrupt}, /* irq25 */
        {0x82, NonHandledInterrupt}, /* irq26 */
        {0x82, NonHandledInterrupt}, /* irq27 */
        {0x82, NonHandledInterrupt}, /* irq28 */
        {0x82, NonHandledInterrupt}, /* irq29 */
};

ba1731 发表于 2013-2-28 14:17:41

表四鄙人纯属随机路过。。。顺带帮顶。。。
页: [1]
查看完整版本: 用STM8S的定时器控制的三位流水灯程序