Tuesday, 20 October 2015

PIC16F1518 SERIAL COMMUNICATION




#include<pic.h>
#include<htc.h>
#define __PIC16F1518_H
#define _XTAL_FREQ   16000000

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF &  WRT_OFF );

void TX(unsigned char Byte);
unsigned int a1;

void interrupt ISR(void)
{
if(RCIF)  // If UART Rx Interrupt
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}

TX(RCREG); // Echo back received char
}
}


void UART(void)
{
TRISC6 = 0;   // TX Pin
TRISC7 = 1;   // RX Pin

SPBRG = 103;
BRGH  = 1;                   // Fast baudrate
SYNC  = 0; // Asynchronous
SPEN  = 1; // Enable serial port pins
CREN  = 1; // Enable reception
SREN  = 0; // No effect
TXIE  = 0; // Disable tx interrupts
RCIE  = 1; // Enable rx interrupts
TXEN  = 0; // Reset transmitter
TXEN  = 1; // Enable the transmitter
}


void TX(unsigned char Byte)  // Writes a character to the serial port
{
while(!TXIF);  // wait for previous transmission to finish
TXREG = Byte;
}

unsigned char RX(void)   // Reads a character from the serial port
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}

        while(!RCIF);  // Wait for transmission to receive
a1 = RCREG;
return a1;
}

void String(unsigned char*p)
{
while(*p)
TX(*p++);
}

void main(void)
{
UART(); // Intialize UART

    String("EMBEDDED_LAB,WWW.SUMMITBHARDWAJ.BLOGSPOT.IN"); // Send string on UART

GIE  = 1;   // Enable global interrupts
        PEIE = 1;   // Enable Peripheral Interrupts

while(1)
{

}
}

1 comment:

  1. Interesting! I´m on a project and i would like to use the PIC16F1518 and compile on CCS compiler..but i can´t find the correct header (16F1518.h). Could you share this header, please? Wich compiler did you use?
    Saludos from Argentina!

    ReplyDelete