In this blog post, we’ll explore how to interface a simple LED with the PIC18F45Q10 microcontroller and make it blink at regular intervals. This is a great beginner project to understand how to work with input/output (I/O) pins, configure the microcontroller, and implement a delay for controlling time-based events.
Components Needed
- PIC18F45Q10 Microcontroller
- LED
- 220Ω Resistor
- Breadboard
- Jumper Wires
- External 20 MHz Crystal Oscillator
- Power Supply (5V)
Circuit Diagram
The circuit for interfacing the LED with the PIC18F45Q10 is very simple. We’ll use pin RA0 (pin 2 on the chip) as an output to control the LED. The LED will be connected to this pin with a current-limiting resistor (220Ω).
The anode (positive) of the LED will be connected to RA0, and the cathode (negative) will be connected to the ground.
Here’s the simple circuit diagram:
Code Breakdown
The following code example demonstrates how to blink an LED using the PIC18F45Q10 microcontroller. We’ll configure the I/O pins, set a delay, and toggle the LED on and off.
/*
* File: main.c
* Author: erraj
*
* Created on January 5, 2025, 2:26 PM
*/
#include <xc.h> // Include the header file for the PIC18F45Q10
// PIC18F45Q10 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config FEXTOSC = HS // External Oscillator mode Selection bits (EC (external clock) above 8 MHz; PFM set to high power)
#pragma config RSTOSC = EXTOSC // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits (device manufacturing default))
// CONFIG2L
#pragma config MCLRE = EXTMCLR // Master Clear Enable bit (MCLR pin (RE3) is MCLR)
// CONFIG3L
#pragma config WDTE = OFF // WDT operating mode (WDT disabled)
// End of configuration bits
#define _XTAL_FREQ 20000000 // Define crystal frequency (20 MHz)
void main(void) {
// Set RA0 as output (LED pin)
TRISA = 0xFF; // Set PORTA as input (default)
TRISA0 = 0; // Set RA0 as output
// Main loop
while(1) {
LATAbits.LATA0 = 1; // Turn LED ON (RA0 high)
__delay_ms(500); // Wait for 1 second
LATAbits.LATA0 = 0; // Turn LED OFF (RA0 low)
__delay_ms(500); // Wait for 1 second
}
}
Explanation:
- Header File (<xc.h>): This file includes all necessary definitions for the PIC18F45Q10 microcontroller. It provides access to register names, macros, and function prototypes for configuration and control.
- Configuration Bits: The configuration bits control how the microcontroller behaves at startup. The key settings in this code are:
- FEXTOSC = HS: This selects the external oscillator mode with a high-speed crystal (20 MHz).
- RSTOSC = EXTOSC: This configures the power-up default oscillator source to be the external oscillator.
- MCLRE = EXTMCLR: This configures the MCLR pin to act as an external reset pin.
- WDTE = OFF: This disables the Watchdog Timer.
- Main Function:
- TRISA = 0xFF: Configures all PORTA pins as inputs by default.
- TRISA0 = 0: Configures RA0 as an output pin.
- Inside the while(1) loop, the LED is toggled by setting RA0 high (turning on the LED) and then low (turning off the LED) with a delay of 500 milliseconds (__delay_ms(500)).
Key Functions:
- LATAbits.LATA0 = 1: This sets RA0 high, turning the LED on.
- LATAbits.LATA0 = 0: This sets RA0 low, turning the LED off.
- __delay_ms(500): This function introduces a delay of 500 milliseconds between each change in the LED state. The __delay_ms() function is based on the defined _XTAL_FREQ, which is set to 20 MHz for this project.
Output
When the circuit is powered up, the LED connected to RA0 will start blinking with a 1-second interval (500 ms ON, 500 ms OFF). The blinking LED demonstrates how the microcontroller can control I/O pins and implement delays. This basic setup can be expanded to include more LEDs or integrated with sensors to create more complex applications.
Watch the Demo Video:
Conclusion
This simple project demonstrates how to interface an LED with the PIC18F45Q10 microcontroller and control it using basic I/O operations. By using an external oscillator and configuring the I/O pins, we can toggle the LED on and off with a delay, effectively creating a blinking LED.
This project serves as a great foundation for beginners looking to understand microcontroller programming and hardware interfacing. You can expand this project by connecting multiple LEDs, adding sensors, or implementing more advanced control mechanisms.
If you have any questions or want to dive deeper into more advanced topics, feel free to leave a comment below! Happy coding!