EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the...

14
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 366364 My email [email protected] http://www.eej.ulst.ac.uk/~ian/modules/ EEE305 1/

Transcript of EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the...

Page 1: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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/

Page 2: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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

Page 3: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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

Page 4: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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

Page 5: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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

Page 6: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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!)

Page 7: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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

Page 8: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

Use the lookup tables in Page 98 of datasheet

Page 9: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .
Page 10: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

Understanding the USART

Page 11: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .
Page 12: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

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

}

Page 13: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

//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.

Page 14: EEE305 Microcontroller Systems Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at .

The SPI and IIC peripherals