蓝桥杯物联网竞赛_STM32L071_7_LORA发送&接收

理论:

LoRa(长距离低功耗无线射频)是利用无线电频谱中的电磁波进行通信的一种技术。它使用无线电频段中的射频信号来传输数据。电磁波是一种波动形式的能量,具有特定的频率和波长,可以传播在真空和大多数介质中,包括空气、水和固体物质等。LoRa技术利用电磁波在射频范围内进行通信,使得设备可以进行远距离的低功耗数据传输。

蓝桥杯物联网竞赛_STM32L071_7_LORA发送&接收_第1张图片
其中PA4是控制数据的传输开始与结束的引脚,低电平有效

LORA发送的是电磁波,在真空中也能通信,具有一定的穿透性,传播范围在数十公里不等

SPI是Serial Peripheral Interface的缩写,是一种同步串行通信接口协议,用于在数字集成电路之间进行通信。SPI协议允许多个设备通过共享同一组数据线进行通信,其中一个设备作为主设备,其他设备作为从设备。SPI协议通常用于连接微控制器、传感器、存储器和其他外围设备。

蓝桥杯官方给了LORA的实现参考代码,我们需要做的是将上述SPI线路配置好,然后编写接口函数,让官方给的LORA功能能与SPI线路连接起来,最终能达到使用的目的

CubMx配置:

蓝桥杯物联网竞赛_STM32L071_7_LORA发送&接收_第2张图片
配置全双工即能边接收数据边发送数据

蓝桥杯物联网竞赛_STM32L071_7_LORA发送&接收_第3张图片
PA4配置为输出模式,因为需要它控制引脚高低电平,控制数据的传输接收与停止

Keil配置:

Function:

#include "Function.h"
#include "i2c.h"
#include "oled.h"
#include "gpio.h"
#include "spi.h"

void OLED_Write(unsigned char type, unsigned char data){
	unsigned char Write_Data[2];
	Write_Data[0] = type;
	Write_Data[1] = data;
	HAL_I2C_Master_Transmit(&hi2c3, 0x78, Write_Data, 2, 0xff);
}

void Function_OLedEnable(unsigned char ms){
	HAL_GPIO_WritePin(OLED_POWER_GPIO_Port, OLED_POWER_Pin, GPIO_PIN_RESET);
	HAL_Delay(ms);
	OLED_Init();
}

unsigned char SPI_WriteRead(unsigned char address, unsigned char data){
	unsigned char TxData[2], RxData[2];
	
	TxData[0] = address;
	TxData[1] = data;
	
	HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET); // 片选,传输开始
	
	HAL_SPI_TransmitReceive(&hspi1, TxData, RxData, sizeof(TxData), 0xff); // 传输与接受功能在一起
	
	HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_SET); // 关闭电源,表示传输结束
	
	return RxData[1]; // 获取接受数据
}
#ifndef __FUNCTION__
#define __FUNCTION__

void OLED_Write(unsigned char type, unsigned char data);
void Function_OLedEnable(unsigned char ms);
unsigned char SPI_WriteRead(unsigned char address, unsigned char data);
#endif

main:

发送端即实验板A板

#include "main.h"
#include "i2c.h"
#include "spi.h"
#include "gpio.h"
#include "Function.h"
#include "oled.h"
#include "lora.h"

 
void SystemClock_Config(void);
 
int main(void)
{
	unsigned char TX[] = {'m', 'r', 6, 8, 8};
 
  HAL_Init();

 
  SystemClock_Config();

 
  MX_GPIO_Init();
  MX_I2C3_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
  Function_OLedEnable(50);
	LORA_Init(); // 初始化传输
 
  while (1)
  {
 
    OLED_ShowNumber(0, 0, 1234, 4, 16);
		LORA_Tx(TX, sizeof(TX));
		OLED_ShowString(0, 2, "SendVictory", 16);
 
  }
 
}

 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

 
 
void Error_Handler(void)
{
 
  __disable_irq();
  while (1)
  {
  }
 
}

接收端即实验板B板:

#include "main.h"
#include "i2c.h"
#include "spi.h"
#include "gpio.h"
#include "Function.h"
#include "oled.h"
#include "lora.h"

 
void SystemClock_Config(void);
 
int main(void)
{
	unsigned char Rec[5];
 
  HAL_Init();

 
  SystemClock_Config();

 
  MX_GPIO_Init();
  MX_I2C3_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
  Function_OLedEnable(50);
	LORA_Init(); // 初始化传输
 
  while (1)
  {
 
    OLED_ShowNumber(0, 0, 1234, 4, 16);
		LORA_Rx(Rec);
	  OLED_ShowChar(0, 2, Rec[0], 16);
		OLED_ShowChar(8, 2, Rec[1], 16);
		OLED_ShowNumber(16, 2, Rec[2], 1, 16);
		OLED_ShowNumber(24, 2, Rec[3], 1, 16);
		OLED_ShowNumber(32, 2, Rec[4], 1, 16);
 
  }
 
}

 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

 
 
void Error_Handler(void)
{
 
  __disable_irq();
  while (1)
  {
  }
 
}

效果:

蓝桥杯物联网竞赛_STM32L071_7_LORA发送&接收_第4张图片

你可能感兴趣的:(蓝桥杯,物联网,stm32)