基于stm32f1的lora开发基础通信实验
一、实验条件
环境:
win10专业版;
keil uv5 ;
串口调试助手;
stm32f1系列单片机;
as32lora模块(这里是泽耀科技的as32_ttl_100)
二、实现功能
1.使用定时器将底层数据通过as32每隔1秒向上层串口发送一段数据帧
2.上层串口可通过as32像底层发送数据
三、实验讲解
1.配置定时器
void Timer_Config(u16 arr,u16 psc)//1s中断
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //定义TIM结构体变量
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //使能TIM2外设
TIM_DeInit(TIM2); //复位时钟TIM2,恢复到初始状态
// 定时时间T计算公式:
// T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK=(3599+1)*(99+1)/72MHz=5us
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //TIM2时钟分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //计数方式
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //初始化
TIM_ClearFlag(TIM2,TIM_FLAG_Update); //清除标志
// 中断方式下,使能中断源
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //使能中断源
//TIM_Cmd(TIM2,ENABLE); //放MIAN里面 //使能TIM2
// 设置优先分级组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //0组,全副优先级
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //选择中断通道,库P166页,
// 选择中断通道。注意:固件库中为XXX_IRQChannel,但该程序预定义为XXX_IRQn,所以要特别注意
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; //响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //启动此通道的中断
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2,ENABLE);//开启定时器
}
2.配置as引脚
void usart3_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
//USART3_TX B.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX B.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART3, &USART_ClockInitStructure);
//---------------------------------------------------------------------------------
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART3, ENABLE); //使能串口
}
3.配置串口,串口中断
void USART3_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)/// 接收到数据
{
Res =USART_ReceiveData(USART3);
RS232_RX_BUF[RS232_RX_STA]=Res; //读取数据
RS232_RX_STA++; //ESP_RX_STA计数,收到多少数据
if(RS232_RX_STA>=6) {
DealBufproc();
}
}
}
4.看门狗配置(防止程序跑死)
//以下是看门狗,作用是防止程序跑死(跑死程序重启)
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //使能对寄存器IWDG_PR和IWDG_RLR的写操作
IWDG_SetPrescaler(prer); //设置IWDG预分频值:设置IWDG预分频值为64
IWDG_SetReload(rlr); //设置IWDG重装载值
IWDG_ReloadCounter(); //按照IWDG重装载寄存器的值重装载IWDG计数器
IWDG_Enable(); //使能IWDG
}
//喂独立看门狗
void IWDG_Feed(void)
{
IWDG_ReloadCounter();//reload
主函数:
int main(void)
{
Delay_Init(72);//用定时器延时
usart1_init(9600);
usart3_init(9600);
Timer_Config(4999,7199);//1s触发一次中断
IWDG_Init(4,625);//看门狗溢出时间1s
while(1)
{
//IWDG_Feed();//重置看门狗溢出时间
Delay_100ms(5);
}
}
四、实验效果
上层串口调试助手实际接收数据:
底层接收数据串口1提示信息:
五、注意点
- 串口乱码问题:检查波特率和串口配置,必须十六进制显示
- 串口接收数据格式:as必须采用十六进制接收和发送
- 数据丢失问题:as32不支持双向通信,同时进行收发就会造成数据的丢失,可以采用as62.这个是双向通信的。
一键三连呀!