📄 main.lst
字号:
C51 COMPILER V7.06 MAIN 02/25/2006 14:24:56 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: f:\Keil\C51\BIN\C51.EXE main.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*******************************************************
2 *************基于51内核的圈圈操作系统*****************
3
4 本程序只供学习使用,未经作者许可,不得用于其它任何用途
5
6
7 main.c file
8
9 Created by Computer-lov
10 Date: 2005.10.27
11
12 Edit date:2006.2.24
13
14 Version V1.2
15 Copyright(C) Computer-lov 2005-2015
16 All rigths reserved
17
18 ******************************************************/
19
20 #include "at89x52.h"
21
22 #include "OS_core.h"
23 #include "task_switch.h"
24 #include "MAIN.H"
25
26 #include "UART.H"
27
28 //#define Debug
29
30
31 //灯
32
33 #define LED1 P1_4
34 #define LED2 P1_5
35 #define LED3 P1_6
36 #define LED4 P1_7
37
38
39 #define ON_LED1() LED1=0
40 #define OFF_LED1() LED1=1
41
42 #define ON_LED2() LED2=0
43 #define OFF_LED2() LED2=1
44
45 #define ON_LED3() LED3=0
46 #define OFF_LED3() LED3=1
47
48 #define ON_LED4() LED4=0
49 #define OFF_LED4() LED4=1
50
51 //按钮
52 #define KEY1 P1_0
53 #define KEY2 P1_1
54 #define KEY3 P1_2
55 #define KEY4 P1_3
C51 COMPILER V7.06 MAIN 02/25/2006 14:24:56 PAGE 2
56
57
58
59 volatile unsigned char second,minute,hour; //系统时钟
60
61 volatile unsigned char Msg_1_Receiver=0; //初始化无任务接收此消息
62
63 /////////////////////////////////////空闲任务,优先级最低///////////////////////////////////
64 ///////////////////////////////////// 注意,永远不要挂起空闲任务 //////////////////////////
65 ////////////////////////// 并且空闲任务必须第一个被创建,且优先级最低 /////////////////////
66 ////////////////在os_core.h中使用#define CPU_STAT后 可以用来统计CPU的使用率 ////////////////
67 void task_idle(void)
68 {
69 1 //static unsigned long int i; //使用static申明局部变量,避免变量被分配到相同的地址
70 1
71 1 #ifdef CPU_STAT //如果需要CPU使用率统计
72 1
73 1 static unsigned long int total_count; //则声明相应变量
74 1 static unsigned long int current_count;
75 1 static unsigned char time,j;
76 1
77 1 total_count=0;
78 1 current_count=0;
79 1
80 1
81 1 time=(unsigned char)OS_Run_Time; //初始化time
82 1
83 1 while(time==(unsigned char)OS_Run_Time); //等待,直到OS_Run_Time的值改变了
84 1
85 1 time=(unsigned char)OS_Run_Time; //保存这时的时间
86 1
87 1 while(1)
88 1 {
89 2 //此时所有任务都处于挂起状态,由统计这时的CPU资源
90 2 //使用跟下面一样的结构,使其生成一样的代码,提高准确性
91 2 if((unsigned char)OS_Run_Time-time>=100) //共统计1秒钟的时间
92 2 {
93 3 //这条语句的作用,是为了保证上下代码结构一样,让编译器生成一样结构的代码
94 3 j=(total_count-current_count)/(total_count);
95 3 break;
96 3 }
97 2 total_count++; //累加CPU资源
98 2 }
99 1
100 1
101 1 for(j=0;j<MAX_TASK;j++) //将挂起的任务唤醒
102 1 {
103 2 OS_pcb[j].Suspend=0;
104 2 }
105 1 #endif
106 1
107 1 while(1) //死循环
108 1 {
109 2
110 2 #ifdef CPU_STAT //如果需要统计CPU使用率
111 2 while(1)
112 2 {
113 3 //测量100个时钟节拍的CPU资源,然后跟没有其它任务运行时的CPU资源比较
114 3 if((unsigned char)OS_Run_Time-time>=100)
115 3 {
116 4 //计算比率
117 4 // CPU使用率越高,则本任务运行的时间就越短,current_count的值就越小
C51 COMPILER V7.06 MAIN 02/25/2006 14:24:56 PAGE 3
118 4 //total_count与current_count的差,占total_count的比例,就是CPU使用率
119 4 //最后结果被放大了100倍,保存在j中。
120 4 j=(total_count-current_count)/(total_count/100);
121 4
122 4
123 4 //将计算结果通过消息发送给接收消息的任务显示
124 4 OS_Send_Msg(OS_Current_ID,Msg_1_Receiver,j);
125 4
126 4 current_count=0; //清0
127 4 time=(unsigned char)OS_Run_Time;
128 4 while(time==(unsigned char)OS_Run_Time);
129 4 time=(unsigned char)OS_Run_Time; //重新开始统计
130 4 break;
131 4 }
132 3 current_count++; //累加CPU资源
133 3 }
134 2 #endif
135 2
136 2 }
137 1 }
138 //////////////////////////////////////////////////////////////////////////////////////////////
139
140 ///////////////////////////////////// 任务1 //////////////////////////
141 void task_1(void)
142 {
143 1 static unsigned int j; //使用static申明局部变量,避免变量被分配到相同的地址
144 1 static unsigned int temp;
145 1 j=0;
146 1
147 1 while(1)
148 1 {
149 2 OS_Delay(10); //延迟10个时钟节拍
150 2 OS_Enter_Critical(); //进入临界段
151 2 temp=OS_Run_Time; //获取当前系统运行的时间值
152 2 OS_Exit_Critical(); //退出临界段
153 2 temp-=j; //j用来保存上一次的时间总数
154 2 //用新的时间总数,减掉上一次的时间总数
155 2 //就可以得到运行的时间之差
156 2 if(temp>=100) //当运行时间之差大于等于100时,表示至少1S过去了,因为1个时钟节拍为10毫秒
157 2 {
158 3 temp/=100; //时钟之差除以100,表示时钟之差为多少秒
159 3 j+=temp*100; //j保存当前的时间总数
160 3 second+=temp; //调整秒的值
161 3 if(second>=60) //如果秒的值大于60
162 3 {
163 4 second-=60;
164 4 minute++;
165 4 if(minute>=60)
166 4 {
167 5 minute-=60;
168 5 hour++;
169 5 if(hour>=24)
170 5 hour-=24;
171 5 }
172 4 }
173 3 get_printer(); //申请打印机资源,显示系统时钟
174 3 prints(" System Time:",0);
175 3 send_a_byte('0'+hour/10); //显示小时
176 3 send_a_byte('0'+hour%10);
177 3 send_a_byte(':');
178 3 send_a_byte('0'+minute/10); //显示分
179 3 send_a_byte('0'+minute%10);
C51 COMPILER V7.06 MAIN 02/25/2006 14:24:56 PAGE 4
180 3 send_a_byte(':');
181 3 send_a_byte('0'+second/10); //显示秒
182 3 send_a_byte('0'+second%10);
183 3 prints("",1); //换行
184 3 give_up_printer(); //让出打印机使用权
185 3 }
186 2 }
187 1 }
188 ////////////////////////////////////////////////////////////////////////////////////////////////
189
190 unsigned char refresh_time=20; //用来调整LED1的闪烁快慢及杠的转动速度
191 ///////////////////////////////////// 任务2 ////////////////////////////////////
192 void task_2(void)
193 {
194 1
195 1 refresh_time=20; //初始化为20
196 1
197 1 while(1)
198 1 {
199 2 get_printer();
200 2 send_a_byte(0x08); //退一格
201 2 prints("/",0); //显示一个 (/)
202 2 give_up_printer();
203 2 OS_Delay(refresh_time); //延迟refresh_time个时钟周期
204 2
205 2 get_printer();
206 2 send_a_byte(0x08);
207 2 prints("-",0); //显示一个 (-)
208 2 give_up_printer();
209 2 OS_Delay(refresh_time);
210 2
211 2 ON_LED1(); //开LED1
212 2
213 2 get_printer();
214 2 send_a_byte(0x08);
215 2 prints("\\",0); //显示一个 (\)
216 2 give_up_printer();
217 2 OS_Delay(refresh_time);
218 2
219 2 get_printer();
220 2 send_a_byte(0x08);
221 2 prints("|",0); //显示一个 (|) 这几个轮流显示,有转动的效果
222 2 give_up_printer();
223 2 OS_Delay(refresh_time);
224 2
225 2 OFF_LED1();
226 2 }
227 1 }
228 ////////////////////////////////////////////////////////////////////////////////////////////////
229
230 ///////////////////////////////////// 任务3 //////////////////////////
231 void task_3(void)
232 {
233 1 static unsigned char buf; //使用static申明局部变量,避免变量被分配到相同的地址
234 1 static unsigned int temp;
235 1 while(1)
236 1 {
237 2 buf=get_a_byte(); //从串口接收一个数据
238 2 LED2=~LED2;
239 2
240 2 get_printer(); //申请打印机使用
241 2 prints(" You pressed \"",0);
C51 COMPILER V7.06 MAIN 02/25/2006 14:24:56 PAGE 5
242 2 send_a_byte(buf); //回显
243 2 prints("\" key!",1);
244 2 give_up_printer();
245 2
246 2 switch(buf)
247 2 {
248 3 case 's': //收到小写的s,秒值减小1
249 3 get_printer();
250 3 prints("Second:",0);
251 3 second--;
252 3 if(second>60)second=59;
253 3 send_a_byte('0'+second/10);
254 3 send_a_byte('0'+second%10);
255 3 prints("",1);
256 3 give_up_printer();
257 3 break;
258 3
259 3 case 'm': //收到小写的m,分值减小1
260 3 get_printer();
261 3 prints("Minute:",0);
262 3 minute--;
263 3 if(minute>60)minute=59;
264 3 send_a_byte('0'+minute/10);
265 3 send_a_byte('0'+minute%10);
266 3 prints("",1);
267 3 give_up_printer();
268 3 break;
269 3
270 3 case 'h': //收到小写的h,小时值减小1
271 3 get_printer();
272 3 prints("Hour:",0);
273 3 hour--;
274 3 if(hour>24)hour=23;
275 3 send_a_byte('0'+hour/10);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -