📄 main.c
字号:
/*******************************************************
*************基于51内核的圈圈操作系统*****************
本程序只供学习使用,未经作者许可,不得用于其它任何用途
main.c file
Created by Computer-lov
Date: 2005.10.27
Edit date:2006.2.24
Version V1.2
Copyright(C) Computer-lov 2005-2015
All rigths reserved
******************************************************/
#include "at89x52.h"
#include "OS_core.h"
#include "task_switch.h"
#include "MAIN.H"
#include "UART.H"
//#define Debug
//灯
#define LED1 P1_4
#define LED2 P1_5
#define LED3 P1_6
#define LED4 P1_7
#define ON_LED1() LED1=0
#define OFF_LED1() LED1=1
#define ON_LED2() LED2=0
#define OFF_LED2() LED2=1
#define ON_LED3() LED3=0
#define OFF_LED3() LED3=1
#define ON_LED4() LED4=0
#define OFF_LED4() LED4=1
//按钮
#define KEY1 P1_0
#define KEY2 P1_1
#define KEY3 P1_2
#define KEY4 P1_3
volatile unsigned char second,minute,hour; //系统时钟
volatile unsigned char Msg_1_Receiver=0; //初始化无任务接收此消息
/////////////////////////////////////空闲任务,优先级最低///////////////////////////////////
///////////////////////////////////// 注意,永远不要挂起空闲任务 //////////////////////////
////////////////////////// 并且空闲任务必须第一个被创建,且优先级最低 /////////////////////
////////////////在os_core.h中使用#define CPU_STAT后 可以用来统计CPU的使用率 ////////////////
void task_idle(void)
{
//static unsigned long int i; //使用static申明局部变量,避免变量被分配到相同的地址
#ifdef CPU_STAT //如果需要CPU使用率统计
static unsigned long int total_count; //则声明相应变量
static unsigned long int current_count;
static unsigned char time,j;
total_count=0;
current_count=0;
time=(unsigned char)OS_Run_Time; //初始化time
while(time==(unsigned char)OS_Run_Time); //等待,直到OS_Run_Time的值改变了
time=(unsigned char)OS_Run_Time; //保存这时的时间
while(1)
{
//此时所有任务都处于挂起状态,由统计这时的CPU资源
//使用跟下面一样的结构,使其生成一样的代码,提高准确性
if((unsigned char)OS_Run_Time-time>=100) //共统计1秒钟的时间
{
//这条语句的作用,是为了保证上下代码结构一样,让编译器生成一样结构的代码
j=(total_count-current_count)/(total_count);
break;
}
total_count++; //累加CPU资源
}
for(j=0;j<MAX_TASK;j++) //将挂起的任务唤醒
{
OS_pcb[j].Suspend=0;
}
#endif
while(1) //死循环
{
#ifdef CPU_STAT //如果需要统计CPU使用率
while(1)
{
//测量100个时钟节拍的CPU资源,然后跟没有其它任务运行时的CPU资源比较
if((unsigned char)OS_Run_Time-time>=100)
{
//计算比率
// CPU使用率越高,则本任务运行的时间就越短,current_count的值就越小
//total_count与current_count的差,占total_count的比例,就是CPU使用率
//最后结果被放大了100倍,保存在j中。
j=(total_count-current_count)/(total_count/100);
//将计算结果通过消息发送给接收消息的任务显示
OS_Send_Msg(OS_Current_ID,Msg_1_Receiver,j);
current_count=0; //清0
time=(unsigned char)OS_Run_Time;
while(time==(unsigned char)OS_Run_Time);
time=(unsigned char)OS_Run_Time; //重新开始统计
break;
}
current_count++; //累加CPU资源
}
#endif
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// 任务1 //////////////////////////
void task_1(void)
{
static unsigned int j; //使用static申明局部变量,避免变量被分配到相同的地址
static unsigned int temp;
j=0;
while(1)
{
OS_Delay(10); //延迟10个时钟节拍
OS_Enter_Critical(); //进入临界段
temp=OS_Run_Time; //获取当前系统运行的时间值
OS_Exit_Critical(); //退出临界段
temp-=j; //j用来保存上一次的时间总数
//用新的时间总数,减掉上一次的时间总数
//就可以得到运行的时间之差
if(temp>=100) //当运行时间之差大于等于100时,表示至少1S过去了,因为1个时钟节拍为10毫秒
{
temp/=100; //时钟之差除以100,表示时钟之差为多少秒
j+=temp*100; //j保存当前的时间总数
second+=temp; //调整秒的值
if(second>=60) //如果秒的值大于60
{
second-=60;
minute++;
if(minute>=60)
{
minute-=60;
hour++;
if(hour>=24)
hour-=24;
}
}
get_printer(); //申请打印机资源,显示系统时钟
prints(" System Time:",0);
send_a_byte('0'+hour/10); //显示小时
send_a_byte('0'+hour%10);
send_a_byte(':');
send_a_byte('0'+minute/10); //显示分
send_a_byte('0'+minute%10);
send_a_byte(':');
send_a_byte('0'+second/10); //显示秒
send_a_byte('0'+second%10);
prints("",1); //换行
give_up_printer(); //让出打印机使用权
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char refresh_time=20; //用来调整LED1的闪烁快慢及杠的转动速度
///////////////////////////////////// 任务2 ////////////////////////////////////
void task_2(void)
{
refresh_time=20; //初始化为20
while(1)
{
get_printer();
send_a_byte(0x08); //退一格
prints("/",0); //显示一个 (/)
give_up_printer();
OS_Delay(refresh_time); //延迟refresh_time个时钟周期
get_printer();
send_a_byte(0x08);
prints("-",0); //显示一个 (-)
give_up_printer();
OS_Delay(refresh_time);
ON_LED1(); //开LED1
get_printer();
send_a_byte(0x08);
prints("\\",0); //显示一个 (\)
give_up_printer();
OS_Delay(refresh_time);
get_printer();
send_a_byte(0x08);
prints("|",0); //显示一个 (|) 这几个轮流显示,有转动的效果
give_up_printer();
OS_Delay(refresh_time);
OFF_LED1();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// 任务3 //////////////////////////
void task_3(void)
{
static unsigned char buf; //使用static申明局部变量,避免变量被分配到相同的地址
static unsigned int temp;
while(1)
{
buf=get_a_byte(); //从串口接收一个数据
LED2=~LED2;
get_printer(); //申请打印机使用
prints(" You pressed \"",0);
send_a_byte(buf); //回显
prints("\" key!",1);
give_up_printer();
switch(buf)
{
case 's': //收到小写的s,秒值减小1
get_printer();
prints("Second:",0);
second--;
if(second>60)second=59;
send_a_byte('0'+second/10);
send_a_byte('0'+second%10);
prints("",1);
give_up_printer();
break;
case 'm': //收到小写的m,分值减小1
get_printer();
prints("Minute:",0);
minute--;
if(minute>60)minute=59;
send_a_byte('0'+minute/10);
send_a_byte('0'+minute%10);
prints("",1);
give_up_printer();
break;
case 'h': //收到小写的h,小时值减小1
get_printer();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -