stm32串口的引入
串口,在STM32F10x芯片一共有5个串口,USART1 2 3 UART4 5 一共5个串口 每一个串口真正发挥作用,2个脚,数据从芯片里出去,另外数据进入到芯片里面去 TX (trasimt) RX (receive) 串口1 USART1 TX PA9 PA10,原理图是这样。是从芯片的使用说明 datasheet看的,是从数据手册引脚描述表格找到的。
UART1的开发流程
- 开启APB2时钟开相关片上外设时钟,USART1 GPIOA,串口除了串口1挂在APB2时钟线上,其余都挂在APB1
- 相关的GPIO口初始化,初始化发送,接收引脚,分别为PA9,PA10
- 初始化UART1的波特率,数据位,停止位等等的配置
- 设置NVIC中断分组,目的是当多个中断发生时的优先级
- 开启中断,在中断函数里面接收发送的函数
- 通过DR寄存器发送函数
stm32的库函数版本
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
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)
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1);
USART_SendData(USART1,Res);
}
}
int main(void)
{
u16 t;
u16 len;
u16 times=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(9600);
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;
printf("\r\n您发送的消息为:\r\n\r\n");
for(t=0;t<len;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
printf("\r\n\r\n");
USART_RX_STA=0;
}
}
串口相关寄存器
1.状态寄存器
2.数据寄存器
3.波特率寄存器
4.控制寄存器1
寄存器版本
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);
mantissa=temp;
fraction=(temp-mantissa)*16;
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2;
RCC->APB2ENR|=1<<14;
GPIOA->CRH&=0XFFFFF00F;
GPIOA->CRH|=0X000008B0;
RCC->APB2RSTR|=1<<14;
RCC->APB2RSTR&=~(1<<14);
USART1->BRR=mantissa;
USART1->CR1|=0X200C;
#if EN_USART1_RX
USART1->CR1|=1<<5;
MY_NVIC_Init(3,3,USART1_IRQn,2);
#endif
}
int main(void)
{
u16 t; u16 len; u16 times=0;
Stm32_Clock_Init(9);
uart_init(72,115200);
delay_init(72);
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3FFF;
printf("\r\n 您发送的消息为:\r\n\r\n");
for(t=0;t<len;t++)
{
USART1->DR=USART_RX_BUF[t];
while((USART1->SR&0X40)==0);
}
printf("\r\n\r\n");
USART_RX_STA=0;
}
}
}
|