1 题目
2 代码
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "rtc.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "lcd.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
double v;
char buf1[20],buf2[20],buf3[20],buf4[20],buf5[20],buf6[20],buf7[20],buf8[20],buf9[20],buf10[20],buf11[20],buf12[20],buf13[20];
struct keys{
int state;
int step;
int cnt;
int cnt2;
}key[5];
double f=250,duty=0.2;
int N;
uint32_t OCcnt=0;
int state;
int start,end,R=1,K=1,TR=1,TK=1;
double V,f7,cnt,hmax,lmax;
int time,SR;
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {0};
/* 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 */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void getADC()
{
HAL_ADC_Start(&hadc2);
v=HAL_ADC_GetValue(&hadc2)*3.3/4096;
}
void LED_Close()
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM3&&htim->Channel==HAL_TIM_ACTIVE_CHANNEL_2)
{
cnt=HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2);
f7=1000000/cnt;
__HAL_TIM_SetCounter(&htim3,0);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM1)
{
key[1].state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
key[2].state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
key[3].state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
key[4].state=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
switch(key[1].step)
{
case 0:
{
if(key[1].state==GPIO_PIN_RESET)
{
key[1].step=1;
}
}
break;
case 1:
{
if(key[1].state==GPIO_PIN_RESET)
{
key[1].step=2;
key[1].cnt++;
LCD_Clear(White);
if(key[1].cnt%3==0)
{
LCD_DisplayStringLine(Line1,(uint8_t*)buf1);
LCD_DisplayStringLine(Line3,(uint8_t*)buf2);
LCD_DisplayStringLine(Line4,(uint8_t*)buf3);
LCD_DisplayStringLine(Line5,(uint8_t*)buf4);
}
if(key[1].cnt%3==1)
{
LCD_DisplayStringLine(Line1,(uint8_t*)buf5);
LCD_SetBackColor(Green);
LCD_ClearLine(Line3);
LCD_DisplayStringLine(Line3,(uint8_t*)buf6);
LCD_SetBackColor(White);
LCD_DisplayStringLine(Line4,(uint8_t*)buf7);
}
if(key[1].cnt%3==2)
{
LCD_DisplayStringLine(Line1,(uint8_t*)buf8);
LCD_DisplayStringLine(Line3,(uint8_t*)buf9);
LCD_DisplayStringLine(Line4,(uint8_t*)buf10);
LCD_DisplayStringLine(Line5,(uint8_t*)buf11);
}
}
else
{
key[1].step=0;
}
}
break;
case 2:
{
if(key[1].state==GPIO_PIN_SET)
{
key[1].step=0;
}
}
break;
}
switch(key[2].step)
{
case 0:
{
if(key[2].state==GPIO_PIN_RESET&&key[1].cnt%3!=2)
{
key[2].step=1;
}
}
break;
case 1:
{
if(key[2].state==GPIO_PIN_RESET)
{
key[2].step=2;
if(key[1].cnt%3==0)
{
if(state==1)
{
end=uwTick;
if(end-start>=1000*5)
{
state=0;
}
}
if(state==0)
{
key[2].cnt++;
}
}
if(key[1].cnt%3==1)
{
key[2].cnt2++;
}
}
else
{
key[2].step=0;
}
}
break;
case 2:
{
if(key[2].state==GPIO_PIN_SET)
{
key[2].step=0;
}
}
break;
}
switch(key[3].step)
{
case 0:
{
if(key[3].state==GPIO_PIN_RESET)
{
key[3].step=1;
}
}
break;
case 1:
{
if(key[3].state==GPIO_PIN_RESET)
{
key[3].step=2;
if(key[2].cnt2%2==0)
{
TR++;
if(TR>10) TR=1;
sprintf(buf6," R=%d",TR);
LCD_ClearLine(Line3);
LCD_DisplayStringLine(Line3,(uint8_t*)buf6);
}
else
{
TK++;
if(TK>10) TK=1;
sprintf(buf7," K=%d",TK);
LCD_ClearLine(Line4);
LCD_DisplayStringLine(Line4,(uint8_t*)buf7);
}
}
else
{
key[3].step=0;
}
}
break;
case 2:
{
if(key[3].state==GPIO_PIN_SET)
{
key[3].step=0;
}
}
break;
}
if(key[1].cnt%3==1)
{
switch(key[4].step)
{
case 0:
{
if(key[4].state==GPIO_PIN_RESET)
{
key[4].step=1;
}
}
break;
case 1:
{
if(key[4].state==GPIO_PIN_RESET)
{
key[4].step=2;
if(key[2].cnt2%2==0)
{
TR--;
if(TR<=0) TR=10;
sprintf(buf6," R=%d",TR);
LCD_ClearLine(Line3);
LCD_DisplayStringLine(Line3,(uint8_t*)buf6);
}
else
{
TK--;
if(TK<=0) TK=10;
sprintf(buf7," K=%d",TK);
LCD_ClearLine(Line4);
LCD_DisplayStringLine(Line4,(uint8_t*)buf7);
}
}
else
{
key[4].step=0;
}
}
break;
case 2:
{
if(key[4].state==GPIO_PIN_SET)
{
key[4].step=0;
}
}
break;
}
}
if(key[1].cnt%3==0)
{
switch(key[4].step)
{
case 0:
{
if(key[4].state==GPIO_PIN_RESET)
{
key[4].step=1;
time=0;
}
}
break;
case 1:
{
if(key[4].state==GPIO_PIN_RESET)
{
key[4].step=2;
}
else
{
key[4].step=0;
}
}
break;
case 2:
{
if(key[4].state==GPIO_PIN_RESET)
{
time++;
}
if(key[4].state==GPIO_PIN_SET)
{
if(time>200)
{
SR=1;
}
else
{
SR=0;
}
key[4].step=0;
}
}
break;
}
}
}
if(htim->Instance==TIM4)
{
V=f7*2*3.14*R/100/K;
sprintf(buf4," V=%.1lf",V);
sprintf(buf3," P=%.0lf%%",duty*100);
if(key[1].cnt%3==0)
{
LCD_ClearLine(Line4);
LCD_ClearLine(Line5);
LCD_DisplayStringLine(Line5,(uint8_t*)buf4);
LCD_DisplayStringLine(Line4,(uint8_t*)buf3);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
if(key[1].cnt%3!=0)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
if(key[1].cnt%3!=1)
{
R=TR;
K=TK;
key[2].cnt2=0;
}
if(key[2].cnt%2==0)
{
if(V>lmax)
{
lmax=V;
sprintf(buf11," NL=%.1lf",lmax);
}
}
if(key[2].cnt%2==1)
{
if(V>hmax)
{
hmax=V;
sprintf(buf10," NH=%.1lf",hmax);
}
}
if(key[1].cnt%3==2)
{
LCD_ClearLine(Line3);
LCD_ClearLine(Line4);
LCD_ClearLine(Line5);
LCD_DisplayStringLine(Line5,(uint8_t*)buf11);
LCD_DisplayStringLine(Line4,(uint8_t*)buf10);
LCD_DisplayStringLine(Line3,(uint8_t*)buf9);
}
if(SR==1)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
if(key[1].cnt%3==1)
{
LCD_ClearLine(Line3);
LCD_ClearLine(Line4);
LCD_SetBackColor(Green);
if(key[2].cnt2%2==0)
{
LCD_ClearLine(Line3);
LCD_DisplayStringLine(Line3,(uint8_t*)buf6);
LCD_SetBackColor(White);
LCD_DisplayStringLine(Line4,(uint8_t*)buf7);
}
else
{
LCD_ClearLine(Line4);
LCD_DisplayStringLine(Line4,(uint8_t*)buf7);
LCD_SetBackColor(White);
LCD_DisplayStringLine(Line3,(uint8_t*)buf6);
}
}
}
if(htim->Instance==TIM6)
{
if(f>125&&f<250)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_9);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM2&&htim->Channel==HAL_TIM_ACTIVE_CHANNEL_2)
{
OCcnt=__HAL_TIM_GetCompare(&htim2,TIM_CHANNEL_2);
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1)==GPIO_PIN_RESET)
{
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,OCcnt+f*(1-duty));
}
else
{
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,OCcnt+f*duty);
}
}
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
HAL_RTC_GetDate(hrtc,&sDate,RTC_FORMAT_BIN);
HAL_RTC_GetTime(hrtc,&sTime,RTC_FORMAT_BIN);
sprintf(buf13," TIME:%02d-%02d-%02d",sTime.Hours,sTime.Minutes,sTime.Seconds);
sprintf(buf12," DATE:%02d-%02d-%02d",sDate.Year,sDate.Month,sDate.Date);
LCD_ClearLine(Line8);
LCD_ClearLine(Line9);
LCD_DisplayStringLine(Line8,(uint8_t*)buf13);
LCD_DisplayStringLine(Line9,(uint8_t*)buf12);
}
/* 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_TIM1_Init();
MX_TIM2_Init();
MX_ADC2_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_RTC_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LED_Close();
LCD_Clear(White);
LCD_SetBackColor(White);
LCD_SetTextColor(Black);
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_OC_Start_IT(&htim2,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,500);
sprintf(buf1," DATA");
sprintf(buf2," M=L");
sprintf(buf8," RECD");
sprintf(buf5," PARA");
sprintf(buf6," R=1");
sprintf(buf7," K=1");
sprintf(buf10," NH=0.0");
LCD_DisplayStringLine(Line1,(uint8_t*)buf1);
LCD_DisplayStringLine(Line3,(uint8_t*)buf2);
LCD_DisplayStringLine(Line4,(uint8_t*)buf3);
LCD_DisplayStringLine(Line5,(uint8_t*)buf4);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
getADC();
if(SR==0)
{
if(v>=1&&v<=3) duty=0.375*v-0.275;
if(v<1) duty=0.1;
if(v>3) duty=0.85;
}
if(key[1].cnt%3==0&&key[2].cnt%2==1&&state==0)
{
N++;
state=1;
start=uwTick;
for(int i=4000;i<=8000;i+=100)
{
f=1000000/i;
HAL_Delay(100);
}
sprintf(buf2," M=H");
LCD_ClearLine(Line3);
LCD_DisplayStringLine(Line3,(uint8_t*)buf2);
}
if(key[1].cnt%3==0&&key[2].cnt%2==0&&key[2].cnt>0&&state==0)
{
N++;
state=1;
start=uwTick;
for(int i=8000;i>=4000;i-=100)
{
f=1000000/i;
HAL_Delay(100);
}
sprintf(buf2," M=L");
LCD_ClearLine(Line3);
LCD_DisplayStringLine(Line3,(uint8_t*)buf2);
}
sprintf(buf9," N=%d",N);
}
/* USER CODE END 3 */
}