关于按键的讨论
时间:10-02
整理:3721RD
点击:
各位走过路过的大神OR初学者,小弟刚接触单片机,现在想编写一个程序实现这样的功能:开关(SwitchH)按下,通道1(CHAN1)通路,按下一个(DOWN_BUTTON)通道2(CHAN2)通路,以此类推。如下代码请各位看看错在哪里:
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit CHAN1 = P0^0;
sbit CHAN2 = P0^1;
sbit CHAN3 = P0^2;
sbit CHAN4 = P0^3;
sbit CHAN5 = P0^4;
sbit CHAN6 = P0^5;
sbit CHAN7 = P0^6;
sbit CHAN8 = P0^7;
sbit CHAN9 = P2^0;
sbit CHAN10 = P2^1;
sbit SwitchH = P1^5;
sbit UP_BUTTON = P1^6;
sbit DOWN_BUTTON = P1^7;
void dealy(unsigned int y)
{
unsigned int x,z;
for(x = y;x > 0;x--)
for(z = 110;z > 0;z--);
}
void main()
{
CHAN1 = 1;
CHAN2 = 1;
CHAN3 = 1;
CHAN4 = 1;
CHAN5 = 1;
CHAN6 = 1;
CHAN7 = 1;
CHAN8 = 1;
CHAN9 = 1;
CHAN10 = 1;
SwitchH = 1;
UP_BUTTON = 1;
DOWN_BUTTON = 1;
while(1)
{
dealy(10);
if (SwitchH == 0)
{
CHAN1 = 0;
}
if (CHAN1 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN1 = 1;
CHAN2 = 0;
}
if (SwitchH == 0)
{
CHAN1 = 1;
}
}
if (CHAN2 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN3 = 0;
CHAN2 = 1;
}
if (UP_BUTTON == 0)
{
CHAN1 = 0;
CHAN2 = 1;
}
if (SwitchH == 0)
{
CHAN2 = 1;
}
}
if (CHAN3 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN4 = 0;
CHAN3 = 1;
}
if (UP_BUTTON == 0)
{
CHAN2 = 0;
CHAN3 = 1;
}
if (SwitchH == 0)
{
CHAN3 = 1;
}
}
if (CHAN4 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN5 = 0;
CHAN4 = 1;
}
if (UP_BUTTON == 0)
{
CHAN3 = 0;
CHAN4 = 1;
}
if (SwitchH == 0)
{
CHAN4 = 1;
}
}
if (CHAN5 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN6 = 0;
CHAN5 = 1;
}
if (UP_BUTTON == 0)
{
CHAN4 = 0;
CHAN5 = 1;
}
if (SwitchH == 0)
{
CHAN5 = 1;
}
}
if (CHAN6 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN7 = 0;
CHAN6 = 1;
}
if (UP_BUTTON == 0)
{
CHAN5 = 0;
CHAN6 = 1;
}
if (SwitchH == 0)
{
CHAN6 = 1;
}
}
if (CHAN7 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN8 = 0;
CHAN7 = 1;
}
if (UP_BUTTON == 0)
{
CHAN6 = 0;
CHAN7 = 1;
}
if (SwitchH == 0)
{
CHAN7 = 1;
}
}
if (CHAN8 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN9 = 0;
CHAN8 = 1;
}
if (UP_BUTTON == 0)
{
CHAN7 = 0;
CHAN8 = 1;
}
if (SwitchH == 0)
{
CHAN8 = 1;
}
}
if (CHAN9 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN10 = 0;
CHAN9 = 1;
}
if (UP_BUTTON == 0)
{
CHAN8 = 0;
CHAN9 = 1;
}
if (SwitchH == 0)
{
CHAN9 = 1;
}
}
if (CHAN10 == 0)
{
if (UP_BUTTON == 0)
{
CHAN9 = 0;
CHAN10 = 1;
}
if (SwitchH == 0)
{
CHAN10 = 1;
}
}
}
}
谢谢各位
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit CHAN1 = P0^0;
sbit CHAN2 = P0^1;
sbit CHAN3 = P0^2;
sbit CHAN4 = P0^3;
sbit CHAN5 = P0^4;
sbit CHAN6 = P0^5;
sbit CHAN7 = P0^6;
sbit CHAN8 = P0^7;
sbit CHAN9 = P2^0;
sbit CHAN10 = P2^1;
sbit SwitchH = P1^5;
sbit UP_BUTTON = P1^6;
sbit DOWN_BUTTON = P1^7;
void dealy(unsigned int y)
{
unsigned int x,z;
for(x = y;x > 0;x--)
for(z = 110;z > 0;z--);
}
void main()
{
CHAN1 = 1;
CHAN2 = 1;
CHAN3 = 1;
CHAN4 = 1;
CHAN5 = 1;
CHAN6 = 1;
CHAN7 = 1;
CHAN8 = 1;
CHAN9 = 1;
CHAN10 = 1;
SwitchH = 1;
UP_BUTTON = 1;
DOWN_BUTTON = 1;
while(1)
{
dealy(10);
if (SwitchH == 0)
{
CHAN1 = 0;
}
if (CHAN1 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN1 = 1;
CHAN2 = 0;
}
if (SwitchH == 0)
{
CHAN1 = 1;
}
}
if (CHAN2 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN3 = 0;
CHAN2 = 1;
}
if (UP_BUTTON == 0)
{
CHAN1 = 0;
CHAN2 = 1;
}
if (SwitchH == 0)
{
CHAN2 = 1;
}
}
if (CHAN3 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN4 = 0;
CHAN3 = 1;
}
if (UP_BUTTON == 0)
{
CHAN2 = 0;
CHAN3 = 1;
}
if (SwitchH == 0)
{
CHAN3 = 1;
}
}
if (CHAN4 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN5 = 0;
CHAN4 = 1;
}
if (UP_BUTTON == 0)
{
CHAN3 = 0;
CHAN4 = 1;
}
if (SwitchH == 0)
{
CHAN4 = 1;
}
}
if (CHAN5 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN6 = 0;
CHAN5 = 1;
}
if (UP_BUTTON == 0)
{
CHAN4 = 0;
CHAN5 = 1;
}
if (SwitchH == 0)
{
CHAN5 = 1;
}
}
if (CHAN6 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN7 = 0;
CHAN6 = 1;
}
if (UP_BUTTON == 0)
{
CHAN5 = 0;
CHAN6 = 1;
}
if (SwitchH == 0)
{
CHAN6 = 1;
}
}
if (CHAN7 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN8 = 0;
CHAN7 = 1;
}
if (UP_BUTTON == 0)
{
CHAN6 = 0;
CHAN7 = 1;
}
if (SwitchH == 0)
{
CHAN7 = 1;
}
}
if (CHAN8 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN9 = 0;
CHAN8 = 1;
}
if (UP_BUTTON == 0)
{
CHAN7 = 0;
CHAN8 = 1;
}
if (SwitchH == 0)
{
CHAN8 = 1;
}
}
if (CHAN9 == 0)
{
if (DOWN_BUTTON == 0)
{
CHAN10 = 0;
CHAN9 = 1;
}
if (UP_BUTTON == 0)
{
CHAN8 = 0;
CHAN9 = 1;
}
if (SwitchH == 0)
{
CHAN9 = 1;
}
}
if (CHAN10 == 0)
{
if (UP_BUTTON == 0)
{
CHAN9 = 0;
CHAN10 = 1;
}
if (SwitchH == 0)
{
CHAN10 = 1;
}
}
}
}
谢谢各位
顶回去