Serial Communications in PIC18F4550

Thread Starter

corsair

Joined Mar 6, 2010
51
Hi guys,

Me again :D and I'm in need of your PIC expertise. I have a problem that I'm working on for my class which states:

PIC Problem 7) serial communication
Install the serial loopback jumper on J_SER, initialize the PIC EUSART for 38400N81 communications, then duplicate the function of problem 6 by using serial TX to report switch events, and serial RX to convert them to display values.
I've never dealt with serial communications before, so I am honestly having trouble understanding what it's even asking. I had to actually look up what EUSART stands for, what the differences between parallel and serial are, what the PIC EUSART does (still fuzzy on this), what baud is, etc.

So first of all, what does it mean to "initialize the PIC EUSART for 38400N81 communications?" I imagine this might be a type of communications protocol, but I'm not sure. Also, with serial communications, can I control my PIC board from my computer?

Edit: So here's an explanation of problem 6 which might help to understand what he is asking. I have 8 switches (just buttons that I push), and a 4-digit, 7segment display. It is layed out such that there are 2 buttons under each digit. When I push switch1, it will increment digit1 on the display. If I push switch2, it will decrement digit1 on the display, switch 3 will increment digit2, etc.
 
Last edited:

t06afre

Joined May 11, 2009
5,934
Hi corsair
38400N81 should be equal to, 38400 bit per second, Non parity, 8 bit pr frame, and 1 is the same as 1 stop bit.
In RS232 bitrate or baudrate will have the same meaning. The datasheet will give you info on how to setup your UART.
Generally speaking, if your PC is equipped with a RS232 port. And your board also have RS232 port. The two devices can communicate, and hence the PC may control your board. To do RS232 communication with your PIC board. It must be equipped with TTL to RS232 level converter. A max232 circuit is often used for this purpose
 
Last edited:

Tahmid

Joined Jul 2, 2008
343
Hi,
In mikroBASIC (or mikroC), you'd simply do:
Rich (BB code):
UART1_Init(38400)
Otherwise, you need to set the SPBRG, TXSTA and RXSTA registers accordingly. If you look at the 16F877A datasheet, it's all explained in chapter 10 - page 113.

Hope this helps.
Tahmid.
 

Thread Starter

corsair

Joined Mar 6, 2010
51
I got a couple of questions concerning setting up the serial communications. So, as mentioned above, I need this set up for:
38400 bit per second, Non parity, 8 bit pr frame, and 1 is the same as 1 stop bit
So here are my questions:
1. Initialize the SPBRGH:SPBRG registers for the
appropriate baud rate. Set or clear the BRGH
and BRG16 bits, as required, to achieve the
desired baud rate.
So, if I want 38400 baud rate, this is equivalent to 9600 hex. I guess I just load 0x96 into SPBRGH and clear SPBRG, correct? Also BRG16 should be set. And what does the BRGH register do? Should this be set?

6. Enable the transmission by setting bit TXEN
which will also set bit TXIF.
8. Load data to the TXREG register (starts
transmission).
Do I load data to the TXREG first then enable TXEN? Also, I was thinking about polling the TXIF. Will it automatically clear when finished? Do I have to worry about the stop bit or does it do that automatically?

The way I'm going to practice this is that I will transmit a signal and receive it. Also, here are my settings for RCSTA. Please let me know if I should correct anything.

bsf RCSTA,SPEN ; Enables Serial Port
bcf RCSTA,RX9 ; Using 8-bit reception
bsf RCSTA,CREN ; Enables Continuous Receive <-- should I turn this off when im done with it? and should this be on while i am transmitting? i figure since this is full-duplex it shouldnt matter

When should I be checking the FERR/OERR bits?

Umm.. yea I think that's it. It's so much to set up :confused:
 

nanovate

Joined May 7, 2007
666
So, if I want 38400 baud rate, this is equivalent to 9600 hex. I guess I just load 0x96 into SPBRGH and clear SPBRG, correct?
Incorrect. Look at Table 20-1 and Example 20-1 and Table 20-3 to see how to calculate the correct values (in the datasheet)

And what does the BRGH register do? Should this be set?
Look at the TXSTA: TRANSMIT STATUS AND CONTROL REGISTER description (Register 20-1)
 
Top