Hello Everyone
I am planning to develop a program that controls three LEDs using a PIC microcontroller and UART communication. The program should respond to specific commands received via UART and adjust the flashing speed of the LEDs accordingly.
Here's how I want it to work:
The key requirement here is that the speed of LED flashing should change only when a new command is received. Once a speed is set, it should keep flashing at that speed until a new command alters the speed.
Additionally, I have already written code that can receive commands from the serial port and send command back to serial port to verify two-way communication via UART.
I've attached a screenshot of an experiment where I typed the number '1233' to showcase this communication

As part of my plan to implement the LED control functionality, I am also planning to generate a 1ms timer to count time intervals for LED flashing. How can I integrate this 1ms timer into my existing code to achieve the desired LED flashing behavior?
Are there any specific techniques I should consider for efficient UART communication and LED control on my microcontroller?"
I am planning to develop a program that controls three LEDs using a PIC microcontroller and UART communication. The program should respond to specific commands received via UART and adjust the flashing speed of the LEDs accordingly.
Here's how I want it to work:
- When 'Command 1' is received, the program should flash the LEDs every 2 seconds.
- When 'Command 2' is received, the program should flash the LEDs every 1 second.
- When 'Command 3' is received, the program should flash the LEDs every half a second.
The key requirement here is that the speed of LED flashing should change only when a new command is received. Once a speed is set, it should keep flashing at that speed until a new command alters the speed.
Additionally, I have already written code that can receive commands from the serial port and send command back to serial port to verify two-way communication via UART.
C:
/*
* File: main.c
* Author: kittu
* Created on 14 September, 2023, 8:35 AM
*
*/
#include <xc.h>
#include <stdio.h>
#include"config.h"
void initializeOscillator(void);
void initializeEUSART1(void);
/*****
* NOTE: This function configures the oscillator settings for the microcontroller.
* It sets the IRCF bits to 110 for HF-INTOSC/2 (8 MHz, default).
*****/
void initializeOscillator(void)
{
OSCCON = 0x60; // Set IRCF bits to 110 for HF-INTOSC/2 (8 MHz, default)
OSCCON2 = 0x00;
OSCTUNE = 0x00;
REFOCON = 0x00;
}
void initializePort() {
// Set LATx registers to initial states (all low)
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
// Set TRISx registers to configure pins as outputs (all output)
TRISA = 0x00;
TRISB = 0x00; // RB0 is RED, RB1 is YELLOW and RB2 is GREEN LED
TRISC = 0x80; // RC7 is TX and RC6 TX
TRISD = 0x00;
TRISE = 0x00;
// Additional configuration for unused peripherals
ANCON0 = 0x00; // Set all analog pins to digital mode
ANCON1 = 0x00; // Set all analog pins to digital mode
CM1CON = 0x00; // Turn off Comparator 1
CM2CON = 0x00; // Turn off Comparator 2
ADCON0 = 0x00; // Disable A/D conversion
ADCON1 = 0x00; // Disable A/D conversion
ADCON2 = 0x00; // Disable A/D conversion
}
/*****
*
* NOTE: This function configures the EUSART1 settings for communication.
*
*****/
void initializeUART1() {
// Configure BAUDCON1 register
// Bit 7: ABDOVF = 0 (Auto-Baud Acquisition Rollover Status bit - Cleared in software)
// Bit 6: RCIDL = 0 (Receive Operation Idle Status bit - Receive operation is active)
// Bit 5: RXDTP = 0 (Received Data Polarity Select bit - Receive data is not inverted)
// Bit 4: TXCKP = 0 (Clock and Data Polarity Select bit - Idle state for transmit is high level)
// Bit 3: BRG16 = 1 (16-Bit Baud Rate Register Enable bit - 16-bit Baud Rate Generator)
// Bit 2: Unimplemented: Read as ?0? (Reserved, read as '0')
// Bit 1: WUE = 0 (Wake-up Enable bit - RX pin is not monitored or rising edge is detected)
// Bit 0: ABDEN = 0 (Auto-Baud Detect Enable bit - Baud rate measurement is disabled or completed)
BAUDCON1 = 0b00001000; // Set BRG16 bit to 1 (16-Bit Baud Rate Register Enable)
// Calculate and set SPBRG1 for a baud rate of approximately 9600 at 8 MHz
SPBRG1 = 207;
SPBRGH1 = 0;
// TXSTA1 register configuration
// Bit 7: CSRC = 0 (Clock Source Select bit - Don't care in Asynchronous mode)
// Bit 6: TX9 = 0 (8-bit transmission)
// Bit 5: TXEN = 1 (Transmit is enabled)
// Bit 4: SYNC = 0 (Asynchronous mode)
// Bit 3: SENDB = 0
// Bit 2: BRGH = 1 (High-speed baud rate)
// Bit 1: TRMT = 1 (TSR is empty initially)
TXSTA1 = 0b00100100; // Configure TXSTA1 register
// RCSTA1 register configuration
// Bit 7: SPEN = 1 (Serial Port Enable bit - Serial port is enabled)
// Bit 6: RX9 = 0 (8-bit reception)
// Bit 5: SREN = 0 (Single Receive Enable bit - Don't care in Asynchronous mode)
// Bit 4: CREN = 1 (Continuous Receive Enable bit - Enables receiver)
// Bit 3: ADDEN = 0 (Address Detect Enable bit - Don't care in Asynchronous mode)
// Bit 2: FERR = 0 (Framing Error bit - No framing error)
// Bit 1: OERR = 0 (Overrun Error bit - No overrun error)
RCSTA1 = 0b10010000; // Configure RCSTA1 register
}
void hardwareInitialize() {
// Initialize the oscillator
initializeOscillator();
// Initialize Port Pins
initializePort();
// Initialize Uart1
initializeUART1();
}
/*****
*
* NOTE: This function waits until the transmitter buffer is empty (TXIF is set)
* and then sends the character by placing it in the TXREG1 register.
*
*****/
void UARTWrite(char Character)
{
// Wait until the transmitter buffer is empty (TXIF is set)
while (TXIF == 0);
// Send the character by placing it in the TXREG1 register
TXREG1 = Character;
}
/*****
*
* NOTE: This function waits until the receiver buffer is full (RCIF is set)
* and then reads the received character from the RCREG1 register.
*
*****/
char UARTRead(void)
{
// Wait until the receiver buffer is full (RCIF is set)
while (RCIF == 0);
// Read the received character from the RCREG1 register
return RCREG1;
}
/*****
*
* main - Main program function
*
*****/
void main(void)
{
// Initialize hardware
hardwareInitialize();
// Delay for 5 seconds
__delay_ms(5000);
while (1)
{
// Define a character to send
char Character = 'U';
char ReceivedChar;
ReceivedChar = UARTRead(); // Read a command from UART
UARTWrite(ReceivedChar); // Send the command back
// Delay for 5 seconds before the next transmission
__delay_ms(5000);
}
}

As part of my plan to implement the LED control functionality, I am also planning to generate a 1ms timer to count time intervals for LED flashing. How can I integrate this 1ms timer into my existing code to achieve the desired LED flashing behavior?
Are there any specific techniques I should consider for efficient UART communication and LED control on my microcontroller?"