Help with UART in PIC18F4431 - Page 1
Forum section: electronics / general

author: a.soliman
author_rank: Newbie
timestamp: April 11, 2015, 08:40:07 pm
content: Hello folks,I need some help configuring USART module for Asynchronous reception 9600 bps in a PIC18F4431 running at 4Mhz .I use XC8 compiler and I read the Peripherals support document included with the compiler.Here is my current configuring code :void USART_Init(){unsigned charconfig,baudconfig;TRISCbits.RC6 = 1;TRISCbits.RC7 = 1;CloseUSART();config =USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX |USART_BRGH_HIGH;baudconfig =BAUD_IDLE_RX_PIN_STATE_HIGH | BAUD_8_BIT_RATE |BAUD_AUTO_OFF | BAUD_WAKEUP_OFF;baudUSART(baudconfig);OpenUSART(config,25);}I need to receive 13 bytes so I use :getsUSART(buff,BUFFER_SIZE);where buff is declared as acharbuff I really don't know where I'm wrong ! but I'm about to give it up ! so please help meThanks in advance

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: April 12, 2015, 07:29:47 am
content: The config and baudconfig parameters are meant to be ANDed together (with &), but you have ORed them (with |). As a result, most of the settings are actually the opposite of what you wanted!Your code should look like this:-Code:config = USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH;baudconfig = BAUD_IDLE_RX_PIN_STATE_HIGH & BAUD_8_BIT_RATE & BAUD_AUTO_OFF & BAUD_WAKEUP_OFF;

author: a.soliman
author_rank: Newbie
timestamp: April 12, 2015, 07:59:26 am
content: Thanks for your reply, but from my understanding I can OR the configurations if I'm defining#defineUSE_OR_MASKSand I'm doing that, But I will try to AND them anyways

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: April 12, 2015, 09:52:11 am
content: Sorry if I jumped to a conclusion. If you define USE_OR_MASKS then using OR is correct, and the code you posted should work. Therefore the problem must lie elsewhere.You didn't actually saywhatis wrong, but I presume it is not receiving the 13 bytes? What does your program do with the data, and how do you know it's not working? Can you postallthe necessary code (including #pragma configs, system clock setup, etc.)?

author: a.soliman
author_rank: Newbie
timestamp: April 12, 2015, 12:57:23 pm
content: Thanks again for your time. Actually the code behaves strangely I receive only the first byte and the OVERRUN bit is set ! I'm configuring the USART of the two controllers with the same code so they should have the baudrate !Here is my Configuration:Code:/** File: Config.h* Author: Ahmed Soliman** Created on April 8, 2015, 5:40 PM*/#ifndef CONFIG_H#defineCONFIG_H#ifdef__cplusplusextern "C" {#endif#define _XTAL_FREQ 4000000// PIC18F4431 Configuration Bit Settings// 'C' source line config statements#include <xc.h>// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.// CONFIG1H#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)#pragma config IESO = OFF // Internal External Oscillator Switchover bit (Internal External Switchover mode disabled)// CONFIG2L#pragma config PWRTEN = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled)// BORV = No Setting// CONFIG2H#pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))#pragma config WDPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)#pragma config WINEN = OFF // Watchdog Timer Window Enable bit (WDT window disabled)// CONFIG3L#pragma config PWMPIN = OFF // PWM output pins Reset state control (PWM outputs disabled upon Reset (default))#pragma config LPOL = HIGH // Low-Side Transistors Polarity (PWM0, 2, 4 and 6 are active-high)#pragma config HPOL = HIGH // High-Side Transistors Polarity (PWM1, 3, 5 and 7 are active-high)#pragma config T1OSCMX = ON // Timer1 Oscillator MUX (Low-power Timer1 operation when microcontroller is in Sleep mode)// CONFIG3H#pragma config FLTAMX = RC1 // FLTA MUX bit (FLTA input is multiplexed with RC1)#pragma config SSPMX = RD1 // SSP I/O MUX bit (SCK/SCL clocks and SDA/SDI data are multiplexed with RD3 and RD2, respectively. SDO output is multiplexed with RD1.)#pragma config PWM4MX = RB5 // PWM4 MUX bit (PWM4 output is multiplexed with RB5)#pragma config EXCLKMX = RC3 // TMR0/T5CKI External clock MUX bit (TMR0/T5CKI external clock input is multiplexed with RC3)#pragma config MCLRE = ON // MCLR Pin Enable bit (Enabled)// CONFIG4L#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)#pragma config LVP = OFF // Low-Voltage ICSP Enable bit (Low-voltage ICSP disabled)// CONFIG5L#pragma config CP0 = OFF // Code Protection bit (Block 0 (000200-000FFFh) not code-protected)#pragma config CP1 = OFF // Code Protection bit (Block 1 (001000-001FFF) not code-protected)#pragma config CP2 = OFF // Code Protection bit (Block 2 (002000-002FFFh) not code-protected)#pragma config CP3 = OFF // Code Protection bit (Block 3 (003000-003FFFh) not code-protected)// CONFIG5H#pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code-protected)#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)// CONFIG6L#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000200-000FFFh) not write-protected)#pragma config WRT1 = OFF // Write Protection bit (Block 1 (001000-001FFF) not write-protected)#pragma config WRT2 = OFF // Write Protection bit (Block 2 (002000-002FFFh) not write-protected)#pragma config WRT3 = OFF // Write Protection bit (Block 3 (003000-003FFFh) not write-protected)// CONFIG6H#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write-protected)#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)// CONFIG7L#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000200-000FFFh) not protected from table reads executed in other blocks)#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (001000-001FFF) not protected from table reads executed in other blocks)#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (002000-002FFFh) not protected from table reads executed in other blocks)#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (003000-003FFFh) not protected from table reads executed in other blocks)// CONFIG7H#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from table reads executed in other blocks)#ifdef__cplusplus}#endif#endif/* CONFIG_H */I realized a mistake in the PIC datasheet ... It says that I should set TRISC0 to 1 !! I'm not sure why is that ! The USART Pins are RC6 and RC7

author: 22swg
author_rank: Frequent Contributor
timestamp: April 12, 2015, 03:47:56 pm
content: Overrun error would suggest wrong Baud rate , what is the source of the serial data ?

author: a.soliman
author_rank: Newbie
timestamp: April 12, 2015, 03:59:39 pm
content: PIC18F4550 with USART module configured exactly the same as the PIC18F4431

author: 22swg
author_rank: Frequent Contributor
timestamp: April 12, 2015, 04:04:28 pm
content: Who's UART ready first. ?

author: a.soliman
author_rank: Newbie
timestamp: April 12, 2015, 05:37:40 pm
content: YOU SOLVED IT ! I put a delay before initializing the UART at the transmitter pic and It's working now

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: April 12, 2015, 06:16:05 pm
content: Quote from: a.soliman on April 12, 2015, 12:57:23 pmOVERRUN occurs when data is coming in faster than it is being read out of the UART. It could be caused by mismatched baud rates/word size/polarity etc., excessive processing overhead, or noise on the rx line. You may get an initial overrun if the other end starts sending data before the receiver is ready.However, getsUSART() reads the UART in a tight loop until it has received the specified number of bytes, even if errors occur. So I am curious as to how you know that the OVERRUN error occurs on the first byte?QuoteThanks for that. The config looks correct, however this is just another fragment of your code and the rest is still a mystery to us. Did you put USE_OR_MASKSbeforeincluding uart.h, are any peripherals generating interrupts that might interfere with UART reception, or is some other part of your code causing the problem? You could clear up this mystery by postingallthe code required to reproduce the error (you can strip out anything you don't think is relevant, so long as the UART receive part still functions). It might also be useful to see the transmitting MCU's code.QuoteYou must have an older version of the datasheet. Download the latest version fromhttp://www.microchip.com/wwwproducts/Devices.aspx?product=PIC18F4431.

author: 22swg
author_rank: Frequent Contributor
timestamp: April 12, 2015, 06:52:51 pm
content: I would have expected the receiver UART needed to be 'ready' first, The first byte overrun error I have seen on a floating RX pin but on pic16F and assemblerconsider reset , all pins inputs ... so TX-------> RX not valid 1/0 signals , TX initialized first so pin at 1 , RX now initialized to a normal level.