void usart3_send_str(char *pbuf)
{
while(pbuf && *pbuf)
{
USART_SendData(USART3,*pbuf++);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
}
}
void ble_set_config(void)
{
//配置模块的名字
usart3_send_str("AT+NAMETeacher.Wen\r\n");
delay_ms();
}
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"
#include "stdio.h"
static GPIO_InitTypeDef GPIO_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
void delay_us(uint32_t nus)
{
uint32_t temp;
SysTick->LOAD =SystemCoreClock//*nus; //时间加载
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //使能滴答定时器开始倒数
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(<<))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
void delay_ms(uint16_t nms)
{
uint32_t temp;
SysTick->LOAD=SystemCoreClock//*nms; //时间加载(SysTick->LOAD为24bit)
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //能滴答定时器开始倒数
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(<<))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
void LED_Init(void)
{
//使能GPIOE,GPIOF时钟
RCC\_AHB1PeriphClockCmd(RCC\_AHB1Periph\_GPIOE | RCC\_AHB1Periph\_GPIOF, ENABLE);
//GPIOF9,F10初始化设置
GPIO\_InitStructure.GPIO\_Pin = GPIO\_Pin\_9 | GPIO\_Pin\_10; //LED0和LED1对应IO口
GPIO\_InitStructure.GPIO\_Mode = GPIO\_Mode\_OUT; //普通输出模式,
GPIO\_InitStructure.GPIO\_OType = GPIO\_OType\_PP; //推挽输出,驱动LED需要电流驱动
GPIO\_InitStructure.GPIO\_Speed = GPIO\_Speed\_100MHz; //100MHz
GPIO\_InitStructure.GPIO\_PuPd = GPIO\_PuPd\_UP; //上拉
GPIO\_Init(GPIOF, &GPIO\_InitStructure); //初始化GPIOF,把配置的数据写入寄存器
//GPIOE13,PE14初始化设置
GPIO\_InitStructure.GPIO\_Pin = GPIO\_Pin\_13 | GPIO\_Pin\_14; //LED2和LED3对应IO口
GPIO\_InitStructure.GPIO\_Mode = GPIO\_Mode\_OUT; //普通输出模式
GPIO\_InitStructure.GPIO\_OType = GPIO\_OType\_PP; //推挽输出
GPIO\_InitStructure.GPIO\_Speed = GPIO\_Speed\_100MHz; //100MHz
GPIO\_InitStructure.GPIO\_PuPd = GPIO\_PuPd\_UP; //上拉
GPIO\_Init(GPIOE, &GPIO\_InitStructure); //初始化GPIOE,把配置的数据写入寄存器
GPIO\_SetBits(GPIOF,GPIO\_Pin\_9 | GPIO\_Pin\_10); //GPIOF9,PF10设置高,灯灭
GPIO\_SetBits(GPIOE,GPIO\_Pin\_13 | GPIO\_Pin\_14);
}
void USART1_Init(uint32_t baud)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //使能USART1时钟
//串口1对应引脚复用映射
GPIO\_PinAFConfig(GPIOA,GPIO\_PinSource9,GPIO\_AF\_USART1); //GPIOA9复用为USART1
GPIO\_PinAFConfig(GPIOA,GPIO\_PinSource10,GPIO\_AF\_USART1); //GPIOA10复用为USART1
//USART1端口配置
GPIO\_InitStructure.GPIO\_Pin = GPIO\_Pin\_9 | GPIO\_Pin\_10; //GPIOA9与GPIOA10
GPIO\_InitStructure.GPIO\_Mode = GPIO\_Mode\_AF; //复用功能
GPIO\_InitStructure.GPIO\_Speed = GPIO\_Speed\_50MHz; //速度50MHz
GPIO\_InitStructure.GPIO\_OType = GPIO\_OType\_PP; //推挽复用输出
GPIO\_InitStructure.GPIO\_PuPd = GPIO\_PuPd\_UP; //上拉
GPIO\_Init(GPIOA,&GPIO\_InitStructure); //初始化PA9,PA10
//USART1 初始化设置
USART\_InitStructure.USART\_BaudRate = baud; //波特率设置
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(USART1, &USART\_InitStructure); //初始化串口1
USART\_Cmd(USART1, ENABLE); //使能串口1
USART\_ITConfig(USART1, USART\_IT\_RXNE, ENABLE); //开启相关中断
//Usart1 NVIC 配置
NVIC\_InitStructure.NVIC\_IRQChannel = USART1\_IRQn; //串口1中断通道
NVIC\_InitStructure.NVIC\_IRQChannelPreemptionPriority=; //抢占优先级3
NVIC\_InitStructure.NVIC\_IRQChannelSubPriority =; //子优先级3
NVIC\_InitStructure.NVIC\_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC\_Init(&NVIC\_InitStructure); //根据指定的参数初始化VIC寄存器
}
void USART3_Init(uint32_t baud)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //使能USART3时钟
//串口3对应引脚复用映射
GPIO\_PinAFConfig(GPIOB,GPIO\_PinSource10,GPIO\_AF\_USART3); //GPIOB10复用为USART3
GPIO\_PinAFConfig(GPIOB,GPIO\_PinSource11,GPIO\_AF\_USART3); //GPIOB11复用为USART3
//USART1端口配置
GPIO\_InitStructure.GPIO\_Pin = GPIO\_Pin\_10 | GPIO\_Pin\_11; //GPIOB10与GPIOB11
GPIO\_InitStructure.GPIO\_Mode = GPIO\_Mode\_AF; //复用功能
GPIO\_InitStructure.GPIO\_Speed = GPIO\_Speed\_50MHz; //速度50MHz
GPIO\_InitStructure.GPIO\_OType = GPIO\_OType\_PP; //推挽复用输出
GPIO\_InitStructure.GPIO\_PuPd = GPIO\_PuPd\_UP; //上拉
GPIO\_Init(GPIOB,&GPIO\_InitStructure); //初始化PB10,PB11
//USART1 初始化设置
USART\_InitStructure.USART\_BaudRate = baud; //波特率设置
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(USART3, &USART\_InitStructure); //初始化串口3
USART\_Cmd(USART3, ENABLE); //使能串口3
USART\_ITConfig(USART3, USART\_IT\_RXNE, ENABLE); //开启相关中断
//Usart3 NVIC 配置
NVIC\_InitStructure.NVIC\_IRQChannel = USART3\_IRQn; //串口3中断通道
NVIC\_InitStructure.NVIC\_IRQChannelPreemptionPriority=; //抢占优先级3
NVIC\_InitStructure.NVIC\_IRQChannelSubPriority =; //子优先级3
NVIC\_InitStructure.NVIC\_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC\_Init(&NVIC\_InitStructure); //根据指定的参数初始化VIC寄存器
}
//重定义fputc
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
void usart1_send_bytes(uint8_t *pbuf,uint32_t len)
{
while(len--)
{
USART_SendData(USART1,*pbuf++);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
void usart1_send_str(char *pbuf)
{
while(pbuf && *pbuf)
{
USART_SendData(USART1,*pbuf++);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
void usart3_send_bytes(uint8_t *pbuf,uint32_t len)
{
while(len--)
{
USART_SendData(USART3,*pbuf++);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
}
}
void usart3_send_str(char *pbuf)
{
while(pbuf && *pbuf)
{
USART_SendData(USART3,*pbuf++);
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);
}
}
void ble_set_config(void)
{
//配置模块的名字
usart3_send_str("AT+NAMEHELLO\r\n");
delay_ms();
}
int main(void)
{
LED\_Init();
//系统定时器初始化,时钟源来自HCLK,且进行8分频,
//系统定时器时钟频率=168MHz/8=21MHz
SysTick\_CLKSourceConfig(SysTick\_CLKSource\_HCLK\_Div8);
//设置中断优先级分组2
NVIC\_PriorityGroupConfig(NVIC\_PriorityGroup\_2);
//串口1,波特率115200bps,开启接收中断
USART1\_Init();
printf("hello ble at test\\r\\n");
//串口3,波特率9600bps,开启接收中断,蓝牙模块出厂设置为9600bps
USART3\_Init();
ble\_set\_config();
while()
{
}
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
uint8_t d;
if(USART\_GetITStatus(USART1, USART\_IT\_RXNE) != RESET) //接收中断
{
//接收数据
d = USART\_ReceiveData(USART1);
//发送数据
usart3\_send\_bytes(&d,);
}
}
void USART3_IRQHandler(void) //串口3中断服务程序
{
uint8_t d;
if(USART\_GetITStatus(USART3, USART\_IT\_RXNE) != RESET) //接收中断
{
//接收数据
d = USART\_ReceiveData(USART3);
//发送数据
usart1\_send\_bytes(&d,);
}
}
main.c
手机扫一扫
移动阅读更方便
你可能感兴趣的文章