硬件设计
(末尾附文件)
仿真图如下所示:
程序设计
#include "main.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "timer.h"
char flag1; //定时时间到标志
u8 counter; //计数器
u8 state; //当前电梯状态
u8 statepre; //之前电梯状态
u8 flag; //电梯上下标志,0下,1上
u8 i;
char in1=0, in2=0, in3=0, in4=0, up1=0, up2=0, up3=0, down2=0, down3=0, down4=0; //KEY
u8 table[]={0xf9, 0xa4,0xb0,0x99, 0xa1, 0xc1, 0xff}; //楼层显示码表,一共是四层
void keyscan(void);
void elvator(void);
void display()
{
GPIOB->ODR &=0xFFFFFFF0 ;
set_DigitalNum(state); //楼乘标号
GPIOB->ODR |=0x00000002 ;
Delay_ms(2);
GPIOB->ODR &=0xFFFFFFF0 ;
set_DigitalNum(flag);
GPIOB->ODR |=0x00000008 ;
Delay_ms(2);
}
void iniclock() //start clock
{
TIM3->CR1|=0x01;
}
void run()
{
for(i=0;i<50;i++)//100ms
{
keyscan();
display();
}
}
int main()
{
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
Delay_Init(); // 延时程序初始化
Led_Init(); // LED灯初始化
MultDigitalNum_Init(); // 数码管初始化
KEY_Init(); // 按键初始化
//TIM3_Int_Init(499,7199);//50ms进入一次中断
state=1;
statepre=1;
flag=1;//上
flag1=0;
while(1)
{
elvator();
}
}
void keyscan(void)
{
//内一按下动作
if(!(GPIOC->IDR & (1 << 0)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 0)))
{
in1=1; //被按下后标记,亮灯,下同
while(!(GPIOC->IDR & (1 << 0)));
}
}
//内二按下动作
if(!(GPIOC->IDR & (1 << 1)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 1)))
{
in2=1; //被按下后标记,亮灯,下同
while(!(GPIOC->IDR & (1 << 1)));
}
}
//内三按下动作
if(!(GPIOC->IDR & (1 << 2)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 2)))
{
in3=1; //被按下后标记,亮灯,下同
while(!(GPIOC->IDR & (1 << 2)));
}
}
//内四按下动作
if(!(GPIOC->IDR & (1 << 3)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 3)))
{
in4=1; //被按下后标记,亮灯,下同
while(!(GPIOC->IDR & (1 << 3)));
}
}
//1上按下动作
if(!(GPIOC->IDR & (1 << 4)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 4)))
{
up1=1;
lup1(1);
while(!(GPIOC->IDR & (1 << 4)));
}
}
//2上按下动作
if(!(GPIOC->IDR & (1 << 5)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 5)))
{
up2=1;
lup2(1);
while(!(GPIOC->IDR & (1 << 5)));
}
}
//3上按下动作
if(!(GPIOC->IDR & (1 << 6)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 6)))
{
up3=1;
lup3(1);
while(!(GPIOC->IDR & (1 << 6)));
}
}
//2下按下动作
if(!(GPIOC->IDR & (1 << 7)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 7)))
{
down2=1;
ldown2(1);
while(!(GPIOC->IDR & (1 << 7)));
}
}
//3下按下动作
if(!(GPIOC->IDR & (1 << 8)))
{
Delay_ms(1); //消抖
if(!(GPIOC->IDR & (1 << 8)))
{
down3=1;
ldown3(1);
while(!(GPIOC->IDR & (1 << 8)));
}
}
.
链接:https://pan.baidu.com/s/1DgzPWbEBcjLpA8_Bk1F_Uw
提取码:0wx5
.