PIC 16F628A UART demo.

This is a demo of how to use libraries when programming PICs.

Main program

// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <stdbool.h>
#include <string.h>
#include "uart_init_header.h"

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable bit (RB4/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// DEFINITIONS
#define _XTAL_FREQ 4000000

// GLOBAL VARIABLES

// FUNCTION PROTOTYPE

// FUNCTIONS

// MAIN PROGRAM
void main() {

    CMCON = 0x07;

    TRISA = 0b00000000;     // All output
    PORTB = 0b00000000;     // All low

    TRISB = 0b00000110;     // RB1 & RB2 set as input ref datasheet
    PORTB = 0b00000000;     // All low  

uart_init();

uart_send_str("\r\n");
uart_send_str("**************************************************\r\n");
uart_send_str("**\t\tTHIS IS A USART TEST\t\t**\r\n");
uart_send_str("**************************************************\r\n");

while (1) {
    // Do nothing forever!
}
}

Uart header file

#ifndef UART_INIT_HEADER_H
#define UART_INIT_HEADER_H

#ifdef  __cplusplus
extern "C" {
#endif

#include <xc.h>
void uart_init();
void uart_tx(char out);
char uart_rx();
void uart_send_str(const char *);

#ifdef  __cplusplus
}
#endif

#endif  /* UART_INIT_HEADER_H */

Uart source file

#include "uart_init_header.h"

void uart_init() {

    TXSTAbits.BRGH = 1; // high baud selection bit, 1=high, 0=low
    TXSTAbits.SYNC = 0; // USART mode selection bit, 1=sync mode, 0=async mode
    TXSTAbits.TX9 = 0;  // 9-bit selection bit, 1=9-bit transmission, 0=8-bit trans
    RCSTAbits.CREN = 1; // continous receive enable bit, 1=Enable continous receive.
    /* 4Mhz -> 9600
     * 4000000 / 16 = 250000
     * 250000 / 9600 = 26.0416
     * 26.0416 - 1 = 25
     */
    SPBRG = 25; // 9600-n-8-1

    PIE1bits.RCIE = 1;  // USART Receive iterrupt enable bit, 1=enable, 0=disable
    RCSTAbits.SPEN = 1; // serial port enable bit, 1=serial port enable, 0=disable
    TXSTAbits.TXEN = 1; // transmit enable bit, 1=Transmit enable, 0=disable  
    return;
}

void uart_tx(char out) {
    while (TXIF == 0);
    TXREG = out;
}

char uart_rx() {
    while (RCIF == 0);
    if (RCSTAbits.OERR) {
        CREN = 0;
        NOP();
        CREN = 1;
    }
    return (RCREG);
}

void uart_send_str(const char *out) {
    while (*out!='\0') {
        uart_tx(*out);
        out++;
    }
}

blogroll

social