0 -【STM32L0】开发板编程总结

开发板GPIO概览图

0 -【STM32L0】开发板编程总结_第1张图片

基础实验所用函数

GPIO输出函数

stm32l0xx_hal_gpio.c

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET);

GPIO输入函数

stm32l0xx_hal_gpio.c

HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14);

GPIO反转函数

stm32l0xx_hal_gpio.c

HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_15);

ms级延时函数

stm32l0xx_hal.c

HAL_Delay(100);

系统复位函数

stm32l0xx_hal_cortex.c

HAL_NVIC_SystemReset();

EEPROM写代码

stm32l0xx_hal_flash_ex.c

void EEPROM_Write(uint32_t address, uint32_t *buff, uint32_t len)
{
  HAL_FLASHEx_DATAEEPROM_Unlock();
  HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address + DATA_EEPROM_BASE, *buff);  
  HAL_FLASHEx_DATAEEPROM_Lock();
}

EEPROM读代码

stm32l0xx_hal_flash_ex.c

void EEPROM_Read(uint32_t address, uint32_t *buff, uint32_t len)
{
  uint32_t *read_address;
  read_address = (uint32_t *)(address + DATA_EEPROM_BASE);  
  HAL_FLASHEx_DATAEEPROM_Unlock();
  while(len--)
  {
    *buff++ = *read_address++;
  }
  HAL_FLASHEx_DATAEEPROM_Lock();  
}

CRC校验函数

stm32l0xx_hal_crc.c

crc_check = HAL_CRC_Calculate(&hcrc, (uint32_t *)VALUE, 2);

DMA函数

stm32l0xx_hal_dma.c

HAL_DMA_Start(&hdma_memtomem_dma1_channel1, 
              (uint32_t)Source_Data, (uint32_t)Destination_Data, 20);

EXTI外部中断函数

stm32l0xx_hal_gpio.c

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_15);
}

ADC检测函数

stm32l0xx_hal_adc.c

  HAL_ADC_Start(&hadc);
  ADC_Value = HAL_ADC_GetValue(&hadc) * (3.3 / 4095);

LPTIM1定时器代码

stm32l0xx_hal_lptim.c

  #define PERIOD 2500  // 系统时钟是32MHz,分频128,那么计时器的时钟是250000Hz。
  // 计数器值设为2500,计时器100次进入中断,总的计数是250000,刚好是1s。
  HAL_LPTIM_Counter_Start_IT(&hlptim1, PERIOD);

  void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  {

  }

UASART串口收发代码

串口重定向

stdio.h

int fputc(int ch, FILE *f)
{
  while((USART2->ISR&(1<<7))==0);
  USART2->TDR = (uint8_t)ch;
  
  return ch;
}

char data[50] = "GXCT IOT USART TEST TX";
printf("%s\n", data);

通过串口中断接收数据

stm32l0xx_hal_uart.c

  HAL_UART_Receive_IT(&huart2, recv_data, 1);

  void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  {
    printf("%s", recv_data);
    HAL_UART_Receive_IT(&huart2, recv_data, 1);
  }  

接收不定长数据

main.c

传递变量

uint8_t recv_buf[256] = {0};
extern uint8_t data[256];
extern uint16_t count;

使能中断

  /* USER CODE BEGIN 2 */
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
	HAL_UART_Receive_IT(&huart2, recv_buf, 1);
  /* USER CODE END 2 */
stm32l0xx_it.c

定义变量

uint8_t data[256] = {0};
uint16_t count = 0;

编写中断处理函数

void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
	if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_RXNE)==SET)
	{
		data[count] = USART2->RDR;
		if(data[count] == 'M')
		{
			count = 0;
			data[count++] = 'M';
		}
		else
		{
			count++;
		}
	}
  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */
  /* USER CODE END USART2_IRQn 1 */
}

外设实验所用函数

OLED初始化代码

void Task_BrdInit(void)
{
    OLED_PowerControl(ENABLE);

    HAL_Delay(200);
    OLED_Init();
    OLED_Clear();

    OLED_ShowString(0, 0, (unsigned char *)"                ", 16);
    OLED_ShowString(0, 2, (unsigned char *)"                ", 16);
}

key按键代码

key.h

#ifndef _KEY_H_
#define _KEY_H_

#include "stm32l0xx_hal.h"

//函数声明
void key_pin_init(void);
void Keyboard_Init(void);
uint8_t Row_Scan(void);
uint8_t Keyboard_Scan(void);
uint8_t USER_Scan(void);

//按键操作宏定义
#define Read_Row_1        HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)
#define Read_Row_2        HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)
#define Write_Col_1_HIGH  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET)
#define Write_Col_1_LOW   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET)
#define Write_Col_2_HIGH  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET)
#define Write_Col_2_LOW   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET)
#define Write_Col_3_HIGH  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET)
#define Write_Col_3_LOW   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET)

#endif

key.c

#include "key.h"

/*矩阵按键引脚初始化*/
void key_pin_init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);

  /*Configure GPIO pins : PA8*/
  GPIO_InitStruct.Pin = GPIO_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PB0 PB1 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : PB6 PB7 */
  GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

/*矩阵按键扫描初始化,将引脚配置成下拉输出模式*/
void Keyboard_Init(void)
{
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);  
}

/*USER按键扫描函数*/
/*按下返回1,松开返回0*/
uint8_t USER_Scan(void)
{
  uint8_t key_stat = 0;
  
  key_stat = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14);
  HAL_Delay(100);
  key_stat = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14);  
  if(!key_stat)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

/*矩阵按键行扫描*/
uint8_t count = 0;
uint8_t Row_Scan(void)
{
  //定义两个行值
  uint8_t Row1=0;
  uint8_t Row2=0;
  
  //读取两个行引脚状态
  Row1 = Read_Row_1;
  Row2 = Read_Row_2;

  if(Row1)
  {
    return 1;
  }
  if(Row2)
  {
    return 2;
  }
  return 0;
}

/*矩阵按键键值扫描*/
uint8_t Keyboard_Scan(void)
{
  uint8_t Row_Value = 0;
  uint8_t key_Value = 0;

  Keyboard_Init();
  
  Write_Col_3_HIGH;  
  Row_Value = Row_Scan();  
  if(Row_Value)
  {
    switch(Row_Value)
    {
      case 1:
        key_Value = 3;
        break;
      case 2:
        key_Value = 6;
        break;
      default:
        break;
    }
  }  

  Write_Col_1_HIGH;
  Write_Col_3_LOW;
  Row_Value = Row_Scan();
  if(Row_Value)
  {
    switch(Row_Value)
    {
      case 1:
        key_Value = 1;
        break;
      case 2:
        key_Value = 4;
        break;
      default:
        break;
    }
  }
  
  Write_Col_1_LOW;
  Write_Col_2_HIGH;  
  Row_Value = Row_Scan();  
  if(Row_Value)
  {
    switch(Row_Value)
    {
      case 1:
        key_Value = 2;
        break;
      case 2:
        key_Value = 5;
        break;
      default:
        break;
    }
  }  

  return key_Value;
}

PWM输出代码

stm32l0xx_hal_tim.c

  TIM3->CCR3 = key_num * 10;
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
  HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);

RTC实现代码

void Task_Main(void)
{
  RTC_DateTypeDef NowDate;
  RTC_TimeTypeDef NowTime;

  char oled_dis_Date[16];
  char oled_dis_Time[16];
  
  HAL_RTC_GetDate(&hrtc, &NowDate, RTC_FORMAT_BIN);
  HAL_RTC_GetTime(&hrtc, &NowTime, RTC_FORMAT_BIN);
  
  sprintf(oled_dis_Date, "  %02d - %02d - %02d ", NowDate.Year, NowDate.Month, NowDate.Date);
  sprintf(oled_dis_Time, "  %02d : %02d : %02d ", NowTime.Hours, NowTime.Minutes, NowTime.Seconds);
    
  OLED_ShowString(0, 0, (unsigned char *)oled_dis_Date, 16);  
  OLED_ShowString(0, 2, (unsigned char *)oled_dis_Time, 16);  
}

ADC检测代码

stm32l0xx_hal_adc.c

HAL_ADC_Start_DMA(&hadc, ADC_DMA_Value, 10);

STS30温度采集代码

STS30.h

#ifndef _STS30_H
#define _STS30_H

#include "iic_sensor.h"

float Get_Temperature(void);

#endif

STS30.c

#include "sts30.h"

float Get_Temperature(void)
{
    uint8_t H_VALUE, L_VALUE, CRC_VALUE;
    uint16_t T_VALUE; //转换完成的数字值
    float temperature;//转换完成的温度值

    Sensor_I2CStart();
    Sensor_I2CSendByte(0x4A << 1);
    Sensor_I2CWaitAck();
    Sensor_I2CSendByte(0x24);//不使能时钟伸缩
    Sensor_I2CWaitAck();
    Sensor_I2CSendByte(0x0b);//中重复性测量精度
    Sensor_I2CWaitAck();
    Sensor_I2CStop();
    HAL_Delay(2);//scl free,最小为1ms

    Sensor_I2CStart();
    Sensor_I2CSendByte(0x4A << 1 | 1);
    Sensor_I2CWaitAck();
    Sensor_I2CStop();
    HAL_Delay(2);//scl free,最小为1ms

    Sensor_I2CStart();
    Sensor_I2CSendByte(0x4A << 1 | 1);
    Sensor_I2CWaitAck();

    H_VALUE = Sensor_I2CReceiveByte();
    Sensor_I2CSendAck();
    L_VALUE = Sensor_I2CReceiveByte();
    Sensor_I2CSendAck();
    CRC_VALUE = Sensor_I2CReceiveByte();
    Sensor_I2CSendNotAck();
    Sensor_I2CStop();

    if (CRC_VALUE)
    {
        ;
    }
    T_VALUE = H_VALUE << 8 | L_VALUE;
    temperature = -45 + 175.0 * T_VALUE / 65535;

    return (temperature);
}

你可能感兴趣的:(蓝桥杯物联网真题备赛,蓝桥杯,物联网)