This my MCU project for my MCU course last term. I spend a lot of time on it and I like it because it is simple but cool.

The CPU is MC89S51. It is an automatic speed control and alarm system based on the ultrasonic ranging. Since it can display the distance on the LCD screen, I call it Range Finder.

The whole working process is:
1. setting the alarm range x1
2. setting the speed control range x2
3. turn on the switch
4. the ultrasonic ranging system would send a signal to the CPU to tell how long it would take for the sound to go to the nearest obstacle in the front and return back
5. the CPU would calculate the distance to the nearest obstacle which is x
6. if x1 > x, the green LED lights
else, the red LED lights and the buzzer rings
7. if x2 > x, the motor works in high speed
else, the motor works in low speed
8. the alarm range x1, the speed and the distance x would be displayed on the LCD screen

It has six modules as a whole in this system:
1the MCU minimum system
2) the ultrasonic ranging module
3) LCD1602 module
4) alarm module
5) DC motor module

the schematic:

experiment:

finished:

And I share my code here, the IDE is Keil4.
I had some Chinese annotation on it since it is required in this course.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
 
#include <AT89x51.H>		//
#include <intrins.h>
#define uchar unsigned char 
#define  RX  P2_0 //echo 
#define  TX  P2_1 //trig
#define LCM_RW  P2_6 //LCD
#define LCM_RS  P2_5
#define LCM_E   P2_7
#define LCM_Data  P0
#define Busy    0x80 //LCMBusy
 
#define led1 P1_4   //LEDLED
#define led2 P1_2   //LEDLED
#define buzzer P2_2 //
 
#define dianji P1_7 //I/O
#define ctrkey P3_6 //
#define ctrkey2 P3_7 
 
void LCMInit(void);//LCD
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);//LCD
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);//LCD
void Delay5Ms(void);//5
void Delay400Ms(void);//400
void Decode(unsigned char ScanCode);
void WriteDataLCM(unsigned char WDLCM);//LCD1602
void WriteCommandLCM(unsigned char WCLCM,BuysC);//LCD
 
unsigned char num = 0,gao_num = 1,di_num = 3; //
unsigned char keyv = 0;  //
 
unsigned char ReadStatusLCM(void);
unsigned char code Range[] = "==RangeFinder==";
unsigned char code Rangex[5][20] = {"L=025","L=050","L=100","L=150"}; //LCD1602
unsigned char code SpeedH[] = " SpeedHigh"; //
unsigned char code SpeedL[] = " Speed_Low"; //
unsigned char code ASCII[13] = "0123456789.-M";
unsigned char code table[]="Distance:000.0cm";//
unsigned char code table1[]="!!! Out of range";//
 
unsignedint  time=0;
unsigned long S=0;
unsigned int  limit=250;   //mm
bit  flag =0;
unsigned char disbuff[4]={ 0,0,0,0,};
 
/********************LCD************************************/
//
voidWriteDataLCM(unsigned char WDLCM) 
{
	ReadStatusLCM(); //
	LCM_Data = WDLCM;
	LCM_RS = 1;
	LCM_RW = 0;
	LCM_E = 0; 
	LCM_E = 0; //
	LCM_E = 1;
}
 
//
void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC0
{
	if (BuysC) ReadStatusLCM(); //
	LCM_Data = WCLCM;
	LCM_RS = 0;
	LCM_RW = 0;	
	LCM_E = 0;
	LCM_E = 0;
	LCM_E = 1;	
}
 
//
unsigned char ReadStatusLCM(void)
{
	LCM_Data = 0xFF; 
	LCM_RS = 0;
	LCM_RW = 1;
	LCM_E = 0;
	LCM_E = 0;
	LCM_E = 1;
	while (LCM_Data& Busy); //
	return(LCM_Data);
}
 
void LCMInit(void) //LCM
{
	LCM_Data = 0;
	WriteCommandLCM(0x38,0); //
	Delay5Ms(); 
	WriteCommandLCM(0x38,0);
	Delay5Ms(); 
	WriteCommandLCM(0x38,0);
	Delay5Ms(); 
 
	WriteCommandLCM(0x38,1); //,
	WriteCommandLCM(0x08,1); //
	WriteCommandLCM(0x01,1); //
	WriteCommandLCM(0x06,1); // 
	WriteCommandLCM(0x0c,1); // 
}
 
//
voidDisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
	Y &= 0x1;
	X &= 0xF;         //X15Y1
	if (Y) X |= 0x40; //+0x40;
	X |= 0x80;        //
	WriteCommandLCM(X, 1); //
	WriteDataLCM(DData);   //
}
 
//
voidDisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
	unsigned char ListLength;
 
ListLength = 0;
	Y &= 0x1;
	X &= 0xF;                      //X15Y1
	while (DData[ListLength]>0x19) //
		{
			if (X <= 0xF)          //X0xF
				{
					DisplayOneChar(X, Y, DData[ListLength]); //
					ListLength++;
					X++;
				}
		}
}
 
//5ms
void Delay5Ms(void)
{
	unsignedintTempCyc = 5552;
	while(TempCyc--);
}
 
//400ms
void Delay400Ms(void)
{
	unsigned char TempCycA = 5;
	unsignedintTempCycB;
	while(TempCycA--)
		{
			TempCycB=7269;
			while(TempCycB--);
		};
}
/*************************************************************/
void delay(uchari)        //
{
ucharj,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void key ()                //
{
   if(ctrkey==0)		   //
   {
     delay(5);             //
	if(ctrkey == 0)
	 {
keyv ++;
if(keyv>= 4)
keyv = 3;
switch(keyv)
         {
case 0:
           limit = 250;  //25cm
break;
case 1:
           limit = 500;  //50cm
break;
case 2:
           limit = 1000; //100cm
break;
		case 3:
		   limit = 1500; //150m
break;
		 }  
		 while(ctrkey == 0);//
	 }
   }
   if(ctrkey2 == 0)		    //
   {
delay(5);
	if(ctrkey2 == 0)
	 {
		if(keyv == 0);
		else
		keyv --;
switch(keyv)
         {
case 0:
           limit = 250;  //25cm
break;
case 1:
           limit = 500;  //50cm
break;
case 2:
           limit = 1000; //100cm
break;
		case 3:
		   limit = 1500; //150m
break;
		 }  
		 while(ctrkey2 == 0);//
	 }	
   }
}
 
/********************************************************/
 
void dispose()      //
{
switch(num)
  {
case 0:
	gao_num=1;     //PWM1
	di_num=3;      //PWM3
	break;
case 1:
	gao_num=1;
	di_num=3;      //
	break;
  }
}
void qudong()       //
{
unsigned char i;
if(di_num!=0)
  {
for(i=0;i<di_num;i++)
	{
	dianji=0;      //PWM
	delay(5);   
	}
  }
 
for(i=0;i<gao_num;i++)
  {
dianji=1;    //PWM
	delay(5);
  }
}
 
/********************************************************/
voidConut(void)
	{
	 time=TH0*256+TL0;//
	 TH0=0;
	 TL0=0;	
	 S=(time*1.7)/10;         //340/2 * 1000 / 1M 1.7mm
     if(S <= limit)           //
     {
      led1 = 1;
      led2 = 0;
buzzer = 0;
num = 0;
DisplayListChar(5, 0, SpeedL);
	 }
     else                   //
     {
      led1 = 0;
      led2 = 1; 
      P3_0 = 0;//??
buzzer = 1;
num = 1;
DisplayListChar(5, 0, SpeedH);
     }
dispose();
qudong();              //
     if((S>=3000)||flag==1) //2m
	 {	
buzzer = 1;
	flag=0;
DisplayListChar(0, 0, Rangex[keyv]);
DisplayListChar(5, 0, SpeedH);
DisplayListChar(0, 1, table1);
	 }
	 else                   //
	 {
disbuff[0]=S%10;
	disbuff[1]=S/10%10;
	disbuff[2]=S/100%10;
	disbuff[3]=S/1000;
DisplayListChar(0, 0, Rangex[keyv]);
if(S <= limit)
      {
DisplayListChar(5, 0, SpeedL);
	  }
else
      {
DisplayListChar(5, 0, SpeedH);
      }
DisplayListChar(0, 1, table);
	DisplayOneChar(9, 1, ASCII[disbuff[3]]);
	DisplayOneChar(10, 1, ASCII[disbuff[2]]);	
	DisplayOneChar(11, 1, ASCII[disbuff[1]]);
DisplayOneChar(12, 1, ASCII[10]);
	DisplayOneChar(13, 1, ASCII[disbuff[0]]);
	 }
	}
/********************************************************/
void zd0() interrupt 1 		 //T0,
{                            //RX
      flag=1;			     //
	  RX=0;
}
/********************************************************/
void  StartModule() 		         //
{
	  TX=1;			                 //trig
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
	  TX=0;//
}
/********************************************************/ 
voidTimer_Count(void)
{
	TR0=1;			    //
    while(RX);			//RX1TH0,TL0flag
    TR0=0;				//RX=0
Conut();			//
}
/*********************************************************/
void main(void)
{
unsignedintvalA;
unsignedinti;
	Delay400Ms();   //LCM
	LCMInit();      //LCM
	Delay5Ms();     //
    led1 = 0;
    led2 = 0;
	buzzer = 1;
DisplayListChar(0, 0, Range);
	DisplayListChar(0, 1, table);
for(i = 0; i< 10; i ++)
    {
Delay400Ms();
    }
    TMOD=0x01;     //T01GATE=1
dianji = 0;
    EA=1;          //
    TH0 = 0;
	TL0 = 0;
    ET0=1;         //T0		
	while(1)
	  {
		RX=1;
	StartModule();//10us
dianji = 0;
for(valA=7510;valA>0;valA--)
	    {
	       if(RX==1)//
		   {
		Timer_Count();
           }
key();
        }
	  }
}