zoukankan      html  css  js  c++  java
  • Easy51RTOS入门级初略分析

    main.c

    #include "reg51.h"
    #include "os_cfg.h"
    #define TASK_DELAY0 TIME_PER_SEC/1 //任务执行频度为1Hz
    #define TASK_DELAY1 TIME_PER_SEC/2 //任务执行频度为2Hz
    #define TASK_DELAY2 TIME_PER_SEC/10 //任务执行频度为10Hz
    #define TASK_DELAY3 TIME_PER_SEC/20 //任务执行频度为20Hz

    void (* code task[])() = {task0,task1,task2,task3};

    sbit LED0 = P1^0;
    sbit LED1 = P1^1;
    sbit LED2 = P1^2;
    sbit LED3 = P1^3;

    /*main主函数*/
    void main(void)
    {
    unsigned char i;
    os_timer0_init();
    EA = 1; //开总中断

    while(1)
    {
    for(i=0;i<MAX_TASK;i++)
    if (task_delay[i]==0) {run(task[i]); break;} //任务优先级调度
    }
    }


    void task0(void)
    {
    LED0 = !LED0;
    task_delay[0] = TASK_DELAY0;
    }

    void task1(void)
    {
    LED1 = !LED1;
    task_delay[1] = TASK_DELAY1;
    }

    void task2(void)
    {
    LED2 = !LED2;
    task_delay[2] = TASK_DELAY2;
    }

    void task3(void)
    {
    static unsigned char state=0;
    switch (state)
    {
    case 0:
    LED3 = !LED3;
    state = 1;
    task_delay[3] = TASK_DELAY3;
    break;

    case 1:
    LED3 = !LED3;
    state = 2;
    task_delay[3] = TASK_DELAY3*2;
    break;

    case 2:
    LED3 = !LED3;
    state = 0;
    task_delay[3] = TASK_DELAY3*4;
    break;

    default:
    break;
    }
    }

    os_cfg.h

    #include "reg51.h"
    #define TIME_PER_SEC 200 //定义任务时钟频率,200Hz
    #define CLOCK 22118400 //定义时钟晶振,单位Hz
    #define MAX_TASK 4 //定义任务数量
    extern void task0(void);
    extern void task1(void);
    extern void task2(void);
    extern void task3(void);
    extern unsigned char task_delay[MAX_TASK];
    extern void run(void (*ptask)());
    extern void os_timer0_init(void);

    os_cfg.c

    #include "os_cfg.h"
    unsigned char task_delay[MAX_TASK];

    //定时器0初始化
    void os_timer0_init(void)
    {
    unsigned char i;
    for(i=0;i<MAX_TASK;i++) task_delay[i]=0; //赋初值task_delay[0]=0,task_delay[1]=0,task_delay[2]=0,task_delay[3]=0
    TMOD = (TMOD & 0XF0) | 0X01; //定时器0工作在模式1,16Bit定时器模式
    TH0 = (65536-CLOCK/TIME_PER_SEC/12) / 256; //CRY_OSC,TIME_PER_SEC在easycfg.h中配置
    TL0 = (65536-CLOCK/TIME_PER_SEC/12) % 256;
    TR0 =1;
    ET0 =1; //开启定时器和中断
    }

    // 系统OS定时中断服务
    void os_timer0(void) interrupt 1
    {
    unsigned char i;
    TH0 = (65536-CLOCK/TIME_PER_SEC/12) / 256; //CRY_OSC,TIME_PER_SEC在easycfg.h中配置
    TL0 = (65536-CLOCK/TIME_PER_SEC/12) % 256;
    for(i=0;i<MAX_TASK;i++) if(task_delay[i]) task_delay[i]--; //每节拍对任务延时变量减1 ,减至 0 后,任务就绪。
    }

    void run(void (*ptask)())
    {
    (*ptask)();
    }

  • 相关阅读:
    Servlet(2):通过servletContext对象实现数据共享
    Servlet(1):Servlet介绍
    MyBatis(4):使用limit实现分页
    MyBatis(3):优化MyBatis配置文件
    MyBatis(2):CRUD操作
    SpringMVC(4):文件上传与下载
    SpringMVC(3):AJAX
    什么是开发环境、测试环境、生产环境、UAT环境、仿真环境
    SQL SERVER添加表注释、字段注释
    SQL中行转列(PIVOT)与列转行(UNPIVOT)
  • 原文地址:https://www.cnblogs.com/chasing/p/3586108.html
Copyright © 2011-2022 走看看