LED点阵
#include <reg52.h>typedef unsigned int u16;
typedef unsigned char u8;
//三八译码器的三个口(数码管位选)
sbit SMG_C = P2^2;
sbit SMG_B = P2^3;
sbit SMG_A = P2^4;
sbit rCLK = P3^5;
sbit SRCLK = P3^6;
sbit SER= P3^4;
//独立按键
sbit KEY1 = P3^1;
sbit KEY2 = P3^0;
sbit KEY3 = P3^2;
sbit KEY4 = P3^3;
//蜂鸣器管脚
sbit BEEP = P2^5;
u8 LED_state = 0;//用于储存LED状态
//LED点阵显示内容坐标
u8 gled_banana = {0x0C,0x34,0xC9,0xF3,0x7D,0x82,0x8E,0x70};
u8 gled_love = {0x38,0x44,0x82,0x41,0x41,0x82,0x44,0x38};
u8 gled_ball = {0x3C,0x42,0x81,0x81,0x81,0x81,0x42,0x3C};
u8 gled_gpurd = {0x00,0x06,0x69,0x91,0x91,0x69,0x06,0x00};
u8 gled_posion = {0x00,0x3E,0xFB,0xF7,0xEF,0xDF,0x3E,0x00};
u8 gled_welcome = {0x48,0xD7,0x60,0x07,0xBD,0xAF,0xBF,0x05};
u8 gled_pay_money = {0x06,0x61,0x91,0xFF,0x91,0xFF,0x91,0x1E};
u8 gled_lie = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
u8 gled_pay_OK = {0x3C,0x42,0x3C,0x00,0xFF,0x18,0x24,0xC3};
u8 gled_no_commodity = {0x12,0x12,0x94,0xF8,0xFE,0x91,0x11,0x13};
u8 gsmg_code_nopoint= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
//数码管上显示的内容:0到9(无小数点)
u8 gsmg_code_ispoint = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};
//数码管上显示的内容:0到9(有小数点)
//数码管上的货物价格
u8 commodit_banana = {0x06,0xcf,0x5b,0x6d,0x3f,0x3f,0x3f,0x3f};
u8 commodit_love = {0x6d,0x5b,0x3f,0x06,0x4f,0x06,0xe6,0x3f};
u8 commodit_ball = {0x7f,0xff,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
u8 commodit_gpurd = {0x07,0x07,0x66,0x6f,0xbf,0x3f,0x3f,0x3f};
u8 commodit_posion = {0x3f,0x3f,0x3f,0xbf,0x06,0x5b,0x6d,0x7f};
#define SMG_A_DP_PORT P0
//led显示界面的标志
#define BANANA 0
#define LOVE 1
#define BALL 2
#define GPURD 3
#define POSION 4
#define WELCOME 5
#define PAY_MONEY 6
#define PAY_OK 7
#define NO_COMMODITY 8
void delay(u16 num)
{
while(num--);
}
// 数码管显示货物价格
void smg_display(u8 commodity)
{
u8 i = 0;
for (i = 0; i < 8; i++)
{
switch (i)
{
case 0: SMG_A = 1; SMG_B = 1; SMG_C = 1;break;
case 1: SMG_A = 1; SMG_B = 1; SMG_C = 0;break;
case 2: SMG_A = 1; SMG_B = 0; SMG_C = 1;break;
case 3: SMG_A = 1; SMG_B = 0; SMG_C = 0;break;
case 4: SMG_A = 0; SMG_B = 1; SMG_C = 1;break;
case 5: SMG_A = 0; SMG_B = 1; SMG_C = 0;break;
case 6: SMG_A = 0; SMG_B = 0; SMG_C = 1;break;
case 7: SMG_A = 0; SMG_B = 0; SMG_C = 0;break;
}
switch(commodity)
{
case 0: SMG_A_DP_PORT = commodit_banana;delay(100);SMG_A_DP_PORT = 0x00;break;
case 1: SMG_A_DP_PORT = commodit_love;delay(100);SMG_A_DP_PORT = 0x00;break;
case 2: SMG_A_DP_PORT = commodit_ball;delay(100);SMG_A_DP_PORT = 0x00;break;
case 3: SMG_A_DP_PORT = commodit_gpurd;delay(100);SMG_A_DP_PORT = 0x00;break;
case 4: SMG_A_DP_PORT = commodit_posion;delay(100);SMG_A_DP_PORT = 0x00;break;
}
}
}
//把信息写入74HC595
void write_hx(u8 dat)
{
u8 i = 0;
for (i=0;i<8;i++)
{
SER = dat>>7;
dat <<= 1;
SRCLK = 0;
delay(1);
SRCLK = 1;
delay(1);
}
rCLK = 0;
delay(1);
rCLK = 1;
delay(1);
}
//蜂鸣器函数
void beep(u16 time_continue)
{
BEEP = 1;
while(time_continue--)
{
BEEP = !BEEP;
delay(100);
}
BEEP = 0;
}
//点击独立按键,K1,K4会切换led点阵显示内容,K2会出现请付款的LED点阵,
//再点K2会出现货物价格,点击K3,LED点阵提示付款成功.
//拔掉插帽可从数字信息转为LED信息
//用于在LED上显示内容,并返回LED状态
u8 LED_display(u8 screen_display)
{
u8 i = 0;
for (i=0;i<8;i++)
{
SMG_A_DP_PORT = gled_lie;
switch (screen_display)
{
case 0: write_hx(gled_banana);break;
case 1: write_hx(gled_love);break;
case 2: write_hx(gled_ball);break;
case 3: write_hx(gled_gpurd);break;
case 4: write_hx(gled_posion);break;
case 5: write_hx(gled_welcome);break;
case 6: write_hx(gled_pay_money);break;
case 7: write_hx(gled_pay_OK);break;
case 8: write_hx(gled_no_commodity);break;
}
delay(100);
write_hx(0x00);
}
return screen_display;
}
//判断按K1和K4后的LED界面呈现情况.返回按键后的LED状态
/*u8 K1_K4_function(u8 current_LED_state)
{
u8 LED_state = 0;
static u8 key = 1;
if (key ==1 && KEY1 == 0)
{
delay(1000);
if (KEY1 == 0)
{
key = 0;
if ((current_LED_state - 1)<0){LED_state = LED_display(NO_COMMODITY);return LED_state;}
if ((current_LED_state - 1)>=0){LED_state = LED_display( current_LED_state - 1);return LED_state;}
}
if (KEY1 == 1){key = 1;}
}
else if (key == 1 && KEY4 == 0)
{
delay(1000);
if (KEY4 == 0)
{
key = 0;
if ((current_LED_state + 1) >= 5){LED_state = LED_display(NO_COMMODITY);return LED_state;}
if ((current_LED_state +1 )<5){LED_state = LED_display(current_LED_state + 1);return LED_state;}
}
if (KEY1 == 1){key = 1;}
}
}*/
//判断按K1和K4后的LED界面呈现情况.返回按键后的LED状态
u8 K1_K4_function(u8 current_LED_state)
{
u8 LED_state = 0;
static u8 key1 = 1; //用于检测 KEY1 是否按下
static u8 key4 = 1; //用于检测 KEY4 是否按下
if (key1 && !KEY1) //先确认 KEY1 是否按下
{
delay(1000); //等待一段时间
if (!KEY1) //再次检测 KEY1 是否按下
{
key1 = 0; //设置 KEY1 为已按下状态
if ((current_LED_state - 1) < 0)
LED_state = LED_display(NO_COMMODITY);
else
LED_state = LED_display(current_LED_state - 1);
}
}
if (KEY1)
key1 = 1; //如果 KEY1 松开,就可以进行下一次按键操作了
if (key4 && !KEY4) //先确认 KEY4 是否按下
{
delay(1000); //等待一段时间
if (!KEY4) //再次检测 KEY4 是否按下
{
key4 = 0; //设置 KEY4 为已按下状态
if ((current_LED_state + 1) >= 5)
LED_state = LED_display(NO_COMMODITY);
else
LED_state = LED_display(current_LED_state + 1);
}
}
if (KEY4)
key4 = 1; //如果 KEY4 松开,就可以进行下一次按键操作了
return LED_state;
}
u8 double_K2 = 1;
//K2一次点击和二次点击后的结果
u8 K2_function(u8 LED_state)
{
static u8 key = 1;
;
if ( key && KEY2 == 0 )
{
delay(1000);
key = 0;
if (KEY2 == 0 && double_K2){LED_display(PAY_MONEY);double_K2 = 0;return PAY_MONEY;}
else if(KEY2 == 0&&double_K2 == 0)
{smg_display(LED_state);
if (KEY3 == 0);{LED_display(PAY_OK);beep(30000);return PAY_OK;}}
}
if (KEY2 == 1){key = 1;}
}
u8 init_state = BANANA;
void main()
{
LED_display(WELCOME);
delay(300000);
LED_display(BANANA);
delay(300000);
while(1)
{
init_state = K1_K4_function(init_state);
init_state = K2_function(init_state);
LED_display(init_state);
}
}
为什么LED点阵刚出现banana之后按独立按键没反应,跳转不到其他的图像 在K1_K4_function函数中,当按键被按下时,需要等待一段时间,以确保按键已经被稳定地按下。但是,在等待的过程中,程序并没有检测按键是否已经松开。因此,如果在等待过程中按键被松开了,程序仍然会认为按键被按下了,从而导致程序出现错误。解决方法是,在等待的过程中,需要检测按键是否已经松开,如果已经松开,则不执行任何操作。 问题分析:
根据代码分析,当按下K1或K4时,应该切换LED点阵显示内容,但是在原始代码中,并没有看到相应的切换逻辑。因此导致按下独立按键后没有反应。
解决方案:
在原始代码中添加切换LED点阵显示内容的逻辑。
可以将以下代码添加到K1_K4_function函数中的相应位置实现功能:
if (key == 1 && KEY1 == 0)
{
delay(1000);
if (KEY1 == 0)
{
key = 0;
if ((current_LED_state - 1) < 0)
LED_state = LED_display(NO_COMMODITY);
else
LED_state = LED_display(current_LED_state - 1);
}
if (KEY1 == 1){key = 1;}
}
else if (key == 1 && KEY4 ==
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
根据代码分析,按下独立按键后应该能够跳转到其他的图像。但是可能存在以下几个问题导致没有反应:
1. 独立按键的连接问题:请确保独立按键的引脚正确地连接到了相应的IO口,并且按键按下时会输出低电平。
2. 按键消抖:在代码中,使用了一个简单的延时函数 delay(1000) 进行按键消抖,但这个时间可能不够长。你可以尝试增加延时时间来确保稳定的按键检测。
3. 按键状态判断:在函数 K1_K4_function 中,判断按键状态时使用了 !KEY1 和 !KEY4,这表示只有在按键按下时才会执行对应的操作。如果你希望按键松开后也能触发操作,可以改为 KEY1 == 0 和 KEY4 == 0。
请检查以上问题,如果问题依然存在,可以进一步排查硬件连接或者调整代码逻辑。
如果问题已经解决,请设置最佳答案
页:
[1]