这里只使用了两根线的最简单串口设置。
硬件环境:STM32F4-Discovery
软件环境:MDK4.7a
实现的功能:1、串口初始化,
2、通过串口发送数据
3、中断方式接收数据,并将接收到的数据回送。
使用库函数操作
首先,配置NVIC使用NVIC_PriorityGroupConfig()设置优先级分组,使用NVIC_Init()对NVIC进行初始化
-
void NVIC_Config()
-
{
-
NVIC_InitTypeDef NVIC_InitStructure;
-
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
-
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
-
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
-
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
-
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
-
-
NVIC_Init(&NVIC_InitStructure);
-
}
-
void USART_Gpio_Config(void)
-
{
-
GPIO_InitTypeDef GPIO_InitStructure;
-
-
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE);
-
-
//PB6->TX PB7->Rx
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
-
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
-
}
-
void USART_Config(void)
-
{
-
USART_Gpio_Config();
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
-
-
USART_InitStructure.USART_BaudRate = 115200;
-
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_Init(USART1,&USART_InitStructure);
-
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
-
-
USART_Cmd(USART1,ENABLE);
-
-
}
-
void USART1_IRQHandler(void)
-
{
-
char c;
-
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
-
{
-
c = USART_ReceiveData(USART1);
-
USART_SendData(USART1,c);
-
}
-
//while(1);
-
}
-
int main(void)
-
{
-
NVIC_Config();
-
USART_Config();
-
while(1)
-
{
-
while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE));
-
USART_SendData(USART1,'b');
-
while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE));
-
USART_SendData(USART1,'a');
-
delay_ms(1000);
-
}
- }