PIC16F-4-UART-Initialization

by Marwen Maghrebi
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000

// Configuration bits
#pragma config FOSC = HS    // High-speed oscillator
#pragma config WDTE = OFF   // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF  // Power-up Timer Enable (PWRT disabled)
#pragma config BOREN = ON   // Brown-out Reset Enable (BOR enabled)
#pragma config LVP = OFF    // Low-Voltage (single-supply) In-Circuit Serial Programming Enable (LVP disabled)
#pragma config CPD = OFF    // Data EEPROM Memory Code Protection (Data EEPROM code protection off)
#pragma config WRT = OFF    // Flash Program Memory Write Enable (Write protection off)
#pragma config CP = OFF     // Flash Program Memory Code Protection (Code protection off)

// Define LED pins
#define LED1 RA0 // LED1 on RA0
#define LED2 RA1 // LED2 on RA1

// Function to initialize UART transmission
void UART_TX_Init(void)
{
    // Set baud rate to 9600 bps
    BRGH = 1;       // High-speed baud rate
    SPBRG = 51;     // Set SPBRG for 9600 bps with 8 MHz clock
    
    // Enable Asynchronous Serial Port
    SYNC = 0;
    SPEN = 1;
    
    // Set RX-TX Pins to be in UART mode
    TRISC6 = 0;     // TX pin (output)
    TRISC7 = 1;     // RX pin (input)
    
    // Enable UART Transmission
    TXEN = 1;
    // Enable UART Reception
    CREN = 1;
}

// Function to initialize LEDs
void LED_Init(void)
{
    TRISA0 = 0; // Set RA0 as output (LED1)
    TRISA1 = 0; // Set RA1 as output (LED2)
    LED1 = 0;   // Turn off LED1 initially
    LED2 = 0;   // Turn off LED2 initially
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00