本文主要是介绍蓝桥杯嵌入式2017年第八届省赛主观题解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 题目
2 代码
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "rtc.h"
#include "tim.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "lcd.h"
#include "i2c_hal.h"
#include "string.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
char buf1[20],buf2[20],buf3[20],buf4[20],buf5[20];
struct keys{int step;int state;int stop;int light;
}key[5];
int time,flag,platform=1,door=1,cnt,flag2;
int t,temp,temp2,i,num,kick,k;
int up,down;
int cnt1,cnt_down1,f1;
double duty1;
int cnt2,cnt_down2,f2;
double duty2;
/* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/* 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 LED_up()
{HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);t=1000000; while(t--);
}
void LED_down()
{HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_RESET);t=1000000; while(t--);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);t=1000000; while(t--);
}
void an(int c)
{HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);if(c==1){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);}if(c==2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);}if(c==3){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);}if(c==4){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);}HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
int judge_light()
{num=0;for(int j=1;j<=4;j++){if(key[j].light==1){num++;}}if(num==0) return 0;else return 1;
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{HAL_RTC_GetTime(hrtc,&sTime,RTC_FORMAT_BIN);HAL_RTC_GetDate(hrtc,&sDate,RTC_FORMAT_BIN);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);LCD_ClearLine(Line8);sprintf(buf3," %02d:%02d:%02d",sTime.Hours,sTime.Minutes,sTime.Seconds);LCD_DisplayStringLine(Line8,(uint8_t*)buf3);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM1){if(flag==1) time++;if(time<=100){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].light==0&&platform!=1){key[1].step=1;}}break;case 1:{if(key[1].state==GPIO_PIN_RESET){key[1].step=2;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);flag=0;time=0;key[1].light=1;}else{key[1].step=0;}}break;case 2:{if(key[1].state==GPIO_PIN_SET){key[1].step=0;flag=1;}}break;}switch(key[2].step){case 0:{if(key[2].state==GPIO_PIN_RESET&&key[2].light==0&&platform!=2){key[2].step=1;}}break;case 1:{if(key[2].state==GPIO_PIN_RESET){key[2].step=2;HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);flag=0;time=0;key[2].light=1;}else{key[2].step=0;}}break;case 2:{if(key[2].state==GPIO_PIN_SET){key[2].step=0;flag=1;}}break;}switch(key[3].step){case 0:{if(key[3].state==GPIO_PIN_RESET&&key[3].light==0&&platform!=3){key[3].step=1;}}break;case 1:{if(key[3].state==GPIO_PIN_RESET){key[3].step=2;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);flag=0;time=0;key[3].light=1;}else{key[3].step=0;}}break;case 2:{if(key[3].state==GPIO_PIN_SET){key[3].step=0;flag=1;}}break;}switch(key[4].step){case 0:{if(key[4].state==GPIO_PIN_RESET&&key[4].light==0&&platform!=4){key[4].step=1;}}break;case 1:{if(key[4].state==GPIO_PIN_RESET){key[4].step=2;HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);flag=0;time=0;key[4].light=1;}else{key[4].step=0;}}break;case 2:{if(key[4].state==GPIO_PIN_SET){key[4].step=0;flag=1;}}break;}}if(time>100){if(kick==0){for(i=platform+1;i<=4&&temp2==0;i++){if(key[i].light==1){temp=i;door=0;temp2=1;break;}if(i==4&&key[i].light==0) kick=1;}if(i==5) kick=1;if(kick==0){cnt++;if(cnt<400&&flag==0){door=0;//开始关门flag=1;}if(cnt==400&&flag==1)//结束关门,开始上楼{cnt=0;flag=2;}if(cnt<600*(temp-platform)&&flag==2){up=1;}if(cnt==600*(temp-platform)&&flag==2)//到达楼层,开始开门,第一次闪烁{up=0;door=1;cnt=0;flag=3;platform=temp;LCD_ClearLine(Line5);t=10000000; while(t--);sprintf(buf2," %d",temp);LCD_DisplayStringLine(Line5,(uint8_t*)buf2);an(temp);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,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);}if(cnt==50&&flag==3)//第二次闪烁{cnt=0;LCD_ClearLine(Line5);t=10000000; while(t--);sprintf(buf2," %d",temp);LCD_DisplayStringLine(Line5,(uint8_t*)buf2);flag=4;}if(cnt==550&&flag==4){cnt=0;flag=0;temp2=0;key[temp].light=0;}if(judge_light()==0){time=0;flag=0;}}}if(kick==1){for(k=platform-1;k>=1&&temp2==0;k--){if(key[k].light==1){temp=k;door=0;temp2=1;break;}}cnt++;if(cnt<400&&flag==0){door=0;//开始关门flag=1;}if(cnt==400&&flag==1)//结束关门,开始上楼{cnt=0;flag=2;}if(cnt<600*(platform-temp)&&flag==2){down=1;}if(cnt==600*(platform-temp)&&flag==2)//到达楼层,开始开门,第一次闪烁{down=0;door=1;cnt=0;flag=3;platform=temp;LCD_ClearLine(Line5); t=10000000; while(t--);sprintf(buf2," %d",temp);LCD_DisplayStringLine(Line5,(uint8_t*)buf2);an(temp);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,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);}if(cnt==50&&flag==3)//第二次闪烁{cnt=0;LCD_ClearLine(Line5);t=10000000; while(t--);sprintf(buf2," %d",temp);LCD_DisplayStringLine(Line5,(uint8_t*)buf2);flag=4;}if(cnt==550&&flag==4){cnt=0;flag=0;temp2=0;key[temp].light=0;}if(judge_light()==0){time=0;flag=0;kick=0;}}}}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM2){if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_4){cnt1=HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_4);cnt_down1=HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_3);f1=10000000/cnt1;duty1=(double)cnt_down1/(double)cnt1;__HAL_TIM_SetCounter(&htim2,0);sprintf(buf4,"f1=%dduty1=%.2lf%%",f1,duty1*100);}}if(htim->Instance==TIM15){if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1){cnt2=HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1);cnt_down2=HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_2);f2=10000000/cnt2;duty2=(double)cnt_down2/(double)cnt2;__HAL_TIM_SetCounter(&htim15,0);sprintf(buf5,"f2=%dduty2=%.2lf%%",f2,duty2*100);}}
}
/* 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_RTC_Init();MX_TIM1_Init();MX_TIM16_Init();MX_TIM17_Init();MX_TIM2_Init();MX_TIM15_Init();/* USER CODE BEGIN 2 */LCD_Init();LCD_Clear(White);LCD_SetBackColor(White);LCD_SetTextColor(Black);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);LCD_DisplayStringLine(Line1,(uint8_t*)" Current Platform");sprintf(buf2," %d",platform);LCD_DisplayStringLine(Line5,(uint8_t*)buf2);LCD_DisplayStringLine(Line8,(uint8_t*)" 00:00:00"); HAL_TIM_Base_Start_IT(&htim1);HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);HAL_TIM_PWM_Start(&htim16,TIM_CHANNEL_1);/*HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_3);HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_4);HAL_TIM_IC_Start_IT(&htim15,TIM_CHANNEL_1);HAL_TIM_IC_Start_IT(&htim15,TIM_CHANNEL_2);*//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */if(down==1&&up==0){ LED_down();HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,600);}if(up==1&&down==0){LED_up();HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET); __HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,800);}if(down==0&&up==0){__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,0);}if(up==0&&down==0){__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,0);}if(door==1){HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,500);} if(door==0){HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,600);}}/* USER CODE END 3 */
}
这篇关于蓝桥杯嵌入式2017年第八届省赛主观题解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!