
Chapter 7. Basic Experiments
7.1. Experiment with Flashing LED
LED, short for light emitting diode, is commonly seen in daily life. We start up this chapter with the simplest experiment on LED flashing.

There are many types of LED. Φ3mm normal luminance LEDs are shown in the below picture.
 
See LED’s symbol. When certain voltage is given between A (anode) and K (cathode), LED will light up. Different LED works on different voltage, ranging from 1.6V to 2.8V. Forward current ranges from 4 to 10mA.
With LED’s voltage and current parameters, it’s easy to calculate a suitable resistor used to limit the current. For example, if system power is 5V, resistor used is 1kΩ, and LED V(on) is 2.0V, then the current on LED is ( 5V - 2V ) / 1000 Ω = 3mA. To increase the luminance, increase the current to 10mA is a good practice. Because ( 5V - 2V ) / 1000Ω = 3mA, we use 330Ω.
See below schematic diagram. The LED’s anode is connected via a resistor to VCC, and its cathode is connected to MCU’s I/O pin. When MCU’s I/O pin becomes low level, the LED will light up. In this experiment, setting P0.0 to be low will light the LED D up.

Likewise, to turn off the LED, what we need to do is just to set P0.0 to high level. It’s also easy to make the LED flashing (lighting on and off in turn). Insert a delay around 300ms between LED on and off will make the LED flashing.
01 #include <reg51.h>
02
03 sbit LED = P0^0;
04
05 void Delay()
06 {
07 unsigned char i,j;
08 for(i=0;i<255;i++)
09 for(j=0;j<255;j++);
10 }
11
12 void main()
13 {
14 while(1)
15 {
16 LED = 0;
17 Delay();
18 LED = 1;
19 Delay();
20 }
21 }
Program Notes
Line 1: include the 8051 register definition header file
Line 3: bit define the I/O pin connecting with LED
Line 5-10 delay function, the delay time depends on the MCU clock
Line 7: define 2 unsigned char variable i and j
Line 8-9: delay certain time by the for loop self-increment
Line 12-21: main function
Line 14: the while loop
Line 16: light the LED up
Line 17: invoke the delay function
Line 18: light the LED off
Line 19: invoke the delay function |