蓝桥杯嵌入式第八届(代查)

void LED_SHOW(uint8_t led)
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15|GPIO_PIN_14|GPIO_PIN_13|GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_8,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,led<<8,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
void LED_Proc(void)
{
	LED_SHOW(LED);
	LED&=0xf0;
	LED|=1<<(Floor-1);
	if(Motor_step==2)
	{
		LED_num++;
		if(Up_Down==1)
		{
			LED|=(0x10<<(LED_num-1));
			if(LED_num==5)
			{
				LED&=0x0f;
				LED_num=0;
			}
		}
		else if(Up_Down==2)
		{
			if(LED_num==5)
			{
				LED&=0x0f;
				LED_num=0;
			}
			LED|=(0x80>>(LED_num-1));	
		}
	}
	else
	{
		LED&=0x0f;
		LED_num=0;
	}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM6)
	{
		if(Key_time_sign_1S==1)
		{
			one_num++;
			if(one_num==10)//一秒
			{
				one_num=0;
				Key_time_sign_1S=2;//楼层选择完毕
				Motor_step=1;
				Go_sign=1;
			}
		}
		if(Motor_step==1)//关门
		{
			four_num++;
			if(four_num==40)
			{
				four_num=0;
				Motor_step=2;
			}
		}
		else if(Motor_step==2)//升降
		{
			six_num++;
			if(six_num==60)
			{
				six_num=0;
				Motor_step=3;
			}
		}
		else if(Motor_step==4)//开门
		{
			four_num++;
			if(four_num==40)
			{
				four_num=0;
				Motor_step=5;//开门完毕
			}
		}
		else if((Floor_sum!=0)&&(Motor_step==5))//有目标层
		{
			tow_num++;
			if(tow_num==20)//两秒后关门开始升降
			{
				Motor_step=1;
				tow_num=0;
			}
		}
		
		LED_Proc();
		
		
	}
}

void LCD_Proc(void)//LCD显示函数
{
	HAL_RTC_GetTime(&hrtc,&RTC_sTime,RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc,&RTC_sDate,RTC_FORMAT_BIN);
	sprintf((char *)LCD_Str,"            %d   ",Floor);
	LCD_DisplayStringLine(Line1,LCD_Str);
	sprintf((char *)LCD_Str," Motor_step: %d",Motor_step);
	LCD_DisplayStringLine(Line2,LCD_Str);
	
	sprintf((char *)LCD_Str,"       %d:%d:%d",RTC_sTime.Hours,RTC_sTime.Minutes,RTC_sTime.Seconds);
	LCD_DisplayStringLine(Line7,LCD_Str);
}

void LED_Init(void)
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15|GPIO_PIN_14|GPIO_PIN_13|GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_8,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}


void Key_Proc(void)//按键程序
{
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET)
	{
		HAL_Delay(10);
		if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET)&&(Key_state==0)&&(Key_time_sign_1S!=2)&&(Floor!=1))//B1-DOWN
		{
			Key_state=1;
			Floor_sum|=0x01;
		}
	}
	else if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)!=GPIO_PIN_RESET)&&(Key_state==1))//B1-UP
	{
		Key_state=0;
		Key_time_sign_1S=1;
	}
	
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET)
	{
		HAL_Delay(10);
		if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET)&&(Key_state==0)&&(Key_time_sign_1S!=2)&&(Floor!=2))//B2-DOWN
		{
			Key_state=2;
			Floor_sum|=0x02;
		}
	}
	else if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)!=GPIO_PIN_RESET)&&(Key_state==2))//B2-UP
	{
		Key_state=0;
		Key_time_sign_1S=1;
	}
	
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET)
	{
		HAL_Delay(10);
		if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET)&&(Key_state==0)&&(Key_time_sign_1S!=2)&&(Floor!=3))//B3-DOWN
		{
			Key_state=3;
			Floor_sum|=0x04;
		}
	}
	else if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)!=GPIO_PIN_RESET)&&(Key_state==3))//B3-UP
	{
		Key_state=0;
		Key_time_sign_1S=1;
	}
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET)
	{
		HAL_Delay(10);
		if((HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET)&&(Key_state==0)&&(Key_time_sign_1S!=2)&&(Floor!=4))//B4-DOWN
		{
			Key_state=4;
			Floor_sum|=0x08;
			
		}
	}
	else if((HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)!=GPIO_PIN_RESET)&&(Key_state==4))//B4-UP
	{
		Key_state=0;
		Key_time_sign_1S=1;
	}
}
void Motor_Proc(void)//电机驱动函数
{
	switch(Motor_step)
	{
		case 0:
			HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);//防止上下行
			__HAL_TIM_SET_PRESCALER(&htim17,300);
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
			sprintf((char *)LCD_Str,"Door:Open    ");
			LCD_DisplayStringLine(Line3,LCD_Str);
			sprintf((char *)LCD_Str,"Motor:Stop   ");
			LCD_DisplayStringLine(Line4,LCD_Str);
			break;
		case 1://关门
			__HAL_TIM_SET_PRESCALER(&htim17,250);
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
			sprintf((char *)LCD_Str,"Door:Closeing");
			LCD_DisplayStringLine(Line3,LCD_Str);
			sprintf((char *)LCD_Str,"Motor:Stop   ");
			LCD_DisplayStringLine(Line4,LCD_Str);
			break;
		case 2://四秒后->上升或下降
			if(Floor_sum>(1<<(Floor-1)))//上升
			{
				HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
				__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,800);
				HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
				Up_Down=1;//上行标志位
				sprintf((char *)LCD_Str,"Door:Close   ");
				LCD_DisplayStringLine(Line3,LCD_Str);
				sprintf((char *)LCD_Str,"Motor:Uping   ");
				LCD_DisplayStringLine(Line4,LCD_Str);		
			}
			else if(Floor_sum<(1<<(Floor-1)))//下降
			{
				HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
				__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,600);
				HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
				Up_Down=2;//下行标志位
				sprintf((char *)LCD_Str,"Door:Close   ");
				LCD_DisplayStringLine(Line3,LCD_Str);
				sprintf((char *)LCD_Str,"Motor:Downing");
				LCD_DisplayStringLine(Line4,LCD_Str);
			}
			break;
		case 3://六秒后到达下一层->判断是否到达目标层
			if(Up_Down==1)
			{
				Floor+=1;
			}
			else if(Up_Down==2)
			{
				Floor-=1;
			}
			if(((Floor_sum>>(Floor-1))&0x01)==0)//未到达目标层
			{
				Motor_step=2;//再次上升或下降
			}
			else if(((Floor_sum>>(Floor-1))&0x01)==1)//到达目标层
			{
				Motor_step=4;//准备开门
			}
		 break;
		case 4://开门->清除该楼层记录
			HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);//防止上下行
			__HAL_TIM_SET_PRESCALER(&htim17,300);
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
			Floor_sum&=~(1<<(Floor-1));//清除该楼层记录
			sprintf((char *)LCD_Str,"Door:Opening");
			LCD_DisplayStringLine(Line3,LCD_Str);
			sprintf((char *)LCD_Str,"Motor:Stop   ");
			LCD_DisplayStringLine(Line4,LCD_Str);
			break;
		case 5://四秒后->开门完毕->判断是否还有目标层
			if(Floor_sum==0)//无目标层
			{
				Motor_step=0;
				Key_time_sign_1S=0;
				sprintf((char *)LCD_Str,"Door:Open   ");
				LCD_DisplayStringLine(Line3,LCD_Str);
				sprintf((char *)LCD_Str,"Motor:Stop   ");
				LCD_DisplayStringLine(Line4,LCD_Str);
			}
			break;
	}
}

/* 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_TIM3_Init();
  MX_TIM17_Init();
  MX_TIM6_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
	LED_Init();
	LCD_Init();
	LCD_SetTextColor(Black);
	LCD_Clear(White);
	LCD_SetBackColor(White);
	HAL_TIM_Base_Init(&htim17);
	HAL_TIM_Base_Init(&htim3);
	HAL_TIM_PWM_Init(&htim17);
	HAL_TIM_PWM_Init(&htim3);
	HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
	HAL_TIM_Base_Start_IT(&htim6);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		LCD_Proc();
		Key_Proc();

		Motor_Proc();
//		__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,800);
  }
  /* USER CODE END 3 */
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDKevin19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值