Top Banner
EEE527 Embedded Systems Lecture 9:Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrum Room 5B18, Tel: 90 366364 voice mail on 6 th ring Email: [email protected] Web site: http://www.eej.ulst.ac.uk "Adapted from the text “Programming 32-bit Microcontrollers in C –Exploring the PIC32 , © 2008.” Lucio di Jasio www.eej.ulster.ac.uk/~ian/modules/EEE527/files
31

EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

Jan 01, 2016

Download

Documents

Everett Eaton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

EEE527Embedded Systems

Lecture 9:Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added

new at end)

Ian McCrum Room 5B18, Tel: 90 366364 voice mail on 6th ringEmail: [email protected] Web site: http://www.eej.ulst.ac.uk

"Adapted from the text “Programming 32-bit Microcontrollers in C –Exploring the PIC32 , © 2008.” Lucio di Jasio

www.eej.ulster.ac.uk/~ian/modules/EEE527/files

Page 2: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Simplified UART block diagramSimplified UART block diagram

figure 19-1 (DS61143)

Page 3: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Baud Rate settingBaud Rate setting

In our case this translates to the following expression:U2BREG = (25,000,000 / 4 / 115,200) -1 = 53.25 To decide how to best round out the result, use the reverse formula to calculate the actual baud-rate and determine the percentage error:Error = ((Fpb / 4 / (U2BREG + 1)) – baud rate) / baud rate %

With a value of 53 -> 115,740 Baud with an error of just 0.47%, With a value of 54 -> 113,636 baud, 1.82% error, Both are within the acceptable tolerance range for a standard RS232 port (+/- 2%) .We can therefore define the constant BRATE as:

#define BRATE 53 // 115,200 Bd (BREGH=1)

Page 4: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Use Excel to Calculate valuesUse Excel to Calculate values

SYS_CLK PB divisor PBCLK Baudrate UxBRG Actual Baudrate Error Actual Baudrate Error =PBCLK/(4*baud) -1' Round Down for Rounded down Round up for Rounded up =PBCLK/(4*(UxBRG+1))'

Number Number Formula Number Formula Formula Formula Formula Formula Formula Formula e.g e.g e.g e.g e.g e.g e.g e.g C8=A8/B8 E8=C8/(4*D8) - 1 =roundown(E8,0) G8=C8/((4*(F8+1)) (G8-D8)/D8 =roundup(E8,0) J8=C8/(4*(I8+1)) (G8-D8)/D8

50000000 2 25000000 115200 53.25347222 53 115740.7407 0.47% 54 113636.3636 -1.82%50000000 2 25000000 38400 161.7604167 161 38580.24691 0.47% 162 38343.55828 -0.61%50000000 2 25000000 19200 324.5208333 324 19230.76923 0.16% 325 19171.77914 -0.31%50000000 2 25000000 9600 650.0416667 650 9600.614439 0.01% 651 9585.889571 -0.15%50000000 2 25000000 1200 5207.333333 5207 1200.076805 0.01% 5208 1199.84642 -0.02%

See the File

EEE527_PIC32MX_BAUD_RATE_GENERATOR.xlsx

Page 5: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

UxMODE registerUxMODE register

register 18-1 (DS61168E)

Page 6: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Important BITs of UxMODEImportant BITs of UxMODE

ON (bit 15) 1 is enabled, UARTx pins controlled by UEN<1:0> and UTXEN

SIDL (bit 13) only relevant in idle mode

IREN (bit 12) 0 = IrDA is disabled

RTSMD (bit 11) 1 = /UxRTS is in Simplex mode

UEN (bits 9-8) 00 = Use UxTX/RX, /UxCTS ,/UxRTS/BCLK just used by PORTx

WAKE (bit 7) only relevant in sleep mode

LPBACK (bit 6) 0 = loopback disabled

ABAUD (bit 5 ) 0 = Auto-Baud rate detection is disabled or completed.

RXINV (bit 4) 0 = UxRX idle state is a ‘1’ ( sent in RS232 as -12V!)

BRGH (bit 3) 1 = High-speed mode – 4x baud clock enabled. {0= x16}

PDSEL (bits2-1) 00 = 8 bit data, no parity {01=8E, 10=8O (odd), 11=9N }

STSEL (bit 0) 0 = 1 stop bit {1 = 2 stop bits}

- - - - - - - - - - - - - - - - 1ux0 1u00 x000 1000 = 0x8888

Page 7: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

UxSTA registerUxSTA register

register 18-2 (DS61168E)

Page 8: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

ADM_EN (bit 24) 0 = no automatic address detect

ADDR<7:0> (bits 23-16) only matter when bit above is set

UTXISEL<1:0> (bits15-14) 01 = raise interrupt when all chars transmitted

UTXINV (bit 13) 0 = UxTX idle state is ‘1’ (if not in IrDA mode)

URXEN (bit 12) 1 = UARTx receiver is enabled

UTXBRK (bit 11) 0 = send no break {1=send start, 12 ‘0’ and stop}

UTXEN (bit 10) 1 = UARTx transmitter is enabled

UTXBF (bit 9) 1 = Transmit buffer is full {0 = room for at least 1 char}

TRMT (bit 8) 0 = Transmit Shift Register is not empty, tx in progress

URXISEL<1:0> (bit 7-6) 00 = Int flag is asserted while rx buffer not empty

ADDEN (bit 5) 0 = Address detect mode disabled

RIDLE (bit 4) 0 = Data is being received {1=receiver is idle}

PERR (bit 3) 1 = Parity error detected for current character

FERR (bit 2) 1 = Framing error detected for current character

OERR (bit 1) 1 = Receiver buffer overrun. Can only be cleared in s/w

URXDA (bit 0) 1 = Receive buffer data available, at least one char.

Important BITs of UxSTAImportant BITs of UxSTA

e.g. U2STA = 0x1400;

Page 9: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

In the DP32 it is simpler to use UART2 – it shares pins In the DP32 it is simpler to use UART2 – it shares pins with the I2C pins going to JP4 & 5with the I2C pins going to JP4 & 5

Use UART2 and PPS -> U2TX/RPB9U2RX/RPB8 NBRemove jumpers JP4 & 5And do not insert IC2C, the 8 pin chip

Page 10: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

You need to program which pins go where – look up Peripheral Pin Select (PPS) in the datasheet. Also the PPS LOCK and UNLOCK sequences.

Page 11: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Next 4 slides give working code; create a project called UART_1 and either

wire RB8 and RB9 to a USB TTL 3.3V Usart,

or a PICKIT2

or to another DP32 - but wire

RB8_board1 to RB9_board2 And

RB9_board2 to RB8_board2

(on the PC run PUTTY or PICKIT2 v2.6.1 (NOT PICKIT3 s/w!)

Page 12: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Code to demo serial i/oCode to demo serial i/o

Modified from http://umassamherstm5.org/tech-tutorials/pic32-tutorials/pic32mx220-tutorials/uart-to-serial-terminal

Page 13: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

NB remove JP4 and JP5 (rotate 180 degrees)

Modified from http://umassamherstm5.org/tech-tutorials/pic32-tutorials/pic32mx220-tutorials/uart-to-serial-terminal

Page 14: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Modified from http://umassamherstm5.org/tech-tutorials/pic32-tutorials/pic32mx220-tutorials/uart-to-serial-terminal

Page 15: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Modified from http://umassamherstm5.org/tech-tutorials/pic32-tutorials/pic32mx220-tutorials/uart-to-serial-terminal

Page 16: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

ExercisesExercises Send data every second to another DP32 and

display it there. (The sending board can be called DP32_1 and the receiver DP32_2)

Send data only when a pushbutton on DP32_1 is pressed.

Send data only when the receiving end says it is ready. (hint wire another wire from a spare i/o line from Dp32_2 to DP32_1.

Use LEDs to show various things

Page 17: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Timer delaysTimer delays These can use an interrupt – see notes for a 1 second ISR using a

flag variable that main polls. A simple delay is a “blocking” wait. E.g

#define DELAY 39062 // assuming 40Mhz clock

// In main near start

T1CON = 0x8030; // prescale 256:1, 40Mhz=25nSec and 25/256=> 6.4usec

Then for a delay use in your code the following two lines (or put in a function)

TMR1=0;PR1=0xFFFF; // Note the 39062 gives a slight inaccuracy.

while(TMR1 < DELAY){;}// wait here for 39062 * 6.4uSecs

// you arrive here after a quarter second…(reasonably accurate…)

Page 18: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

You can also use the plib library (this code needs modified for the DP32! Do not use as is

 PPSUnLock;                        // Allow PIN Mapping       PPSOutput(4, RPB10, U2TX);   // MAP Tx to PB10      PPSInput (2, U2RX, RPB11);   // MAP Rx to PB11 PPSLock;                        // Prevent Accidental Mapping

   // Configure UART2

   UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); 

   UARTSetLineControl(UART2 ,UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); 

   UARTSetDataRate(UART2, GetPeripheralClock(), BaudRate);    UARTEnable(UART2 ,UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));   

This code is explained athttp://www.eevblog.com/forum/microcontrollers/pic32mx-quickstart/15/

Page 19: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Print a message Function using plibPrint a message Function using plib

void Serial_print(char *buffer){    while(*buffer != (char)0)    {      while(!UARTTransmitterIsReady(UART2));       UARTSendDataByte(UART2, *buffer++);    }    while(!UARTTransmissionHasCompleted(UART2));    UARTSendDataByte(UART2, '\r');    UARTSendDataByte(UART2, '\n'); } 

This code is explained athttp://www.eevblog.com/forum/microcontrollers/pic32mx-quickstart/15/

Page 20: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Sending and Receiving Data using Sending and Receiving Data using handshake lines – CTS and RTS handshake lines – CTS and RTS

(manually)(manually)int putU2( int c){ while ( CTS); // wait for !CTS, clear to send while ( U2STAbits.UTXBF); // wait while Tx buffer full U2TXREG = c; return c;} // putU2

char getU2( void){ RTS = 0; // assert Request To Send !RTS while ( !U2STAbits.URXDA); // wait for a new char to arrive RTS = 1; return U2RXREG; // read char from receive buffer}// getU2

Could be worth adding the lines, just before the return c;

while( !U2STAbits.TRMT);

Page 21: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Serial terminal programs on the PCSerial terminal programs on the PC Hyperterminal – pre windows 7 in all versions RealTerm - most excellent, doesn’t work W8 Putty – usually used for network login MPIDE has a good serial monitor

Use USB to serial convertors if the PC has no serial ports PICKit 2 can do USB to Serial conversion (but not yet

working on the PICKit 3) Select 3.3V before plugging in. You can buy USB to Serial convertors, either full RS232 or

just TTL UART. Be careful you do not damage the board! You want 3.3Volts maximum Also several I have used output on pins labelled RCV and input on TX – I had to use a scope to check!

Page 22: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

HyperTerminal Setup (windows XP only)HyperTerminal Setup (windows XP only)

Page 23: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

RealTerm runs on XP and windows 7 (but not 8)RealTerm runs on XP and windows 7 (but not 8)

Page 24: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

PUTTY can perform serial PUTTY can perform serial terminal functionsterminal functions

When programming the PIC32 and with the UART output connected to Putty many random characters are sent from the PIC to PUTTY.

If the handshaking is left at the default XON/XOFF then PUTTY may receive a XOFF (control-S) from the PIC and you have to quit and restart PUTTY after every programming

Alternatively select the correct handshaking protocol. Such as clicking on the Serial menu option and selecting “NONE” or “HARDWARE RTS/CTS”

Use the Device manager to check the COM port of the USB-TTL adaptor

Ensure the Speed is correct

Page 25: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

PICKit2 comes with UART Software – PICKit2 comes with UART Software – NB NOT the PICKit3 (yet)NB NOT the PICKit3 (yet)

Wire up the PICKit2 asPICKit Pin 1 - No ConnectionPICKit Pin 2 - 3V3PICKit Pin 3 - GNDPICKit Pin 4 – DP32 Pin 7 (Tx) RB14PICKit Pin 5 - DP32 Pin 10 (Rx) RA1PICKit Pin 6 - No Connection

Start the PICKit 2 application and select Tools-->UART Tool

Page 26: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

The PICKit2 has other uses;The PICKit2 has other uses;You can also use the Logic Analyzer Mode.

Click 'Exit UART Tool' and start the Logic ToolSelect 'Analyser' if it is not on by default.Set the Sample rate to 100 Khz and the Trigger to Ch1 \ (falling edge)Click Capture and,  When your code sends 'Hello World!' you should see…

Page 27: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Tips and TricksTips and TricksTo re-direct the output stream of the standard C library (stdio.h) functions such as printf() to a UART:

Define the function: _mon_putc() Note that a “weak” definition is already provided in the library to send the default

output stream (stdout) to UART2 (convenient for all Explorer16 users).

Similarly define: _mon_getc() A default “weak” version is already provided in the library as well, connecting

UART2 receiver to the input stream (stdin). Weak means that the compiler won’t complain when you define a new function

with the same name, it will simply replace it with the new one you provide.

NOTEYou are responsible for the UART initialization! Before the first call to any stdio function (printf()…) make sure the UART2 is enabled and the baud rate is set correctly.

Page 28: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Code for serial i/o, allowing printf & putsCode for serial i/o, allowing printf & puts

By adding a function called _mon_putc() the linker will use it for calls to printf() and puts()

Page 29: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Page 30: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

Once you define _mon_putc() any call to Once you define _mon_putc() any call to printf or puts will just workprintf or puts will just work

Page 31: EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: 90 366364 voice.

From : Di Jasio - Programming 32-bit Microcontrollers in C with additions by Ian McCrum

I couldn’t get it working for I couldn’t get it working for _mon_getc() – I expected gets to _mon_getc() – I expected gets to work… instead use code belowwork… instead use code below