当前位置: 首页 > news >正文

云南网站建衡阳的房地产网站建设

云南网站建,衡阳的房地产网站建设,企业的互联网推广,h5和html5的区别蓝桥杯嵌入式第8届真题(完成) STM32G431 题目 分析和代码 对比第六届和第七届#xff0c;这届的题目在逻辑思维上确实要麻烦不少#xff0c;可以从题目看出#xff0c;这届题目对时间顺序的要求很严格#xff0c;所以就可以使用状态机的思想来编程#xff0c;拿到类似题…蓝桥杯嵌入式第8届真题(完成) STM32G431 题目 分析和代码 对比第六届和第七届这届的题目在逻辑思维上确实要麻烦不少可以从题目看出这届题目对时间顺序的要求很严格所以就可以使用状态机的思想来编程拿到类似题目不要急着写代码一定要先分析好步骤想出状态转换的逻辑后在根据逻辑写代码写起来就很快了。 main.c /* USER CODE BEGIN Header */ /********************************************************************************* file : main.c* brief : Main program body******************************************************************************* attention** h2centercopy; Copyright (c) 2021 STMicroelectronics.* All rights reserved./center/h2** This software component is licensed by ST under BSD 3-Clause license,* the License; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************/ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include main.h #include rtc.h #include tim.h #include gpio.h/* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include key.h #include led.h #include string.h #include stdio.h /* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ extern struct Key key[4];//4个按键 uint8_t lcdtext[30]; //lcd显示的内容 uint8_t status[30]; //lcd显示的内容 uint8_t led;//LED状态 uint8_t setfloor[5]{0,0,0,0,0};//四层楼为了方便1-4所以数组大小为5 uint32_t key_time_1s; //按键按下后等待的1s uint32_t open_or_close_door_time_4s; //开门或者关门所需时间 uint32_t up_or_down_dir_time_6s; //电梯往上一楼或者往下一楼所需的时间 uint32_t wait_time_2s; //电梯在每层楼等待时间 uint8_t current_floor 1;//当前所在层 RTC_DateTypeDef D; //用于显示日期 RTC_TimeTypeDef T; //用于显示时间 uint8_t process_status 0;//执行的状态 uint8_t dir 2;//是上还是下默认停止 //下面用于流水灯的部分 uint32_t lastUpdateTime 0; // 上次更新LED状态的时间 const uint32_t updateInterval 200; // 更新间隔以毫秒为单位 uint8_t flow_led_enable 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 led_process(void); void key_process(void); void lcd_process(void); void status_process(void);/* 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_TIM2_Init();MX_RTC_Init();MX_TIM16_Init();MX_TIM17_Init();/* USER CODE BEGIN 2 */HAL_TIM_Base_Start_IT(htim2);LCD_Init();/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);LED_display(0x00);sprintf((char *)lcdtext, %d,current_floor); //默认为1楼LCD_DisplayStringLine(Line3,lcdtext);while (1){led_process();lcd_process();key_process();status_process();/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */ }/*** brief System Clock Configuration* retval None*/ void SystemClock_Config(void) {RCC_OscInitTypeDef RCC_OscInitStruct {0};RCC_ClkInitTypeDef RCC_ClkInitStruct {0};RCC_PeriphCLKInitTypeDef PeriphClkInit {0};/** Configure the main internal regulator output voltage*/HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;RCC_OscInitStruct.HSIState RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.LSIState RCC_LSI_ON;RCC_OscInitStruct.PLL.PLLState RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM RCC_PLLM_DIV2;RCC_OscInitStruct.PLL.PLLN 20;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();}/** Initializes the CPU, AHB and APB buses 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_DIV1;RCC_ClkInitStruct.APB2CLKDivider RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(RCC_ClkInitStruct, FLASH_LATENCY_2) ! HAL_OK){Error_Handler();}/** Initializes the peripherals clocks*/PeriphClkInit.PeriphClockSelection RCC_PERIPHCLK_RTC;PeriphClkInit.RTCClockSelection RCC_RTCCLKSOURCE_LSI;if (HAL_RCCEx_PeriphCLKConfig(PeriphClkInit) ! HAL_OK){Error_Handler();} }/* USER CODE BEGIN 4 */ void key_process(void) {if((process_status0||process_status1))//没有按键按下时和按键按下1s内还可以继续按{sprintf((char *)status, wait key );if(key[0].key_flagcurrent_floor!01) //不能是当前楼层{key[0].key_flag 0;setfloor[1] 1;//设置目标楼层led|0x01; //设置对应led打开LED_display(led);//打开对应ledprocess_status 1;//按键按下后进入1s等待状态key_time_1s uwTick; //更新按键按下时时间}if(key[1].key_flagcurrent_floor!11){key[1].key_flag 0;setfloor[2] 1;led|0x02;LED_display(led);process_status 1;key_time_1s uwTick;}if(key[2].key_flagcurrent_floor!21){key[2].key_flag 0;setfloor[3] 1;led|0x04;LED_display(led);process_status 1;key_time_1s uwTick;}if(key[3].key_flagcurrent_floor!31){key[3].key_flag 0;setfloor[4] 1;led|0x08;LED_display(led);process_status 1;key_time_1s uwTick;}}}void lcd_process(void) {HAL_RTC_GetDate(hrtc,D,RTC_FORMAT_BIN);HAL_RTC_GetTime(hrtc,T,RTC_FORMAT_BIN);sprintf((char *)lcdtext, FLOOR);LCD_DisplayStringLine(Line1,lcdtext); // sprintf((char *)lcdtext, %d,current_floor); // LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext, %02d:%02d:%02d,T.Hours,T.Minutes,T.Seconds);LCD_DisplayStringLine(Line4,lcdtext);//LCD_ClearLine(Line7);sprintf((char *)lcdtext,%s,status);LCD_DisplayStringLine(Line7,lcdtext);sprintf((char *)lcdtext, floors:%d-%d-%d-%d,setfloor[1],setfloor[2],setfloor[3],setfloor[4]);LCD_DisplayStringLine(Line8,lcdtext); }void status_process(void) {if(process_status){switch(process_status){case 1: //状态1等待1s内是否有按键按下{sprintf((char *)status, wait key 1s );if((uwTick-key_time_1s)1000) //如果1s到了进入下一个关门状态{sprintf((char *)status, key_1s_yes );process_status 2;}}break;case 2: //状态2开始关门{sprintf((char *)status, close door );HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);//关门__HAL_TIM_SET_COMPARE(htim17,TIM_CHANNEL_1,250); //占空比50%HAL_TIM_PWM_Start(htim17,TIM_CHANNEL_1);process_status 3; //进入等待关门状态open_or_close_door_time_4s uwTick;//更新关门时间}break;case 3://状态34s关门时间等待关完门开始判断电梯是上行还是下行{sprintf((char *)status,wait close door 4s );if(uwTick-open_or_close_door_time_4s4000) //关门后开始判断上下行{sprintf((char *)status,close door yes );int up 0, down 0;HAL_TIM_PWM_Stop(htim17,TIM_CHANNEL_1);// 检查上行for(int i current_floor 1; i 5; i) { //如果电梯数组中存在比当前楼层高的楼层被设置if(setfloor[i] 1) {up 1;break;}}// 检查下行for(int i current_floor - 1; i 1; i--) { //如果电梯数组中存在比当前楼层低的楼层被设置if(setfloor[i] 1) {down 1;break;}}// 判断方向if(up !down) { //只有往上dir 1;} else if(down !up) { //只有往下dir 0;} else if(up down) { //上下都有先向上dir 1;} else {dir 2; //都没有}sprintf((char *)status, move );sprintf((char *)lcdtext, dir:%d--%d,up,down);LCD_DisplayStringLine(Line9,lcdtext);if(dir1)//上行{flow_led_enable 1;HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);__HAL_TIM_SET_COMPARE(htim16,TIM_CHANNEL_1,800); //占空比80%HAL_TIM_PWM_Start(htim16,TIM_CHANNEL_1);up_or_down_dir_time_6s uwTick;//更新上行时间process_status 4;}else if(dir0)//下行{flow_led_enable 1;HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);__HAL_TIM_SET_COMPARE(htim16,TIM_CHANNEL_1,600); //占空比80%HAL_TIM_PWM_Start(htim16,TIM_CHANNEL_1);up_or_down_dir_time_6s uwTick;//更新上行时间process_status 4;}else if(dir2)//既不需要上行也不需要下行回到电梯状态{process_status 0;}}}break;case 4://状态4等待6s上下行时间{sprintf((char *)status, wait move 6s );if(uwTick-up_or_down_dir_time_6s6000) //上下行时间到了{sprintf((char *)status, move yes );if(dir1) //如果是上行{current_floor1;}else{current_floor-1;}HAL_TIM_PWM_Stop(htim16,TIM_CHANNEL_1);//setfloor[current_floor] 0;//已到达该层// 显示楼层号并闪烁两次for(int i 0; i 2; i) // 闪烁两次{sprintf((char *)lcdtext, %d,current_floor);LCD_DisplayStringLine(Line3, lcdtext); // 显示楼层号HAL_Delay(500); // 延时500msLCD_ClearLine(Line3); // 清除楼层号显示HAL_Delay(500); // 延时500ms}// 再次显示楼层号sprintf((char *)lcdtext, %d,current_floor);LCD_DisplayStringLine(Line3, lcdtext);switch(current_floor){case 1:{led ~0x01; // 使用位清除操作关闭LED1}break;case 2:{led ~0x02; // 关闭LED2}break;case 3:{led ~0x04; // 关闭LED3}break;case 4:{led ~0x08; // 关闭LED4}break;}LED_display(led); // 更新LED显示flow_led_enable 0; // 关闭流水灯led 0x0F; // 保持低四位状态不变关闭高四位LEDLED_display(led); // 更新LED显示open_or_close_door_time_4s uwTick;//更新开门时间process_status 5;}}break;case 5 ://状态5:等待开门4s时间到{sprintf((char *)status, wait open door 4s );if(uwTick-open_or_close_door_time_4s4000)//打开门{sprintf((char *)status, open door yes );wait_time_2s uwTick;//更新等待时间process_status 6;}}break;case 6 ://状态5:等待开门4s时间到{sprintf((char *)status, wait 2s );if(uwTick-wait_time_2s2000)//每层停留时间{sprintf((char *)status, wait 2s yes );wait_time_2s uwTick;//更新等待时间process_status 2;}}break;}}}void led_process(void) {static uint8_t flow_led_state 0; // 初始状态为0表示流水灯未激活if((uwTick - lastUpdateTime) updateInterval flow_led_enable){lastUpdateTime uwTick; // 更新最后一次更新时间if(flow_led_state 0) // 如果流水灯未激活根据方向初始化流水灯状态{flow_led_state (dir 1) ? 0x08 : 0x01; // 从左侧或右侧开始}else{if(dir 1) // 上行从右到左流水{flow_led_state 1; // 向左移动if(flow_led_state 0x08) // 如果超过了最左侧重置到最右侧{flow_led_state 0x01;}}else if(dir 0) // 下行从左到右流水{flow_led_state 1; // 向右移动if(flow_led_state 0x01) // 如果超过了最右侧重置到最左侧{flow_led_state 0x08;}}}// 更新LED状态仅修改高四位保持低四位不变// 注意这里假设flow_led_state只影响一个LED需要根据实际情况调整led (led 0x0F) | (flow_led_state 4); // 将流水灯状态左移4位合并到led的高四位LED_display(led); // 更新LED显示} }/* USER CODE END 4 *//*** brief This function is executed in case of error occurrence.* retval None*/ void Error_Handler(void) {/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state *//* USER CODE END Error_Handler_Debug */ }#ifdef USE_FULL_ASSERT /*** brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* param file: pointer to the source file name* param line: assert_param error line source number* retval None*/ void assert_failed(uint8_t *file, uint32_t line) {/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,tex: printf(Wrong parameters value: file %s on line %d\r\n, file, line) *//* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ key.c #include key.hstruct Key key[4] {0,0,0,0};extern uint8_t process_status; void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {if(htim-InstanceTIM2){if((process_status0||process_status1)){key[0].key_gpio HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[1].key_gpio HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[2].key_gpio HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[3].key_gpio HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);for(int i 0;i4;i){switch(key[i].key_index){case 0:{if(key[i].key_gpio0){key[i].key_index1;}}break;case 1:{if(key[i].key_gpio0){key[i].key_flag 1;key[i].key_index2;}else{key[i].key_index0;}}break;case 2:{if(key[i].key_gpio1){key[i].key_index0;}}break;}}}}} 我一共将这个题目分为了6个状态 状态0 此状态用于等待按键按下以设置目标楼层只要右按键按下就将对应的楼层数组置1打开对应的led记录当前按键按下的时间最后进入状态1 状态1 此状态的进入是由于存在按键被按下设置了目标楼层该状态一直等待1s的到来在状态0和状态1状态下按键仍然可以按下因为某个按键按下后在1s内仍然可以设置目标楼层每一次重新按下按键倒计时都会刷新同样为了防止其余状态下按键仍然可以按下导致flag置1在key.c的定时器回调函数中也只有状态0和状态1才能判断按键是否按下 状态2 此状态为开始关门状态根据题目要求将PA5置低电平同时设置TIM17通道1的占空比为50% 更新关门时间进入状态3 状态3 题目要求开关门都需要4s所以此状态为等待关门状态等待结束后开始根据setfloor数组确定当前电梯是向上还是向下设置的楼层会导致4种状态 只有比当前楼层高的楼层被设置dir1,往上走只有比当前楼层低的楼层被设置dir0往下走比当前楼层高的和低的都设置例如当前楼层是2层比2层低的1层和比2层高的34层都被设置dir1,同样先向上走再往下走默认状态都没有被设置在按键时已经限制条件不能设置当前楼层dir2保持在当前楼层 根据dir的取值设置上行还是下行并打开对应PWM输出和电平信号更新电梯开始上下行时间或者是就在当前层然后回到状态0等待设置目标楼层 状态4 题目要求上行或者下行都需要6s该状态是为了等待6s到来时间到来后根据dir将当前楼层1或者-1同时当前楼层闪烁两次关闭当前楼层对应的led灯关闭流水灯然后更新开始开门时间进入状态5 状态5: 等待开门时间4s时间到达后更新每层停留的2s时间进入下一个状态状态6 状态6: 每层的等待时间2s等待完后回到状态2开始关门继续按顺序执行直到没有目标楼层回到状态0等待按键按下。 led.c #include led.hvoid LED_display(uint8_t led) {HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOC,led8,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET); } led_process用于处理流水灯根据电梯运行方向确定流水灯方向led (led 0x0F) | (flow_led_state 4); 这句的意思是首先保持低四位的保持不变然后将流水灯的状态左移4位与之相或从而实现不影响低四位led的效果 为了便于观察状态添加了一些表示状态的信息显示在lcd上时间控制大部分使用滴答定时器uwTick以防止delay阻塞程序
http://www.w-s-a.com/news/774435/

相关文章:

  • 网站首页效果图wordpress 在线教育
  • 电商网站开发团队广西桂林商贸旅游技工学校
  • 网站模板文件怎么下载东莞常平镇邮政编码
  • 建网站需要什么wordpress误删的后果
  • wordpress无插件实现网站地图做阿里巴巴网站店铺装修费用
  • 英文互动网站建设南宁住房和城乡建设局网站
  • 威海微网站建设乐清建网站哪家强
  • 网站和app的开发成本saas系统开发教程
  • ps切片工具做网站大气简洁网站
  • 网至普的营销型网站建设wordpress邮箱验证插件下载
  • 找权重高的网站方法张家港早晨网站建设
  • WordPress数据库添加管理员关键词优化举例
  • 河南国基建设集团--官方网站wordpress qode
  • 做农村电子商务的网站有哪些内容静态网站模板古典
  • 导航网站设计方案个人网站推广方法
  • 网站排名易下拉教程防wordpress花园
  • 计算机网站建设 是什么意思现在网站建站的主流语言是什么
  • php网站跟随导航西安百姓网免费发布信息网
  • 濮阳做公司网站html5 特效网站
  • ppt设计器怎么打开深圳seo网络推广营销
  • 建设银行网站用360浏览器建设信用卡中心网站
  • 创建公司网站 教程广州建设局
  • 详述网站建设的过程简答题ui培训设计怎么样
  • 动易网站官网ppt主题大全素材
  • 怎样用eclipse做网站可以做宣传图的网站
  • 哪里可以做游戏视频网站做网站平台应该注意哪些
  • 网站后期推广是谁来做网页制作步骤作答题
  • 全屋装修设计定制整装成都网站优化多少钱
  • html5购物网站模板一个网站两个数据库
  • 个人网站怎么做微信支付网站建设项目介绍