|
STM32 4個串口同時使用,中斷接收,支持連續接收數據,很好用.
主程序
int main(void)
{
u16 aaa=1;
ChipHalInit(); //片內硬件初始化
ChipOutHalInit(); //片外硬件初始化
GPIO_SetBits(GPIOD,GPIO_Pin_8);
USART1_Puts("USART1 TEST 57600\r\n");
delay(100);
USART2_Puts("USART2 TEST 57600\r\n");
delay(100);
USART3_Puts("USART3 TEST 57600\r\n");
delay(100);
USART4_Puts("UART4 TEST 57600\r\n");
delay(100);
GPIO_ResetBits(GPIOD,GPIO_Pin_8);
delay(50000);
GPIO_Write(GPIOD,0XFFFF);
GPIO_ResetBits(GPIOD,GPIO_Pin_8);
#if 1
while(1)
{
delay(5000);
GPIO_Write(GPIOD,aaa);
if(aaa>=0x8000)
{
aaa = 1;
}
else
aaa = aaa<<1;
}
#endif
}
串口配置程序
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
//使能串口1,PA,AFIO總線
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_AFIO |
RCC_APB2Periph_USART1 ,
ENABLE);
/* A9 USART1_Tx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽輸出-TX
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* A10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入-RX
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = Uart1_Band_Rate_Set;
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;
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;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//使能串口2時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// A2 做T2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// A3 做R2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = Uart2_Band_Rate_Set;
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;
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;
USART_ClockInit(USART2, &USART_ClockInitStructure);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
//串口2使用接收中斷
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
// PB10 做T3X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// PB11 做R3X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = Uart3_Band_Rate_Set;
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;
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;
USART_ClockInit(USART3, &USART_ClockInitStructure);
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
// PB10 做T4X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// PB11 做R4X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = Uart4_Band_Rate_Set;
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;
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;
USART_ClockInit(UART4, &USART_ClockInitStructure);
USART_Init(UART4, &USART_InitStructure);
USART_Cmd(UART4, ENABLE);
USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
}
接收中斷程序
void USART1_IRQHandler(void)
{
//接收中斷
#if 0
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
Uart1_Get_Data=USART_ReceiveData(USART1);
Uart1_Get_Flag=1;
USART1_Putc(Uart1_Get_Data);
}
#endif
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
TIM_Cmd(TIM3, DISABLE);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
UART1_Data_Buff[UART1_Data_Buff_Count++] = USART_ReceiveData(USART1);
if(UART1_Data_Buff[UART1_Data_Buff_Count-1] == 0x0a)
{
if(UART1_Data_Buff[UART1_Data_Buff_Count-2] == 0x0d)
{
UART1_CMD_Process_ALL_Pointer++;
for(UART1_CMD_Process_ALL_Count__ = 0;UART1_CMD_Process_ALL_Count__
{
UART1_CMD_Process_ALL[UART1_CMD_Process_ALL_Pointer][UART1_CMD_Process_ALL_Count__] = UART1_Data_Buff[UART1_CMD_Process_ALL_Count__];
}
UART1_Data_Buff_Count = 0;
UART1_Data_Flag = 1;
}
}
}
//溢出-如果發生溢出需要先讀SR,再讀DR寄存器 則可清除不斷入中斷的問題
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //讀SR
USART_ReceiveData(USART1); //讀DR
}
}
轉http://worldcreativedesign.com/read.php?tid=7
|
|