⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 实时系统的正确性不仅依耐系统计算的逻辑结果
💻 C
字号:
/*******************************************************


******************************************************/

 #include "STC516.h"

#include "OS_core.h"
#include "task_switch.h"
#include "MAIN.H"

#include "UART.H"

//#include<reg51.h>

//#include <at89c52.h>

#include <STDIO.H>
#include <absacc.h>
#define uchar unsigned char
#define uint  unsigned int


#define shuzi P0
#define weixuan P2
#define key P1
#define TIME 2

code uchar sz[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar keybit;
int data1,data2,data3,data4;

static  uchar i1,i2,i3,i4,i5,i6,i7,i8;


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

  }
}
void task_4(void)

{

while(1)
  {
	 get_printer();   //申请打印机资源,显示系统时钟
    //prints("                       System Time:",0);
     send_a_byte(i8);  //显示小时
     send_a_byte(i7);
	 send_a_byte(i6);
     send_a_byte(i5);


  
     give_up_printer();   //让出打印机使用权
	 OS_Delay(40);	
		
		}
}
//////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////  任务1    //////////////////////////
void task_1(void)
{
 
 
   
  
    while(1)
	{
weixuan=0x01;
shuzi=sz[data1/10];
OS_Delay(TIME);
weixuan=0x02;
shuzi=sz[data1%10];
OS_Delay(TIME);

weixuan=0x04;
shuzi=sz[data2/10];
OS_Delay(TIME);
weixuan=0x08;
shuzi=sz[data2%10];
OS_Delay(TIME);

weixuan=0x10;
shuzi=sz[data3/10];
OS_Delay(TIME);
weixuan=0x20;
shuzi=sz[data3%10];
OS_Delay(TIME);

weixuan=0x40;
shuzi=sz[data4/10];
OS_Delay(TIME);
weixuan=0x80;
shuzi=sz[data4%10];
OS_Delay(TIME);
shuzi=0x00;
weixuan=0x00;
		
		}
    
  
}
////////////////////////////////////////////////////////////////////////////////////////////////

unsigned char refresh_time=20;    //用来调整LED1的闪烁快慢及杠的转动速度
/////////////////////////////////////   任务2    ////////////////////////////////////
void task_2(void)
{

 uchar a;

 while(1)
  {

	a=key&0x0f;
	if(a!=0x00) 
		{
	    OS_Delay(150);	
		if(a==0x0e)      {keybit=1; while((key&0x0f)==0x0e);  }
		else if(a==0x0d) {keybit=2;	while((key&0x0f)==0x0d); }
		else if(a==0x0b) {keybit=3; while((key&0x0f)==0x0b);  }
		else if(a==0x07) {keybit=4;	while((key&0x0f)==0x07); }
		else  keybit=0;
		}
	   
 

   
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////    任务3     //////////////////////////
void task_3(void)
{
  while(1)
  {
      
  if(keybit!=0)
	{
	 if(keybit==1)      {keybit=0;data1++;if(data1==100)data1=0;}
	 else if(keybit==2) {keybit=0;data2++;if(data2==100)data2=0;}
	 else if(keybit==3) {keybit=0;data3++;if(data3==100)data3=0;}
	 else if(keybit==4) {keybit=0;data4++;if(data4==100)data1=0;}
	}
	// while(com8255C==0xff);

   
  //OS_Delay(50);
  
  
   
  //OS_Delay(1);
   
  
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////

unsigned char msg;   //保存消息用的一个变量,也可以使用一个结构体来处理更多的信息

/////////////////////////////////////    任务4 //////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////主函数//////////////////////////////////////////////////////
void main(void)
{
 EA=0;                    //关中
 ET2=1;                   //定时器2开中断

 T2CON=0x00;              //定时器自动重装模式
 T2MOD=0x00;              //如果提示这里编译通不过,可将本行删除;或自己将定义添上
					 //TH2 = 0xFF;
					 //   TL2 = 0xEE;           //因为keil自带的at89x52.h中没有T2MOD的定义
					//RCAP2H=0xB1;
					//RCAP2L=0xE0;             //定时时间为10ms

RCAP2H=0xf5;
RCAP2L=0xEE;              //定时时间为10ms

data1=data2=data3=data4=0;

 UART_init();           //初始化串口为9600波特率

 
 OS_Init();           //OS初始化


//参数意义    优先级      任务入口地址     消息指针
 OS_Task_Create(255,(unsigned int)(&task_idle),0);   //任务0初始化
 
// OS_Task_Create(3,(unsigned int)(&task_1),0);      //任务1初始化
 
 OS_Task_Create(2,(unsigned int)(&task_2),0);      //任务2初始化
 OS_Task_Create(1,(unsigned int)(&task_3),0);      //任务3初始化
 OS_Task_Create(0,(unsigned int)(&task_1),(unsigned char)&msg); //任务4初始化
 // OS_Task_Create(4,(unsigned int)(&task_5),0);      //任务1初始化
 
 OS_Start();      //OS开始调度
          //死循环。定时器中断发生后,任务开始调度
 
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -