Serial Communication Based on stm32 debugging Algorithm

Source: Internet
Author: User

You often need to view some specific parameters during the development process. You can use paintf to print the output and observe the specific variable values. The stm32 is integrated with the serial port function of usart, which can be directly output to the computer (host computer) through the serial port ). It is very convenient to use, and you do not need to write code. You only need to configure it.

 

You can see the above results with simple settings.

 

Configuration method:

1. Redirect the printf output function int fputc (int ch, file * F)

2. Configure the usart serial port of stm32f10x

 

Redirection method stdio. h output content

Int fputc (int ch, file * f) {// wait until the usart1 data is sent completely (the sending region is blank) while (! (Usart1-> Sr & usart_sr_txe); // fill in the sending register. The data + check bit can contain up to 9 usart1-> DR = (ch & 0x1ff); Return (CH );}

Start usart1 serial port of stm32

1. Enable reuse

2. Enable a pin (usart1 in pa9, pa10)

3. Enabling usart1

4. Set pa9 (TX) to reuse the push-pull output.

5. Set pa10 (RX) input to float

6. Set serial port parameters: baud rate, data bit, verification, stop bit, and traffic control

7. Enable serial port, enable input, and enable output

OK

Void serial_init (void) {/// set serial port debugging /// output: usart1 // pin: pa9 (TX), pa10 (RX) // baud rate: 9600 // data bit: 8 bit (default) (CR1) // verification: none (default) (CR1) // stop bit: 1 bit (default) (CR2) // throttling: none (default) (MCM) /// after clearing the settings, the default system status int I is configured above; // enable the reusable function to enable gpioa, enable usart1 RCC-> apb2enr | = enabled | rcc_apb2enr_iopaen | rcc_apb2enr_usart1en; // disable the ing to ensure that usart uses pa9, pa10 afio-> mapr & = ~ Afio_mapr_usart1_remap; // clear pa9, pa10 status gpioa-> CRH & = ~ (Optional | gpio_crh_mode9 | gpio_crh_cnf10 | gpio_crh_mode10); // set pa9 transmission to reusable push-pull output 2 MHz gpioa-> CRH |=gpio_cr_afout_pp2mhz & (optional | gpio_crh_mode9 ); // set the pa10 receiver to reuse the pull-down mode gpioa-> CRH | = gpio_cr_afin_pulldown & (gpio_crh_cnf10 | gpio_crh_mode10 ); // set the baud rate to 9600 // calculation method // system clock/(16-Division * baud rate) // baud = 72,000,000/(16*9600) = 468.75 // integer part <4 + INTEGER (decimal part * 16) // 468 <4 + 0.75*16 usart1-> BRR = _ usart_brr (systemcoreclock, 9600); // clear register status usart1-> CR1 = usart_cr1_rest; usart1-> Cr2 = usart_cr2_rest; // stop bit 1 usart1-> Cr 3 = usart_cr3_rest; // No control flow is used // prevent unnecessary information from being generated for (I = 0; I <0x1000; I ++) _ Nop (); // usart1 enabling, enable the output. Enable the input of usart1-> CR1 = usart_cr1_ue | usart_cr1_te | usart_cr1_re ;}

 

The key output targeting mainly uses two registers throughout the process:

A Status Register to check whether sending is empty (usart_st_txe)

A data register used to send data

Int main (void) {uint8_t UD = 'a'; delay_init (); // initialize the serial port debugging serial_init (); // use registers to directly output ~ Z while (UD <= 'Z') {While (! (Usart1-> Sr & usart_sr_txe); usart1-> DR = UD; UD ++ ;}// print the redirected output string printf ("\ ntest! \ N "); printf (" usart1 enable, enable output, enable input \ n "); UD = 'a'; while (1) {delay (20 ); // speed is reduced too quickly to view results (MS latency) // use registers to directly output while (! (Usart1-> Sr & usart_sr_txe); usart1-> DR = UD; UD ++; // use the print output line feed if (UD> 'Z ') {UD = 'a'; printf ("\ n ");}};}

 

For specific configuration registers, see reference manual.

 

Complete code

# Define gpio_cr_reset (uint32_t) 0x44444444 # define gpio_cr_mode_input (uint32_t) 0x00000000 # define partition (uint32_t) 0x22222222 # define partition (uint32_t) 0x11111111 # define tuning (uint32_t) 0x33333333 # define tuning (uint32_t) 0x00000000 # define tuning (uint32_t) 0x44444444 # define tuning (gpio_cr_mode_2mhz | idle) # define transform (outputs | outputs) # define transform (uint32_t) 0x88888888 # define transform (uint32_t) 0 xcccccccc # define transform (gpio_cr_mode_2mhz | gpio_cr_afo_pushpull) // reuse the push-pull output, 2 MHz # define gpio_cr_afin_float (uint32_t) 0x44444444 // reuse open-drain input # define pull (uint32_t) 0x88888888 // reuse pull-down input # define usart_cr1_rest (uint32_t) 0x00000000 # define usart_cr2_rest (uint32_t) 0x00000000 # define usart_cr3_rest (uint32_t) 0x00000000
Constant Definition
/*************************************** * ********************************* @ File serial. C * @ author fpack * @ version V1.0 * @ date 2014-9-1 * @ brief alimama serial debugging ******************** **************************************** * **********/# include <stdio. h> # include "armsis. H "/* ------------------------------------------------------------------------------ define baudrate setting (BRR) for usart *----------- --------------------------------------------------------------- */# DEFINE _ Div (_ pclk, _ Baud) (_ pclk * 25)/(4 * _ Baud )) # DEFINE _ divmant (_ pclk, _ Baud) (_ Div (_ pclk, _ Baud)/100) # DEFINE _ divfraq (_ pclk, _ Baud) (_ Div (_ pclk, _ Baud)-(_ divmant (_ pclk, _ Baud) * 100 )) * 16 + 50)/100) # DEFINE _ usart_brr (_ pclk, _ Baud) (_ divmant (_ pclk, _ Baud) <4) | (_ divfraq (_ pclk, _ Baud) & 0x0f )) // Struct _ file {int handle;/* Add whatever you need here */}; // file _ stdout; // file _ stdin; int fputc (int ch, file * f) {// wait until the usart1 data is sent completely (the sending region is blank) while (! (Usart1-> Sr & usart_sr_txe); usart1-> DR = (ch & 0x1ff); Return (CH);} void serial_init (void) {//// set the serial port debugging //// output: usart1 // pin: pa9 (TX), pa10 (RX) // baud rate: 9600 // data bit: 8 bit (default) (CR1) // verification: none (default) (CR1) // stop bit: 1 bit (default) (CR2) // Flow Control: none (default) () /// after clearing the settings, the system is configured as the default int I state. // enable the function to reuse gpioa, enable usart1 RCC-> apb2enr | = rcc_apb2enr_afioen | rcc_apb2enr_iopaen | RCC _ Apb2enr_usart1en; // close the ing to ensure that usart uses pa9, pa10 afio-> mapr & = ~ Afio_mapr_usart1_remap; // clear pa9, pa10 status gpioa-> CRH & = ~ (Optional | gpio_crh_mode9 | gpio_crh_cnf10 | gpio_crh_mode10); // set pa9 transmission to reusable push-pull output 2 MHz gpioa-> CRH |=gpio_cr_afout_pp2mhz & (optional | gpio_crh_mode9 ); // set the pa10 receiver to reuse the pull-down mode gpioa-> CRH | = gpio_cr_afin_pulldown & (gpio_crh_cnf10 | gpio_crh_mode10 ); // set the baud rate to 9600 // calculation method // system clock/(16-Division * baud rate) // baud = 72,000,000/(16*9600) = 468.75 // integer part <4 + INTEGER (decimal part * 16) // 468 <4 + 0.75*16 usart1-> BRR = _ usart_brr (systemcoreclock, 9600); // clear register status usart1-> CR1 = usart_cr1_rest; usart1-> Cr2 = usart_cr2_rest; // stop bit 1 usart1-> Cr 3 = usart_cr3_rest; // No control flow is used // prevent unnecessary information from being generated for (I = 0; I <0x1000; I ++) _ Nop (); // usart1 enabling, enable the output. Enable the input of usart1-> CR1 = usart_cr1_ue | usart_cr1_te | usart_cr1_re ;}
Serial port configuration code after sorting out serial. c
/*************************************** * ********************************* @ File serial. H * @ author fpack * @ version V1.0 * @ date 2014-9-1 * @ brief alimama websocket debugging ******************** **************************************** * **********/# ifndef _ serial_h __# DEFINE _ serial_h __# ifdef _ cplusplus extern "C" {# endifvoid serial_init (void); # ifdef _ cplusplus} # endif
Serial Port debugging header serial. h

 

Serial Communication Based on stm32 debugging Algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.