配置4g模块事前准备:需要一个开发板,就比如串口3为4g模块用来和串口1互发。
usart1.c
#include "usart1.h"
#include "usart2.h"
#include "usart3_4g.h"
#include "string.h"
uint8_t U1_R_Buff[512];
uint8_t U1_R_Length=0;
uint8_t U1_R_IDLE=0;//等0说明还在发送 1代表发送完毕
/*
PA9 TX 复用推挽输出
PA10 RX 浮空输入
波特率 115200
位数 8bit
停止位 1
无校验
*/
#define GPIO_USART1_Pin_TX GPIO_Pin_9
#define GPIO_USART1_Pin_RX GPIO_Pin_10
void USART1_Config(void)
{
//1.开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//2.创建功能结构体
GPIO_InitTypeDef GPIO_Iint_Usart1_Struct;
//3.结构体赋能 TX
GPIO_Iint_Usart1_Struct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Iint_Usart1_Struct.GPIO_Pin = GPIO_USART1_Pin_TX;
GPIO_Iint_Usart1_Struct.GPIO_Speed = GPIO_Speed_50MHz;
//TX初始化
GPIO_Init(GPIOA,&GPIO_Iint_Usart1_Struct);
//4.结构体赋能 RX
GPIO_Iint_Usart1_Struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Iint_Usart1_Struct.GPIO_Pin = GPIO_USART1_Pin_RX;
//RX初始化
GPIO_Init(GPIOA,&GPIO_Iint_Usart1_Struct);
//5.开串口1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//6.创建串口功能结构体
USART_InitTypeDef USART1_Iint_Struct;
//7.配置串口功能
USART1_Iint_Struct.USART_BaudRate = 9600;
USART1_Iint_Struct.USART_HardwareFlowControl =USART_HardwareFlowControl_None;//无硬件流控制
USART1_Iint_Struct.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;//发送接收
USART1_Iint_Struct.USART_Parity = USART_Parity_No;//无校验
USART1_Iint_Struct.USART_StopBits = USART_StopBits_1; //1个停止位
USART1_Iint_Struct.USART_WordLength = USART_WordLength_8b;//八个数据位
//8.串口功能初始化
USART_Init(USART1,&USART1_Iint_Struct);
//9.串口使能
USART_Cmd(USART1,ENABLE);
//配置串口中断
USART1_NVIC_Config();
}
//串口中断
void USART1_NVIC_Config(void)
{
//1.定义结构体
NVIC_InitTypeDef NVIC_InitStruct;
//2.结构体赋值
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn; //中断通道
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能对应的中断
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //次级优先级
//3.中断初始化
NVIC_Init(&NVIC_InitStruct);
//4.开启对应的中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启接收中断
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);//开启空闲中断
}
//单字节发送
void UART1_Send_Byte(uint8_t date)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
//USART_FLAG_TC为正在发送标志
USART_SendData(USART1,date);//通过移位寄存器将数据发送给串口1
}
//发送数组的函数
void UART1_Send_Buff(uint8_t *buf,uint16_t length)
{
uint16_t i=0;
for(i=0;i<length;i++)
{
UART1_Send_Byte(buf[i]);
}
}
//发送字符串
void UART1_Send_Str(uint8_t *str)
{
while((*str)!='\0')
{
UART1_Send_Byte(*str++);
}
}
/*
printf fputc 的重写 能够使用printf函数
包含头文件: #include "stdio.h"
在魔法棒 勾选 Use MicroLIB
仿真:不进mian函数 因为为勾选 Use MicroLIB LDR R0, =SystemInit
*/
//int fputc(int ch,FILE *f)
//{
// UART1_Send_Byte(ch);
// return ch;
//}
//中断服务函数
void USART1_IRQHandler(void)
{
uint8_t date=0;
//1.判断接收中断是否发生
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
date=USART_ReceiveData(USART1);//通过串口1里读取到的数据移位到date内
USART_SendData(UART5,date);//通过移位寄存器将数据发送给串口1
USART_SendData(USART3,date);//通过移位寄存器将数据发送给串口1
USART_SendData(USART2,date);//通过移位寄存器将数据发送给串口1
U1_R_Buff[U1_R_Length++]=date;
//考虑数组越界
if(U1_R_Length>=200)
U1_R_Length=0;
//清除中断标志位
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
//获取标志位状态
if(USART_GetITStatus(USART1,USART_IT_IDLE)==SET)
{
date=USART_ReceiveData(USART1);//将数据赋值给date
// UART2_Send_Buff(U1_R_Buff,sizeof(U1_R_Buff));//通过串口2将数据发送给串口1
U1_R_IDLE=1;
}
}
usart1.h
#ifndef __USART1_H_
#define __USART1_H_
#include "stm32f10x.h"
#include "delay.h"
#include "stdio.h"
extern uint8_t U1_R_Buff[512];
extern uint8_t U1_R_Length;
extern uint8_t U1_R_IDLE;
void USART1_Config(void);
void USART1_NVIC_Config(void);
void UART1_Send_Byte(uint8_t date);
void UART1_Send_Buff(uint8_t *buf,uint16_t length);
void UART1_Send_Str(uint8_t *str);
//int fputc(int ch,FILE *f);
#endif
usart3.c
#include "usart3_4g.h"
uint8_t U3_R_Buff[512];
uint8_t U3_R_Length=0;
uint8_t U3_R_IDLE=0;//等0说明还在发送 1代表发送完毕
void USART3_4G_Config(void)
{
//1.开串口3的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//2.创建GPIO结构体
GPIO_InitTypeDef GPIO_USART3_Struct;
//3.配置TX功能
GPIO_USART3_Struct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_USART3_Struct.GPIO_Pin = GPIO_USART3_Pin_TX;
GPIO_USART3_Struct.GPIO_Speed = GPIO_Speed_50MHz;
//4.初始化TX管脚
GPIO_Init(GPIOB,&GPIO_USART3_Struct);
//5.配置RX功能
GPIO_USART3_Struct.GPIO_Pin = GPIO_USART3_Pin_RX;
GPIO_USART3_Struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//6.初始化RX管脚
GPIO_Init(GPIOB,&GPIO_USART3_Struct);
//7.开启串口3时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
//8.创建串口3结构体
USART_InitTypeDef USART3_Iint_Struct;
USART3_Iint_Struct.USART_BaudRate = 9600;
USART3_Iint_Struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控制
USART3_Iint_Struct.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;//收发
USART3_Iint_Struct.USART_Parity = USART_Parity_No;
USART3_Iint_Struct.USART_StopBits = USART_StopBits_1;
USART3_Iint_Struct.USART_WordLength = USART_WordLength_8b;//八个数据位
//9.初始化串口3
USART_Init(USART3,&USART3_Iint_Struct);
//10.串口使能
USART_Cmd(USART3,ENABLE);
//11.配置串口中断
USART3_NVIC_Config();
}
void USART3_NVIC_Config(void)
{
//1.创建中断结构体
NVIC_InitTypeDef NVIC_InitStruct;
//2.配置中断功能
NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;//开启中断
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能对应的中断
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;//抢占次级优先级
//3.中断初始化
NVIC_Init(&NVIC_InitStruct);
//4.开启对应的中断
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//开启接收中断
USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);//开启空闲中断
}
//单字节发送
void USART3_Send_Byte(uint8_t date)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);
//USART_FLAG_TC为正在发送标志
USART_SendData(USART3,date);//通过移位寄存器将数据发送给串口1
}
//发送数组的函数
void USART3_Send_Buff(uint8_t *buf,uint16_t length)
{
uint16_t i=0;
for(i=0;i<length;i++)
{
USART3_Send_Byte(buf[i]);
}
}
//发送字符串
void USART3_Send_Str(uint8_t *str)
{
while((*str)!='\0')
{
USART3_Send_Byte(*str++);
}
}
/*
printf fputc 的重写 能够使用printf函数
包含头文件: #include "stdio.h"
在魔法棒 勾选 Use MicroLIB
仿真:不进mian函数 因为为勾选 Use MicroLIB LDR R0, =SystemInit
*/
//int fputc(int ch,FILE *f)
//{
// USART3_Send_Byte(ch);
// return ch;
//}
//中断服务函数
void USART3_IRQHandler(void)
{
uint8_t date=0;
//1.判断接收中断是否发生
if(USART_GetITStatus(USART3,USART_IT_RXNE)==SET)
{
date=USART_ReceiveData(USART3);
USART_SendData(USART1,date);//通过移位寄存器将数据发送给串口1
U3_R_Buff[U3_R_Length++] = date;//通过串口1里读取到的数据移位到接收数组内
//考虑数组越界
if(U3_R_Length>=200)
U3_R_Length=0;
//清除中断标志位
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
}
//获取标志位状态
if(USART_GetITStatus(USART3,USART_IT_IDLE)==SET)
{
USART_ReceiveData(USART3);
// USART3_Send_Buff(U3_R_Buff,sizeof(U3_R_Buff));
U3_R_IDLE=1;
}
}
usart3.h
#ifndef __USART3_4G_H_
#define __USART3_4G_H_
#include "stm32f10x.h"
#include "stdio.h"
extern uint8_t U3_R_Buff[512];
extern uint8_t U3_R_Length;
extern uint8_t U3_R_IDLE;//等0说明还在发送 1代表发送完毕
/*
4g模块
串口3通信
TX---PB10
RX---PB11
*/
#define GPIO_USART3_Port_TX GPIOB
#define GPIO_USART3_Port_RX GPIOB
#define GPIO_USART3_Pin_TX GPIO_Pin_10
#define GPIO_USART3_Pin_RX GPIO_Pin_11
void USART3_4G_Config(void);
void USART3_NVIC_Config(void);
void USART3_Send_Byte(uint8_t date);
void USART3_Send_Buff(uint8_t *buf,uint16_t length);
void USART3_Send_Str(uint8_t *str);
//int fputc(int ch,FILE *f);
#endif
自此,软件代码已经完成,请看下节讲解