电路原理图如下:
#include "main.h"
#include "stm32f1xx_hal.h"
/* USER CODE BEGIN Includes */
#include "STM32_LCD1602.h"
#include "FLSH.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim4;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
//下面定义关于蜂鸣器的一些
#define Beep_GPIO GPIOB
#define Beep_Pin GPIO_PIN_0
unsigned char Beep1=0;
//下面定义IO
#define Key1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13)
#define Key2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)
#define Beep_0 HAL_GPIO_WritePin(Beep_GPIO,Beep_Pin,GPIO_PIN_RESET)
#define Beep_1 HAL_GPIO_WritePin(Beep_GPIO,Beep_Pin,GPIO_PIN_SET)
#define LED0_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_RESET)
#define LED0_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_SET)
#define LED1_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET)
#define LED1_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET)
#define JDQ_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET)
#define JDQ_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET)
//下面定义关于按键用的
unsigned char key1_flag=0;
unsigned char key2_flag=0;
unsigned char sec1=0;
unsigned char sec2=0;
//下面定义关于酒精的一些变量
unsigned int Alcohol = 0;
unsigned char Read_ADC = 0;
unsigned char Alcohol_H = 0;
//设定关于定时器的
unsigned int Time4_ms=0;
//下面定义存储部分用到的
unsigned char memory_flag=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM4_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
void memory()
{ if(memory_flag==1)
{
memory_flag=0;
Write_flsh_byte(0x0800F000,Alcohol_H);
}
}
void read_memory()
{
Alcohol_H=readFlash(0x0800F000);
if(Alcohol_H>100) Alcohol_H=20;
}
void Display()
{
LCD1602_write(0,0x80);
LCD1602_writebyte(" Alcohol :");
LCD1602_write(1,0x30+Alcohol/100%10);
LCD1602_write(1,0x30+Alcohol/10%10);
LCD1602_write(1,0x30+Alcohol%10);
LCD1602_writebyte("% ");
LCD1602_write(0,0xC0);
LCD1602_writebyte("Alcohol_H:");
LCD1602_write(1,0x30+Alcohol_H/100%10);
LCD1602_write(1,0x30+Alcohol_H/10%10);
LCD1602_write(1,0x30+Alcohol_H%10);
LCD1602_writebyte("% ");
}
void Key_Dispose()
{
if(!Key1)
{
if(key1_flag==1||sec1==0)
{
key1_flag=0;
if(Alcohol_H<100) Alcohol_H++;
}
}
else
{
if(key1_flag==0)
{
key1_flag=1;
memory_flag = 1;
}
sec1=2;
}
if(!Key2)
{
if(key2_flag==1||sec2==0)
{
key2_flag=0;
if(Alcohol_H>0) Alcohol_H--;
}
}
else
{
if(key2_flag==0)
{
key2_flag=1;
memory_flag = 1;
}
sec2=2;
}
}
void Get_ADC()
{
if(Read_ADC==1)
{
Read_ADC=0;
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,0xffff); //等待检测结束
Alcohol=((HAL_ADC_GetValue(&hadc1)))/31; //就目前电路来说,测的数据是 0-2.5V电压,也就是 0 - 3100AD 对应 0 - 100%。 2.5V-3100Ad 是因为 STM32 12位AD 3.3V=4095AD
HAL_ADC_Stop(&hadc1); //结束AD检测
}
}
void Police()
{
if(Alcohol_H<Alcohol)
{
Beep1=1;
LED0_1;
LED1_0;
JDQ_0;
}
else
{
Beep1=0;
LED0_0;
LED1_1;
JDQ_1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) //定时器设定的是每隔1ms执行一次
{
if(htim==&htim4)
{
Time4_ms++;
if(Time4_ms%50==0) Key_Dispose();
if(Time4_ms%100==0)
{
if(Beep1==1) Beep_GPIO->ODR^= Beep_Pin;
else Beep_0;
}
if(Time4_ms%1000==0)
{
Read_ADC=1;
if(sec1!=0) sec1--;
if(sec2!=0) sec2--;
}
if(Time4_ms>=2000) Time4_ms=0;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
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_ADC1_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
HAL_Delay(100);
LCD1602_cls();
HAL_TIM_Base_Start_IT(&htim4);
read_memory();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
memory();
Display();
Get_ADC();
Police();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses 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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* ADC1 init function */
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
/* TIM4 init function */
static void MX_TIM4_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim4.Instance = TIM4;
htim4.Init.Prescaler = 63;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 1000;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_10, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
/*Configure GPIO pins : PB0 PB10 PB11 PB12 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : PB13 PB14 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
代码:
STM32.7z
(2.9 MB, 下载次数: 70)
|