打印

【编程经验谈2】注重通用!要求写通用定时函数,谁来迎战

[复制链接]
5798|46
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xlsbz|  楼主 | 2013-12-7 10:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 xlsbz 于 2013-12-16 19:57 编辑



TIM.rar (16.06 KB) 通用定时器.rar (18.85 KB)




编写如下形式,或者自己的别的形式
Bool   TimeUp(延时时间,或者别的参数)
{
         具体代码;
}
---------------------------
应用
if  (TimeUp(100)) {
    //do something!!!
}

if  (TimeUp(200)) {
    //do something!!!
}
if  (TimeUp(110)) {
    //do something!!!
}

在keil下写 或 MDK

说个思路也行。当然最好是贡献自己的代码了。






相关帖子

沙发
ayb_ice| | 2013-12-7 11:46 | 只看该作者
先把问题说清楚,

使用特权

评论回复
板凳
dirtwillfly| | 2013-12-7 12:13 | 只看该作者
不知道楼主说的通用,是在什么范围内通用?

使用特权

评论回复
评论
xlsbz 2013-12-7 13:40 回复TA
就是楼下这种,有啥好思路?期待。。。。。。。。。。。 
地板
xlsbz|  楼主 | 2013-12-7 13:19 | 只看该作者
本帖最后由 xlsbz 于 2013-12-13 23:56 编辑

r就是类似这种 我写了一个,高人别 拍砖。

类似这种,用KEIL软仿真 可以看出效果,在附件里

使用特权

评论回复
评论
bhsdlmj 2013-12-7 13:47 回复TA
牛牛牛 年 
5
xlsbz|  楼主 | 2013-12-7 13:34 | 只看该作者
这是基本配置  选用  8051

void main (void)
{
  /* 初始化Timer0 */
        TMOD |= 0x01;                                             
        TH0  = 0xFC;
        TL0  = 0x18;
    TR0  = 1;        
    ET0  = 1;
        EA = 1;

        P0 = 0;
        P1 = 0;
        P2 = 0;
        P3 = 0;
        while (1) {
       
        }
}


void Timer0_Server(void) interrupt 1
{
    TH0 = 0xFC;   //加载初始值
        TL0 = 0x18;
                              
}

使用特权

评论回复
6
xlsbz|  楼主 | 2013-12-7 13:40 | 只看该作者
dirtwillfly 发表于 2013-12-7 12:13
不知道楼主说的通用,是在什么范围内通用?

就是4楼这种,有啥好思路?期待。。。。。。。。。。。

使用特权

评论回复
7
bhsdlmj| | 2013-12-7 13:46 | 只看该作者
xlsbz 发表于 2013-12-7 13:19
就是类似这种 我写了一个,高人别拍砖。

类似这种,用KEIL软仿真 可以看出效果,在附件里 ...

:handshake

使用特权

评论回复
8
misra| | 2013-12-7 13:48 | 只看该作者
xlsbz 发表于 2013-12-7 13:19
就是类似这种 我写了一个,高人别拍砖。

类似这种,用KEIL软仿真 可以看出效果,在附件里 ...

可以公开源码么?

使用特权

评论回复
9
ayb_ice| | 2013-12-7 13:59 | 只看该作者
本帖最后由 ayb_ice 于 2013-12-7 14:00 编辑



// 献丑了
// 没有编译,没有测试,可能有错误,BUG,只能当示意代码
typedef enum{
false = 0,
true = 1,
}bool;
typedef unsigned int U16;

typedef struct{
U16 set;
U16 counter;
}t_TIME;
t_TIME time[8];  // 最大8个不同的定时
bit flag_10ms = 0;

void time_init(void);
void time_set(U16 t);
bool time_assert(U16 t);
void time_kernal(void);
void delay_10ms(void);
#define MS /10
#define SEC *1000MS
//______________________________________________________________
// main
//______________________________________________________________
//
// 主函数
//
void main(void)
{
time_init();
time_set(100MS);
time_set(200MS);
time_set(210MS);
time_set(10SEC);
while(1)
{
  if(time_assert(100MS) == true){ // TimeUp
   ...
  }
  if(time_assert(200MS) == true){
   ...
  }
  if(time_assert(210MS) == true){
   ...
  }
  if(time_assert(10SEC) == true){
   ...
  }
  time_kernal();
  delay_10ms();
}
}
//______________________________________________________________
// time_init
//______________________________________________________________
//
// 初始化
//
void time_init(void)
{
U8 i;
for(i=0; i<8; i++){
  time.set = 0;
  time.counter = 0;
}
TMOD = 0x01;
ET0 = 1;
TR0 = 1;
EA = 1;
}
//______________________________________________________________
// time_set
//______________________________________________________________
//
// 定时设置
//
void time_set(U16 t)
{
U8 i;
if(!t){
  return false;
}
for(i=0; i<8; i++){
  if(time.set == t){
   return true;
  }
}
for(i=0; i<8; i++){
  if(time.set == 0){
   time.set = t;
   time.counter = t;
   return true;
  }
}
return false;
}
//______________________________________________________________
// time_assert
//______________________________________________________________
//
// 定时断言
//
bool time_assert(U16 t)
{
U8 i;
if(!t){
  return false;
}
for(i=0; i<8; i++){
  if(time.set == t){
   if(time.counter == 0){
    return true;
   }
  }
}
return false;
}
//______________________________________________________________
// time_kernal
//______________________________________________________________
//
// 定时内核
//
void time_kernal(void)
{
U8 i;
for(i=0; i<8; i++){
  if(time.set != 0){
   time.counter--;
   if(time.counter == -1){
    time.counter = time.set
   }
  }
}
}
void delay_10ms(void)
{
while(!flag_10ms){
  PCON |= 0x01;
}
flag_10ms = 0;
}

void isr_t0(void) interrupt 1
{
TR0 = 0;
TH0 = (65536-10000) / 256;
TL0 = (65536-10000) % 256;
TR0 = 1;
flag_10ms = 1;
}




使用特权

评论回复
10
ayb_ice| | 2013-12-7 14:01 | 只看该作者



// 献丑了
// 没有编译,没有测试,可能有错误,BUG,只能当示意代码

typedef enum{
        false = 0,
        true = 1,

}bool;

typedef unsigned int U16;


typedef struct{
        U16        set;
        U16 counter;
}t_TIME;

t_TIME time[8];                // 最大8个不同的定时

bit flag_10ms = 0;


void time_init(void);
void time_set(U16 t);
bool time_assert(U16 t);
void time_kernal(void);
void delay_10ms(void);

#define MS        /10
#define SEC        *1000MS

//______________________________________________________________
// main
//______________________________________________________________
//
// 主函数
//
void main(void)
{
        time_init();

        time_set(100MS);
        time_set(200MS);
        time_set(210MS);
        time_set(10SEC);

        while(1)
        {
                if(time_assert(100MS) == true){        // TimeUp
                        ...
                }
                if(time_assert(200MS) == true){
                        ...
                }
                if(time_assert(210MS) == true){
                        ...
                }
                if(time_assert(10SEC) == true){
                        ...
                }

                time_kernal();

                delay_10ms();
        }

}

//______________________________________________________________
// time_init
//______________________________________________________________
//
// 初始化
//
void time_init(void)
{
        U8 i;

        for(i=0; i<8; i++){
                time[i].set = 0;
                time[i].counter = 0;
        }

        TMOD = 0x01;
        ET0 = 1;
        TR0 = 1;
        EA = 1;
}

//______________________________________________________________
// time_set
//______________________________________________________________
//
// 定时设置
//
void time_set(U16 t)
{
        U8 i;

        if(!t){
                return false;
        }

        for(i=0; i<8; i++){
                if(time[i].set == t){
                        return true;
                }
        }

        for(i=0; i<8; i++){
                if(time[i].set == 0){
                        time[i].set = t;
                        time[i].counter = t;
                        return true;
                }
        }

        return false;
}

//______________________________________________________________
// time_assert
//______________________________________________________________
//
// 定时断言
//
bool time_assert(U16 t)
{
        U8 i;

        if(!t){
                return false;
        }

        for(i=0; i<8; i++){
                if(time[i].set == t){
                        if(time[i].counter == 0){
                                return true;
                        }
                }
        }

        return false;
}

//______________________________________________________________
// time_kernal
//______________________________________________________________
//
// 定时内核
//
void time_kernal(void)
{
        U8 i;

        for(i=0; i<8; i++){
                if(time[i].set != 0){
                        time[i].counter--;
                        if(time[i].counter == -1){
                                time[i].counter = time[i].set
                        }
                }
        }
}

void delay_10ms(void)
{
        while(!flag_10ms){
                PCON |= 0x01;
        }

        flag_10ms = 0;
}


void isr_t0(void) interrupt 1
{
        TR0 = 0;
        TH0 = (65536-10000) / 256;
        TL0 = (65536-10000) % 256;
        TR0 = 1;

        flag_10ms = 1;
}






使用特权

评论回复
评分
参与人数 1威望 +5 收起 理由
xlsbz + 5 赞一个!
11
xlsbz|  楼主 | 2013-12-7 14:14 | 只看该作者
ayb_ice 发表于 2013-12-7 14:01
// 献丑了
// 没有编译,没有测试,可能有错误,BUG,只能当示意代码

               if(time_assert(100MS) == true){        // TimeUp
                        ...
                }
               if(time_assert(100MS) == true){        // TimeUp
                        ...
                }

都调用100MS好用否?请问?

使用特权

评论回复
12
ayb_ice| | 2013-12-7 14:28 | 只看该作者
本帖最后由 ayb_ice 于 2013-12-7 14:29 编辑
xlsbz 发表于 2013-12-7 14:14
if(time_assert(100MS) == true){        // TimeUp
                        ...
       ...


可以没有问题的

事实上我的项目框架一直都是这样工作的,只是具体形式有点区别罢了

使用特权

评论回复
13
ZG11211| | 2013-12-7 14:28 | 只看该作者
通用代表着臃肿,还是喜欢自己慢慢敲代码

使用特权

评论回复
14
xlsbz|  楼主 | 2013-12-7 14:56 | 只看该作者
本帖最后由 xlsbz 于 2013-12-7 15:49 编辑
ayb_ice 发表于 2013-12-7 14:01
// 献丑了
// 没有编译,没有测试,可能有错误,BUG,只能当示意代码

没啥bug。完全能运行
就是time_set返回void
还有少几个  分号。

我得好好看看。

抛砖引玉了。 多谢。

我是砖  你是玉 哈哈  别误解了!

使用特权

评论回复
15
xlsbz|  楼主 | 2013-12-7 14:57 | 只看该作者
ZG11211 发表于 2013-12-7 14:28
通用代表着臃肿,还是喜欢自己慢慢敲代码

不通用 就得天天敲。通用就敲一阵子。

使用特权

评论回复
16
hdp7891000| | 2013-12-7 16:31 | 只看该作者
uint32 timer_global;

timer_init(xms);//定时器初始化为xms,需要的各种时间的最小公约数

timer_interupt()
{
    //其他代码
    timer_global++;
}

//几个任务定义几个变量
uint32 timer_last;
uint32 timer_last1;
uint32 timer_last2;
uint32 timer_last3;

TimerUp(time,uint32* timer_last)
{
    if(timerg_lobal - (* timer_last) >= time)
         return 1;
    else
         return 0;
}


伪代码,实在不想调。。。

使用特权

评论回复
17
lhchen922| | 2013-12-7 16:37 | 只看该作者
本帖最后由 lhchen922 于 2013-12-7 16:46 编辑
k_timeout = jiffies + K_REC_TIME_OUT;  /* set the time out  */
.
.
.
                if (k_timeout >= jiffies) /* time out e */
                {
                      //do something!!!
                }

使用特权

评论回复
18
lhchen922| | 2013-12-7 16:47 | 只看该作者
jiffies 是1ms的时钟节拍

使用特权

评论回复
19
efen| | 2013-12-7 16:49 | 只看该作者
我见过比较好的用法就是LWIP里面的定时了
在定时器中断update,然后任务处理lwip_timer_handle就行了
void Time_Update(void)
{
  LocalTime += SYSTEMTICK_PERIOD_MS;
}

void lwip_timer_handle(__IO uint32_t localtime)
{

  /* TCP periodic process every 250 ms */
  if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
  {
    TCPTimer =  localtime;
    tcp_tmr();
  }
  /* ARP periodic process every 5s */
  if (localtime - ARPTimer >= ARP_TMR_INTERVAL)
  {
    ARPTimer =  localtime;
    etharp_tmr();
  }

使用特权

评论回复
20
lhchen922| | 2013-12-7 16:50 | 只看该作者
本帖最后由 lhchen922 于 2013-12-7 16:52 编辑
INTERFACE unsigned long ulEventTimerOutCounter[TO_MAX_EVENT];
INTERFACE unsigned long ulEventTimer[TO_MAX_EVENT];

#define GET_EVENT_STARTUP_TIMER(event)                                (ulEventTimer[event] = _GetSystemCount())
#define RESET_EVENT_TIMER(event)                                        (GET_EVENT_STARTUP_TIMER(event))
#define EVENT_TIMEOUT_COUNTER(event)                                (ulEventTimerOutCounter[event])
#define SET_EVENT_TIMEOUT_COUNTER(event,counter)        (ulEventTimerOutCounter[event] = counter)
#define GET_EVENT_TIMEOUT_COUNTER(event)                        (ulEventTimerOutCounter[event])
#define DIFF_TIME_FROM_NOW(time)                                        (_GetSystemCount() - time)
#define EVENT_DIFF_TIMER_FROM_NOW(event)                        (DIFF_TIME_FROM_NOW(ulEventTimer[event]))
#define IS_EVENT_TIMEOUT(event)                                         ((EVENT_DIFF_TIMER_FROM_NOW(event) == EVENT_TIMEOUT_COUNTER(event)) ? TRUE : FALSE)

vu32 _GetSystemCount(void)
{
    return jiffies;
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

190

主题

1614

帖子

4

粉丝