Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network and Systems Laboratory nslab.ee.ntu.edu.tw.

Similar presentations


Presentation on theme: "Network and Systems Laboratory nslab.ee.ntu.edu.tw."— Presentation transcript:

1 Network and Systems Laboratory nslab.ee.ntu.edu.tw

2 Network and Systems Laboratory nslab.ee.ntu.edu.tw Administrations Week 10 (4/20): Term project check point #1 Make your robot car move Forward, backward, turn left, turn right Avoid Obstacles

3 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART UART: Universal Asynchronous Receiver Transmitter Two wires: Rx, Tx Full duplex Asynchronous No common clock required Without common clock How do they communicate?? A Rx Tx B Tx Rx

4 Network and Systems Laboratory nslab.ee.ntu.edu.tw Universal data rates Two devices agree on same data rate Baudrate: 1200, 2400, 4800, 9600, …, (bits per second) A (1 MHz system clock) B (8 MHz system clock) UART Baudrate generator 9600 Baudrate: 9600 bps Length of a bit = 1/9600 (seconds)

5 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART Data Send one character at a time Data One start bit Pull-down the line 7 or 8 bits data One or two stop bit Pull-up the line for one or two slots Simple error checking: parity (optional) Even parity: If the data has odd number of 1, parity bit = 1 (make it even); else parity bit = 0 Odd parity: If the data has even number of 1, parity bit = 1 (make it odd); else parity bit = 0 Four parameters for UART communication Baudrate, data-bit, parity, stop-bit We wrote: 9600 8N1, Means: baudrate=9600, 8-bit data, no parity, 1 stop bit This bit became parity bit when parity checking is enabled

6 Network and Systems Laboratory nslab.ee.ntu.edu.tw Applications MSP430 UART USB serial IC (FT232) PC or other systems RS232 IC RS485 IC Other systems COM PORT USB All you need to do is properly configure UART for both sides (baudrate, data- bit, parity, stop-bit), and start sending/receiving data Other Sensors Today’s Labs

7 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART Modules On MSP430 Two UART modules on MSP430F1611 USART 0 and USART 1 USART Universal Synchronous/Asynchronous Receive Transmit The USART 0 module support 3 protocols UART SPI I 2 C The USART 1 module support 2 protocols UART SPI Two protocols cannot operate simultaneously on one module

8 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART Modules On MSP430 I2C pins SPI pins UART pins There are multiplexed with digital I/O pins, remember to set the PxSEL register properly

9 Network and Systems Laboratory nslab.ee.ntu.edu.tw USART Connections On Taroko MSP430F1611 USART0 USART1 Expansion Connector (U2) CC2420 Radio IC FT232 USB serial IC PC UART I2C SPI The UART pins and I2C pins are connected to the expansion connector, so you can connect external sensors through UART or I2C interface on USART0 module Taroko use SPI on USART0 module to control the radio IC. The USART0 module is shared If you connected an external sensor through UART/I2C interface When you are using radio, you cannot read sensor When you are reading sensor, you cannot use radio Taroko Taroko Schematic

10 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART Mode Two devices Three or more devices (multiprocessor format) Character Format A Rx Tx B Tx Rx A Rx Tx B Tx Rx C Tx Rx Seldom use, we wouldn’t talk about it Usually we use 8- bit data For multiprocessor format, not use

11 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART Receive and Transmit Receive Transmit Set URXEx= 1 to enable the receiver When the module detects a valid start bit, it will start collect the rest of the bits When all the bits are collected, the received character will be written to a buffer register (UxRXBUF) and issues an interrupt. Set UTXEx= 1 to enable the transmitter When data is written to the transmit buffer (UxTXBUF), a transmission start To transmit another byte. You MUST wait until the previous data has transmitted. If it is ready to transmit A flag UTXIFGx will set to 1 Two ways to detect An Interrupt A while loop to check the flag

12 Network and Systems Laboratory nslab.ee.ntu.edu.tw Baud Rate Generation UART Clock Source Divider Desire baud rate DCO (SMCLK) 32768 Hz (ACLK) For Example: UART Clock Source = 32768 Desire baud rate = 2400 Divider = 32768/2400 = 13.653 UxBR = 13, = 0.653 The divider setting controlled by three 8-bit registers UxBR0, UxBR1, and UxMCTL The equation UxBR = (UxBR1 << 8) + UxBR0 Total bits 1 start, 8 data, 1 parity, 1 stop 11 bits in total (n = 11) Integer partFractional part UART clock source frequency must larger than (3*baud rate)!

13 Network and Systems Laboratory nslab.ee.ntu.edu.tw Modulations UxMCTL determine the fractional part You know the summation of m i should be 7 But what is the right combination 01101011 UxMCTL Bit 7 Bit 0 m0m0 m1m1 m2m2 m3m3 m4m4 m5m5 m6m6 m7m7 m8m8 m9m9 m 10 For Example: UART Clock Source = 32768 Desire baud rate = 2400 Divider = 32768/2400 = 13.653 UxBR = 13, = 0.653

14 Network and Systems Laboratory nslab.ee.ntu.edu.tw Determining the UxMCTL Individual bit error Baud rate, BRCLK, and UxBR are known Choose a combination of m 0 … m 7 For each bit, calculate the bit error Find the maximum bit error among all the bits This is the bit error of this combination Choose another combination and calculate again Select the combination that has minimum bit error 01101011 Bit 7 Bit 0 m0m0 m1m1 m2m2 m3m3 m4m4 m5m5 m6m6 m7m7 m8m8 m9m9 m 10

15 Network and Systems Laboratory nslab.ee.ntu.edu.tw Example Useful resource!!! http://mspgcc.sourceforge.net/baudrate.html 01101011 Bit 7 Bit 0 m0m0 m1m1 m2m2 m3m3 m4m4 m5m5 m6m6 m7m7 m8m8 m9m9 m 10 10110101 Bit 7 Bit 0 m0m0 m1m1 m2m2 m3m3 m4m4 m5m5 m6m6 m7m7 m8m8 m9m9 m 10

16 Network and Systems Laboratory nslab.ee.ntu.edu.tw UART Interrupts Two interrupt vectors Transmission interrupt vector Reception interrupt vector Transmission interrupt When UxTXBUF (transmit buffer) is ready to accept another character (previous transmission is complete) An interrupt request is generated UTXIFGx interrupt flag is set The interrupt request is clear automatically when The request is serviced Or, a character is written to UxTXBUF Reception interrupt When a character is received An interrupt request is generated URXIFGx interrupt flag is set The interrupt request is clear automatically when The request is serviced Or, UxRXBUF is read

17 Network and Systems Laboratory nslab.ee.ntu.edu.tw Wait For Transmission To transmit another byte. You MUST wait until the previous data has transmitted Two ways to do By Interrupt 1.Enable transmit interrupt 2.When it is ready to transmit another byte, it will generate an interrupt request 3.Transmit next byte in the ISR By a while loop 1.No matter you enable or disable the transmit interrupt, the UTXIFGx will set to 1 when it is ready to transmit another byte 2.Use a while loop to check the flag While(!(IFG1 & UTXIFG0) 3.Transmit next byte after the while loop Could possibly hang in the while loop Program will branch to the ISR, need extra CPU cycles

18 Network and Systems Laboratory nslab.ee.ntu.edu.tw UxCTL You MUST held UART logic in reset state when you setting the UART registers

19 Network and Systems Laboratory nslab.ee.ntu.edu.tw UxTCTL Select clock source If you want to disable the transmitter, wait until this bit is set to 1

20 Network and Systems Laboratory nslab.ee.ntu.edu.tw UxRCTL Error Detection Framing error A low stop bit detected (stop bit must be high) Parity error When you enable parity check, an parity error detected Overrun error A new character received before previous character has been read Break detect More than 10 bits of zero detected

21 Network and Systems Laboratory nslab.ee.ntu.edu.tw Other Registers Baud rate control registers UxBR0, UxBR1, and UxMCTL Receive and transmit buffer UxRXBUF and UxTXBUF Module enable registers Enable/disable transmitter and receiver For UART0 For UART1

22 Network and Systems Laboratory nslab.ee.ntu.edu.tw Interrupt Registers Interrupt enable registers Interrupt flag registers For UART0For UART1 For UART0

23 Network and Systems Laboratory nslab.ee.ntu.edu.tw Initialization/Re-configuration UART There are specific process flow to follow 1. Set SWRST 2. Initialize all UART registers when SWRST=1 3. Enable UART module (ME1 or ME2) 4. Clear SWRST (SWRST=0) 5. Enable interrupts (optional) Failure to follow this process may result in unpredictable USART behavior

24 Network and Systems Laboratory nslab.ee.ntu.edu.tw Problems With Clock Source To generate appropriate baud rate, you must know the frequency of UART clock source Two clock source avaible ACLK = 32768 Hz SMCLK = DCO (frequency??) UART clock source frequency must larger than (3*baud rate)! For 32768 clock source, max baud rate is 10922 (9600) Problems What if you need faster baud rate Error increase when baud rate is close to clock rate We need faster clock source

25 Network and Systems Laboratory nslab.ee.ntu.edu.tw Set DCO We talk about how to adjust DCO frequency in lab 2 But it is not very accurate We can use a more accurate clock to set the frequency of DCO precisely Use timer capture mode Latch the current timer counter value into TxCCRx register Use an accurate clock as a capture source Use DCO as timer clock source Timer Clock source Capture source Counter increase at every raising edge Capture at every raising edge When a capture occurred, the value of timer counter is copy to the TxCCRx register You know the frequency of capture source. frequency of timer clock source = (Capture_2 – Capture_1) * Freq. of capture sourcev Capture_1 Example: Timer clock source = DCO Capture source = ACLK/4 = 32768/4 = 4096 Hz If Capture_1 = 3000 Capture_2 = 3250 Freq. of DCO = (3250 – 3000) * 4096 = 1024000 Hz You can adjust the DCO by setting the registers until you get the desire frequency you want

26 Network and Systems Laboratory nslab.ee.ntu.edu.tw Today’s Labs Lab 1: Set DCO You don’t have to write the routine by yourself, use the sample code on course webpage (sample code form TI) The routine sets the ACLK to 32768/4 = 4096 Hz It use a variable “DELTA” to set the frequency of DCO DON’T set the DCO frequency > 8 MHz Use program in previous lab See the LED flashed at a certain rate If DCO = 1000000 Hz i = 125000 The LED should flash approximately every second int is 16-bit long Set DCO This while loop takes exactly 8 cycles

27 Network and Systems Laboratory nslab.ee.ntu.edu.tw Lab 2: Send to PC Use the sample file on course webpage Setting UART registers UART setting Baudrate = 57600, 8-bit data, no parity, 1 stop bit Use the program in Lab 3, sense the acceleration Send the acceleration values (x, y, z axes) to your computer Use a terminal program (realterm) to receive the values

28 Network and Systems Laboratory nslab.ee.ntu.edu.tw Lab 3: Receive From PC From the terminal program on PC, send the following byte sequences (in Hexidecimal) to Taroko 5A A5 53 54 41 52 54 0D 0A 5A A5 53 4C 45 45 50 0D 0A UART setting Baudrate = 9600, 8-bit data, no parity, 1 stop bit Configure your Taroko to receive these two sequence correctly

29 Network and Systems Laboratory nslab.ee.ntu.edu.tw Taroko USB driver Download Taroko USB virtual com port driver http://www.ftdichip.com/Drivers/CDM/CDM%202.02.04.exe Install driver Plug Taroko to you computer’s USB port, you should see Device manager

30 Network and Systems Laboratory nslab.ee.ntu.edu.tw Terminal Program Realterm http://realterm.sourceforge.net/ Download Realterm program http://sourceforge.net/project/showfiles.php?group_id=67297 Install the program Open the program by this way Click Start-up -> click run "C:\Program Files\BEL\Realterm\realterm.exe" baud=9600 port=63 Enter baud rate Enter COM port

31 Network and Systems Laboratory nslab.ee.ntu.edu.tw Realterm Display format

32 Network and Systems Laboratory nslab.ee.ntu.edu.tw Realterm UART setting Click here can open/close the port

33 Network and Systems Laboratory nslab.ee.ntu.edu.tw Realterm Capture data to a file

34 Network and Systems Laboratory nslab.ee.ntu.edu.tw Realterm Send out data


Download ppt "Network and Systems Laboratory nslab.ee.ntu.edu.tw."

Similar presentations


Ads by Google