zoukankan      html  css  js  c++  java
  • 定时器延时算法设计

    1.江苏宏云JMT系列芯片

    注意注释中有部分写的不太对,

    2.初始化定时器,产生1mS自加中断

    //56M时钟 

    u16,16位自加最多可以计时 1~65536/2 计时不会出错

    /*
    ;功能:在56M时钟下产生1mS中断
    */
    void timer1Init()
    {
        TMOD|=0x10;//16bit定时,不使能GATE门 
        TL1=0x40;//56M下配置0x2540即1mS
        TH1=0x25;
    }
    /*
    ;功能:timer1中断,1mS计数timer_mS_count自加一次
    */
    void ISR_timer1DelyCnt() interrupt 3
    {
        TR1=0;
        pinLed1_PA1=~pinLed1_PA1;
        timer_mS_count++;//1mS中断
        TL1=0x40;//56M下配置0x2540即1mS
        TH1=0x25;
        TR1=1;
    }
    /*
    ;功能:设置需要延时的时间,单位由timer_mS_count自加中断间隔决定,mS
    ;入参:需要的mS计时数
    ;返回值:用于作为函数checkDelay()的入参检测对应的时间是否到达
    */
    //下面两个函数配合使用达到延时作用
    u16 setDelay(u16 mySet_mS_Timer)
    {
          return(timer_mS_count + mySet_mS_Timer - 1);                                             
    }
    /*
    ;功能:用来检测是否到达所配置的时间点
    ;入参:函数setDelay()设置的mS对应的返回值
    ;返回值:0表示时间未到
    ;                1表示时间到达
    */
    u8 checkDelay (u16 mySetTimer)//返回非零表示计时结束
    {
         return(((mySetTimer - timer_mS_count) & 0x8000) >> 8);//当(t - timer_mS_count)为正则&之后为0,当变为负数后因为是无符号整数,产生无穷大,那么非零
    }
    /*
    ;功能:组合函数时间精确计时,单线程模式计时
    ;入参:计时时长,单位:mS
    */
    void delay_ms(u16 mydealyTimer)//延时多少mS
    {
         u16 timerRelCount;
         timerRelCount = setDelay(mydealyTimer);//设置mS对应的计数值
         while (!checkDelay(timerRelCount));//检测计数值一直到时间到达为止
    }

    u8,8位自加最多可以计时1 ~ 256/2计时不会出错

     
    u8 mySet_mS_Timer ;

    /*
    ;功能:在56M时钟下产生1mS中断 */ void timer1Init() { TMOD|=0x10;//16bit定时,不使能GATE门 TL1=0x40;//56M下配置0x2540即1mS TH1=0x25; } /* ;功能:timer1中断,1mS计数timer_mS_count自加一次 */ void ISR_timer1DelyCnt() interrupt 3 { TR1=0; pinLed1_PA1=~pinLed1_PA1; timer_mS_count++;//1mS中断 ,u8 TL1=0x40;//56M下配置0x2540即1mS TH1=0x25; TR1=1; } /* ;功能:设置需要延时的时间,单位由timer_mS_count自加中断间隔决定,mS ;入参:需要的mS计时数 ;返回值:用于作为函数checkDelay()的入参检测对应的时间是否到达 */ //下面两个函数配合使用达到延时作用 u8 setDelay(u8 mySet_mS_Timer) { return(timer_mS_count + mySet_mS_Timer - 1); } /* ;功能:用来检测是否到达所配置的时间点 ;入参:函数setDelay()设置的mS对应的返回值 ;返回值:0表示时间未到 ; 1表示时间到达 */ u8 checkDelay (u8 mySetTimer)//返回非零表示计时结束 { return((mySetTimer - timer_mS_count) & 0x80 );//当(t - timer_mS_count)为正则&之后为0,当变为负数后因为是无符号整数,产生无穷大,那么非零 } /* ;功能:组合函数时间精确计时,单线程模式计时 ;入参:计时时长,单位:mS */ void delay_ms(u8 mydealyTimer)//延时多少mS { u16 timerRelCount; timerRelCount = setDelay(mydealyTimer);//设置mS对应的计数值 while (!checkDelay(timerRelCount));//检测计数值一直到时间到达为止 }
  • 相关阅读:
    September 29th 2017 Week 39th Friday
    September 28th 2017 Week 39th Thursday
    September 27th 2017 Week 39th Wednesday
    September 26th 2017 Week 39th Tuesday
    September 25th 2017 Week 39th Monday
    September 24th 2017 Week 39th Sunday
    angular2 学习笔记 ( Form 表单 )
    angular2 学习笔记 ( Component 组件)
    angular2 学习笔记 ( Http 请求)
    angular2 学习笔记 ( Router 路由 )
  • 原文地址:https://www.cnblogs.com/fx427103/p/4744601.html
Copyright © 2011-2022 走看看