STM32串列埠除錯進入死迴圈

2021-09-26 21:08:57 字數 4202 閱讀 2015

最近在除錯stm32串列埠,發現串列埠傳送資料總是進入如下死迴圈

while(usart_getflagstatus(uart5, usart_flag_txe) == reset);
經檢查發現串列埠時鐘沒有開,開啟串列埠時鐘後串列埠傳送資料正常

rcc_apb1periphclockcmd(rcc_apb1periph_uart5, enable);	//開啟串列埠時鐘
附上串列埠配置完整**

void uart_init(void)

{ /* 定義結構體變數 */

gpio_inittypedef gpio_initstructure;

usart_inittypedef usart_initstructure;

nvic_inittypedef nvic_initstructure;

/*開啟時鐘 */

rcc_apb2periphclockcmd(rcc_apb2periph_usart1 | rcc_apb2periph_gpioa | rcc_apb2periph_gpiob, enable); //ê¹äüusart1£¬gpioaê±öó

rcc_apb1periphclockcmd(rcc_apb1periph_uart5, enable); //ê¹äüusart1£¬gpioaê±öó

/* 初始化gpio口*/

//rs4851 usart1_tx gpioa.9

gpio_initstructure.gpio_pin = gpio_pin_9;

gpio_initstructure.gpio_speed = gpio_speed_50mhz;

gpio_initstructure.gpio_mode = gpio_mode_af_pp; //¸´óãíæíìêä³ö

gpio_init(gpioa, &gpio_initstructure);//³õê¼»¯gpioa.9

//usart1_rx gpioa.10³õê¼»¯

gpio_initstructure.gpio_pin = gpio_pin_10;//pa10

gpio_initstructure.gpio_mode = gpio_mode_in_floating;//¸¡¿õêäèë

gpio_init(gpioa, &gpio_initstructure);//³õê¼»¯gpioa.10

//rs4851 re/de

gpio_initstructure.gpio_pin = gpio_pin_0;//pb0

gpio_initstructure.gpio_speed = gpio_speed_50mhz;

gpio_initstructure.gpio_mode = gpio_mode_out_pp;// íæíìêä³ö

gpio_init(gpiob, &gpio_initstructure);

//uart5_tx gpioc.12

gpio_initstructure.gpio_pin = gpio_pin_12; //pc12

gpio_initstructure.gpio_speed = gpio_speed_50mhz;

gpio_initstructure.gpio_mode = gpio_mode_af_pp; //¸´óãíæíìêä³ö

gpio_init(gpioc, &gpio_initstructure);//³õê¼»¯pc12

//uart5_rx gpiod.2³õê¼»¯

gpio_initstructure.gpio_pin = gpio_pin_2;//pd2

gpio_initstructure.gpio_mode = gpio_mode_in_floating;//¸¡¿õêäèë

gpio_init(gpiod, &gpio_initstructure);//³õê¼»¯gpiod.2

/* nvicåäöã */

//usart1 nvic åäöã

nvic_initstructure.nvic_irqchannel = usart1_irqn;

nvic_initstructure.nvic_irqchannelpreemptionpriority = 3 ; //çàõ¼óåï輶3

nvic_initstructure.nvic_irqchannelsubpriority = 3; //×óóåï輶3

nvic_initstructure.nvic_irqchannelcmd = enable; //irqí¨µàê¹äü

nvic_init(&nvic_initstructure); //¸ù¾ýö¸¶¨µä²îêý³õê¼»¯vic¼ä´ææ÷

//uart5 nvic åäöã

nvic_initstructure.nvic_irqchannel = uart5_irqn;

nvic_initstructure.nvic_irqchannelpreemptionpriority = 3 ; //çàõ¼óåï輶3

nvic_initstructure.nvic_irqchannelsubpriority = 2; //×óóåï輶2

nvic_initstructure.nvic_irqchannelcmd = enable; //irqí¨µàê¹äü

nvic_init(&nvic_initstructure); //¸ù¾ýö¸¶¨µä²îêý³õê¼»¯vic¼ä´ææ÷

/* usartåäöã */

//usart1 ³õê¼»¯éèöã

usart_initstructure.usart_baudrate = 9600;//´®¿ú²¨ìøâê

usart_initstructure.usart_wordlength = usart_wordlength_8b;//×ö³¤îª8î»êý¾ý¸ñê½

usart_initstructure.usart_stopbits = usart_stopbits_1;//ò»¸öí£ö¹î»

usart_initstructure.usart_parity = usart_parity_no;//îþææå¼ð£ñéî»

usart_initstructure.usart_hardwareflowcontrol = usart_hardwareflowcontrol_none;//îþó²¼þêý¾ýá÷¿øöæ

usart_initstructure.usart_mode = usart_mode_rx | usart_mode_tx; //êõ·¢ä£ê½

usart_init(usart1, &usart_initstructure); //³õê¼»¯´®¿ú1

//uart5 ³õê¼»¯éèöã

usart_initstructure.usart_baudrate = 9600;//´®¿ú²¨ìøâê

usart_initstructure.usart_wordlength = usart_wordlength_8b;//×ö³¤îª8î»êý¾ý¸ñê½

usart_initstructure.usart_stopbits = usart_stopbits_1;//ò»¸öí£ö¹î»

usart_initstructure.usart_parity = usart_parity_no;//îþææå¼ð£ñéî»

usart_initstructure.usart_hardwareflowcontrol = usart_hardwareflowcontrol_none;//îþó²¼þêý¾ýá÷¿øöæ

usart_initstructure.usart_mode = usart_mode_rx | usart_mode_tx; //êõ·¢ä£ê½

usart_init(uart5, &usart_initstructure); //³õê¼»¯´®¿ú5

usart_itconfig(usart1, usart_it_rxne, enable); //¿ªæô´®¿ú1½óêüöð¶ï

usart_itconfig(uart5, usart_it_rxne, enable); //¿ªæô´®¿ú5½óêüöð¶ï

usart_cmd(usart1, enable); //ê¹äü´®¿ú1

usart_cmd(uart5, enable); //ê¹äü´®¿ú5

以上為完成配置**。

STM32串列埠除錯

改為串列埠中斷接收方式,發現也只能進入一次接收中斷函式,把開啟中斷函式放入接收中斷函式內,可以連續進入接收中斷函式了,但是資料丟失的問題仍然存在,又經過一番除錯發現將處理資料函式從接收中斷函式中取出,問題就解決了。然而採用串列埠dma方式時,將處理函式從中斷函式中取出,讀取的資料還是錯誤的。唉,現在...

stm32串列埠通訊

通訊方式 1 同步通訊 帶時鐘同步訊號傳輸 spi,iic等 2 非同步通訊 不帶時鐘用途訊號 usrt,單匯流排 stm32串列埠引腳表 串口號 rxd txd 1 pa10 pa9 2 pa3pa2 3 pb11 pb10 4 pc11 pc10 5 pd2pc12 通訊過程 接收過程 由rx入...

STM32串列埠通訊

1.常用的串列埠相關暫存器 1 usart sr狀態暫存器,對應的庫函式 flagstatus usart getflagstatus usart typedef usartx,uint16 t usart flag 2 usart dr資料暫存器,對應的庫函式 void usart senddat...