Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <xc.h>
- #include <string.h>
- #include <stdio.h>
- #include <htc.h>
- #define _XTAL_FREQ 20000000
- void InitUART(void);
- void SendByteSerially(unsigned char Byte);
- unsigned char ReceiveByteSerially(void);
- void SendStringSerially(const unsigned char* st);
- void main()
- {
- GIE = 1; // Enable global interrupts
- PEIE = 1; // Enable Peripheral Interrupts
- InitUART();
- TRISA = 0x00;
- PORTA = 0x00;
- while(1)
- {
- }
- }
- void interrupt ISR(void)
- {
- if(RCIF) // If UART Rx Interrupt
- {
- if(OERR) // If over run error, then reset the receiver
- {
- CREN = 0;
- CREN = 1;
- }
- SendByteSerially(RCREG); // Echo back received char
- }
- }
- void InitUART(void)
- {
- TRISC6 = 1; // TX Pin
- TRISC7 = 1; // RX Pin
- SPBRG = ((_XTAL_FREQ/16)/9600) - 1;
- 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
- TX9 = 0; // 8-bit transmission
- RX9 = 0; // 8-bit reception
- TXEN = 0; // Reset transmitter
- TXEN = 1; // Enable the transmitter
- }
- void SendByteSerially(unsigned char Byte) // Writes a character to the serial port
- {
- while(!TXIF); // wait for previous transmission to finish
- TXREG = Byte;
- }
- unsigned char ReceiveByteSerially(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
- return RCREG;
- }
- void SendStringSerially(const unsigned char* st)
- {
- while(*st)
- SendByteSerially(*st++);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement