SERIAL COMMUNICATION
UART
Transmission
#include<pic.h>
#define _XTAL_FREQ 16000000
void main() {
SPEN = 1; // enables serial port
SYNC = 0; // setting in asynchronous mode
BRGH = 0; // selects low speed baud rate
SPBRG = 25; // setting baud rate @ 9600
TXEN = 1; // enables transmission
TX9 = 0; // sets transmission in 8-bit mode
while (1) {
if (TRMT == 1) // check status of transmit shift register for empty
{
TXREG = 'A'; // writes ascii value of letter A
}
}
}
Reception
Note– i have used PORTB to see incoming ascii value
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
SPEN = 1; // enables serial port
SYNC = 0; // setting in asynchronous mode
BRGH = 0; // selects low speed baud rate
SPBRG = 25; // setting baud rate @ 9600
CREN = 1; // enables reception
RX9 = 0; // sets reception in 8-bit mode
TRISB = 0;
PORTB = 0;
while (1)
{
PORTB = (RCREG - 0x30); // writes ascii value of letter A
}
}
See software UART at http://jimmyjosep.blogspot.in/2013/11/under-standing-uartsoft.html
EEPROM
Steps to Read
- Specify the memory location in EEADR (EEADR=4;)
- Clear the EEPGD bit (EEPGD=0 😉
- Read the value from EEDATA register (unsigned short int a =EEDATA 😉
Steps to Write
- Specify the memory location in EEADR (EEADR=4;)
- Write the value to EEDATA register (EEDATA=7;)
- Set WREN (WREN=1;)
- Clear the EEPGD bit (EEPGD=0 😉
- Write 55h to EECON2 (EECON2=0x55;)
- Write AAh to EECON2 (EECON2=0xAA;)
- Set the WR bit (WR=1;)
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0x3f7a);
unsigned short int eeread();
eewrite(unsigned short int , unsigned short int );
main()
{
eewrite(4,'1');
unsigned short int a = eeread(); // data from eeprom
TRISB = 0;
PORTB = a;
while(1) ;
}
unsigned short int eeread()
{
EEADR = 4;
EEPGD = 0;
unsigned short int z = EEDATA;
return z;
}
eewrite(unsigned short int x, unsigned short int y)
{
EEADR = x;
EEDATA = y;
WREN = 1;
EEPGD = 0 ;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
}