EEE305 Microcontroller Systems
Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8
Teaching resources are at www.eej.ulst.ac.uk
My office 5B18, telephone 028 90 366364My email [email protected]
http://www.eej.ulst.ac.uk/~ian/modules/EEE305 1/
Normal serial transmission of the ASCII character set uses the RS232 PORT found on PCs
RS232 is an old interface; it uses +/-12 Volts and therefore needs line drivers and line receivers to convert from the normal 0 and +5 volt logic signals of the microcontroller
In the old days “bit” timing was imprecise, RS232 sends each character with its own timing synchronisation, the intercharacter delay is an asynchronous one.
All timing is done from the leading (falling) edge of the start bit and is valid for one character.
This means that the line has to idle in a ‘1’ state and the start bit has to be a ‘0’
Two characters could be transmitted back to back, we have to finish each with a ‘1’ stop bit
So to send 8 data bits requires sending at least 10 bits.
The logic is upside down, a ‘0’ is +12 Volts and a ‘1’ is -12 volts, the line driver/receivers invert
There are standard bit rates; sending one change of state for each data bit this is the BAUD rate
e.G 1200 Baud, 2400 Baud, 4800 baud, 9600 baud etc.,
There are variations; you can send 7 or 8 data bits, one or two stop bits, and an extra bit just before the stop bits known as the parity bit. The format 8N1 is typical
RS232 has standard connectors
• The transmit and receive wires are uni-directional, hence we have DTE and DCE when data TERMINAL equipment is wired to a data COMMUNICATION equipment such as a modem.
• The PC is given a DTE port – male pins, transmitting on PIN 3, labelled TxD
• A receiving device (using a 1:1 cable) should have a female 9 pin D-type connector, it receives on PIN 3, still (confusingly) labelled TxD – take care!
Use Hyperterminal on the PC if XPUse REALTERM if windows 7Use TERMINAL.EXE if windows 8 (by Br@y++)Use minicom in Linux
The serial port on the PIC
• Is a Superset of an RS232 port known as the Addressable Universal Synchronous Asynchronous Receiver Transmitter (USART)
• Some texts just call it a Serial Communications Interface or SCI ( there is also a SPI and IIC interface – these are different)
• The USART has three important registers TXSTA, RCSTA, SPBRG as well as data registers and interrupt registers
So a good guess to configure this might be;
CSRC = x, TX9=0TXEN = 1,SYNC=0BRGH= ? Probably ‘1’
We read TRMT, put a ‘1’ in it to make it empty
TX9D = X or 0
i.e 0b00100110
Guess; SPEN = 1, RX9 = 0CREN = 1, ADDEN=0
FERR and OERR are read to see if errors have occurred
RX9D is read if using 9 bit mode (stick to 8 bits!)
Baud rate Generator
• Depends on PIC crystal, and TXSTA<2> (BRGH)
• But there are lookup tables on page 98 of the datasheet for common baud rates and clock frequencies, for both BRGH= 0 or 1
Use the lookup tables in Page 98 of datasheet
Understanding the USART
void serial_setup(void){
/* * Comms setup: */
#define BAUD 19200#define DIVIDER 25 // lookup from table – 4MHz and we want 9600#define HIGH_SPEED 1
SPBRG=DIVIDER;BRGH=HIGH_SPEED; //data rate for sendingSYNC=0; //asynchronousSPEN=1; //enable serial port pinsCREN=1; //enable receptionSREN=0; //no effectTXIE=0; //disable tx interruptsRCIE=0; //disable rx interruptsTX9=0; //8-bit transmissionRX9=0; //8-bit receptionTXEN=0; //reset transmitterTXEN=1; //enable the transmitter
}
//writes a character to the serial portvoid putch(unsigned char c){
while(!TXIF) //set when register is empty{
clear_usart_errors_inline;CLRWDT();
}TXREG=c; // send data to USARTDelayUs(60);
}
//gets a character from the serial port without timeoutunsigned char getch(void){
while(!RCIF){
CLRWDT();clear_usart_errors_inline;
}return RCREG;
}
unsigned char dummy;
#define clear_usart_errors_inline \if (OERR) \{ \ TXEN=0; \ TXEN=1; \ CREN=0; \ CREN=1; \} \if (FERR) \{ \ dummy=RCREG; \ TXEN=0; \ TXEN=1; \}
Most examples omit the reference to errors, it is only needed if running comms for more than 24 hours and an interruption occurs, such as a source of RS232 powering off half way through a transfer.
The SPI and IIC peripherals
Top Related