UART: Universal Asynchronous RX/TX 12/23/2016 Richard Kuo Assistant Professor
OutLine 8. NuMicro_UART.ppt UART Interface Introduction Ex. UART to Bluetooth module (UART1_HC05) WiFi module Introduction Ex. UART to WiFi module (UART0_ESP8266) HC-05 (BT2.0) ESP8266 (WiFi)
UART (Universal Asynchronous Receiver Transimitter) Serial Transmit Start Bit Data Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 Parity Stop Drive LSB MSB Sample Sample Sample Sample Sample Sample Sample Sample Sample Sample Sample Serial Receive Start Bit Data Bit 0 Data Bit 1 Data Bit 2 Data Bit 3 Data Bit 4 Data Bit 5 Data Bit 6 Data Bit 7 Parity Bit Stop Bit LSB MSB
RS-232 DB9 Connector 1 2 3 4 5 6 7 8 9 Data Carrier Detect Data Set Ready Receive Data Request To Send Clear To Send Ring Indicator Transmit Data Data Terminal Ready Signal Ground
UART Baud Rate Setting vs Oscillator Frequencies
RS-232/422/485 Comparison RS-232 RS-422 RS-485 No. of Driver 1 32 No. of Receiver 10 Mode of Operation Half duplex Full duplex Max. Distance 15m 1200m Max. Speed at 12m 20Kbps 10Mbps 35Mbps Max. Speed at 1200m 1Kbps 100Kbps Min. Output Voltage +-5V +-2.0V +-1.5V Max. Output Voltage +-25V +-6V -7~12V Receiver Input Range +-15V +-10V
Nu-LB-NUC140 UART w RS232 transceiver connector RS232 Transceiver
UART schematic to DB9 pin2 RX SP232-TX from DB9 pin3 TX SP232-RX
NuMicro MCU UART function NuMicro provide up to 3 UART ports (some support up to 2 ports) programmable baud-rate programmable serial-interface Built-in TX/RX buffers (FIFO) : 16 bytes Support hardware flow-control (/CTS及/RTS) on UART0 & UART1 Support IrDA SIR function (Infrared transmission use) UART0,1,2 Pins : UART0-RX=PB0, UART0-TX=PB1 UART1-RX=PB4, UART1-TX=PB5 UART2-RX=PD14, UART2-TX=PD15
UART Clock Selection of Nano102 12/16MHz ~32/50MHz 32768Hz 12~24MHz
UART Clock Selection of NUC140 HIRC PLL HXT
UART Block Diagram
UART function calls UART_Open(UARTn, Baudrate); UART_Read(UARTn, ReadBuffer, No_of_bytes); UART_Read(UART1, RX_Buffer, 8); UART_READ(UART1); // read 1 byte UART_Write(UARTn, WriteBuffer, No_of_bytes); UART_Write(UART1, TX_Buffer, 8); UART_WRITE(UART1, out_data); // write 1 byte
UART Port & INT Initialization void Init_HC06(void) { UART_Open(UART1,9600); // enable UART1 at 9600 baudrate UART_ENABLE_INT(UART1, UART_IER_RDA_IE_Msk); NVIC_EnableIRQ(UART1_IRQn); }
UART IRQ Handler #define RXBUFSIZE 8 volatile char RX_buffer[RXBUFSIZE]; void UART1_IRQHandler(void) { uint32_t u32IntSts= UART1->ISR; // UART interrupt status bit if(u32IntSts & UART_IS_RX_READY(UART1)) { UART_Read(UART1, RX_buffer, RXBUFSIZE); printf("%s\n", RX_buffer); }
UART Protocol Setting Even, odd or no-parity 5 ~ 8bits 1 ~ 2bits UART_Open(port, baudrate) uart->TLCTL = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1 | UART_TLCTL_RFITL_1BYTE | UART_TLCTL_RTS_TRI_LEV_1BYTE;
UART Auto Flow Control
Bluetooth 2.0 Module
Bluetooth module (HC-05) Connections N.C. MCU TX MCU RX GND VCC HC-05 can operate at 5V
Connecting HC-05 to Nu-LB-NUC140 Android phone running BlueTerm HC-05 BT2.0 module UART1 pins PB0 = RX0 PB1 = TX0
UART1_HC05 // // UART1_HC05 : using UART1 to receive charactors from HC-05 bluetooth module // smartphone running a bluetooth terminal for user to key in charactors // Board : NuTiny-EVB-Nano102 // MCU : Nano102SC2AN (LQFP64) // Module : HC-05 (Bluetooth 2.0) // Features : // UART1 to HC05 : transmit/receive data through Bluetooth // baudrate=9600, databit=8, stopbit=1, paritybit=0, flowcontrol=None // Connections // STATE N.C. (LED indication) // RXD to PC8 /UART1-TX (Nano102SC2AN LQFP64 pin15) // TXD to PC7 /UART1-RX (Nano102SC2AN LQPF64 pin14) // GND to Gnd // VCC to Vcc (3.3~6V) // EN N.C. (AT command mode) UART1 pin connections
UART1_HC05 #include <stdio.h> #include <string.h> #include "Nano1X2Series.h“ #include “MCU_init.h” #include “SYS_init.h” Volatile char RX_buffer[8]; Char BT_Flag =0; void UART1_IRQHandler(void) { uint8_t in_char; uint32_t u32IntSts= UART1->ISR; if(u32IntSts & UART_IS_RX_READY(UART1)) { UART_READ(UART1,RX_buffer, 8); } Include MCU sereis header file Include MCU clock & pin configuration for SYS_init.c to use Include SYS_init header file Clear UART1 ISR bit Check if UART1 RX is ready Read 8 charactors from UART1
UART1_HC05 void Init_HC05(void) { UART_Open(UART1,9600); // enable UART1 at 9600 baudrate UART_ENABLE_INT(UART1, UART_IER_RDA_IE_Msk); NVIC_EnableIRQ(UART1_IRQn); } int32_t main() SYS_Init(); Init_HC05(); printf("Nano102 + Bluetooth running...\n"); while(1) { if (BT_Flag==1) { printf("%s\n", RX_buffer); BT_Flag=0; } Initialize UART1 for HC-05 UART1 enable interrupt generation CPU enable to take UART1 interrupt Printf to output message to Keil MDK on PC
Keil MDK running debug session with UART_HC05 sample code Smartphone need to run a Bluetooth Terminal to key in data for HC-05 to receive the charctors Receive charactors from bluetooth Which is transmitted by smartphone’s Bluetooth Terminal
proj_BT2RC
proj_BT2RC usage Turn Battery Smartphone bluetooth scan HC-05 (password = 1234) Smartphone run BT2RC app Select Bluetooth Device to connect Touch button to send command to MCU
proj_BT2RC (NuDip-mini58) // // proj_BT2RC : Bluetooth Remote Control two-wheel Car (or Vacuum Robot) // EVB : NuDip-Mini58 // MCU : Mini58FDE (TSSOP-20) // BT : HC-05 (Bluetooth 2.0) // DCM : +6V/+12V DC motor // : L298N (dual DC-motor driver IC) // Features : // UART1 to HC05 : transmit/receive data through Bluetooth // baudrate=9600, databit=8, stopbit=1, paritybit=0, flowcontrol=None// // HC05 connections // VCC to Vcc // GND to GND // TX to UART1_RXD_P14 (P1.4) // RX to UART1_TXD_P15 (P1.5) // L298N connections // P04 : connected to L298N-IN1 // P05 : connected to L298N-IN2 // P06 : connected to L298N-IN3 // P07 : connected to L298N-IN4
proj_BT2RC #include <stdio.h> #include <string.h> #include "Mini58Series.h" #include "MCU_init.h" #include "SYS_init.h" #define RXBUFSIZE 8 // Global variables uint8_t Text[64]; uint8_t RX_buffer[RXBUFSIZE]; uint8_t command; uint8_t BT_Flag =0; void UART1_IRQHandler(void) { uint32_t u32IntSts= UART1->INTSTS; if(u32IntSts & UART_IS_RX_READY(UART1)) { UART_Read(UART1, RX_buffer, RXBUFSIZE); command=RX_buffer[0]; BT_Flag=1; } void Init_UART(void) { UART_Open(UART1,9600); // enable UART1 at 9600 baudrate UART_ENABLE_INT(UART1, UART_INTEN_RDAIEN_Msk); NVIC_EnableIRQ(UART1_IRQn); } void Init_GPIO(void) GPIO_SetMode(P0, BIT4, GPIO_MODE_OUTPUT); GPIO_SetMode(P0, BIT5, GPIO_MODE_OUTPUT); GPIO_SetMode(P0, BIT6, GPIO_MODE_OUTPUT); GPIO_SetMode(P0, BIT7, GPIO_MODE_OUTPUT); P04=0; P05=0; P06=0; P07=0;
proj_BT2RC (NuDip-mini58) void Init_Timer0(void) { TIMER_Open(TIMER0, TMR0_OPERATING_MODE, TMR0_OPERATING_FREQ); // 10Hz TIMER_EnableInt(TIMER0); NVIC_EnableIRQ(TMR0_IRQn); TIMER_Start(TIMER0); } void Forward(void) { P04=1; P05=0; P06=1; P07=0; } void Backward(void) { P04=0; P05=1; P06=0; P07=1; } void Left(void) { P04=0; P05=1; P06=1; P07=0; } void Right(void) { P04=1; P05=0; P06=0; P07=1; } void Stop(void) { P04=0; P05=0; P06=0; P07=0; } void TMR0_IRQHandler(void) { if (BT_Flag==1) { switch(command) { case 'F' : Forward(); break; case 'B' : Backward(); break; case 'L' : Left(); break; case 'R' : Right(); break; case 'S' : Stop(); break; default: break; } BT_Flag=0; // clear timer interrupt flag TIMER_ClearIntFlag(TIMER0);
proj_BT2RC (NuDip-mini58) int32_t main() { SYS_Init(); Init_GPIO(); Init_UART(); Init_Timer0(); while(1); }
WiFi brief Wi-Fi, or Wireless Fidelity, is a term that is used generically to refer to any product or service using any type of 802.11 technology. Wi-Fi networks operate in the unlicensed 2.4 and 5 GHz radio bands, with an 11 Mbps (802.11b) or 54 Mbps (802.11a) data rate, respectively. Gen I 802.11 defined in 1997,use 2.4GHz only,2Mbit/s max. Gen II 802.11b use 2.4GHz only,11Mbit/s max Gen III 802.11g use 2.4GHz or 5GHz,54Mbit/s max Gen IV 802.11n use 2.4G & 5GHz,20 or 40MHz bandwidth 72 or 150M max. Gen V 802.11ac 5GHz only
WiFi Module : ESP8266 ESP-01 ESP-03 Pin Connections: VCC : 3.3V GND : GND CH_PD : to Vcc (/Chip_PowerDown) TXD : to UART-RX RXD : to UART-TX Pin Connections: VCC : 3.3V GND : GND GPIO15: to GND CH_PD : to Vcc (/Chip_PowerDown) TXD : to UART-RX RXD : to UART-TX
WiFi Module: ESP8266 ESP-07 Pin Connections: VCC : to 3.3V GROUND: to GND CH_PD : to 3.3V (/Chip_PowerDown) TXD : to UART-RX RXD : to UART-TX Note: independent power source to ESP-07 or adding 470uF cap on Vcc to Gnd !
ESP8266 AT Command Set http://www.pridopia.co.uk/pi-doc/ESP8266ATCommandsSet.pdf
ESP8266 AT Command Set
ESP8266 first time user guide Steps and note AT+CWMODE=3 change the working mode to 3, AP+STA, only use the most versatile mode 3 (AT+RST may be necessary when this is done.) AT+RST restart the module, received some strange data, and "ready" Join Router AT+CWLAP search available wifi spot AT+CWJAP=“ssid”, “password” join my mercury router spot AT+CWJAP=? check if connected successfully, or use AT+CWJAP? AT+CIPMUX=1 turn on multiple connection TCP Client AT+CIPSTART=1,"TCP","192,168.191.2",8088 connect to remote TCP server 192.168.191.2 (the PC), open port 8088 AT+CIPMODE=1 optionally enter into data transmission mode AT+CIPSEND=4, 5 send data via channel 4, 5 bytes length (see socket test result below, only "elect" received), link will be "unlink" when no data go through TCP Server AT+CIPSERVER=1,8088 setup TCP server, on port 8088, 1 means enable AT+CIFSR check module IP address PC as a TCP client connect to module using socket test, send data
WiFi module as TCP server AT+CWLAP AT+CWMODE=1 (STA) AT+CWJAP=“ssid”, “password” AT+CIPMUX=1 (Multiple) Setup WiFi to join WiFi network Set WiFi module as TCP Server Find out TCP Server IP address AT+CIPSERVER=1,8088 AT+CIFSR (checking IP address) run TCPClient app on smartphone type transmit to TCPserver (ESP8266) Smartphone run TCP Client App Keyin TCP Server IP address:port Keyin data to transmit
WiFi module as TCP client AT+CWMODE=1 (STA) AT+CWJAP=“ssid”, “password” AT+CIPMUX=1 (Multiple) Setup WiFi to join WiFi network AT+CIPSTART=1,”TCP”,”192.168.191.2”,8088 AT+CIPSEND=1, no_bytes (channel=1, send bytes of GET http string) Set WiFi module as TCP Client Find out TCP Client IP address Smartphone run TCP Server App Keyin TCP Client IP address:port Keyin HTTP command to transmit >GET http://192.168.191.2 HTTP/1.0 (Sending HTTP command GET/PUT to TCP Server on PC, ESP8266 run as TCP cleint)
WiFi ESP8266 connected to UART1 WiFi ESP8266 set as TCP Server NUC140 connect UART0 to ESP8266, and run USBD_VCOM PC run HyperTerminal, key in AT commands to set up ESP8266 TCP Client App running at Android phone TCP Client 終端輸入字串,可傳輸至TCP Server TCP Client TCP Server
UART1_ESP8266 ESP8266
UART1_ESP8266 // // smpl_UART1_ESP8266 : ESP8266 set as TCP server, smartphone run a TCP client // SEMIHOST will display data from TCP client // Board : Nu-LB-NUC140 // MCU : NUC140VE3CN (LQFP100) // Module : ESP8266 (WiFi) #include <stdio.h> #include <string.h> #include "NUC100Series.h" #define AT_quo "\"" #define AT_comma "," #define AT_end "\r\n" #define AT_cmd "AT" #define AT_CWMODE "AT+CWMODE=3" #define AT_CWJAP "AT+CWJAP=\“myAsus\",\"0972211921\"" #define AT_CIPMUX "AT+CIPMUX=1" #define AT_CIPSERVER "AT+CIPSERVER=1,8088" #define AT_CIFSR "AT+CIFSR"
UART1_ESP8266 void UART1_IRQHandler(void) { uint8_t in_char; uint32_t u32IntSts= UART1->ISR; if(u32IntSts & UART_IS_RX_READY(UART1)) { in_char = UART_READ(UART1); printf("%c", in_char); } void Init_UART1(void) UART_Open(UART1,115200); // enable UART1 at 115200 baudrate UART_ENABLE_INT(UART1, UART_IER_RDA_IE_Msk); NVIC_EnableIRQ(UART1_IRQn);
UART1_ESP8266 void Init_ESP8266(void) { uint8_t i; char ESP8266_cmd[32]=""; // AT strcpy(ESP8266_cmd,AT_cmd); strcat(ESP8266_cmd,AT_end); UART_Write(UART1, ESP8266_cmd, strlen(ESP8266_cmd)); CLK_SysTickDelay(100000); // AT+CWMODE=3 strcpy(ESP8266_cmd,AT_CWMODE); UART_Write(UART1, ESP8266_cmd,strlen(ESP8266_cmd)); // AT+CWJAP=“myAsus","0972211921" strcpy(ESP8266_cmd,AT_CWJAP); for (i=0; i<25; i++) CLK_SysTickDelay(1000000); // AT+CPIMUX=1 strcpy(ESP8266_cmd,AT_CIPMUX); // AT+CIPSERVER=1,8088 strcpy(ESP8266_cmd,AT_CIPSERVER); strcat(ESP8266_cmd,AT_end); UART_Write(UART1, ESP8266_cmd, strlen(ESP8266_cmd)); CLK_SysTickDelay(100000); // AT+CIFSR strcpy(ESP8266_cmd,AT_CIFSR); printf("\nESP8266 running TCP server...\n\n"); } int32_t main() { SYS_Init(); Init_UART1(); printf("NUC140 UART1 to WiFi ESP8266\n"); Init_ESP8266(); while(1){
DEBUG_ENABLE_SEMIHOST - printf
References http://rancidbacon.com/files/kiwicon8/ESP8266_WiFi_Module_Quick_Start_Guide_v_1.0.4.pdf ESP8266 AT Commands Set
Important Notice ! This educational material are neither intended nor warranted for usage in systems or equipment, any malfunction or failure of which may cause loss of human life, bodily injury or severe property damage. Such applications are deemed, “Insecure Usage”. Insecure usage includes, but is not limited to: equipment for surgical implementation, atomic energy control instruments, airplane or spaceship instruments, the control or operation of dynamic, brake or safety systems designed for vehicular use, traffic signal instruments, all types of safety devices, and other applications intended to support or sustain life. All Insecure Usage shall be made at user’s own risk, and in the event that third parties lay claims to the author as a result of customer’s Insecure Usage, the user shall indemnify the damages and liabilities thus incurred by using this material. Please note that all lecture and sample codes are subject to change without notice. All the trademarks of products and companies mentioned in this material belong to their respective owners.