이번 대회의 로라 목표
- LoRa 통신 이용 및 통신 지연 시간 줄이기
0) 로라를 사용할 장소의 특성
https://www.makethefuture.shell/en-gb
우리 대회는 7월 4~9일, 인도네시아 Lombok에서 열린다.
보통 7월 인도네시아의 날씨는 최저 24°C, 최고 33°C라고 한다.
습도는
1) 로라 모듈 특성
- 작동 온도는 -30~85°C인데, 밀폐된 차에 오래 있지만 않는다면,
즉 공기 순환이 어느정도 있다면 동작 가능할 것이다.
- 주파수 범위는 410~525MHz인데, 433MHz를 사용할 것이다.
주파수가 높아지면 통신 속도는 높아지지만, 회절이 덜 일어나 통신 가능 거리가 줄어든다.
- SPI Interface를 이용한다.
- 안테나 방식은 IPEX이다. 우리가 구매한 안테나는 SMA인데,
어댑터를 사용하면 될 듯 하다.
https://www.amazon.com/Antenna-Half-Wave-Magnetic-Wireless-Repeater/dp/B07CBR485W/ref=sr_1_4?crid=2IZ5TXYSX638Y&keywords=433mhz+antenna+lora+10dbi&qid=1672151076&sprefix=433mhz+antenna+lora+10dbi%2Caps%2C286&sr=8-4
2) SPI Interface
추후 보강 예정
3) STM32로 실습 - tx
핀 설정
SPI2 관련 핀 | |||||
PB10 | SPI2_SCK | clock 신호선 | 기본으로 설정되어 있음 | ||
PC1 | SPI2_MOSI | master out, slave in 신호선 | |||
PC2 | SPI2_MISO | master in, slave out 신호선 | |||
GPIO 핀 (따로 설정해야 함) | |||||
PA8 | DIO0 | GPIO input | |||
PC7 | RESET | GPIO output | |||
PB5 | NSS | 슬레이브 선택 신호선 | GPIO output | ||
전원 | |||||
STM보드의 3.3V, GND에 연결 | |||||
SPI통신을 위한 설정
위 설정은 Tx, Rx 동일하게 해야 함
헤더파일이다. 추가 방법은
라이브러리 추가
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "rtc.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "LoRa.h"
//#include "liquidcrystal_i2c.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
volatile int gTimerCnt=0, B1_count=0, _B1_count;
volatile int _sec=0, sec=0, min=0, hour=0, Timer_on_Flag=0, B1_Flag = 0;
uint8_t time_buffer[256];
LoRa myLoRa;
uint16_t lora_init;
uint8_t Tx_information[20] = {0,};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void Init_LoRa();
void LoRa_Send();
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_RTC_Init();
MX_TIM3_Init();
MX_SPI2_Init();
/* USER CODE BEGIN 2 */
void Init_LoRa();
if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
_sec = sec;
HAL_Delay(1);
if (_B1_count > 100)
{
sec = 0;
min = 0;
hour = 0;
Timer_on_Flag = 0;
}
if (_sec != sec)
{
//time print by UART
sprintf(time_buffer, "%2d:%2d:%2d\n\n\r", hour, min, sec);
HAL_UART_Transmit(&huart2, time_buffer, strlen(time_buffer), 100);
//time print by CLCD
//HD44780_Init(2);
//HD44780_SetCursor(0,0);
//HD44780_PrintStr(time_buffer);
}
LoRa_Send();
HAL_Delay(100);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 180;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim)
{
B1_count++;
if (Timer_on_Flag == 1)
{
gTimerCnt++;
if (gTimerCnt == 1000)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
//MAJOR PART of this function
gTimerCnt = 0;
sec++;
}
if(sec>59) { sec=0; min++; }
if(min>59) { min=0; hour++; }
if(hour>23) { hour=0; }
}
}
void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin)
{
switch (GPIO_Pin)
{
case B1_Pin:
B1_Flag = 1 - B1_Flag;
if (B1_Flag == 1) B1_count = 0;
if (B1_Flag == 0)
{
_B1_count = B1_count;
Timer_on_Flag = 1 - Timer_on_Flag;
}
break;
default:
;
}
}
void Init_LoRa()
{
myLoRa = newLoRa();
myLoRa.CS_port = NSS_GPIO_Port;
myLoRa.CS_pin = NSS_Pin;
myLoRa.reset_port = RESET_GPIO_Port;
myLoRa.reset_pin = RESET_Pin;
myLoRa.DIO0_port = DIO0_GPIO_Port;
myLoRa.DIO0_pin = DIO0_Pin;
myLoRa.hSPIx = &hspi2;
myLoRa.frequency = 433 ; // LoRa Frequency = 433MHz
myLoRa.spredingFactor = SF_7; // SpreadingFactor = SF_7
myLoRa.bandWidth = BW_125KHz; // BandWidth = BW_125KHz
myLoRa.crcRate = CR_4_5; // crcRate = CR_4_5
myLoRa.power = POWER_20db; // Power db = 20db
myLoRa.overCurrentProtection = 120 ; // CurrentProtection = 100mA
myLoRa.preamble = 10 ;
LoRa_reset(&myLoRa);
lora_init = LoRa_init(&myLoRa);
}
void LoRa_Send()
{
// Velocity1, Velocity2, Acceleration1, Acceleration2, Check1, Check2, Current, Voltage, Minute, Second
for(int i = 0; i<20;i++)
{
Tx_information[i] = i;
}
LoRa_transmit(&myLoRa, Tx_information, 20, 100);
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
4) rx측
동일하게 PIN Configuration, Clock configuration, 하고
헤더파일 추가하고
'프로젝트 기록 > 23SEMA_Control_STM32' 카테고리의 다른 글
[LoRa] 1편. 통신을 위한 준비 (3) | 2023.01.10 |
---|---|
[STM32제어] ADC Interrupt+DMA로 받아오기 (0) | 2023.01.09 |
[STM32] STM Studio 사용법 (0) | 2023.01.09 |
[Timer] 스톱워치 만들기 (0) | 2023.01.08 |
[STM32] LSE 선택하는 법 (0) | 2023.01.07 |