Lab 2 Embedded 2003
Lab 2 Embedded 2003
CLO1 /20
Learning Outcomes:
OBJECTIVE
1. Write program in C language in MPLABX IDE.
2. Draw circuit in Proteus and simulate the output.
THEORY
Push Buttons are mechanical switches. Then can make or break connection between two
terminals and comes back to stable state when released. They are called as Push to ON or
Push to OFF switches respectively.
EQUIPMENT
PROCEDURE
1. At desktop or any location drive, create a folder named PW. In the folder, create
subfolder as below.
2. Open MPLABX, follow the previous step to create a C main file. Write the program below.
Program 1
#include <xc.h>
#define XTAL 64000000
#define SW1 PORTCbits.RC0 //define RC0 as Switch 1
#define Led1 LATAbits.LATA0 //define RA0 as LED1
#define Led2 LATBbits.LATB0 //define RB0 as LED2
void main(void)
{
TRISCbits.TRISC0=1; //set RC0 as input
TRISAbits.TRISA0=0; //set RA0 as output
TRISBbits.TRISB0=0; //set RB0 as output
Save your program as Lab2a.c. Build all the program until successful. If any error
occurs, fix it.
OBSERVATIONS
This program controls two LEDs using a push button with a PIC18F microcontroller. When
the button (SW1) is pressed, both LEDs turn ON; otherwise, they remain OFF. The circuit
uses RC0 as an input for the switch and RA0, RB0 as outputs for the LEDs. The logic runs
continuously in a while loop. To improve the design, a debounce mechanism can be added
to prevent unwanted flickering. 🚀
RESULT :
Conclusion
This program controls two LEDs using a push button with a PIC18F microcontroller. When
the button (SW1) is pressed, both LEDs turn ON; otherwise, they remain OFF. The circuit
uses RC0 as an input for the switch and RA0, RB0 as outputs for the LEDs. The logic runs
continuously in a while loop. To improve the design, a debounce mechanism can be added
to prevent unwanted flickering. 🚀
Design the circuit for program in Figure 1 in Proteus and execute it.
Observation:
State your observation of the circuit.
RESULT :
OBSERVATION
This program controls two LEDs using a push button on a PIC18F45K22 microcontroller.
When the button (RC0) is pressed, both LEDs (RA0, RB0) turn ON; otherwise, they remain
OFF. The program continuously checks the button state in a loop, making it simple but
effective for basic digital input handling.
3. Next, write the program below. Fix the error and draw the circuit in proteus. State your
observation.
#include <xc.h>
#define XTAL 64000000
#define SW1 PORTCbits.RC0 //define RC0 as Switch 1
#define SW2 PORTCbits.RC1 //define RC1 as Switch 2
#define Led1 LATAbits.LATA0 //define RA0 as LED1
#define Led2 LATBbits.LATB0 //define RB0 as LED2
void main(void)
{
TRISCbits.TRISC0=1; //set RC0 as input
TRISCbits.TRISC1=1; //set RC1 as input
TRISAbits.TRISA0=0; //set RA0 as output
TRISBbits.TRISB0=0; //set RB0 as output
Led1=0; Led2=0; //set Led1 and Led2
OBSERVATIONS
This program controls two LEDs using two switches. When SW1 is pressed, LED1 turns on,
and when SW2 is pressed, LED2 turns on. If no switch is pressed or both are pressed, both
LEDs remain off. A 1-second delay is added when switching states.
5.0 DISCUSSION
In this practical, we explored the use of push buttons as inputs and LEDs as outputs with a
PIC18F microcontroller. The code was written in C using MPLAB X IDE and then simulated in
Proteus to verify its functionality.
Key observations and findings:
Program 1: Had a syntax error due to an unnecessary semicolon (if(SW1==0);), which
caused the condition to be ignored. Once corrected, pressing SW1 turned both LEDs
ON, and releasing it turned them OFF.
Program 2: Used two switches to control two LEDs independently, but there was an
error in the delay function (delay(1000); instead of _delay(1000);). Once fixed, the
LEDs responded correctly to their respective switches.
These experiments demonstrated how digital inputs (switches) can be used to control
digital outputs (LEDs) and how logic conditions in C programming affect microcontroller
behavior.
6.0 CONCLUSION
This experiment taught us how to control LEDs using push buttons with a PIC18F
microcontroller. We wrote, debugged, and simulated programs in MPLAB X IDE and Proteus.
The key takeaways include properly setting input/output pins, fixing coding errors, and
understanding basic microcontroller logic. This practical helped build a strong foundation for
future embedded system applications.