USART_InitTypeDef USART_InitStruct;
//system_stm32f4xx.c 316 8
//stm32f4xx.h 123 25000000 8000000
//1.打开对应时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//2.复用GPIO功能
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
//3.UART配置
调用USART_Init()
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
USARTx USART控制接口 取值范围 :USART1~6
uint32_t USART_BaudRate;
功能:配置波特率
寄存器:BBR寄存器
取值范围:115200
uint16_t USART_WordLength;
功能:配置字长
寄存器:CR1寄存器 12位
取值范围:USART_WordLength_8b USART_WordLength_9b
uint16_t USART_StopBits;
功能:配置停止位
寄存器:CR2 12 13寄存器
取值范围:USART_StopBits_1 ..
uint16_t USART_Parity;
功能:奇偶校验
寄存器:CR1 9 10
取值范围:USART_Parity_No 无奇偶校出验位 USART_Parity_Even USART_Parity_Odd
uint16_t USART_Mode;
功能:开启发送和接收
寄存器:CR1 2 3
取值范围:USART_Mode_Rx USART_Mode_Tx
uint16_t USART_HardwareFlowControl;
功能:控制流
寄存器:CR3 8 9
取值范围:USART_HardwareFlowControl_None
代码示例:
//实现透传 通过PC发送一个数据给开发板
//开发板在接收到数据之后,返回给PC
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//1.打开USART控制器时钟
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//PA9 PA10 配置成复用
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
//GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//选择复用关系
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2 );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2 );
------配置USART——-
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//发送和接收都需要打开
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2, &USART_InitStruct);
初始化串口, 对于接收,接收端不知道数据什么时候来,需要打开中断。
/*
USART_ITConfig
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
三个参数
USARTx 1~6
中断类型:USART_IT_RENX
NewState ENABLE DISABLE
*/
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//NVIC管理器
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0;
NVIC_Init(&NVIC_InitStruct);
//USART2串口使能
USART_Cmd(USART2, ENABLE);
USART操作
/*
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
*/
void USART2_IRQHandler(void)
{
u8 data =0;
//1.判断中断类型
/*
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
*/
//SR RXNE只要DR 有数据,该位会自动置1
//当读取该位时候,会自动清零
if(USART_GetITStatus(USART2,USART_IT_RXNE) == SET)
{
data = USART_ReceiveData(USART2);
USART_SendData(USART2, data);
switch(data )
{
case ‘0’:
LED1 = 0;
break;
case ‘1’:
LED1 = 1;
break;
case ‘2’:
BEEP = 1;
break;
case ‘3’:
BEEP = 0;
break;
case ‘4’:
LED2 = 0;
break;
case ‘5’:
LED2 = 1;
break;
case ‘6’:
LED1 = 1;
LED2 = 1;
BEEP = 0;
break;
default:
break;
}
//清空状态
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/269424.html