数码管按键程序求助!已做出流水灯、呼吸灯、计时器、简易电子琴,现在想综合
时间:10-02
整理:3721RD
点击:
【求助】数码管按键程序求助!我已经做出流水灯、呼吸灯、计时器、简易电子琴,现在我想用四个按键来控制这个四个功能,但是按键模块不会
以下程序是我尝试的,仅仅只是把计时器和呼吸灯加上去,但是连正常程序都生成不了,KEY3,KEY4我还没加。请问是什么问题?定时器中断我也不太会,真心求一个按键控制模块!
#include"stc15.h"
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char
unsigned char F_key=0; //标识三个按键的状态
//独立按键
sbit KEY1=P3^2;
sbit KEY2=P3^3;
sbit KEY3=P3^5;
sbit KEY4=P3^6;
sbit y1=P4^5;
sbit y2=P4^4;
//四个灯
sbit p1=P0^0;
sbit p2=P0^1;
sbit p3=P0^2;
sbit p4=P0^3;
uint CYCLE=1200,PWM_LOW=0;//定义周期并赋值
uint right,left=0;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e };
void kzleft(uint left)
{
y1=1;
y2=0;
P2=table[left];
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=3000;y>0;y--);
}
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 22;
j = 3;
k = 227;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay(unsigned int t)
{
while(--t);
}
//数码管显示
void shumaguan(void)
{
int a = 0,b = 0,c = 0;
y1=1;
P2=0xfe;//数码管
//强推挽
P4M0=1<<4 | 1<<5;
P4M1=0x00;
while(1)
{
if(KEY2==0)
{
right=0;
left=0;
}
if(KEY1==0)
{
KEY1=~KEY1;
while(KEY1);
delay(150);
}
y1=0;
y2=1;
P2=0xFF;
P2=table[right];
for(b = 0; b <200; b ++) ; //消影
P2=0xFF;
kzleft(left);
for(b = 0; b <200; b ++) ; //消影
a ++;
if(a > 1000)
{
a = 0,c++;
right ++;
if(right >9)
{
right =0;
left ++;
if(left >9)
{
left =0;
}
}
}
switch(c)
{
case 1: P0=0Xfe; break;
case 2: P0=0Xfd; break;
case 3: P0=0Xfb; break;
case 4: P0=0Xf7; break;
}
if(c==4) c=0;
}
}
//呼吸灯
void huxideng(void)
{
unsigned int i;
i=1;
for(;i<5;i++)
{
while (i==1) //主循环
{
p1=1;
Delay(60000); //特意加延时,可以看到熄灭的过程
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++) //PWM_LOW表示低
//电平时间,这个循环中低电平时长从1累加到CYCLE(周期)的值,即600次
{
p1=0;
Delay(PWM_LOW);//延时长度,600次循环中从1加至599
p1=1;
Delay(CYCLE-PWM_LOW);//延时长度,600次循环中从599减至1
}
p1=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--) //与逐渐变亮相反的过程
{
p1=0;
Delay(PWM_LOW);
p1=1;
Delay(CYCLE-PWM_LOW);
}
i=2;
}
while (i==2) //主循环
{
p2=1;
Delay(60000);
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++)
{
p2=0;
Delay(PWM_LOW);
p2=1;
Delay(CYCLE-PWM_LOW);
}
p2=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--)
{
p2=0;
Delay(PWM_LOW);
p2=1;
Delay(CYCLE-PWM_LOW);
}
i=3;
}
while (i==3) //主循环
{
p3=1;
Delay(60000);
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++)
{
p3=0;
Delay(PWM_LOW);
p3=1;
Delay(CYCLE-PWM_LOW);
}
p3=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--)
{
p3=0;
Delay(PWM_LOW);
p3=1;
Delay(CYCLE-PWM_LOW);
}
i=4;
}
while (i==4) //主循环
{
p4=1;
Delay(60000);
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++)
{
p4=0;
Delay(PWM_LOW);
p4=1;
Delay(CYCLE-PWM_LOW);
}
p4=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--)
{
p4=0;
Delay(PWM_LOW);
p4=1;
Delay(CYCLE-PWM_LOW);
}
i=0;
}
}
}
void main()
{
while(1)
{
if(KEY1==0)
if( F_key==0)
{
F_key=1;
//此处添加 按键 1 的跳变程序或函数
shumaguan();
}
else
if( F_key==1)
{
F_key=0;
}
if(KEY2==0)
if( F_key==0)
{
F_key=2;
//此处添加 按键 2 的跳变程序或函数
huxideng();
}
else
if( F_key==2)
{
F_key=0;
}
if(KEY3==0)
if( F_key==0)
{
F_key=3;
}
else
if( F_key==3)
{
F_key=0;
}
if(KEY4==0)
if( F_key==0)
{
F_key=4;
}
else
if( F_key==4)
{
F_key=0;
}
}
}
以下程序是我尝试的,仅仅只是把计时器和呼吸灯加上去,但是连正常程序都生成不了,KEY3,KEY4我还没加。请问是什么问题?定时器中断我也不太会,真心求一个按键控制模块!
#include"stc15.h"
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char
unsigned char F_key=0; //标识三个按键的状态
//独立按键
sbit KEY1=P3^2;
sbit KEY2=P3^3;
sbit KEY3=P3^5;
sbit KEY4=P3^6;
sbit y1=P4^5;
sbit y2=P4^4;
//四个灯
sbit p1=P0^0;
sbit p2=P0^1;
sbit p3=P0^2;
sbit p4=P0^3;
uint CYCLE=1200,PWM_LOW=0;//定义周期并赋值
uint right,left=0;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e };
void kzleft(uint left)
{
y1=1;
y2=0;
P2=table[left];
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=3000;y>0;y--);
}
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 22;
j = 3;
k = 227;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay(unsigned int t)
{
while(--t);
}
//数码管显示
void shumaguan(void)
{
int a = 0,b = 0,c = 0;
y1=1;
P2=0xfe;//数码管
//强推挽
P4M0=1<<4 | 1<<5;
P4M1=0x00;
while(1)
{
if(KEY2==0)
{
right=0;
left=0;
}
if(KEY1==0)
{
KEY1=~KEY1;
while(KEY1);
delay(150);
}
y1=0;
y2=1;
P2=0xFF;
P2=table[right];
for(b = 0; b <200; b ++) ; //消影
P2=0xFF;
kzleft(left);
for(b = 0; b <200; b ++) ; //消影
a ++;
if(a > 1000)
{
a = 0,c++;
right ++;
if(right >9)
{
right =0;
left ++;
if(left >9)
{
left =0;
}
}
}
switch(c)
{
case 1: P0=0Xfe; break;
case 2: P0=0Xfd; break;
case 3: P0=0Xfb; break;
case 4: P0=0Xf7; break;
}
if(c==4) c=0;
}
}
//呼吸灯
void huxideng(void)
{
unsigned int i;
i=1;
for(;i<5;i++)
{
while (i==1) //主循环
{
p1=1;
Delay(60000); //特意加延时,可以看到熄灭的过程
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++) //PWM_LOW表示低
//电平时间,这个循环中低电平时长从1累加到CYCLE(周期)的值,即600次
{
p1=0;
Delay(PWM_LOW);//延时长度,600次循环中从1加至599
p1=1;
Delay(CYCLE-PWM_LOW);//延时长度,600次循环中从599减至1
}
p1=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--) //与逐渐变亮相反的过程
{
p1=0;
Delay(PWM_LOW);
p1=1;
Delay(CYCLE-PWM_LOW);
}
i=2;
}
while (i==2) //主循环
{
p2=1;
Delay(60000);
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++)
{
p2=0;
Delay(PWM_LOW);
p2=1;
Delay(CYCLE-PWM_LOW);
}
p2=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--)
{
p2=0;
Delay(PWM_LOW);
p2=1;
Delay(CYCLE-PWM_LOW);
}
i=3;
}
while (i==3) //主循环
{
p3=1;
Delay(60000);
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++)
{
p3=0;
Delay(PWM_LOW);
p3=1;
Delay(CYCLE-PWM_LOW);
}
p3=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--)
{
p3=0;
Delay(PWM_LOW);
p3=1;
Delay(CYCLE-PWM_LOW);
}
i=4;
}
while (i==4) //主循环
{
p4=1;
Delay(60000);
for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++)
{
p4=0;
Delay(PWM_LOW);
p4=1;
Delay(CYCLE-PWM_LOW);
}
p4=0;
for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--)
{
p4=0;
Delay(PWM_LOW);
p4=1;
Delay(CYCLE-PWM_LOW);
}
i=0;
}
}
}
void main()
{
while(1)
{
if(KEY1==0)
if( F_key==0)
{
F_key=1;
//此处添加 按键 1 的跳变程序或函数
shumaguan();
}
else
if( F_key==1)
{
F_key=0;
}
if(KEY2==0)
if( F_key==0)
{
F_key=2;
//此处添加 按键 2 的跳变程序或函数
huxideng();
}
else
if( F_key==2)
{
F_key=0;
}
if(KEY3==0)
if( F_key==0)
{
F_key=3;
}
else
if( F_key==3)
{
F_key=0;
}
if(KEY4==0)
if( F_key==0)
{
F_key=4;
}
else
if( F_key==4)
{
F_key=0;
}
}
}
数码管
灯
这个问题已经牵扯到单片机编程的高级阶段了!给你一个思路:使用中断可以实现你所说的功能!
是不是利用计时器啊!是这样吗?具体怎么加在按键功能里呢?计时器赋初值我也还没搞清楚
TMOD=0x01;
TH0 = (65535 - 50000)/256; //定时器赋初值
TL0 = (65535 - 50000)%256;
EA = 1; //cpu开中断
TR0 = 1; //利用定时器0
ET0 = 1; //外部中断允许
是不是利用计时器啊!是这样吗?具体怎么加在按键功能里呢?计时器赋初值我也还没搞清楚
TMOD=0x01;
TH0 = (65535 - 50000)/256; //定时器赋初值
TL0 = (65535 - 50000)%256;
EA = 1; //cpu开中断
TR0 = 1; //利用定时器0
ET0 = 1; //外部中断允许
用if去判断IO口去执行相应的功能
你已经做出简易电子琴,这么还不会按键程序?