Activity 2. LEDs on PORTB turn ON and OFF in accordance with a program selected with the switches connected on PORTD
The purpose of this activity is to program the microcontroller so that the LEDs connected to PORTB turn ON and OFF in accordance with a program selected with the switches connected on PORTD.
(30 minutes)
Activity 2
Step 1. The circuit is drawn in the Proteus Design Suite. In this step 8 LEDs are connected to the PORTB parallel output port and 8 switches to the PORTD input port.

Step 2. The program in C language is written.
Write in CCS Compiler the program in C language
#include<main.h> //The file <main.h> contains the initial settings
//This file must be in the same folder
//with your project
#byte PORTB=0xF81 //F81 is the address of the PORTB data register
// in the data memory of the PIC18F4550 microcontroller.
// It's a Special Function Register (SFR)
#byte PORTD=0xF83 //F83 is the address of the PORTB data register
// in the data memory of the PIC18F4550 microcontroller.
// It's a Special Function Register (SFR)
// ********* Main program ************************
void main()
{ // Opening bracket of main()
set_tris_b(0x00); // PORTB becomes output(Direction Register=0000 0000)
set_tris_d(0xff); // PORTD becomes input(Direction Register=1111 1111)
PORTB=0b00000000; // PORTB takes the initial value 0000 0000
int a; // definition of integer variable a for storing the value
// of PORTD
int i; // Integer variable we use inside the for
// With the while (TRUE){ } the content inside the brackets
// is executed endless
// The word TRUE corespondes to a true condition.
// Instead of TRUE we could use the condition 5>1
// or any othere condition that is always true.
while(TRUE) { //Eternal loop (contition always true)
a=PORTD; //The content of PORTD data register is transfered
// to the variable a
switch (a){
case 0: PORTB=0xFF; delay_ms(100); PORTB=0x00; delay_ms(100);
// Progran turning ON and OFF when a=0 (PORTD = 0000 0000)
break;
case 1: PORTB=0b11000011;delay_ms(100);PORTB=0b00111100;delay_ms(100);
// Progran turning ON and OFF when a=1 (PORTD = 0000 0001)
break;
case 2: PORTB=0b10101010;delay_ms(100);PORTB=0b01010101;delay_ms(100);
// Progran turning ON and OFF when a=2 (PORTD = 0000 0010)
break;
case 3: PORTB=0b10000000; for (i=1; i<=7; i++)
{delay_ms(50); PORTB=PORTB/2;}
delay_ms(50);
// Progran turning ON and OFF when a=3 (PORTD = 0000 0011)
break;
case 4: PORTB=0xF0; delay_ms(100); PORTB=0x0F; delay_ms(100);
// Progran turning ON and OFF when a=4 (PORTD = 0000 0100)
break;
} // Closing bracket of switch ()
} //Closing bracket of while ()
} // Closing bracket of main ()
Step 3. Use the CCS C Compiler to translate the programm from C language to the microcontroller machine code. Load to the microcontroller the hex file (machine code) that was created from the CCS Compiler.
Step 4. Run the simulation and check by setting to PORTD the values 0, 1, 2, 3, 4 the correspondent program of turning ON and OFF the LEDs is executed.