PIC UART help needed - Page 1
Forum section: electronics / general

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 07:10:22 am
content: I am trying to use the peripheral libraries & get a PIC18F2550 to send a character every second through UART. But either I receive invalid data bytes or I don't receive anything at all. Where am I going wrong?I have attached the code. Please let me know if anything else is required. I am using XC8 compiler.Code -//peripheral libraries#include <plib/usart.h>#define _XTAL_FREQ 8000000#define USE_AND_MASKSvoid delay(unsigned long x);unsigned char config, baud;void main(){config=USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_LOW;baud=0x1C; //9600bpsOpenUSART(config, baud); //configure & enable UARTwhile(1){WriteUSART('A');delay(1000);}}//delay functionvoid delay(unsigned long x){unsigned long m;for(m=0; m<x; m++){__delay_ms(1);}}

author: Skimask
author_rank: Super Contributor
timestamp: February 25, 2014, 07:43:41 am
content: Nothing like a schematic to help a guy figure something out.Nothing like knowing what's at the receiving end of all of this.Nothing like a decent description of everything.Three things that apparently don't exist...

author: AlfBaz
author_rank: Super Contributor
timestamp: February 25, 2014, 09:14:30 am
content: I don't see any clock initialisation codeYou have defined _XTAL_FREQ as 8 meg but nothing in your code show it being used.How did you calculated the baud rate it's dependant on Fosc. You need to know Fosc to calculate the baud value

author: Psi
author_rank: Super Contributor
timestamp: February 25, 2014, 09:27:29 am
content: yeah, baud doesnt look right, try 0x0C or check the datasheet formula

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:12:05 am
content: Quote from: AlfBaz on February 25, 2014, 09:14:30 am_XTAL_FREQ is required for __delay_ms(). I have used UART on this PIC before (I had written the functions for the UART myself then) & had used 0x1C at that time. According to the datasheet, for an 8MHz crystal attached to the PIC, this gives a bit rate of 9600bps.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:14:01 am
content: Quote from: Psi on February 25, 2014, 09:27:29 amOkay will try that but I don't think thats the problem. I saw the example in the documentation for the XC8 compiler. They used 51 (gives 2400 bps for an 8Mhz crystal). That didn't work either.

author: AlfBaz
author_rank: Super Contributor
timestamp: February 25, 2014, 10:20:35 am
content: Quote from: Udayan Sinha on February 25, 2014, 10:12:05 amhmm I must of missed it. I didn't see 28 (0x1C) in any of the tables in the data sheet for 8MHz

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:25:16 am
content: Quote from: Skimask on February 25, 2014, 07:43:41 amThe TX of the PIC is connect to the RX of a PICkit-2. So I am reading the values on the serial monitor provided with the PICkit application.And I have 100nF capacitors between Vdd & Vss. My guess is that the problem is with the code & not the circuit since I used the same circuit to blink an LED before trying out the UART. Feel free to look through the schematic though.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:28:20 am
content: Quote from: AlfBaz on February 25, 2014, 10:20:35 amHey no wait its 0x0C. My bad. Let me try with 0x0C.

author: diyaudio
author_rank: Frequent Contributor
timestamp: February 25, 2014, 10:33:27 am
content: /** P R O C E S S O R C O N F I G U R E **************************************************/#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF // CONFIG1H#pragma config PWRT = OFF, BOREN = OFF, BORV = 30 // CONFIG2L#pragma config WDTEN = OFF, WDTPS = 32768 // CONFIG2H#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC // CONFIG3H#pragma config STVREN = ON, LVP = OFF, XINST = OFF, DEBUG=OFF // CONFIG4L#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L#pragma config CPB = OFF, CPD = OFF // CONFIG5H#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L#pragma config EBTRB = OFF#define _XTAL_FREQ 8000000void send_usart_data(unsigned char* data){/** USART ****************************************************************/// 86 At 8Mhz of oscillator frequency & baud rate of 115200.CloseUSART();BAUDCONbits.BRG16 = 1;OpenUSART(USART_TX_INT_OFF &USART_RX_INT_OFF &USART_ASYNCH_MODE &USART_EIGHT_BIT &USART_CONT_RX &USART_BRGH_HIGH,86); //86 for baud of 115200putrsUSART (data);CloseUSART(); //turn off usart if was previously on}I used putrsUSART (data); you seem to have used WriteUSART('A'); Im using XC-8

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:35:34 am
content: Quote from: diyaudio on February 25, 2014, 10:33:27 amYeah because I am only sending a byte of data at a time. putrsUART() is used to send multiple characters.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:36:19 am
content: Got it working. The baud rate was wrong as pointed out. Working with 0x0C. Thanks a lot guys.

author: dannyf
author_rank: Super Contributor
timestamp: February 25, 2014, 11:51:38 am
content: UART is very slow. So you may want to figure out how to use the interrupt to free up the mcu when it is transmitting on uart.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 11:54:30 am
content: Quote from: dannyf on February 25, 2014, 11:51:38 amYes I know how to use interrupts. My aim here was to try & get a feel of the peripheral libraries. That is why I wrote such a simple code.

author: dannyf
author_rank: Super Contributor
timestamp: February 25, 2014, 12:06:02 pm
content: As to your code, you can calculate the baud rate value from the oscillator frequency and desired baud rate. The code would then work any user-specified baud rate.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: February 25, 2014, 12:18:26 pm
content: Quote from: dannyf on February 25, 2014, 12:06:02 pmHmmm you are right. Thanks for the advice.