普中开源电子分享网

 找回密码
 立即注册
搜索
查看: 397|回复: 1

电梯实验

[复制链接]

2

主题

12

帖子

90

积分

注册会员

Rank: 2

积分
90
发表于 2024-2-19 14:21:07 | 显示全部楼层 |阅读模式
#include "reg52.h"
#include "MATH.h"
typedef unsigned int u16;       
typedef unsigned char u8;
typedef unsigned long u32;
typedef int uf16;       
typedef char uf8;
typedef long uf32;
sbit KEY1=P3^1;
sbit KEY2=P3^0;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
sbit up=P2^0;
sbit down=P2^1;
sbit dy=P2^2;
#define KEY1_PRESS        1
#define KEY2_PRESS        2
#define KEY3_PRESS        3
#define KEY4_PRESS        4
#define KEY_UNPRESS        0
u8 current=1;
u8 call=0;
u8 key=0;
u8 Floordifference=0;       
void delay_10us(u32 ten_us)
{
        while(ten_us--);       
}
u8 key_scan(u8 mode)
{
        static u8 key=1;

        if(mode)key=1;
        if(key==1&&(KEY1==0||KEY2==0||KEY3==0||KEY4==0))
        {
                delay_10us(1000);
                key=0;
                if(KEY1==0)
                        return KEY1_PRESS;
                else if(KEY2==0)
                        return KEY2_PRESS;
                else if(KEY3==0)
                        return KEY3_PRESS;
                else if(KEY4==0)
                        return KEY4_PRESS;       
        }
        else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1)       
        {
                key=1;
                return KEY_UNPRESS;                               
        }
        return KEY_UNPRESS;               
}
void assignment()
{
         while(1)
        {       
                        key=key_scan(0);
                        if(key==KEY1_PRESS)
                        {
                                call=KEY1_PRESS;
                        }
                        else if(key==KEY2_PRESS)
                        {
                                call=KEY2_PRESS;
                        }
                        else if(key==KEY3_PRESS)
                        {
                                call=KEY3_PRESS;
                    }
                        else if(key==KEY4_PRESS)
                        {
                                call=KEY4_PRESS;
                        }
                        else if(KEY1==0||KEY2==0||KEY3==0||KEY4==0)
                        {
                        break;
                        }
        }
}
void lift()
{
       
        assignment();
        if(current<call)
        {
            Floordifference=call-current;
                current=call;
                up=0;
                delay_10us(Floordifference*50000);
                up=1;
        }
        else if(current>call)
        {
            Floordifference=current-call;
                current=call;
                down=0;
                delay_10us(Floordifference*50000);
                down=1;
        }               
//        else if(current==call)
//        {       
//                dy=0;
//                delay_10us(50000);
//                dy=1;
//        }               
}
void main()
{
while(1)
{
lift();
}
}
回复

使用道具 举报

2

主题

12

帖子

90

积分

注册会员

Rank: 2

积分
90
 楼主| 发表于 2024-2-19 14:23:59 | 显示全部楼层
大佬们看看哪儿出了问题,反正就是编译也可以,物理运算应该也可以,就是单片机它老是卡bug,大家请多多指教。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|普中开源电子分享网 粤ICP备16123577号-2

GMT+8, 2024-4-27 19:40 , Processed in 0.086623 second(s), 30 queries .

Powered by 论坛搭建 X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表