void usart_config(u32 BaudRate)

void usart_config(u32 BaudRate)
{
// // 数据发送过程
// RCC->APB2RSTR &= ~(1<<14); //USART1RST置零,USART1复位 (USART1 reset)
// //1.通过在USART_CR1寄存器上置位UE位来激活USART;
// USART1->CR1 |= 1<<13; //UE位置1:USART模块使能;
// //2.编程USART_CR1的M位来定义字长;
// USART1->CR1 &= ~(1<<12); //M位置0:一个起始位,8个数据位,n个停止位;
// //3.在USART_CR2中编程停止位的位数;
// USART1->CR2 &= ~(3<<12); //STOP: 停止位,00:1个停止位;
// //4.如果采用多缓冲器通信,配置USART_CR3中得DMA使能位(DMAT),
// //  按多缓冲器通信中的描述配置DMA寄存器;
// //5.设置USART_CR1中的TE位,发送一个空闲帧为第一次数据发送;
// USART1->CR1 |= 1<<3; //TE: 发送使能,1:发送被使能。
// //6.利用USART_BRR寄存器选择要求的波特率。
// //设置波特率为9600  
// USART1->BRR = mantissa;
// //7.把要发送的数据写进USART_DR寄存器(此动作清除TXE位);
// //发送一个数据“A”
// //在只有一个缓冲器的情况下,对每个待发送的数据重复步骤7.


USART_InitTypeDef USART_InitStructure;


//开启USART1EN:USART1时钟使能 (USART1 clock enable)
RCC->APB2ENR |= 1<<14; //置位USART1EN1:USART1时钟开启。


/*--配置USART1,
6.利用USART_BRR寄存器选择要求的波特率。波特率:9600,
2.编程USART_CR1的M位来定义字长;8位数据位,
3.在USART_CR2中编程停止位的位数;1位停止位,
奇偶校验关闭--*/
USART_InitStructure.USART_BaudRate = BaudRate; //设置波特率为9600 
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置8位数据位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置1位停止位
USART_InitStructure.USART_Parity = USART_Parity_Odd;   //设置奇偶校验关闭
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //设置硬件流控制关闭
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送使能,接收使能
// USART_InitStructure.USART_Clock = USART_Clock_Enable;
// USART_InitStructure.USART_CPOL = USART_CPOL_High;
// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
// USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1, &USART_InitStructure); //根据上述设置寄存器,配置USART1


/*--1.通过在USART_CR1寄存器上置位UE位来激活USART--*/
USART_Cmd(USART1, ENABLE);

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void USART2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* config USART2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);//¿ªÆôGPIOAʱÖÓ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//¿ªÆôUSART2ʱÖÓ /* USART2 GPIO config */ /* Configure USART2 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//¸´ÓÃÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART2 mode config */ 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(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); /*Enable usart2 receive interrupt*/ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÇÀÕ¼ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÏìÓ¦ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
05-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值