标题:
我有两个问题,一个是我写的单片机按键控制0~9递增递减在实现的时候有问题,希望指出
[打印本页]
作者:
萌萌萌新aa
时间:
2020-3-28 17:40
标题:
我有两个问题,一个是我写的单片机按键控制0~9递增递减在实现的时候有问题,希望指出
#include <STC12C5A60S2.H>
#define uchar unsigned char
#define uint unsigned int
sbit DU=P2^0;
sbit WE=P2^1;
sbit key1=P3^0;
sbit key2=P3^1;
uchar code duma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(uint z) //???????
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void main()
{
uchar count1=0;
uchar count2=9;
while(1)
{
if(key1==0)
{
delay(500);
if(key1==0)
{
count1++;
if(count1==10)
{
count1=0;
}
WE=1;
P0=0Xfd;
WE=0;
DU=1;
P0=duma[count1%10];
DU=0;
while(!key1);
}
}
if(key2==0)
{
delay(500);
if(key2==0)
{
count2--;
if(count2==-1)
{
count2=9;
}
WE=1;
P0=0Xfd;
WE=0;
DU=1;
P0=duma[count2%10];
DU=0;
while(!key2);
}
}
}
}
还有一个问题是请问我这个程序怎么改成0~99或0~999递增递减,
作者:
cyf7175
时间:
2020-3-28 18:59
count1, count2改为,都用 count全局变量,出现-1,那么不能用uchar(无符号型)
作者:
cyf7175
时间:
2020-3-28 19:12
你的段码已经给到了P0,再给位(片选)给0(你选的是共阴的管子)。
作者:
萌萌萌新aa
时间:
2020-3-31 17:21
cyf7175 发表于 2020-3-28 18:59
count1, count2改为,都用 count全局变量,出现-1,那么不能用uchar(无符号型)
那我这个程序怎么改成0~99递增递减呢
作者:
wulin
时间:
2020-3-31 21:31
0~9999递增递减
#include <STC12C5A60S2.H>
#define uchar unsigned char
#define uint unsigned int
sbit DU=P2^0;
sbit WE=P2^1;
sbit key1=P3^0;
sbit key2=P3^1;
uchar code duma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar qian,bai,shi,ge,num;
uint count=0;
bit flag1=0,flag2=0;
void delay(uint z)//12MHz晶振1T MCU
{
uint x,y;
for(x=z;x>0;x--)
for(y=1250;y>0;y--);//12T MCU改为125
}
void main()
{
while(1)
{
if(key1==0)
{
delay(10);
if(key1==0&&flag1==0)
{
flag1=1;
if(count<9999)
count++;
}
}
else flag1=0;
if(key2==0)
{
delay(10);
if(key2==0&&flag2==0)
{
flag2=1;
if(count>0)
count--;
}
}
else flag2=0;
qian=duma[count/1000%10];
bai=duma[count/100%10];
shi=duma[count/10%10];
ge =duma[count%10];
P0=0X00;DU=1;DU=0;
switch(num)
{
case 0: P0=0xfe;WE=1;WE=0;P0=qian;DU=1;DU=0; break;
case 1: P0=0xfd;WE=1;WE=0;P0=bai;DU=1;DU=0; break;
case 2: P0=0xfb;WE=1;WE=0;P0=shi;DU=1;DU=0; break;
case 3: P0=0xf7;WE=1;WE=0;P0=ge; DU=1;DU=0; break;
}
delay(1);
num++;
if(num==4)
num=0;
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1