stm32雙串列埠收發
阿新 • • 發佈:2019-02-14
#include "sys.h"
#include "usart.h"
#include "delay.h"
u8 usart1_buf[100]={0},usart2_buf[100]={0},usart3_buf[100]={0};
u16 index1=0,index2=0,index3=0,flag1=0,flag2=0,flag3=0;
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //使能USART1,GPIOA時鐘
/*************UART1********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//搶佔優先順序3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子優先順序3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的引數初始化VIC暫存器
USART_InitStructure.USART_BaudRate = bound;//串列埠波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位資料格式
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_Init(USART1, &USART_InitStructure); //初始化串列埠1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啟串列埠接受中斷
USART_Cmd(USART1, ENABLE); //使能串列埠1
/***************UART2******************/
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);//初始化GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//搶佔優先順序3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子優先順序3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的引數初始化VIC暫存器
//USART 初始化設定
USART_InitStructure.USART_BaudRate = bound;//串列埠波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位資料格式
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_Init(USART2, &USART_InitStructure); //初始化串列埠2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//開啟串列埠接受中斷
USART_Cmd(USART2, ENABLE); //使能串列埠2
/****************UART3***********************/
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB.10
//USART3_RX GPIOB.11初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB.11
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //搶佔優先順序4
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優先順序3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的引數初始化VIC暫存器
//USART3 初始化設定
USART_InitStructure.USART_BaudRate = 115200; //串列埠波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長為8位資料格式
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_Init(USART3, &USART_InitStructure); //初始化串列埠3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //開啟串列埠接受中斷
USART_Cmd(USART3, ENABLE);
}
void USART1_IRQHandler(void)
{
u16 code;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//Removal of receiving interrupt flag
code=USART_ReceiveData(USART1);
usart1_buf[index1] = code;
index1++;
if(code == 0x0a)
{
index1 = 0;
flag1 = 1;
}
}
}
void USART2_IRQHandler(void)
{
u16 code;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
code=USART_ReceiveData(USART2);
usart2_buf[index2] = code;
index2++;
if(code == 0x0a)
{
index2 = 0;
flag2 = 1;
}
}
}
void USART3_IRQHandler(void)
{
u16 code;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
code=USART_ReceiveData(USART3);
usart3_buf[index3] = code;
index3++;
if(code == 0x0a)
{
index3 = 0;
flag3 = 1;
}
}
}
void USART1_Send(u8 *str)
{
while(*str!=0x0A)
{
USART_GetFlagStatus(USART1, USART_FLAG_TC);
USART_SendData(USART1, *str++);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
}
USART_SendData(USART1, 0x0a);
}
void USART2_Send(u8 *str)
{
while(*str!=0x0A)
{
USART_GetFlagStatus(USART2, USART_FLAG_TC);
USART_SendData(USART2, *str++);
while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
}
USART_SendData(USART2, 0x0a);
}
void USART3_Send(u8 *str)
{
while(*str!=0x0A)
{
USART_GetFlagStatus(USART3, USART_FLAG_TC);
USART_SendData(USART3, *str++);
while( USART_GetFlagStatus(USART3,USART_FLAG_TC)!= SET);
}
USART_SendData(USART3, 0x0a);
}
/*******************main***********************/
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "buzzer.h"
#include "string.h"
int main(void)
{
u8 Zigb_Head[]="ZigB:";
u8 buf[100];
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
Buzzer_Init();
LED_Init();
while(1)
{
if(flag2==1)
{
LED0=0;
flag2=0;
USART3_Send(usart2_buf);
memset(usart2_buf,0,sizeof(usart2_buf));
}
else if(flag3==1)
{
LED1=0;
flag3=0;
memcpy(buf,Zigb_Head,sizeof(Zigb_Head));
strcat(buf,usart3_buf);
USART2_Send(buf);
memset(buf,0,sizeof(buf));
}
delay_ms(500);
LED1=1;
LED0=1;
delay_ms(500);
}
}