LORA模块开发基础教程目录
- 物联网国赛LORA模块开发基础教程—开发环境配置
- 物联网国赛LORA模块开发基础教程—输出(LED)
- 物联网国赛LORA模块开发基础教程—输入(按键)
- 物联网国赛LORA模块开发基础教程—OLED屏幕
- 物联网国赛LORA模块开发基础教程—传感器
- 物联网国赛LORA模块开发基础教程—定时器
- 物联网国赛LORA模块开发基础教程—串口收发
- 物联网国赛LORA模块开发基础教程—读写内部FALSH
- 物联网国赛LORA模块开发基础教程—点对点通信
物联网国赛LORA模块开发基础教程(通用库)—定时器
1.本节内容
2.本节函数
setTimer2Callback(Time2Handler);
Tim2McuInit(1);
Tim2McuInit(0);
setTimer2Callback(Time3Handler);
Tim3McuInit(1);
void SysTick_Handler( void )
{
HAL_IncTick( );
HAL_SYSTICK_IRQHandler( );
}
3.本节代码
main.c(定时器2、定时器3在此源代码文件初始化)
#include <string.h>
#include "board.h"
#include "hal_key.h"
#include "tim-board.h"
#include "timer_handles.h"
void Init() {
BoardInitMcu();
BoardInitPeriph();
keys_init();
setTimer2Callback(Time2Handler);
Tim2McuInit(1);
setTimer3Callback(Time3Handler);
Tim3McuInit(4);
}
void KeyDownHandler(void) {
}
void handlerPre10Ms(void) {
for (int delay = 0; delay < 30; delay++) {
HAL_Delay(10);
}
}
int main( void )
{
Init();
while( 1 )
{
}
}
timer_handles.c(定时器2、定时器3在此源代码文件使用)
#include "timer_handles.h"
#include "key_scaner.h"
uint16_t Time2_Time = 1000;
uint16_t Time3_Time = 1000;
void Time2Handler(){
keyScanner();
if(!Time2_Time--)
{
Time2_Time = 1000;
GpioToggle(&Led1);
}
}
void Time3Handler(){
if(!Time3_Time--)
{
Time3_Time = 1000;
GpioToggle(&Led2);
}
}
board.c(滴答定时器在此源代码文件使用)
#include "board.h"
#define ID1 ( 0x1FF80050 )
#define ID2 ( 0x1FF80054 )
#define ID3 ( 0x1FF80064 )
Gpio_t Led1;
Gpio_t Led2;
Gpio_t Key2;
Adc_t Adc;
Uart_t Uart1;
#if defined( USE_USB_CDC )
Uart_t UartUsb;
#endif
static void BoardUnusedIoInit( void );
static void SystemClockConfig( void );
static void CalibrateSystemWakeupTime( void );
static void SystemClockReConfig( void );
static TimerEvent_t CalibrateSystemWakeupTimeTimer;
static bool McuInitialized = false;
static bool SystemWakeupTimeCalibrated = false;
static void OnCalibrateSystemWakeupTimeTimerEvent( void )
{
SystemWakeupTimeCalibrated = true;
}
static uint8_t IrqNestLevel = 0;
void BoardDisableIrq( void )
{
__disable_irq( );
IrqNestLevel++;
}
void BoardEnableIrq( void )
{
IrqNestLevel--;
if( IrqNestLevel == 0 )
{
__enable_irq( );
}
}
void BoardInitPeriph( void )
{
GpioInit( &Led1, LED_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &Led2, LED_2, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &Key2, KEY_2, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
GpioWrite( &Led1, 1 );
GpioWrite( &Led2, 1 );
GpioWrite( &Key2, 1 );
}
void BoardInitMcu( void )
{
if( McuInitialized == false )
{
#if defined( USE_BOOTLOADER )
SCB->VTOR = FLASH_BASE | 0x3000;
#endif
HAL_Init( );
SystemClockConfig( );
#if defined( USE_USB_CDC )
UartInit( &UartUsb, UART_USB_CDC, NC, NC );
UartConfig( &UartUsb, RX_TX, 115200, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
DelayMs( 1000 );
#endif
RtcInit( );
BoardUnusedIoInit( );
}
else
{
SystemClockReConfig( );
}
SpiInit( &SX1276.Spi, RADIO_MOSI, RADIO_MISO, RADIO_SCLK, NC );
SX1276IoInit( );
if( McuInitialized == false )
{
McuInitialized = true;
if( GetBoardPowerSource( ) == BATTERY_POWER )
{
CalibrateSystemWakeupTime( );
}
}
}
void BoardDeInitMcu( void )
{
Gpio_t ioPin;
AdcDeInit( &Adc );
SpiDeInit( &SX1276.Spi );
SX1276IoDeInit( );
GpioInit( &ioPin, OSC_HSE_IN, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &ioPin, OSC_HSE_OUT, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &ioPin, OSC_LSE_IN, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &ioPin, OSC_LSE_OUT, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
}
uint32_t BoardGetRandomSeed( void )
{
return ( ( *( uint32_t* )ID1 ) ^ ( *( uint32_t* )ID2 ) ^ ( *( uint32_t* )ID3 ) );
}
void BoardGetUniqueId( uint8_t *id )
{
id[7] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) ) >> 24;
id[6] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) ) >> 16;
id[5] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) ) >> 8;
id[4] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) );
id[3] = ( ( *( uint32_t* )ID2 ) ) >> 24;
id[2] = ( ( *( uint32_t* )ID2 ) ) >> 16;
id[1] = ( ( *( uint32_t* )ID2 ) ) >> 8;
id[0] = ( ( *( uint32_t* )ID2 ) );
}
#define FACTORY_POWER_SUPPLY 3300
#define VREFINT_CAL ( *( uint16_t* )0x1FF80078 )
#define ADC_MAX_VALUE 4095
#define BATTERY_MAX_LEVEL 4150
#define BATTERY_MIN_LEVEL 3200
#define BATTERY_SHUTDOWN_LEVEL 3100
static uint16_t BatteryVoltage = BATTERY_MAX_LEVEL;
uint16_t BoardBatteryMeasureVolage( void )
{
return BATTERY_MAX_LEVEL;
}
uint32_t BoardGetBatteryVoltage( void )
{
return BatteryVoltage;
}
uint8_t BoardGetBatteryLevel( void )
{
uint8_t batteryLevel = 0;
BatteryVoltage = BoardBatteryMeasureVolage( );
if( GetBoardPowerSource( ) == USB_POWER )
{
batteryLevel = 0;
}
else
{
if( BatteryVoltage >= BATTERY_MAX_LEVEL )
{
batteryLevel = 254;
}
else if( ( BatteryVoltage > BATTERY_MIN_LEVEL ) && ( BatteryVoltage < BATTERY_MAX_LEVEL ) )
{
batteryLevel = ( ( 253 * ( BatteryVoltage - BATTERY_MIN_LEVEL ) ) / ( BATTERY_MAX_LEVEL - BATTERY_MIN_LEVEL ) ) + 1;
}
else if( ( BatteryVoltage > BATTERY_SHUTDOWN_LEVEL ) && ( BatteryVoltage <= BATTERY_MIN_LEVEL ) )
{
batteryLevel = 1;
}
else
{
batteryLevel = 255;
}
}
return batteryLevel;
}
static void BoardUnusedIoInit( void )
{
Gpio_t ioPin;
if( GetBoardPowerSource( ) == BATTERY_POWER )
{
GpioInit( &ioPin, USB_DM, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &ioPin, USB_DP, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
}
GpioInit( &ioPin, BOOT_1, PIN_ANALOGIC, PIN_OPEN_DRAIN, PIN_NO_PULL, 0 );
GpioInit( &ioPin, WKUP1, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
#if defined( USE_DEBUGGER )
HAL_DBGMCU_EnableDBGStopMode( );
HAL_DBGMCU_EnableDBGSleepMode( );
HAL_DBGMCU_EnableDBGStandbyMode( );
#else
HAL_DBGMCU_DisableDBGSleepMode( );
HAL_DBGMCU_DisableDBGStopMode( );
HAL_DBGMCU_DisableDBGStandbyMode( );
GpioInit( &ioPin, SWDIO, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
GpioInit( &ioPin, SWCLK, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
#endif
}
void SystemClockConfig( void )
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
__HAL_RCC_PWR_CLK_ENABLE( );
__HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE1 );
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.PLLMUL = RCC_PLL_MUL8;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
if( HAL_RCC_OscConfig( &RCC_OscInitStruct ) != HAL_OK )
{
assert_param( FAIL );
}
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 )
{
assert_param( FAIL );
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if( HAL_RCCEx_PeriphCLKConfig( &PeriphClkInit ) != HAL_OK )
{
assert_param( FAIL );
}
HAL_SYSTICK_Config( HAL_RCC_GetHCLKFreq( ) / 1000 );
HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK );
HAL_NVIC_SetPriorityGrouping( NVIC_PRIORITYGROUP_4 );
HAL_NVIC_SetPriority( SysTick_IRQn, 0, 0 );
}
void CalibrateSystemWakeupTime( void )
{
if( SystemWakeupTimeCalibrated == false )
{
TimerInit( &CalibrateSystemWakeupTimeTimer, OnCalibrateSystemWakeupTimeTimerEvent );
TimerSetValue( &CalibrateSystemWakeupTimeTimer, 1000 );
TimerStart( &CalibrateSystemWakeupTimeTimer );
while( SystemWakeupTimeCalibrated == false )
{
TimerLowPowerHandler( );
}
}
}
void SystemClockReConfig( void )
{
__HAL_RCC_PWR_CLK_ENABLE( );
__HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE1 );
__HAL_RCC_HSE_CONFIG( RCC_HSE_ON );
while( __HAL_RCC_GET_FLAG( RCC_FLAG_HSERDY ) == RESET )
{
}
__HAL_RCC_PLL_ENABLE( );
while( __HAL_RCC_GET_FLAG( RCC_FLAG_PLLRDY ) == RESET )
{
}
__HAL_RCC_SYSCLK_CONFIG ( RCC_SYSCLKSOURCE_PLLCLK );
while( __HAL_RCC_GET_SYSCLK_SOURCE( ) != RCC_SYSCLKSOURCE_STATUS_PLLCLK )
{
}
}
uint16_t SysTick_Time = 1000;
void SysTick_Handler( void )
{
if(!SysTick_Time--)
{
SysTick_Time = 1000;
}
HAL_IncTick( );
HAL_SYSTICK_IRQHandler( );
}
uint8_t GetBoardPowerSource( void )
#if (USE_BATTERY != false)
{
return BATTERY_POWER;
}
#else
{
return USB_POWER;
}
#endif
#ifdef USE_FULL_ASSERT
void assert_failed( uint8_t* file, uint32_t line )
{
while( 1 )
{
}
}
#endif
4.实验效果
5.本节相关知识
切忌:定时器里千万千万不要写延时函数
6.本节资源
百度网盘: 本节源码
7.结束语
欢迎加入学习交流QQ群
748555394
|