EEE305 Microcontroller Systems

Download Report

Transcript EEE305 Microcontroller Systems

EEE502 Embedded Systems Design
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/
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
Use Hyperterminal on the PC if XP
female 9 pin D-type
Use REALTERM if windows 7
connector, it receives on PIN
Use TERMINAL.EXE if windows 8 (by Br@y++)
3, still (confusingly) labelled
Use minicom in Linux
TxD – take care!
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=0
TXEN = 1,SYNC=0
BRGH= ? 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 = 0
CREN = 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;
SYNC=0;
SPEN=1;
CREN=1;
SREN=0;
TXIE=0;
RCIE=0;
TX9=0;
RX9=0;
TXEN=0;
TXEN=1;
}
//data rate for sending
//asynchronous
//enable serial port pins
//enable reception
//no effect
//disable tx interrupts
//disable rx interrupts
//8-bit transmission
//8-bit reception
//reset transmitter
//enable the transmitter
//writes a character to the serial port
void putch(unsigned char c)
{
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=c;
// send data to USART
DelayUs(60);
}
//gets a character from the serial port without timeout
unsigned 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
Place holder, come back and look at lecture L7B
later …