EUSART configuration issues...

EUSART configuration issues...

Postby HardyOne » Fri Dec 19, 2014 5:14 am

I'm attempting to learn the C programming language and micro-controller applications concurrently. Recently, I purchased an Olimex PIC-USB-STK board with the objective of learning the interfacing related to all of the functions on the board.

https://www.olimex.com/Products/PIC/Dev ... SB-STK.pdf

So far, I've written and successfully run a "Blinky.c" program. My next goal is to configure the EUSART module for communication with Tera Term so that I can create more advanced programs. Unfortunately, I'm having trouble configuring the EUSART module and writing a program that is useful as well as flexible enough to get me through the rest of the lessons that I have envisioned.

In the book I'm reading about the C programming language, there are numerous references to printf, scanf, and all sorts of other UNIX related library functions that don't necessarily equate to functions in the XC8 compiler, or maybe I'm wrong? I'm at a point now where learning more UNIX related C isn't going to help me with micro-controllers or their related compilers.

From what I've gathered in this forum and from the internetl, writing a program that echoes back the same character via RS232 communication is probably a good step in the right direction for me. I've attempted to write a program that echoes back a character and this is about as far as I've gotten. I haven't been able to get my program to compile correctly and churning through Microchip application notes, datasheets, web tutorials and the like just isn't cutting it. I've read the data sheet and feel that I understand the architecture, but can't figure out how to get around sending 8 bits at a time instead of printf("Hello World\n") or scanf.

**A gentleman named RIC on the MICROCHIP forums sent me here and suggested that I send "U" characters to the Tera Term terminal instead of trying to echo a character back and forth. The following code is my attempt at doing so.

Here is my code so far:

Code: Select all
// PIC18F4550 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.

// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSC_HS // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)


And:

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "USART.h"

void setup (void)
{

/*The pins of the Enhanced USART are multiplexed
*with PORTC. In order to configure RC6/TX/CK and
*RC7/RX/DT/SDO as an EUSART:
 */

    RCSTAbits.SPEN = 1; //SPEN - Serial port enable bit
    TRISC = 0b11000000;

/*The operation of the Enhanced USART module is
*controlled through three registers:
*Transmit Status and Control (TXSTA)
*Receive Status and Control (RCSTA)
*Baud Rate Control (BAUDCON)
 */

    TXSTAbits.TXEN = 1; //Transmit Enable Bit
    TXSTAbits.SYNC = 0; //EUSART Mode 1 = Sync 0 = Async

   // See above for SPEN - Serial port enable bit
    RCSTAbits.CREN = 1; //Continuous Receive Enable Bit 1 = enables receiver

    BAUDCONbits.ABDEN = 1; //Enable baud rate measurement on the next character
}



void main()
{

void setup();

unsigned char data1;

    while(PIR1bits.TXIF == 0);
   
        data1 = 0x55; //U character

        TXREG = data1;
     
}
HardyOne
 
Posts: 6
Joined: Fri Dec 19, 2014 5:06 am

Re: EUSART configuration issues...

Postby ric » Fri Dec 19, 2014 6:28 am

That would be me ;)
It really would be better to post the entire program in a single block.
Breaking it up often means the faulty bit is left out.

Delete this:
Code: Select all
    BAUDCONbits.ABDEN = 1; //Enable baud rate measurement on the next character


and change this:
Code: Select all
void setup();

unsigned char data1;

to
Code: Select all
unsigned char data1;

setup();


and you are missing any code to setup the BAUDCON and SPBRG registers.
You can probably use the default values for BAUDCON, but SPBRG must be set to the required baud rate.
What is your cpu clock speed?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: EUSART configuration issues...

Postby HardyOne » Fri Dec 19, 2014 6:53 am

I changed my code to reflect your instructions. I don't have a clue how the SPBRG and SPBRGH registers operate. The code I borrowed had some fancy equation that I never deciphered: Desired Baud Rate = FOSC/(64 ([SPBRGH:SPBRG] + 1)) Does it want to see a hexidecimal, decimal, I don't know. The datasheet doesn't have an example. My oscillator speed is whatever this thing needs to talk to my PC 8,000,000 should give me a value of SPBRG = 12? or SPBRG = 0x0C?
HardyOne
 
Posts: 6
Joined: Fri Dec 19, 2014 5:06 am

Re: EUSART configuration issues...

Postby vloki » Fri Dec 19, 2014 12:34 pm

You can find some basic USART code here
-> viewtopic.php?p=448#p448
(3 posts ...)

<edit> May be it would be good if you tell us a bit of your hardware.
How the PIC is connected to the PC in detail ?
Do you have a scope ?
Do you have a debugger ?
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: EUSART configuration issues...

Postby HardyOne » Fri Dec 19, 2014 3:59 pm

I've got PICKit3, an analog scope, I'm using Windows 7 x64, Mplab x IDE2.10 with xc8 v1.32 and I'm using an serial to usb converter. The board I'm using is an Olimex PIC-USB-STK board that can be found here https://www.olimex.com/Products/PIC/Dev ... SB-STK.pdf

This last thing I did was inert SPBRG:SPBRGH = 15; into my code. My PIC board is sending data but it looks like this in the Tera Term terminal:

cæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîgæîâîcæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîcæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîcæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîgæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîgæîâîgæîæîcæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîcæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîcæîæîcæîæîcæîæîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîcæîâîgæîâîgæîâîgæîâîgæîæîcæîæ
HardyOne
 
Posts: 6
Joined: Fri Dec 19, 2014 5:06 am

Re: EUSART configuration issues...

Postby vloki » Fri Dec 19, 2014 6:10 pm

Why do you think your PIC runs at 8MHz ?
(any writes to OSCCON register)

What baudrate did you choose in TeraTerm ?
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: EUSART configuration issues...

Postby ric » Fri Dec 19, 2014 10:13 pm

HardyOne wrote:This last thing I did was inert SPBRG:SPBRGH = 15; into my code.

SPBRG and SPBRGH are two separate 8 bit registers used to hold a 16 bit value.
The datasheet uses the notation "SPBRGH:SPBRG" to represent the 16 bit value you get when the two registers are used together.
You can't just right to them together using that notation, you have to write the lower 8 bits to SPBRG, and the upper 8 bits to SPBRGH (although you can just ignore SPBRGH if the BRG16 flag is not set.)

You need to pay attention to things like "what speed is your PIC running at", "what divide ratio are you using in the PIC's USART to set the baud rate" (i.e. what is in SPBRG, SPBRGH, the BRG16 flag and, and the BRG flag.), "what baud rate the PC is set to".
All those things don't happen by magic, You have to make sure they all match for communication to work.

Did you look at the example code that vloki linked to?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: EUSART configuration issues...

Postby HardyOne » Sat Dec 20, 2014 6:34 am

ric wrote:SPBRG and SPBRGH are two separate 8 bit registers used to hold a 16 bit value.
The datasheet uses the notation "SPBRGH:SPBRG" to represent the 16 bit value you get when the two registers are used together.


OK, I can fix that up.

ric wrote:You can't just right to them together using that notation, you have to write the lower 8 bits to SPBRG, and the upper 8 bits to SPBRGH (although you can just ignore SPBRGH if the BRG16 flag is not set.)

You need to pay attention to things like "what speed is your PIC running at", "what divide ratio are you using in the PIC's USART to set the baud rate" (i.e. what is in SPBRG, SPBRGH, the BRG16 flag and, and the BRG flag.), "what baud rate the PC is set to".
All those things don't happen by magic, You have to make sure they all match for communication to work.


I found the baud rate chart in the datasheet and it all makes a lot more sense now. I chose a value of SPBRG = 0x0C; this corresponds to 8MHZ as calculated using the forumlas in the datasheet. I omitted the BRGH and BRG16 because I don't think they are relevant. I checked the settings of Tera Term and it is 9600 Baud rate, 8bit data, no parity, 1 bit stop no flow control. I haven't done anything to the OSSCON register. Should I configure the OSCON register? I set it to 0SCCON = 0b00000111;

vloki wrote:Did you look at the example code that vloki linked to?


Yes, I did. His example looks pretty similar to mine now. Unfortunately, mine doesn't work though.

Here is my latest code version:

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "USART.h"

void setup (void)
{

/*The pins of the Enhanced USART are multiplexed
*with PORTC. In order to configure RC6/TX/CK and
*RC7/RX/DT/SDO as an EUSART:
 */

    RCSTAbits.SPEN = 1; //SPEN - Serial port enable bit
    TRISC = 0b11000000;

/*The operation of the Enhanced USART module is
*controlled through three registers:
*Transmit Status and Control (TXSTA)
*Receive Status and Control (RCSTA)
*Baud Rate Control (BAUDCON)
 */

    TXSTAbits.TXEN = 1; //Transmit Enable Bit
    TXSTAbits.SYNC = 0; //EUSART Mode 1 = Sync 0 = Async

   // See above for SPEN - Serial port enable bit
    RCSTAbits.CREN = 1; //Continuous Receive Enable Bit 1 = enables receiver

  //BAUDCONbits.ABDEN = 1; //Enable baud rate measurement on the next character

    SPBRG = 0x0C;
    SPBRGH = 0x00;

    OSCCON = 0b00000111;
}



void main()
{
unsigned char data1;

    setup();

    while(PIR1bits.TXIF == 0);
   
        data1 = 0x55; //U character

        TXREG = data1;
     
}
HardyOne
 
Posts: 6
Joined: Fri Dec 19, 2014 5:06 am

Re: EUSART configuration issues...

Postby ric » Sat Dec 20, 2014 8:07 am

HardyOne wrote:...
Should I configure the OSCON register? I set it to 0SCCON = 0b00000111;

Why that value?
That sets the internal timer to 31 kHz, and tells the PIC to use the internal timer.
If you want 8MHz, then you need to write 0b01110010

This code will run once and then exit
Code: Select all
void main()
{
    unsigned char data1;

    setup();

    while(PIR1bits.TXIF == 0);
   
        data1 = 0x55; //U character

        TXREG = data1;
     
}

Change it to
Code: Select all
void main(void)
{
unsigned char data1;

    setup();
    while (1)    // loop forever
    {
        while(PIR1bits.TXIF == 0)
            ;    //wait until the transmit register is ready
   
        data1 = 0x55; //U character
        TXREG = data1;
    }
}

You don't need data1 at all for now, but I left it in as I guess you will modify this to send other stuff later.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: EUSART configuration issues...

Postby vloki » Sat Dec 20, 2014 11:36 am

HardyOne wrote:
ric wrote:Did you look at the example code that vloki linked to?

Yes, I did. His example looks pretty similar to mine now. Unfortunately, mine doesn't work though.

I have the feeling you have no clue about Fosc and baudrate ...
I said there are three posts. Have a look at the 3rd one. May be it helps.
Code: Select all
//--------------------------------------------------------------------- __init()
    OSCCONbits.IRCF = IRCF_4MHZ;

#define FOSC        4000000 // in a global header
#define BAUDRATE    19200   // ""
//#define SPBRG_VAL (((FOSC/BAUDRATE)+32)/64)-1   // BRG16=0, BGH=0 !
//#define SPBRG_VAL (((FOSC/BAUDRATE)+8)/16)-1    // BRG16=0, BGH=1 !
#define SPBRG_VAL (((FOSC/BAUDRATE)+2)/4)-1     // BRG16=1, BGH=1 !

    baud1USART(BAUD_IDLE_RX_PIN_STATE_HIGH &
                 BAUD_IDLE_TX_PIN_STATE_HIGH &
                 BAUD_16_BIT_RATE &
                 BAUD_WAKEUP_OFF &
                 BAUD_AUTO_OFF);

    Open1USART( USART_TX_INT_OFF & USART_RX_INT_OFF &
                USART_ASYNCH_MODE & USART_EIGHT_BIT &
                USART_CONT_RX &
                USART_BRGH_HIGH & USART_ADDEN_OFF,
                SPBRG_VAL );
baud-x-USART() and Open-x-USART() are functions provided by the compiler libs,
where -x- represents the number of the usart module if there are more than one.
For PICs with only one USART module the names are baudUSART() and OpenUSART()
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Next

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 10 guests

cron