找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1919|回复: 1
打印 上一主题 下一主题
收起左侧

stm8 tim1 如何实现定时 不明白都不行

[复制链接]
跳转到指定楼层
楼主
ID:7209 发表于 2016-10-14 14:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/* MAIN.C file
*
* Copyright (c) 2002-2016 STMicroelectronics
*/



/***
曾经给TIM1  捏了无数次,希望能帮到后来的兄弟免遭揉捏之苦,我将持续分析TIM1 的东西发上来给大家共同学习,保障每次一看就明白。


*/
#include "stm8s105c4.h"
void gpio_init(void);
void tim1_init(void);
void delay(unsigned int time);

void gpio_init(void)
{
                PB_ODR |=0X01;
                PB_DDR |=0X01;
                PB_CR1 |=0X01;
                PB_CR2 |=0X01;
}

void tim1_init()
{
                //设置预分频 系数
                TIM1_PSCRH = 0;
                TIM1_PSCRL = 19;
                //默认系统时钟 2Mhz  2000.000/20 = 100khz
                //周期 1/100.000 = 0.00001s
                //开启溢出中断
                TIM1_IER = 0X01;
                //设置定时值
                TIM1_ARRH = (unsigned char) (5000>>8);
                TIM1_ARRL = (unsigned char )5000;
                //设置定时器初值
                TIM1_CNTRH = (unsigned char) (5000>>8);
                TIM1_CNTRL = (unsigned char )5000;
//这产生1 HZ 的方波  高电平500ms  低电平500毫秒
/*
*  50000 从什么地方毛出来的 是预装载值
*  50000 =定时时间 / 周期
*  比如想 产生10HZ 的方波   
*   1/10HZ = 0.1s  就是50ms高电平 50ms低电平
*  0.05s/0.00001s = 5000
*  装载值 = 5000
*  0.00001s 从哪里来的啊啊啊啊
*  系统默认是 内部时钟 2mhz   2000.000/(19+1) = 100KHZ
*  1/100KZH = 0.00001s
*  

*/


}
void delay(unsigned int time)
{
                while(time--);
}

void  main()
{
               
               
                gpio_init();
                tim1_init();
                TIM1_CR1 |=0X01;
                _asm("rim");
          while (1);
}


@far @interrupt void TIM1_OVF_IRQ(void)
{
                TIM1_SR1 &= 0XFE;
                PB_ODR ^=0X01;  //取反 PB0
}


记得修改中断 11 ,否则还是不行的。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:7209 发表于 2016-10-14 14:14 | 只看该作者
还是把,中断这部分发上来,否则,还真有人,实验不通过
/*        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 TIM1_OVF_IRQ(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, TIM1_OVF_IRQ}, /* 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, NonHandledInterrupt}, /* irq23 */
        {0x82, NonHandledInterrupt}, /* irq24 */
        {0x82, NonHandledInterrupt}, /* irq25 */
        {0x82, NonHandledInterrupt}, /* irq26 */
        {0x82, NonHandledInterrupt}, /* irq27 */
        {0x82, NonHandledInterrupt}, /* irq28 */
        {0x82, NonHandledInterrupt}, /* irq29 */
};
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表