日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(3)stm32开发之串口的调试

發布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (3)stm32开发之串口的调试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天把stm32的串口配置起來,把經驗跟大家共享下

?

總的函數如下

void USART1Configuration(void)
{
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,ENABLE);
??? USART1_GPIO_Configuration();
??? USART1_Param_Configuration();
??? NVIC_Configuration();? /*串口使用中斷方式才用到*/
??? //打開發送接收中斷
??? USART_ITConfig(USART1, USART_IT_TXE, ENABLE);?
??? USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
??? USART_Cmd(USART1,ENABLE); /*使能串口*/

}


時間配置就不講了
首先是配置USART的GPIO口
/*******************************************************************************
* Name : UART1_GPIO_Configuration
* Deion : Configures the uart1 GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/

void USART1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Configure USART1_Tx as alternate push-pull
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);

// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

然后是配置串口參數
/*******************************************************************************
* Name : UART1_Configuration
* Deion : Configures the uart1
* Input : None
* Output : None
* Return : None
*******************************************************************************/

void USART1_Param_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure);

USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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;
/* Configure USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
}


然后是在中斷設置,需要修改stm32f10x_it.c 中的串口中斷函數 并且需要修改void NVIC_Configuration(void)函數

修改NVIC_Configuration函數
/*******************************************************************************
* Name : NVIC_Configuration
* Deion : Configures NVIC and Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//

/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;? /*這個串口中斷入口可以查詢stm32f10.h得到看有哪些中斷入口*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

?

串口中斷處理函數文件

/* Private macro -------------------------------------------------------------*/
#define countof(a)?? (sizeof(a) / sizeof(*(a)))

/* Private variables ---------------------------------------------------------*/
uint8_t TxBuffer[160];
uint8_t RxBuffer[RxBufferSize];
uint8_t NbrOfDataToTransfer = TxBufferSize;
uint8_t NbrOfDataToRead = RxBufferSize;
uint8_t TxCounter = 0;
uint16_t RxCounter = 0;


//串口中斷

void USART1_IRQHandler(void)
{
??? /*接收中斷*/
??? u16 i;
????
??? if(USART_GetFlagStatus(USART1,USART_IT_RXNE)== SET)
??? {??????????????
??????? i = USART_ReceiveData(USART1);
??????? USART_SendData(USART1,i);???? /*回顯*/
??????? while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
??????? {
??????? }???????????????
??? }

???
??? //發送中斷,很少用到
??? if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
??? {
??????? USART_SendData(USART1, TxBuffer[TxCounter++]);
??????? if (NbrOfDataToTransfer==TxCounter)?? /*最后一個字節不要,因為字符串最后一個是\0*/
??????? {
??????????? //發送字節結束
??????????? USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
??????? }
??? }
???
}

?


至此 串口就可以工作起來了,但是還有很重要一點,就是編譯時要打開target-Use ?MicroLIB勾上,就可以了。

發送中斷應該說有兩個一個是發送完中斷,一個是緩沖區空中斷,你說的問題應該是發生在緩沖區空中斷中.
當發送一幀數據時,如果發生緩沖區空中斷或發送完畢中斷時,還有數據要繼續發送,那就發送下一個數據,如果數據已經都發送完了,則處理方式上有點區別,如果用緩沖區空中斷,則而將中斷關閉,如果用發送完畢中斷,則直接返回就可以了

?

但是要用到printf來打印到串口1的話,需要對printf重定向到usart1,printf函數不用到中斷

?/*******************************************************************************
?* Function Name?? : int fputc(int ch, FILE *f)
?* Description???? : Retargets the C library printf function to the USART.printf重定向
?* Input?????????? : None
?* Output????????? : None
?* Return????????? : None
?*******************************************************************************/
?int fputc(int ch, FILE *f)
?{

??? /* Write a character to the USART */
??? USART_SendData(USART1, (u8) ch);
???? /* Loop until the end of transmission */
??? while((USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET))
??? {
??? }????
??? return ch;
?}
?


?/*******************************************************************************
?* Function Name?? : int fgetc(FILE *f)
?* Description???? : Retargets the C library printf function to the USART.fgetc重定向
?* Input?????????? : None
?* Output????????? : None
?* Return????????? : 讀取到的字符
?*******************************************************************************/
?int fgetc(FILE *f)
?{
??? /* Loop until received a char */
??? while((USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET))
??? {
??? }
??
????? /* Read a character from the USART and RETURN */
??? return (USART_ReceiveData(USART1));
?}

到這里,usart1就能打印出printf的內容了

總結

以上是生活随笔為你收集整理的(3)stm32开发之串口的调试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。