#include "stc8h.h"
#include "intrins.h"
#define MAX_TASKS 2
#define MAX_TASK_DEP 32
unsigned char idata task_sp[MAX_TASKS]; //任务的堆栈指针,简化方便我们当前操作系统只有2个task
unsigned char idata task_stack[MAX_TASKS][MAX_TASK_DEP];//每一个task任务的堆栈
unsigned char task_id;//当前任务号,从0开始
typedef enum{
TASK_RUNNING,//运行状态
TASK_SUSPENDED//挂起状态
}TaskStatus;
typedef struct{
unsigned char id;//任务id
TaskStatus status;//任务状态
unsigned int delay_count;//延迟计算器
unsigned int delay_duration;//延迟时间
}Task;
Task idata tasks[MAX_TASKS] = {
{0,TASK_RUNNING,0,0},
{1,TASK_RUNNING,0,0},//两个任务都是默认运行状态
};
void Timer0_init(void);//原型函数
void sleep(unsigned int,unsigned int);
void sleep(unsigned int task_id,unsigned int delay_ms)
{
tasks[task_id].status = TASK_SUSPENDED;
tasks[task_id].delay_count = 0;
tasks[task_id].delay_duration = delay_ms;
}
void Timer0_init(void)//定时1ms中断
{
AUXR |=0x80;
TMOD &=0xF0;
TL0 = 0x40;
TH0 = 0xA2;
TF0 = 0;
TR0 = 1;
}
//void Delay1000ms() //@24.000MHz
//{
// unsigned char i, j, k;
// _nop_();
// _nop_();
// i = 122;
// j = 193;
// k = 128;
// do
// {
// do
// {
// while (--k);
// } while (--j);
// } while (--i);
//}
//定义一个任务切换的函数(任务调度器)
void task_switch()
{
task_sp[task_id]=SP;
task_id++;
if(task_id ==MAX_TASKS)
{
task_id=0;
}
SP=task_sp[task_id];
}
void task0()//第0号任务
{
// static unsigned int a=3;
P5M0=0x00;
P5M1=0x00;
P53=1;
while(1)
{
// a=a+3;
if(tasks[0].status == TASK_SUSPENDED)
{
task_switch();
continue;
}
sleep(0,1000);
P53=~P53;
task_switch();
}
}
void task1()//第1号任务
{
static unsigned int b=5;
while(1)
{
if(tasks[1].status == TASK_SUSPENDED)
{
task_switch();
continue;
}
sleep(1,1000);
b=b+5;
task_switch();
}
}
void Timer0_ISR(void) interrupt 1
{
unsigned char i;
for(i=0;i<MAX_TASKS;i++)
{
if(tasks[i].status == TASK_SUSPENDED)
{
tasks[i].delay_count++;
}
if(tasks[i].delay_count>=tasks[i].delay_duration)
{
tasks[i].status = TASK_RUNNING;//睡眠结束
tasks[i].delay_count=0;
}
}
}
//fn是一个函数指针,注意数据类型是int
//tid_task id 是8位的, 0,1
//下面函数作用是将一个task的函数指针放入对应的堆栈空间里面
void task_load(unsigned int fn,unsigned char tid)//加载任务
{
task_sp[tid]=task_stack[tid]+1;
task_stack[tid][0]=fn&0xff;
task_stack[tid][1]=fn>>8;
}
void main()
{
Timer0_init();
task_load(task0,0);//把task0装载入内存
task_load(task1,1);//把task1装载入内存
task_id=0;
SP=task_sp[0];
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于STC8的最小操作系统
共9个文件
uvopt:1个
os_demo:1个
m51:1个
需积分: 1 2 下载量 13 浏览量
2023-10-01
19:21:14
上传
评论
收藏 33KB RAR 举报
温馨提示
使用时间片切换的形式,实现简易的基于STC8的任务切换操作系统,作为入门操作系统前铺垫,压缩包为keil工程文件
资源推荐
资源详情
资源评论
收起资源包目录
OS_DEMO.rar (9个子文件)
OS_DEMO
Objects
OS_DEMO 50KB
OS_DEMO.lnp 95B
main.obj 50KB
OS_DEMO.build_log.htm 973B
Listings
OS_DEMO.m51 8KB
main.lst 6KB
OS_DEMO.uvproj 14KB
OS_DEMO.uvopt 8KB
main.c 3KB
共 9 条
- 1
资源评论
小白研究僧学习嵌入式
- 粉丝: 1499
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功