首页 » 技术分享 » 433M串口模块无线通信(STM32)

433M串口模块无线通信(STM32)

 

一、前期准备
单片机:STM32F103C8T6*2
开发环境:MDK5.14
库函数:标准库V3.5
433M模块:淘宝有售
在这里插入图片描述
二、实验效果
2.1 Master 初始化
在这里插入图片描述
2.2 Slave 初始化
在这里插入图片描述
2.3 Master发送,Slave接收
在这里插入图片描述
在这里插入图片描述
2.4 Slave发送,Master接收
在这里插入图片描述
在这里插入图片描述

三、驱动原理
按照文档配置,SET脚置1为配置模式,绿色灯会亮起,SET脚清零为工作模式。
需要完整工程或者有问题的请加QQ:1002521871,验证:呵呵。

四、驱动代码
4.1 Master
wireless_433m.h

#ifndef __CONF_433M_H__
#define	__CONF_433M_H__
#include "stm32f10x.h"
#include "gpio.h"
#include "uart.h"
//Rate of Work
#define		Rate_Defualt		0x00		//20dBm
#define		Rate_1dBm			0x01
#define		Rate_2dBm			0x02
#define		Rate_5dBm			0x03
#define		Rate_8dBm			0x04
#define		Rate_11dBm			0x05
#define		Rate_14dBm			0x06
#define		Rate_17dBm			0x07
#define		Rate_20dBm			0x08

//Baudrate 
#define		Buad_Default		0x00		//9600
#define		Buad_1200			0x01
#define		Buad_2400			0x02
#define		Buad_4800			0x03
#define		Buad_9600			0x04
#define		Buad_19200			0x05
#define		Buad_38400			0x06
#define		Buad_57600			0x07
#define		Buad_115200			0x08

//RF Baudrate
#define		RF_Buad_Defualt		0x00		//10k bps
#define		RF_Baud_500			0x01
#define		RF_Baud_2400		0x02
#define		RF_Baud_10000		0x03
#define		RF_Baud_20000		0x04
#define		RF_Baud_40000		0x05
#define		RF_Baud_100000		0x06
#define		RF_Baud_150000		0x07
#define		RF_Baud_250000		0x08

//Serial Port Format
#define		Serial_Mode_default	0x00		//8N1
#define		Serial_Mode_8N1		0x01
#define		Serial_Mode_8O1		0x02
#define		Serial_Mode_8E1		0x03

//Frequency Band 
#define		Frequency_Default	0x02
#define		Frequency_433M		0x00
#define		Frequency_315M		0x01
#define		Frequency_433M_1	0x02
#define		Frequency_868M_915M	0x03


/* Master IO Definitions */
#define		M_TXD_Pin		GPIO_Pin_2
#define		M_RXD_Pin 		GPIO_Pin_3
#define		M_SET			PAout(5)
#define		M_Mode_Config	M_SET = 0
#define		M_Mode_Work		M_SET = 1

#define		M_Test_Key		PAin(0)

#define		RecLen		64

extern uint8_t M_RecData[RecLen];
extern uint8_t *M_pIndex;
extern uint8_t *M_pWrite;
extern uint8_t *M_pRead;
extern uint8_t M_DataLen;

extern void Wireless_433MConfiguration(void);
extern void Wireless_433MInitalized(void);
#endif

wireless_433m.c

#include "conf_433m.h"

uint8_t M_RecData[RecLen] = {0};
uint8_t *M_pIndex = M_RecData;
uint8_t *M_pWrite = M_RecData;
uint8_t *M_pRead = M_RecData;
uint8_t M_DataLen = 0;

void Wireless_433MConfiguration(void)
{
	 GPIO_InitTypeDef    GPIO;
    
    //Enable APB2 Bus
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    //Register IO 
    GPIO.GPIO_Pin   = GPIO_Pin_5;
    GPIO.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_Init(GPIOA, &GPIO);
	
	GPIO.GPIO_Pin   = GPIO_Pin_0;
    GPIO.GPIO_Mode  = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO);
	
	M_Mode_Config;
}

void Wireless_433MInitalized(void)
{
	uint8_t Wireless_433_Conf[15] = {0};
	uint16_t Base_Freq = 430;
	uint8_t i = 0;
	uint16_t Checksum = 0;
	//F8 00 00 00 28 12 34 12 34 00 00 02 B6
	//0  1  2  3  4  5  6  7  8  9  10 11 12
	Wireless_433_Conf[0] = 0xF8;
	Wireless_433_Conf[1] = Buad_Default;
	Wireless_433_Conf[2] = Rate_Defualt;
	Wireless_433_Conf[3] = (uint8_t)(((434 - Base_Freq)*10) / 256);
	Wireless_433_Conf[4] = (uint8_t)(((434 - Base_Freq)*10) % 256);
	Wireless_433_Conf[5] = 0x11;
	Wireless_433_Conf[6] = 0x11;
	Wireless_433_Conf[7] = 0x22;
	Wireless_433_Conf[8] = 0x22;
	Wireless_433_Conf[9] = RF_Buad_Defualt;
	Wireless_433_Conf[10] = Serial_Mode_default;
	Wireless_433_Conf[11] = Frequency_Default;
	
	for (i = 1; i < 12; i ++)
	{
		Checksum += Wireless_433_Conf[i];
	}
	Wireless_433_Conf[12] = (uint8_t)(Checksum % 256);
	
	printf("Master Config Cmd: ");
	for (i = 0; i < 13; i ++)
	{
		printf("%02X ", Wireless_433_Conf[i]);
	}
	printf("\r\n");
	
	for (i = 0; i  < 13; i ++)
	{
		USART_Send(USART2, Wireless_433_Conf[i]);
	}
	
	while(M_DataLen == 0);
	
	printf("M_RecData: %s\r\n", M_RecData);
	if (M_DataLen == 2)
	{
		if (M_RecData[0] == 'O' && M_RecData[1] == 'K')
			printf("Config Master PASS!\r\n");
	}
	else if (M_DataLen == 5)
	{
		printf("Config Master Error!\r\n");
	}
	else
	{
		printf("DataLen = %d!\r\n", M_DataLen);
	}
	
	M_DataLen = 0;
	memset(M_RecData, 0, RecLen);
	M_Mode_Work;
}

4.2 Slave
wireless_433m.h

#ifndef __CONF_433M_H__
#define	__CONF_433M_H__
#include "stm32f10x.h"
#include "gpio.h"
#include "uart.h"
//Rate of Work
#define		Rate_Defualt		0x00		//20dBm
#define		Rate_1dBm			0x01
#define		Rate_2dBm			0x02
#define		Rate_5dBm			0x03
#define		Rate_8dBm			0x04
#define		Rate_11dBm			0x05
#define		Rate_14dBm			0x06
#define		Rate_17dBm			0x07
#define		Rate_20dBm			0x08

//Baudrate 
#define		Buad_Default		0x00		//9600
#define		Buad_1200			0x01
#define		Buad_2400			0x02
#define		Buad_4800			0x03
#define		Buad_9600			0x04
#define		Buad_19200			0x05
#define		Buad_38400			0x06
#define		Buad_57600			0x07
#define		Buad_115200			0x08

//RF Baudrate
#define		RF_Buad_Defualt		0x00		//10k bps
#define		RF_Baud_500			0x01
#define		RF_Baud_2400		0x02
#define		RF_Baud_10000		0x03
#define		RF_Baud_20000		0x04
#define		RF_Baud_40000		0x05
#define		RF_Baud_100000		0x06
#define		RF_Baud_150000		0x07
#define		RF_Baud_250000		0x08

//Serial Port Format
#define		Serial_Mode_default	0x00		//8N1
#define		Serial_Mode_8N1		0x01
#define		Serial_Mode_8O1		0x02
#define		Serial_Mode_8E1		0x03

//Frequency Band 
#define		Frequency_Default	0x02
#define		Frequency_433M		0x00
#define		Frequency_315M		0x01
#define		Frequency_433M_1	0x02
#define		Frequency_868M_915M	0x03


/* Slave IO Definitions */
#define		S_TXD_Pin		GPIO_Pin_2
#define		S_RXD_Pin 		GPIO_Pin_3
#define		S_SET			PAout(5)
#define		S_Mode_Config	S_SET = 0
#define		S_Mode_Work		S_SET = 1

#define		S_Test_Key		PAin(0)

#define		RecLen		64

extern uint8_t S_RecData[RecLen];
extern uint8_t *S_pIndex;
extern uint8_t *S_pWrite;
extern uint8_t *S_pRead;
extern uint8_t S_DataLen;

extern void Wireless_433MConfiguration(void);
extern void Wireless_433MInitalized(void);
#endif

wireless_433.c

#include "conf_433m.h"

uint8_t S_RecData[RecLen] = {0};
uint8_t *S_pIndex = S_RecData;
uint8_t *S_pWrite = S_RecData;
uint8_t *S_pRead = S_RecData;
uint8_t S_DataLen = 0;

void Wireless_433MConfiguration(void)
{
	 GPIO_InitTypeDef    GPIO;
    
    //Enable APB2 Bus
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    //Register IO 
    GPIO.GPIO_Pin   = GPIO_Pin_5;
    GPIO.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_Init(GPIOA, &GPIO);
	
	GPIO.GPIO_Pin   = GPIO_Pin_0;
    GPIO.GPIO_Mode  = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO);
	
	S_Mode_Config;
}

void Wireless_433MInitalized(void)
{
	uint8_t Wireless_433_Conf[15] = {0};
	uint16_t Base_Freq = 430;
	uint8_t i = 0;
	uint16_t Checksum = 0;
	//F8 00 00 00 28 12 34 12 34 00 00 02 B6
	//0  1  2  3  4  5  6  7  8  9  10 11 12
	Wireless_433_Conf[0] = 0xF8;
	Wireless_433_Conf[1] = Buad_Default;
	Wireless_433_Conf[2] = Rate_Defualt;
	Wireless_433_Conf[3] = (uint8_t)(((434 - Base_Freq)*10) / 256);
	Wireless_433_Conf[4] = (uint8_t)(((434 - Base_Freq)*10) % 256);
	Wireless_433_Conf[5] = 0x22;
	Wireless_433_Conf[6] = 0x22;
	Wireless_433_Conf[7] = 0x11;
	Wireless_433_Conf[8] = 0x11;
	Wireless_433_Conf[9] = RF_Buad_Defualt;
	Wireless_433_Conf[10] = Serial_Mode_default;
	Wireless_433_Conf[11] = Frequency_Default;
	
	for (i = 1; i < 12; i ++)
	{
		Checksum += Wireless_433_Conf[i];
	}
	Wireless_433_Conf[12] = (uint8_t)(Checksum % 256);
	
	printf("Slave Config Cmd: ");
	for (i = 0; i < 13; i ++)
	{
		printf("%02X ", Wireless_433_Conf[i]);
	}
	printf("\r\n");
	
	for (i = 0; i  < 13; i ++)
	{
		USART_Send(USART2, Wireless_433_Conf[i]);
	}
	
	while(S_DataLen == 0);
	
	printf("S_RecData: %s\r\n", S_RecData);
	if (S_DataLen == 2)
	{
		if (S_RecData[0] == 'O' && S_RecData[1] == 'K')
			printf("Config Slave PASS!\r\n");
	}
	else if (S_DataLen == 5)
	{
		printf("Config Slave Error!\r\n");
	}
	else
	{
		printf("DataLen = %d!\r\n", S_DataLen);
	}
	
	S_DataLen = 0;
	memset(S_RecData, 0, RecLen);
	S_Mode_Work;
}

由于作者能力有限,有不妥之处欢迎指正,邮箱alex_hua@foxmail.com

转载自原文链接, 如需删除请联系管理员。

原文链接:433M串口模块无线通信(STM32),转载请注明来源!

0