Activity 1. Turn ON/OFF four LEDs

The purpose of this activity is to turn ON 4 LEDs that are connected to the PORTB of the microcontroller

(60 minutes)


Activity 1

Step 1. The circuit is drawn in the Proteus Design Suite. In this step 8 LEDs are connected to the PORTD parallel port. A voltmeter and an ammeter are also connected to check the voltage drop across one LED and the current through this LED.




Step 2. The values of the resistors in series with the LEDs are calculated so that the current through the LEDs is in the range from 10 to 15 mA. The voltage drop across the animated red LEDs at the Proteus Design Suite is 2,2 V.

Tip1. We accept that the voltage drop across the red LED is 2,2 V and we calculate the value of the resistor so that the current through the resistor (and the LED) is 10 mA.

Tip2. The voltage drop across a LED depends on the color of the LED and we should consult the manufacturers datasheet



Step 3. The program in C language is written.

#include <main.h> // the file main.h with the

                             // initial settings is included

                             // This file must be placed in the same

                             // folder with the project

                             // Also the 18F4550.h file must exist

                             // in the same folder with the project

#byte PORTB =0xF81 // We attribute to the memory posistion 0xF81

                                 // the name PORTB

                                 // This means that we define a 8 bit

                                 // variable whose value will be stored

                                 // to the memory position F81h

                                 // The memory position F81h is the PORTB

                                 // data register

 

void main(void){

 

set_tris_b(0x00);  // PORTB is defined as output

                          // The PORTB direction register

                          // is given the value 0000 0000

 

PORTB =0b11110000;  //The PORTB data register is given

                                   //the value 1111 0000

 

     while(TRUE) {  } // eternal loop                           

               }               // closes the bracket of main()



Step 4. The program is compiled with the use of CCS C compiler to the microcontroller machine code.




Step 5. The machine code is loaded to the microcontroller.



Step 6. The animation is activated and we check that the 8 LEDs turn ON. We check that the voltage drop across the LEDs and the current through them are as predicted.




Step 7. Suggested modifications and discussion:
    • Change the value of a resistor in series to a LED to 1 KΩ and check if the LED turns ON. Give an explanation to what you observe
    • Change the value of a resistor in series to a LED to 330 Ω and check if the luminance of the LED changes. Give an explanation to what you observe