Purpose:
Master the use of output,
delay
,
LED and digital tube.
Experimental equipment and software:
Computer;
Keil
,
Proteus
, MCU Elf.
Experiment content:
Traffic lights are an indispensable and important part of people's lives and are of great significance. Just a few days after getting started with single-chip microcomputer, with the most
Simple control technology can also design simple traffic lights.
Experimental requirements:
1. There are
3 LEDs
in each of the east-west and north-south directions,
which are red, yellow and green .
2.
First the red light
for 10s
, then the green light
for 7s
, and finally the yellow light flashes
for 3s
.
3.
Through the
BCD
digital tube, the countdown of the whole cycle of
20s is displayed
.
4.
It can only be delayed by software
delay
, and hardware timer cannot be used.
related information:
1. How to use Keil
and
Proteus .
2.
The output of the microcontroller.
3. LED
control.
4.
Nixie tube control.
5.delay
software delay.
Preliminary knowledge:
1. How to use Keil
and
Proteus .
2. C
language, microcontroller pin output,
delay
software delay.
Experimental steps:
1.
Draw the
Proteus
circuit diagram, add
51
single-chip microcomputers,
LEDs
, and digital tubes.
2.
Create a new
Keil
project, write programs, generate
Hex
, and control traffic lights.
3. Load Hex
into the circuit and run it.
Code display:
#include <reg51.h>
#include <stdio.h>
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
sbit led5=P2^4;
sbit led6=P2^5;
char led_mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(int ms) //ÑÓʱ³ÌÐò
{
int j,k;
for(j=0;j<ms;j++) //ÑÓʱms
for(k=0;k<124;k++); //´óÔ¼1ºÁÃëµÄÑÓʱ
}
void display(int num) {
P3 = led_mod[num / 10]; // ʮλÊý
P1 = led_mod[num % 10]; // ¸öλÊý
}
void main(void)
{
int seconds = 20;
int p,k;
led1=0;led2=0;led3=0;
P1=led_mod[7];
P3=led_mod[7];
led1=1;led2=1;led3=1;
led4=1;led5=1;led6=1;//³õʼ»¯
while (1)
{
seconds = 20;
for(p=20;p>10;p--)
{
led1=1;led2=1;led3=0;
led4=0;led5=1;led6=1; //ºìÁÁ
display(seconds);
seconds--;
delay(1000);
}
for(k=0;k<3;k++)//»ÆÉÁ
{
led1=1;led2=0;led3=1;
led4=1;led5=0;led6=1;
delay(500);
led1=1;led2=1;led3=1;
led4=1;led5=1;led6=1;
seconds--;
display(seconds);
delay(500);
}
for(p=7;p>1;p--)
{
led1=0;led2=1;led3=1;
led4=1;led5=1;led6=0;//ÂÌÁÁ
seconds--;
display(seconds);
delay(1000);
}
//ÓÒ±ßÒ»ÅŵÆ
seconds = 20;
led1=1;led2=1;led3=1;
led4=1;led5=1;led6=1;//³õʼ»¯
for(p=20;p>10;p--)
{
led1=0;led2=1;led3=1;
led4=1;led5=1;led6=0; //ºìÁÁ
display(seconds);
seconds--;
delay(1000);
}
for(k=0;k<3;k++)//»ÆÉÁ
{
led1=1;led2=0;led3=1;
led4=1;led5=0;led6=1;
delay(500);
led1=1;led2=1;led3=1;
led4=1;led5=1;led6=1;
seconds--;
display(seconds);
delay(500);
}
for(p=7;p>1;p--)
{
led1=1;led2=1;led3=0;
led4=0;led5=1;led6=1;//ÂÌÁÁ
seconds--;
display(seconds);
delay(1000);
}
}
}