int main(void) { uint8_t a=0;//LED高低電壓控制 /* System Clocks Configuration */ RCC_Configuration(); //系統時鍾設置 /*嵌套向量中斷控制器 說明了USART1搶占優先級級別0(最多1位) ,和子優先級級別0(最多7位) */ NVIC_Configuration(); //中斷源配置 /*對控制LED指示燈的IO口進行了初始化,將端口配置為推挽上拉輸出,口線速度為50Mhz。PA9,PA10端口復用為串口1的TX,RX。 在配置某個口線時,首先應對它所在的端口的時鍾進行使能。否則無法配置成功,由於用到了端口B, 因此要對這個端口的時鍾 進行使能,同時由於用到復用IO口功能用於配置串口。因此還要使能AFIO(復用功能IO)時鍾。*/ GPIO_Configuration(); //端口初始化 USART_Config(USART1); //串口1初始化 while (1) { if(rec_f == 1) { //判斷是否收到一幀有效數據 rec_f = 0; for(i=0; i<sizeof(TxBuffer1); i++) //發送字符串 { USART_SendChar(USART1,TxBuffer1[i]); Delay(0x0000ff0); } /*for(i=0;i<TxCounter1;i++)//發送字符串 { USART_SendChar(USART1,RxBuffer1[i]); } */ if(a==0) { GPIO_SetBits(GPIOD, GPIO_Pin_2); //LED1 明暗閃爍 a=1; } else { GPIO_ResetBits(GPIOD, GPIO_Pin_2); a=0; } } } }
main函數如上。
相關變量
uint8_t TxBuffer1[] = "USART Interrupt Example: This is USART1 DEMO"; uint8_t RxBuffer1[], rec_f, tx_flag, i; __IO uint8_t TxCounter1 = 0x00; __IO uint8_t RxCounter1 = 0x00; uint32_t Rec_Len;
串口中斷函數配置如下所示:
//-------------------------------------------------------------------------------------- void USART_SendChar(USART_TypeDef* USARTx,uint8_t data) { USART_SendData(USARTx,data); while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET); } //-------------------------------------------------------------------------------------- void USART_Config(USART_TypeDef* USARTx) { USART_InitStructure.USART_BaudRate = 19200; //速率19200bps USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數據位8位 USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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 USARTx */ USART_Init(USARTx, &USART_InitStructure); //配置串口參數函數 /* Enable USARTx Receive and Transmit interrupts */ USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); //使能接收中斷 USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); //使能發送緩沖空中斷 /* Enable the USARTx */ USART_Cmd(USARTx, ENABLE); } //-------------------------------------------------------------------------------------- void Delay(__IO uint32_t nCount) { for(; nCount!=0; nCount--); } /*-------------------------------------------------------------------------------------- 系統時鍾配置為72MHZ+外設時鍾配置*/ void RCC_Configuration(void) { SystemInit(); RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOD |RCC_APB2Periph_GPIOA, ENABLE); } //-------------------------------------------------------------------------------------- void GPIO_Configuration(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1控制--PD2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出 GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //復用開漏輸入 GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口 } //-------------------------------------------------------------------------------------- void NVIC_Configuration(void) { /* 結構聲明*/ NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ /* Configure one bit for preemption priority */ /* 優先級組 說明了搶占優先級所用的位數,和子優先級所用的位數 在這里是1, 7 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //設置串口1中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優先級 0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子優先級為0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能 NVIC_Init(&NVIC_InitStructure); }
在中斷服務函數中編寫usart函數。
//串口1 中斷服務程序 void USART1_IRQHandler(void) { unsigned int i; //判斷讀寄存器是否非空 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { //將讀寄存器的數據緩存到接收緩沖區里 RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1); //判斷結束標志是否是0x0d 0x0a if(RxBuffer1[RxCounter1-2] == 0x0d && RxBuffer1[RxCounter1-1] == 0x0a) { for(i=0; i< RxCounter1; i++) TxBuffer1[i] = RxBuffer1[i]; //將接收緩沖器的數據轉到發送緩沖區,准備轉發 //接收成功標志 rec_f = 1; //發送緩沖區結束符 TxBuffer1[RxCounter1] = 0; TxCounter1 = RxCounter1; RxCounter1 = 0; } } //這段是為了避免STM32 USART 第一個字節發不出去的BUG if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { //禁止發緩沖器空中斷, USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } }
運行結果如下,在發送去不填寫任何字符,直接發送,顯示RT Interrupt Example: This is USART1 DEMO,說明前三個字符已經被占用替換了。
試驗平台alienteck mini stm32V1.9 stm32f103rbt6