新手上路
- 积分
- 32
- 金钱
- 32
- 注册时间
- 2017-8-24
- 在线时间
- 13 小时
|
1金钱
//串口1初始化(形参为可设置的波特率)
//RXD -- PA9 -- USART_TX
//TXD -- PA10 -- USART_RX
void USART1_Init(u32 btl)
{
float div;
int temp;
// u32 pro;
RCC->AHB1ENR |= (1<<0); //使能GPIOA
RCC->APB2ENR |= (1<<4); //使能USART1时钟源
GPIOA->MODER &= ~(0XF<<18);
GPIOA->MODER |= (0XA<<18); //复用功能模式
GPIOA->AFR[1] &= ~(0XFF<<4);
GPIOA->AFR[1] |= (0X77<<4); //AF7 PA9/PA10 与USART1进行链接
GPIOA->OTYPER &= ~(1<<9); //推挽输出
GPIOA->PUPDR &= ~(0X3<<18);
GPIOA->PUPDR |= (0X1<<18); //上拉
GPIOA->PUPDR &= ~(0X3<<20);
GPIOA->PUPDR |= (0X1<<20); //上拉
div =(float) 84000000 / (16 * btl); //设置波特率Tx/Rx 波特率Tx/Rx = fCK/ 8 x (2-OVER8) x USARTDIV
temp =(int) div;
div = div - temp; //小数部分
USART1->BRR = (temp << 4) + (div * 16); //填入波特率寄存器
USART1->CR1 = 0;
USART1->CR1 |= (1<<2); //RE:接收器使能
USART1->CR1 |= (1<<3); //TE:发送器使能
USART1->CR1 |= (1<<13); //使能USART
USART1->CR1 |= (1<<5); // RXNEIE:串口接收中断使能 外设的配置
// NVIC_SetPriorityGrouping(5); //设置优先级分组
// pro = NVIC_EncodePriority(5, 1, 2); //根据优先级组别和优先级的级别,得到最终设置的值
// NVIC_SetPriority(USART1_IRQn, pro); //设置优先级寄存器
NVIC_EnableIRQ(USART1_IRQn); //使能内核里面,串口对应的中断通道
}
void USART1_IRQHandler(void)
{
static u8 i = 0;
u8 tmp;
tmp = USART1->DR;
if(tmp != '#')
{
ch_data[i] = tmp;
i++;
}
else
{
ch_data[i] = '\0';
revch_state = i;
i = 0;
}
}
|
|