单片机开发设计过程示例
//LED0亮 //函数:交替点亮高4位与低4位LED
//函数:闪烁点亮LED
9
9
P3=0x00; led_delay(); }
void led_delay(void) {
unsigned char i,j; for(i=0;i<220;i++) for(j=0;j<220;j++)
; }
void delay10ms(void) {
6
6
void forward(void) {
P3=0xfe; led_delay(); P3=0xfd; led_delay(); P3=0xfb; led_delay(); P3=0xf7; led_delay(); P3=0xef; led_delay(); P3=0xdf; led_delay(); P3=0xbf; led_delay(); P3=0x7f;
P3=0xfd;
//LE8D1亮
8
led_delay(); P3=0xfe; led_delay(); }
void Alter(void) {
P3=0x0f; led_delay(); P3=0xf0; led_delay(); }
void blink (void) {
P3=0xff; led_delay();
keyval=3; if(S4==0)
keyval=4; } }
//函数功能:键盘扫描
//Check whether a key is pressed //delay 10ms to debounce (去抖动) //S1 is pressed // S2 is pressed // S3 is pressed // S4 is pressed
//函数功能:正向流水点亮LED
//LED0亮 // delay function //LED1亮
//LED2亮
//LED3亮
//LED4亮
//LED5亮
//LED6亮
//LED7亮
7
7
led_delay(); }
void backward(void)
//函数:反向流水点亮LED
{
P3=0x7f;
//Defining the variable keyval, //storing key value
void main(void)
//main function
{
keyval=0; while(1)
//Key value initialized as 0 // while loop
4
{
key_scan(); switch(keyval)
//LED7亮
led_delay();
P3=0xbf;
//LED6亮
led_delay();
P3=0xdf;
//LED5亮
led_delay();
P3=0xef;
//LED4亮
led_delay();
P3=0xf7;
//LED3亮
led_delay();
P3=0xfb;
//LED2亮
led_delay();
//Define S1 as P1.0 pin
sbit S2=P1^1;
//Define S2 as P1.1 pin
sbit S3=P1^2;
//Define S3 as P1.2 pin
sbit S4=P1^3;
//Define S4 as P1.3 pin
unsigned char keyval;
//Call keyboard scan function
{
case 1:forward();
//call forward flow water light function
break;
//drop out of switch loop
case 2:backward(); // call backward function
Topic 3: Design process of 8051 MCU
Design example: Control of LED flowing water lighs --LED流水灯点亮的控制
1
Requirements (设计要求):
The MCU system has 4 independent keys K1 ~ K4 and 8 LED
• Press the K3, and the top and bottom LEDs will be on
alternately;
• Press K4, 8 LEDs of P3 port will blink.
2
schematic circuit diagram
3
1.Open Proteus ISIS, draw the schematic circuit diagram.
unsigned char i,j; for(i=0;i<100;i++) for(j=0;j<100;j++)
; }
//函数:延时
//函数:软件消抖延时10ms
10
10
lights. 4 keys are connected at P1.0 ~ p1.3 pins to control
whether the LED is on or off. 8 LED are connected at P3 port.
See the schematic circuit diagram.
• Press the K1, 8 LEDs of P3 port will be on in the forward
direction (from top to bottom);
• Press the K2, 8 LEDs of P3 port turn on in reverse (from
bottom to top);
2. Build a project in Keil μVision3,and write a source ‘*.c’ .
Reference source program:
#include<reg51.h>
// head file,difining 51 MCU CPU registers
sbit S1=P1^0;
break;
case 3:Alter();
break;
case 4:blink ();
பைடு நூலகம்
break;
}
}
}
5
5
void key_scan(void) {
P1=0xff; if((P1&0x0f)!=0x0f) {
delay10ms(); if(S1==0)
keyval=1; if(S2==0)
keyval=2; if(S3==0)