The serial port, can be understood to be used and the upper computer (for example, the PC) dialogue, this section experiment realizes is STM32 receives the host computer sends over the information, in the literal return the past
Stm32 has 5-way serial port, this section is one of the USB serial port (USART1) and learning IO port, the first to configure the serial port, because the atom has been well written in the Sys file, the slag on the spirit of the idea of not making wheels (in fact, it is too difficult not to understand Orz), first jump past.
Therefore, this project only needs a main function can
#include "led.h" #include "sys.h" #include "delay.h" #include "usart.h" void init (void) {delay_init (); Led_init (); Nvic_configuration ();//Set Interrupt grouping Uart_init (9600);//serial port initialization, baud rate 9600}int main (void) {U8 i,len;u16 times=0;init (); while (1) { if (usart_rx_sta&0x8000)//Received data {len=usart_rx_sta&0xfff//printf ("\ r \ n Mud sent message: \ r \ n"); for (i=0;i<len;i+ +) {Usart1->dr=usart_rx_buf[i];while ((usart1->sr&0x40) ==0);//wait for Send end}printf ("\r\n\r\n"); usart_rx_sta=0;} else//{++times;if (times%5000==0) printf ("\r\nlove\r\n") when data is not received, if (times%30==0) led0=! Led0;delay_ms (10);}}
Perhaps the above code is a bit confused, we turned to usart.c inside a piece of code
Serial 1 Interrupt Service Program//note, read USARTX->SR can avoid inexplicable error U8 Usart_rx_buf[usart_rec_len]; Receive buffer, maximum Usart_rec_len bytes.//Receive status//bit15, receive completion flag//bit14, receive 0x0d//bit13~0, number of valid bytes received U16 usart_rx_sta=0; Receive Status Tokens
0x8000 corresponding to 2 binary 100000000000000
0X3FFF corresponding to 2 binary 0011111111111111
So it's obvious. (feeling more and more away from the bottom)
After the program is burned in, open the Serial Debugging assistant interface displays as follows:
You will also see that the LEDs have been flashing. The program should be fine.
Cortex_m3_stm32 Embedded Learning Note (iii): Serial port Experiment (serial communication)