(3)stm32开发之串口的调试
今天把stm32的串口配置起來(lái),把經(jīng)驗(yàn)跟大家共享下
?
總的函數(shù)如下
void USART1Configuration(void)
{
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,ENABLE);
??? USART1_GPIO_Configuration();
??? USART1_Param_Configuration();
??? NVIC_Configuration();? /*串口使用中斷方式才用到*/
??? //打開發(fā)送接收中斷
??? USART_ITConfig(USART1, USART_IT_TXE, ENABLE);?
??? USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
??? USART_Cmd(USART1,ENABLE); /*使能串口*/
}
時(shí)間配置就不講了
首先是配置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);
}
然后是配置串口參數(shù)
/*******************************************************************************
* 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);
}
然后是在中斷設(shè)置,需要修改stm32f10x_it.c 中的串口中斷函數(shù) 并且需要修改void NVIC_Configuration(void)函數(shù)
修改NVIC_Configuration函數(shù)
/*******************************************************************************
* 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;? /*這個(gè)串口中斷入口可以查詢stm32f10.h得到看有哪些中斷入口*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
?
串口中斷處理函數(shù)文件
/* 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)
??????? {
??????? }???????????????
??? }
???
??? //發(fā)送中斷,很少用到
??? if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
??? {
??????? USART_SendData(USART1, TxBuffer[TxCounter++]);
??????? if (NbrOfDataToTransfer==TxCounter)?? /*最后一個(gè)字節(jié)不要,因?yàn)樽址詈笠粋€(gè)是\0*/
??????? {
??????????? //發(fā)送字節(jié)結(jié)束
??????????? USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
??????? }
??? }
???
}
?
至此 串口就可以工作起來(lái)了,但是還有很重要一點(diǎn),就是編譯時(shí)要打開target-Use ?MicroLIB勾上,就可以了。
發(fā)送中斷應(yīng)該說(shuō)有兩個(gè)一個(gè)是發(fā)送完中斷,一個(gè)是緩沖區(qū)空中斷,你說(shuō)的問(wèn)題應(yīng)該是發(fā)生在緩沖區(qū)空中斷中.
當(dāng)發(fā)送一幀數(shù)據(jù)時(shí),如果發(fā)生緩沖區(qū)空中斷或發(fā)送完畢中斷時(shí),還有數(shù)據(jù)要繼續(xù)發(fā)送,那就發(fā)送下一個(gè)數(shù)據(jù),如果數(shù)據(jù)已經(jīng)都發(fā)送完了,則處理方式上有點(diǎn)區(qū)別,如果用緩沖區(qū)空中斷,則而將中斷關(guān)閉,如果用發(fā)送完畢中斷,則直接返回就可以了
?
但是要用到printf來(lái)打印到串口1的話,需要對(duì)printf重定向到usart1,printf函數(shù)不用到中斷
?/*******************************************************************************
?* 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的內(nèi)容了
總結(jié)
以上是生活随笔為你收集整理的(3)stm32开发之串口的调试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 比亚迪秦 PLUS DM-i 2023
- 下一篇: verilog的学习