找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5731|回复: 3
打印 上一主题 下一主题
收起左侧

按键切换流水灯模式问题(单片机外部中断)

[复制链接]
跳转到指定楼层
楼主
ID:287932 发表于 2018-3-7 18:59 来自手机 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
如图,想用一个按键切换流水灯的多种流动模式,但是单片机程序不会,不知道是否需要用到中断。新手不是很会中断,求教程序如何编写

YY2AOK$EN6}ZWRY4_0SE2IK.png (46.38 KB, 下载次数: 33)

YY2AOK$EN6}ZWRY4_0SE2IK.png

OI0PDW6H_YXKZVB3425M2JH.png (75.01 KB, 下载次数: 48)

OI0PDW6H_YXKZVB3425M2JH.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:287932 发表于 2018-3-7 22:58 来自手机 | 只看该作者
新手求教啊
回复

使用道具 举报

板凳
ID:164602 发表于 2018-3-8 08:22 | 只看该作者
我这里有一个与你说的类似的程序,它是用外部中断来切换点阵显示的程序,你自己看看,学习学习,再变成你自己的程序吧。
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int     //宏定义

sbit KEY=P3^4;      //定义画面切换按键,可以不定义,外部中断会自己认的

uchar KY;
uchar disp;
uchar pic=0,num=0;      //pic为按键次数;num为中断次数

uchar code love[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x3F,0x00,0x20,0x00,0x20,0x00,0x20,
0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x0F,
0x04,0x10,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x04,0x10,0xF8,0x0F,0x00,0x00,
0x00,0x00,0x00,0x00,0xFE,0x07,0x00,0x08,0x00,0x10,0x00,0x20,0x00,0x20,0x00,0x10,
0x00,0x08,0xFE,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x3F,0x82,0x20,0x82,0x20,
0x82,0x20,0x82,0x20,0x82,0x20,0x82,0x20,0x82,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*LOVE*/
};

uchar code loveyou[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x78,0x00,0xFC,0x00,0xFE,0x01,0xFE,0x03,0xFE,0x07,0xFE,0x0F,0xFE,0x1F,0xFC,0x3F,
0xF8,0x7F,0xFC,0x3F,0xFE,0x1F,0xFE,0x0F,0xFE,0x07,0xFE,0x03,0xFE,0x01,0xFC,0x00,
0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00/*心形图案*/
};

uchar code hehe[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x01,0x40,0x01,0xC0,0x01,0x00,0x00,0x00,0x00,
0xF0,0x0F,0x08,0x10,0x04,0x20,0x00,0x00,0x00,0x00,0xF0,0x3F,0x08,0x00,0x04,0x00,
0x04,0x00,0x04,0x00,0x08,0x00,0xF0,0x3F,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x20,
0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x00,0x00,0x00,0xF0,0x3F,0x08,0x00,0x04,0x00,
0x04,0x00,0x04,0x00,0x08,0x00,0xF0,0x3F,0x00,0x00,0x00,0x00,0x04,0x20,0x08,0x10,
0xF0,0x0F,0x00,0x00,0x00,0x00,0xC0,0x01,0x40,0x01,0xC0,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00/*呵呵o(∩_∩)o图案*/
};

unsigned char code hanzi[] = {

0x80,0x40,0x40,0x40,0x30,0x42,0x1E,0x42,0x10,0x42,0x10,0x42,0x10,0x42,0xFF,0x7F,
0x10,0x42,0x10,0x42,0x10,0x42,0x10,0x42,0x10,0x42,0x10,0x40,0x00,0x40,0x00,0x00,/*"生",0*/
/* (16 X 16 , 宋体 ) */

0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0x82,0x40,0x82,0x40,0x82,0x40,0x82,0x40,
0x82,0x40,0x82,0x40,0x82,0x40,0xFE,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"日",1*/
/* (16 X 16 , 宋体 ) */

0x00,0x01,0xE0,0x00,0x00,0x00,0xFF,0xFF,0x10,0x00,0x20,0x81,0x08,0x41,0x08,0x31,
0x08,0x0D,0xFF,0x03,0x08,0x0D,0x08,0x31,0xF8,0x41,0x00,0x81,0x00,0x81,0x00,0x00,/*"快",2*/
/* (16 X 16 , 宋体 ) */

0x00,0x00,0x00,0x20,0xE0,0x10,0x9C,0x08,0x84,0x06,0x84,0x40,0x84,0x80,0xF4,0x7F,
0x82,0x00,0x82,0x00,0x83,0x02,0x82,0x04,0x80,0x08,0x80,0x30,0x00,0x00,0x00,0x00,/*"乐",3*/
/* (16 X 16 , 宋体 ) */

0x20,0x00,0x24,0x08,0x24,0x48,0x24,0x84,0xFE,0x7F,0x23,0x02,0x22,0x41,0x20,0x40,
0x20,0x20,0xFF,0x13,0x20,0x0C,0x22,0x14,0x2C,0x22,0xA0,0x41,0x20,0xF8,0x00,0x00,/*"我",4*/
/* (16 X 16 , 宋体 ) */

0x80,0x00,0x64,0x41,0x2C,0x21,0x34,0x91,0x24,0x89,0x24,0x87,0xEC,0x4D,0x32,0x55,
0x22,0x25,0x22,0x25,0x32,0x55,0x2E,0x4D,0x23,0x81,0xA2,0x80,0x60,0x80,0x00,0x00,/*"爱",5*/
/* (16 X 16 , 宋体 ) */

0x00,0x01,0x80,0x00,0x60,0x00,0xF8,0xFF,0x07,0x00,0x40,0x10,0x20,0x0C,0x18,0x03,
0x0F,0x40,0x08,0x80,0xC8,0x7F,0x08,0x00,0x08,0x01,0x28,0x06,0x18,0x18,0x00,0x00,/*"你",6*/
/* (16 X 16 , 宋体 ) */

0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"!",7*/
/* (16 X 16 , 宋体 ) */

}; //汉字编码
/********显示编码END*******************/

/**********函数声明******************/
void display1(void); //显示汉字子函数
void display2(void); //显示LOVE图案子函数
void display3(void); //显示心形图案子函数
void display4(void); //显示笑脸图案子函数

/*********n(us)延时子程序***********/
void DelayUs(uint M)
{
    uint x;
    for(x=0;x<=M;x++);
}
/**********INT0中断服务程序***********/
void intersvr0(void) interrupt 0 using 1
{
    KY=~KY;  //每个摇动来回水银开关会在摆幅两端分别产生下降沿中断,只提取其中一次(从左向右摇才显示)
    if(KY==0)
    {
        num++;      //计算中断次数
        switch(pic)       //选择画面
        {
            case 0:
                display1();break;
            case 1:
                display2();break;
            case 2:
                display3();break;
            case 3:
                display4();break;
            default:
                display1();
         }
   }
}
/*********显示子程序1(汉字)*********/
void display1(void)
{
    uchar i;
    if(num>10)
    {
        disp++;
        num=0;
    }      //12个汉字分为3次显示完(每次显示4个),每中断10次切换
    if(disp>=2)
    disp=0;
    DelayUs(3400);      //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
    for(i=0;i<64;i++)
    {
        P1=~hanzi[disp*128+i*2];
        P2=~hanzi[disp*128+i*2+1];
        DelayUs(95);
        P2=0xff;
        P1=0xff;
    }
}
/***********显示子程序2(LOVE)********/
void display2(void)
{
    uchar i;
    DelayUs(2000);
    for(i=0;i<64;i++)
    {
        P1=~love[i*2];
        P2=~love[i*2+1];
        DelayUs(130);
    }
}
/*******显示子程序3(心形图案)*******/
void display3(void)
{
    uchar i;
    DelayUs(1100);
    for(i=0;i<64;i++)
    {
        P1=~loveyou[i*2];
        P2=~loveyou[i*2+1];
        DelayUs(200);
    }
}
/********显示子程序4(呵呵o(∩_∩)o图案)********/
void display4(void)
{
    uchar i;
    DelayUs(2500);
    for(i=0;i<64;i++)
    {
        P1=~hehe[i*2];
        P2=~hehe[i*2+1];
        DelayUs(120);
    }
}
/*****主函数*****/
void main(void)
{
    IT0=1;
    EX0=1;
    EA=1;       //开中断,下降沿中断
    KY=0;
    while(1)      //主程序中只检测按键
    {
        if(KEY==0)      //画面切换键按下
        {
            DelayUs(10000);    //按键去抖
            if(KEY==0);
            pic++;
                        if(pic>3)pic=0;
                        while(KEY==0);
                       
        }

    }
}

回复

使用道具 举报

地板
ID:289136 发表于 2018-3-8 12:56 | 只看该作者
可以定义一个变量记录按下的次数来控制流动方式
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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