2. Asynchronous serial communication
Bibliography
microcontroller PIC 4550 documentation (PDF file)
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 2
PPU IUT Cachan
microcontroller PIC 4550 documentation (PDF file)
MPLAB_C18 librairies documentation (PDF files)
MPLAB_C18 header file xxx.h
MPLAB_C18 c file sources (mccsrcxxx)
3. Introduction
UART TX (PORTC RC7)
RX (PORTC RC6)
Transmission line (0/5V)
Reception line (0/5V)
TXREG
RXREG
TRMT bit
TRMT bit = 1 if transmission buffer is empty
RCIF bit
microcontroller
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 3
SPBRG = 8 or 16 bits Baud Rate
8 bits TX configration
8 bits Rx configuration
TRMT bit = 1 if transmission buffer is empty
TRMT bit = 0 if transmission buffer is full
RCIF bit = 1 if reception buffer is full
RCIF bit = 0 if reception buffer is empty
In the documentation : EUSART
4. Connexion with a PC through a RS232 serial link
USART TX
TXREG
RXREG
microcontroller
max 232
Port COM
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 4
RX
0/5 Volt +12/-12 Volt
In this configuration, we may use the PC as a supervisor
5. Transmission of one byte
1
2
3
TCLK
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 5
byte of 8 or 9 bits (to add a possible parity bit)
2
3
baud rate = 1/TCLK = FOSC / 16 / (SPBRG + 1) or FOSC/ 64 / (SPBRG + 1)
for us FOSC = 48 MHz
each transmitted byte is framed by a start bit and a stop bit
6. Possible transmission flow chart
configure USART
write the byte in TXREG
transmission buffer empty ?
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 6
write the byte in TXREG
another transmission ?
close USART
7. Transmission in C language using the microchip library
char text_emission[20]= " HELLO WORLDn " , u ;
unsigned int spbrg;
spbrg = 77 ; //48000000/(64*9600) -1 9600 bauds
OpenUSART
(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,spbrg);
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 7
header file : usart.h
for(i=0;text_emission[i] != 0;i++) // character by character
{
}
CloseUSART();
while(BusyUSART()==1); // wait for buffer empty
u = text_emission[i];
putcUSART(u); // transmission
8. Let’s go back to the OpenUSART function
OpenUSART
(
USART_TX_INT_OFF
&
USART_RX_INT_OFF
&
USART_ASYNCH_MODE
// interrupts transmission and reception OFF
// asynchronous mode (possibly synchronous)
spbrg = 77 ; //48000000/(64*9600) -1 9600 bauds
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 8
USART_ASYNCH_MODE
&
USART_EIGHT_BIT
&
USART_CONT_RX
&
USART_BRGH_LOW,
spbrg
);
// asynchronous mode (possibly synchronous)
// 8 bits word (possibly 9 bits)
// continous reception (possibly single)
// low speed (:64) (possibly high :16)
// baud rate = FOSC / 64 / (spbrg + 1)
Prototype : OpenUSART(char config , unsigned int spbrg);
9. Reception of one byte (without interrupt)
1 2
3
TCLK
bit 4 bit 5 bit 6 bit 6bit 2 bit 3bit 0 bit 1 stopstart
reception line
RCF bit
(buffer full)
4 RCIF bit is cleared when the RECREG is read
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 9
for us FOSC = 48 MHz
baud rate = 1/TCLK = FOSC / 16 / (SPBRG + 1) or FOSC/ 64 / (SPBRG + 1)
each byte (8 or 9 bits) is framed by a start bit and a stop bit
to error bits may be read after reception : FERR (Format) and OERR (Overrun)
10. Possible reception flow chart
configure USART
reception buffer empty ?
read the byte in RXREG
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 10
read the byte in RXREG
another reception ?
close USART
here, it’s possible to test
FERR and OERR
11. Reception in C language (without interrupt) using the microchip library
header file : usart.hchar reception ;
unsigned int spbrg;
spbrg = 77 ; // 48000000/(64*9600) -1 9600 bauds
OpenUSART
(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,spbrg);
for(i=0;i<10;i++) // expecting 10 characters
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 11
for(i=0;i<10;i++) // expecting 10 characters
{
}
CloseUSART();
while(!DataRdyUSART()); //waiting for buffer full
reception = ReadUSART();
//reading the character in the reception buffer
// here we could add the test of FERR and OERR
lcd_putc(reception); //this character is sent to the LCD display
12. Workshops
1st exercise : Send a message from the microcontroller to the PC
3rd exercise : State machine : supervise the robot with the hyperterminal :
d : the right wheel turns forward D : the right wheel turns backward
l : the right wheel turns forward L : the right wheel turns backward
spacebar : the wheel stops p : displays the value of th potentiometer
1 : displays the value of the right exterior optical sensor
2 : displays the value of the right interior optical sensor
2nd exercice : Send a message from the PC to the LCD display
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 12
Connect the asynchronous port of the microcontroller to the PORT COM
of the PC
Run HyperTerminal, configure it at 9600 bauds, 8 bits, no parity, disconnect
(telephone icon)
Write, build, program the microcontroller and run your program
Open the Hyperterminal connection (telephone icon)
2 : displays the value of the right interior optical sensor
3 : displays the value of the left exterior optical sensor
4 : displays the value of the left interior optical sensor