標籤:ati 變數 平台 length i++ float dem else isa
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
STM32串口中斷執行個體二