EUSART configuration issues...

Re: EUSART configuration issues...

Postby HardyOne » Sun Dec 21, 2014 2:52 am

I want to configure the EUSART for full-duplex asynchronous mode and have the 18F4550 send the letter U to my Terminal.

Should I configure the pins like in traditional programs? The datasheet says that all I need to do is set TRISC, but should I mess with
PORTC and LATC?

Here are some additional features that need to be addressed according to the datasheet:

Automatic baud rate detection and calibration, automatic wake up on Sync Break reception and 12-bit Break
Character transmit.

-I wanted to set baud rate automatic detection but ric said it was a bad idea. I'm leaving the last two alone here
and assuming that they default to off.

Also from the datasheet:

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:
• SPEN bit (RCSTA<7>) must be set (= 1)
• TRISC<7> bit must be set (= 1)
• TRISC<6> bit must be set (= 1)
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)
These are detailed on the following pages in
Register 20-1, Register 20-2 and Register 20-3,
respectively.

-I'm pretty sure I addressed all of this already.


vloki, I thought that my original baud rate calculation was correct. Here is my calculation:

For a device with FOSC of 8 MHz, desired baud rate of 9600, Asynchronous mode, 8-bit BRG:
Desired Baud Rate = FOSC/(64 ([SPBRGH:SPBRG] + 1))
Solving for SPBRGH:SPBRG:
X = ((FOSC/Desired Baud Rate)/64) – 1
= ((8000000/9600)/64) – 1
= [12.0208] = 12
Calculated Baud Rate = 8000000/(64 (12 + 1))
= 9615.38
Error = (Calculated Baud Rate – Desired Baud Rate)/Desired Baud Rate
= (9615 – 9600)/9600 = 0.16%

How does OSCCON even affect the EUSART? I couldn't find mention of it anywhere in the EUSART section.

I employed the preprocessor directive #define _XTAL_FREQ 8000000 in my .h file because that's what the Utube video said to do that got me this far.
I set the OSCCON register with 0b00000111 because vloki told me to do something to it and it seemed like a good idea. I read page 34 of that datasheet and got so excited that I went and changed things in my IDE right after the first mention of OSCCON, which is bits 2:0. If I would have read further, maybe I would have changed more stuff.
Honestly, I don't have a clue how to work this thing, there isn't any starter documentation anywhere that explains how to configure this, and I figured that this project was within reach before I started. I've done multiple things in AVR ASM, ADC stuff, Read Encoders, Comparator stuff and didn't have too much trouble. Am I wrong to try this and should I take a step back or in a different direction? What should I study?

vloki, Baud1USART()?? Where did that come from? I'm having trouble with a while(1) loop. In general, I'm confused about calling functions across files, multiple source files, advanced library functions, etc. The book I'm studying, C Programming, a modern approach, doesn't even get into stuff like this until the end of the book and It's UNIX specific. I tried reading the XC8 compiler documentation a few days ago and it's brutal for a noob like myself.
HardyOne
 
Posts: 6
Joined: Fri Dec 19, 2014 5:06 am

Re: EUSART configuration issues...

Postby ric » Sun Dec 21, 2014 7:12 am

HardyOne wrote:Should I configure the pins like in traditional programs? The datasheet says that all I need to do is set TRISC, but should I mess with
PORTC and LATC?

No. Just do what the datasheet says, which you are.

Automatic baud rate detection and calibration, automatic wake up on Sync Break reception and 12-bit Break
Character transmit.

-I wanted to set baud rate automatic detection but ric said it was a bad idea. I'm leaving the last two alone here
and assuming that they default to off.

Correct. The "automatic" you see there does NOT mean it takes care of itself, you have to go through a specific sequence, getting the other end to send a known character at a specific time.
Just make life easy for yourself, decide upon a specific baud rate, and set everything to use that.

vloki, I thought that my original baud rate calculation was correct. Here is my calculation:

For a device with FOSC of 8 MHz, desired baud rate of 9600, Asynchronous mode, 8-bit BRG:
Desired Baud Rate = FOSC/(64 ([SPBRGH:SPBRG] + 1))
Solving for SPBRGH:SPBRG:
X = ((FOSC/Desired Baud Rate)/64) – 1
= ((8000000/9600)/64) – 1
= [12.0208] = 12
Calculated Baud Rate = 8000000/(64 (12 + 1))
= 9615.38
Error = (Calculated Baud Rate – Desired Baud Rate)/Desired Baud Rate
= (9615 – 9600)/9600 = 0.16%

How does OSCCON even affect the EUSART? I couldn't find mention of it anywhere in the EUSART section.

See "FOSC" in the calculation? That is the speed of your PIC's main oscillator, which is controlled by the OSCCON register!

I employed the preprocessor directive #define _XTAL_FREQ 8000000 in my .h file because that's what the Utube video said to do that got me this far.

That just tells the compiler how fast your PIC is running, so it can calculate software time delays correctly. It does NOT have any effect on the speed of your oscillator.

I set the OSCCON register with 0b00000111 because vloki told me to do something to it and it seemed like a good idea. I read page 34 of that datasheet and got so excited that I went and changed things in my IDE right after the first mention of OSCCON, which is bits 2:0. If I would have read further, maybe I would have changed more stuff.

"Write something" doesn't mean "write anything".
Your baud rate calculations assume your PIC's oscillator is running at 8MHz, so it would be a good idea to actually make it run at 8MHz.
All you need to do is look in the datasheet for your PIC, and read what it tells you about the OSCCON register. It shows exactly which bits control the speed, and shows you what to set them to to get 8MHz. I also told you the correct value a few posts ago.
I didn't know that off the top of my head, I did exactly what I just described, opened the PIC18F4550 datasheet, searched for references to OSCCON until I got to the detailed explanation of that register, and saw what bits were required to get 8MHz.


Honestly, I don't have a clue how to work this thing, there isn't any starter documentation anywhere that explains how to configure this, and I figured that this project was within reach before I started. I've done multiple things in AVR ASM, ADC stuff, Read Encoders, Comparator stuff and didn't have too much trouble. Am I wrong to try this and should I take a step back or in a different direction? What should I study?

The PIC18F4550 datasheet would be a good place. Read through the entire chapter describing the UART.
I also feel you are not paying attention to what vloki and I have been telling you.
Did you actually try the two changes I suggested in my previous post, or did vloki's following post confuse you again.
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 » Sun Dec 21, 2014 7:38 pm

Thank you. My code works now. I erroneously assumed that #define _XTAL_FREQ 8000000 was setting the internal oscillator speed.
HardyOne
 
Posts: 6
Joined: Fri Dec 19, 2014 5:06 am

Previous

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 7 guests

cron