为了方便调试,在Data界面里面多显示了Vmax 和Vmin(现在注释掉了,调试时可以打开) 必要的注释在代码里有
bsp_led_key.c
本程序唯一分离出的子函数,有按键扫描和显示LED的作用
#include "bsp_led_key.h"
void LED_Disp(uint8_t LED)
{
HAL_GPIO_WritePin(GPIOC,0xFF00,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,LED << 8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LE_GPIO_Port,LE_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(LE_GPIO_Port,LE_Pin,GPIO_PIN_RESET);
}
uint8_t KEY_Scan(void)
{
if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin) == GPIO_PIN_RESET)
return 1;
if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin) == GPIO_PIN_RESET)
return 2;
if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin) == GPIO_PIN_RESET)
return 3;
if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin) == GPIO_PIN_RESET)
return 4;
return 0;
}
主函数部分(时间原因大部分子函数没有分出来)
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "gpio.h"
#include "lcd.h"
#include "stdio.h"
#include "string.h"
#include "bsp_led_key.h"
typedef struct
{
float Vmax;
float Vmin;
} V_Number_Storage_Type;
V_Number_Storage_Type V_Number_Storage[2];
uint8_t KEY_VAL;
uint8_t KEY_OLD;
uint8_t KEY_DOWN;
_Bool face = 0;
u8 ucLED = 0x00;
float Vmax_Number = 3.0;
float Vmin_Number = 1.0;
int8_t Tim_number = 0;
char LCD_buf[20];
void SystemClock_Config(void);
void KEY_Proc(void);
void LCD_Proc(void);
void LED_Proc(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC2_Init();
MX_TIM1_Init();
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LED_Disp(0X00);
while (1)
{
KEY_Proc();
LCD_Proc();
LED_Disp(ucLED);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
HAL_PWREx_ControlVoltageScaling(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.PLLM = RCC_PLLM_DIV1;
RCC_OscInitStruct.PLL.PLLN = 10;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
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_2) != HAL_OK)
{
Error_Handler();
}
}
uint16_t getADC(void)
{
uint16_t adc = 0;
HAL_ADC_Start(&hadc2);
adc = HAL_ADC_GetValue(&hadc2);
return adc;
}
void KEY_Proc(void)
{
KEY_VAL = KEY_Scan();
KEY_DOWN = KEY_VAL&(KEY_VAL^KEY_OLD);
KEY_OLD = KEY_VAL;
switch(KEY_DOWN)
{
case 1:
face ^= 1;
LCD_Clear(Black);
if(face == 0)
{
if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin >= 1)
{
Vmax_Number = V_Number_Storage[0].Vmax;
Vmin_Number = V_Number_Storage[0].Vmin;
}
else if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin < 1)
{
Vmax_Number = V_Number_Storage[1].Vmax;
Vmin_Number = V_Number_Storage[1].Vmin;
}
}
break;
case 2:
if(face == 1)
{
V_Number_Storage[0].Vmax = Vmax_Number;
V_Number_Storage[0].Vmin = Vmin_Number;
Vmax_Number = Vmax_Number + 0.1;
if(Vmax_Number > 3.3)
{
Vmax_Number = 0.0;
}
V_Number_Storage[0].Vmax = Vmax_Number;
if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin < 1)
{
ucLED |= 0x02;
LED_Disp(ucLED);
}
else
{
ucLED &= ~0x02;
LED_Disp(ucLED);
}
}
break;
case 3:
if(face == 1)
{
Vmin_Number = Vmin_Number + 0.1;
if(Vmin_Number > 3.3)
{
Vmin_Number = 0.0;
}
V_Number_Storage[0].Vmin = Vmin_Number;
if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin < 1)
{
ucLED |= 0x02;
LED_Disp(ucLED);
}
else
{
ucLED &= ~0x02;
LED_Disp(ucLED);
}
}
break;
}
}
void LED_Proc()
{
LED_Disp(ucLED);
}
void Start_TIM(void)
{
if((getADC()*3.3/4096)>=Vmin_Number)
{
Tim_number = 0;
HAL_TIM_Base_Start_IT(&htim1);
}
}
void LCD_Proc(void)
{
if(face == 0)
{
V_Number_Storage[1].Vmin = Vmin_Number;
V_Number_Storage[1].Vmax = Vmax_Number;
sprintf(LCD_buf," Data ");
LCD_DisplayStringLine(Line0,(uint8_t*)LCD_buf);
sprintf(LCD_buf," V:%.2fV ",getADC()*3.3/4096);
LCD_DisplayStringLine(Line2,(uint8_t*)LCD_buf);
sprintf(LCD_buf," T:%d ",Tim_number);
LCD_DisplayStringLine(Line3,(uint8_t*)LCD_buf);
if((getADC()*3.3/4096)<Vmin_Number)
Start_TIM();
ucLED &= ~0x02;
}
else if(face == 1)
{
sprintf(LCD_buf," Para ");
LCD_DisplayStringLine(Line0,(uint8_t*)LCD_buf);
sprintf(LCD_buf," Vmax:%.1fV ",Vmax_Number);
LCD_DisplayStringLine(Line2,(uint8_t*)LCD_buf);
sprintf(LCD_buf," Vmin:%.1fV ",Vmin_Number);
LCD_DisplayStringLine(Line3,(uint8_t*)LCD_buf);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if(htim ->Instance == TIM1)
{
if((getADC()*3.3/4096) <= Vmax_Number)
{
Tim_number++;
ucLED |= 0x01;
LED_Disp(ucLED);
}
else
{
HAL_TIM_Base_Stop_IT(&htim1);
ucLED &= ~0x01;
LED_Disp(ucLED);
}
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
CUBMX配置如下: 1.时钟树 2.TIM1 (NVIC设置成update)
3.ADC2(PB15) 4.GPIO
|