Author Topic: Help with UART in PIC18F4431  (Read 5312 times)

0 Members and 1 Guest are viewing this topic.

Offline a.solimanTopic starter

  • Newbie
  • Posts: 7
Help with UART in PIC18F4431
« on: April 11, 2015, 08:40:07 pm »
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 char config,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 a char buff [13]


I really don't know where I'm wrong ! but I'm about to give it up ! so please help me
Thanks in advance
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Help with UART in PIC18F4431
« Reply #1 on: April 12, 2015, 07:29:47 am »
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: [Select]
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;
 

Offline a.solimanTopic starter

  • Newbie
  • Posts: 7
Re: Help with UART in PIC18F4431
« Reply #2 on: April 12, 2015, 07:59:26 am »
Thanks for your reply, but from my understanding I can OR the configurations if I'm defining #define USE_OR_MASKS and I'm doing that, But I will try to AND them anyways
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Help with UART in PIC18F4431
« Reply #3 on: April 12, 2015, 09:52:11 am »
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 say what is 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 post all the necessary code (including #pragma configs, system clock setup, etc.)? 
 

Offline a.solimanTopic starter

  • Newbie
  • Posts: 7
Re: Help with UART in PIC18F4431
« Reply #4 on: April 12, 2015, 12:57:23 pm »
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: [Select]
/*
 * File:   Config.h
 * Author: Ahmed Soliman
 *
 * Created on April 8, 2015, 5:40 PM
 */

#ifndef CONFIG_H
#define CONFIG_H

#ifdef __cplusplus
extern "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
 

Offline 22swg

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: gb
Re: Help with UART in PIC18F4431
« Reply #5 on: April 12, 2015, 03:47:56 pm »
Overrun error would suggest wrong Baud rate , what is the source of the serial data  ?
Check your tongue, your belly and your lust. Better to enjoy someone else’s madness.
 

Offline a.solimanTopic starter

  • Newbie
  • Posts: 7
Re: Help with UART in PIC18F4431
« Reply #6 on: April 12, 2015, 03:59:39 pm »
PIC18F4550 with USART module configured exactly the same as the PIC18F4431
 

Offline 22swg

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: gb
Re: Help with UART in PIC18F4431
« Reply #7 on: April 12, 2015, 04:04:28 pm »
Who's UART ready first. ?
Check your tongue, your belly and your lust. Better to enjoy someone else’s madness.
 

Offline a.solimanTopic starter

  • Newbie
  • Posts: 7
Re: Help with UART in PIC18F4431
« Reply #8 on: April 12, 2015, 05:37:40 pm »
YOU SOLVED IT ! I put a delay before initializing the UART at the transmitter pic and It's working now :)  :-+ :-+ :-+
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Help with UART in PIC18F4431
« Reply #9 on: April 12, 2015, 06:16:05 pm »
Actually the code behaves strangely I receive only the first byte and the OVERRUN bit is set !
OVERRUN 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?

Quote
I'm configuring the USART of the two controllers with the same code so they should have the baudrate !
Here is my Configuration:
Thanks 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_MASKS before including 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 posting all the 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.     

Quote
I realized a mistake in the PIC datasheet ... It says that I should set TRISC0 to 1 !!
You must have an older version of the datasheet. Download the latest version from http://www.microchip.com/wwwproducts/Devices.aspx?product=PIC18F4431.
« Last Edit: April 12, 2015, 09:31:17 pm by Bruce Abbott »
 

Offline 22swg

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: gb
Re: Help with UART in PIC18F4431
« Reply #10 on: April 12, 2015, 06:52:51 pm »
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 assembler  ;D consider 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. 
Check your tongue, your belly and your lust. Better to enjoy someone else’s madness.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf