微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > PCB设计问答 > PCB设计学习讨论 > 单片机摇摇棒有关控制变量的问题

单片机摇摇棒有关控制变量的问题

时间:10-02 整理:3721RD 点击:
/*****中断服务程序*****/
void intersvr0(void) interrupt 0
{
       
        KY=~KY;
       //每个摇动来回水银开关会在摆幅两端分别产生下降沿中断,只提取其中一次(从左向右摇才显示)
if(KY==0)
{
   num++;     //计算中断次数
   switch(pic)      //选择画面
   {
        case 0:{display1_ltor();}break;
    case 1:{display2_ltor();}break;
    case 2:{display3();}break;
    case 3:{display4();}break;
    default:{display1_ltor();}  
   }
}
       
}
上面是中断程序
控制变量ky初值为0,摇摇棒从左往右或者从右往左都会产生一个中断,按照程序所描述是不是说 第一次中断不送数据,第二次中断送数据,第3次又送。
那这样来分析的话摇摇棒有可能从左往右的时候送数据,也有可能从右往左的时候送数据,
但是实际上之后摇摇棒从左往右时才送数据啊,
有点搞不懂
哪位大神能看看我的分析哪里出了问题,感激不尽!



在这里,我只能祝愿小编早日能找到这个问题的解决办法了,另外还是想问一下小编,你所说的“摇摇棒”是不是演唱会里观众手中挥舞的发光棒么?
补充:刚才必应一下,原来是这个,关键部件是水银开关,回到题目,在这里,设A为左边,B为右边,因为摇摇棒在摇晃的时候,路径是一个扇形,即A与B的路径也是一个扇形,在摇摇棒摇摆的时候,在A点位置首先程序会将变量KY设置为1,然后调用中断显示画面,那个变量pic用来设置显示哪一幅画面,在摇摆到B点位置时程序将变量KY设置为0,然后调用中断,显然无法显示画面,至于程序如何知道摇摇棒在A点还是B点,关键还是水银开关。

感谢大佬顶贴,有个问题,在A点程序怎么将KY置1的

KY=0时才调用显示程序display啊

KY=1,那么~KY就为0.

我现在的理解是KY初值为0,在A点触发中断,执行KY=~KY,取反之后为1,调用不了显示程序。
看你的解释是说在触发中断之前还有个让ky由0变1的操作吗

在A点和B点会各进行一次对KY的操作。

我看程序里也没这个操作啊

#include <AT89X52.h>
#define uchar unsigned char
#define uint unsigned int    //宏定义
#define KEY P3_7      //定义画面切换按键
uchar KY;    //KY作用在后面说明     
uchar disp;     //显示汉字指针
uchar pic=0,num=0;     //pic为按键次数;num为中断次数
//取字模方式请参考取字模文件夹内说明文档
uchar code dis1[] = {
0x04,0x10,0x24,0x08,0x44,0x06,0x84,0x01,0x64,0x82,0x9C,0x4C,0x40,0x20,0x30,0x18,
0x0F,0x06,0xC8,0x01,0x08,0x06,0x08,0x18,0x28,0x20,0x18,0x40,0x00,0x80,0x00,0x00,/*"欢",0*/
0x40,0x00,0x40,0x40,0x42,0x20,0xCC,0x1F,0x00,0x20,0x00,0x40,0xFC,0x4F,0x04,0x44,
0x02,0x42,0x00,0x40,0xFC,0x7F,0x04,0x42,0x04,0x44,0xFC,0x43,0x00,0x40,0x00,0x00,/*"迎",1*/
0x80,0x00,0x60,0x00,0xF8,0xFF,0x07,0x00,0x04,0x80,0xE4,0x81,0x24,0x45,0x24,0x29,
0x24,0x11,0xFF,0x2F,0x24,0x41,0x24,0x41,0x24,0x81,0xE4,0x81,0x04,0x80,0x00,0x00,/*"使",2*/
0x00,0x80,0x00,0x60,0xFE,0x1F,0x22,0x02,0x22,0x02,0x22,0x02,0x22,0x02,0xFE,0x7F,
0x22,0x02,0x22,0x02,0x22,0x42,0x22,0x82,0xFE,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,/*"用",3*/
0x08,0x02,0x08,0x01,0x89,0x00,0xEE,0xFF,0x98,0x00,0x00,0x03,0x00,0x00,0xF8,0x1F,
0x88,0x08,0x88,0x08,0xFF,0xFF,0x88,0x08,0x88,0x08,0xF8,0x1F,0x00,0x00,0x00,0x00,/*"神",4*/
0x40,0x00,0x40,0x00,0x44,0x00,0x44,0x1E,0x64,0x12,0x54,0x12,0x4C,0x12,0x47,0x12,
0x4C,0x1E,0x54,0x40,0x64,0x80,0xC4,0x7F,0x44,0x00,0x40,0x00,0x40,0x00,0x00,0x00,/*"奇",5*/
0x00,0x80,0x00,0x60,0xFE,0x9F,0x4A,0x80,0xAA,0x4F,0xFE,0x4A,0x9A,0x2A,0xEA,0x1A,
0x83,0x6F,0xAA,0xBA,0x9A,0xAA,0xFE,0xBA,0x9A,0xEF,0x2A,0x80,0x42,0xC0,0x00,0x00,/*"魔",6*/
0x40,0x10,0x60,0x38,0x50,0x14,0x4C,0x12,0x43,0x11,0xC0,0x14,0x30,0x38,0x04,0x00,
0x04,0x00,0x04,0x00,0x04,0x40,0x04,0x80,0x04,0x40,0xFC,0x3F,0x00,0x00,0x00,0x00,/*"幻",7*/
0x10,0x02,0x10,0x42,0x10,0x81,0xFF,0x7F,0x90,0x00,0x00,0x00,0x94,0x02,0x64,0x7A,
0x44,0x42,0x54,0x42,0xE2,0x7F,0x42,0x42,0x63,0x42,0x5A,0xFA,0x00,0x02,0x00,0x00,/*"摇",8*/
0x10,0x02,0x10,0x42,0x10,0x81,0xFF,0x7F,0x90,0x00,0x00,0x00,0x94,0x02,0x64,0x7A,
0x44,0x42,0x54,0x42,0xE2,0x7F,0x42,0x42,0x63,0x42,0x5A,0xFA,0x00,0x02,0x00,0x00,/*"摇",9*/
0x10,0x04,0x10,0x03,0xD0,0x00,0xFF,0xFF,0x90,0x00,0x10,0x01,0x44,0x12,0x54,0x11,
0xD4,0x14,0x74,0x14,0x5F,0xFF,0x54,0x14,0xD4,0x14,0x54,0x11,0x44,0x12,0x00,0x00,/*"棒",10*/
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,/*"!",11*/
};
uchar code dis2[] = {
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 dis3[] = {
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 dis4[] = {
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图案*/
};
/*****函数声明*****/
void display1(void);
void display2(void);
void display3(void);
void display4(void);
/*****n(us)延时子程序*****/
void DelayUs(uint N)
{
uint x;
for(x=0; x<=N;x++);
}
/*****中断服务程序*****/
void intersvr0(void) interrupt 0
{
        EA=0;
        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();}  
                }
        }
        EA=1;
}
/*****显示子程序1*****/
void display1(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}     //每中断4次切换一屏显示
        if(disp>=3)        disp=0;                          //一屏4个字,显示几屏disp就大于等于几
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可(括号内数值偏小时,字会偏左)
        for(i=0;i<64;i++)
        {
                P0=~dis1[disp*128+i*2];
                P2=~dis1[disp*128+i*2+1];
                DelayUs(110);  //此延时是字的宽度,一般不用修改
        }
}
/*****显示子程序2*****/
void display2(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}
        if(disp>=1)        disp=0;               
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
        for(i=0;i<64;i++)
        {
                P0=~dis2[disp*128+i*2];
                P2=~dis2[disp*128+i*2+1];
                DelayUs(110);
        }
}
/*****显示子程序3*****/
void display3(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}  
        if(disp>=1)        disp=0;                          
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
        for(i=0;i<64;i++)
        {
                P0=~dis3[disp*128+i*2];
                P2=~dis3[disp*128+i*2+1];
                DelayUs(110);
        }
}
/*****显示子程序4*****/
void display4(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}     
        if(disp>=1)        disp=0;               
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
        for(i=0;i<64;i++)
        {
                P0=~dis4[disp*128+i*2];
                P2=~dis4[disp*128+i*2+1];
                DelayUs(110);
        }
}
/*****主函数*****/
void main(void)
{
IT0=1;
EX0=1;
EA=1;      //开中断,下降沿中断
KY=0;
KEY=1;
while(1)     //主程序中只检测按键
{
  if(KEY==0)     //画面切换键按下
  {
     DelayUs(10000);     //按键去抖
     if(KEY==0);
     pic++;
     while(KEY==0);
  }
  if(pic>3)pic=0;
}
}
/*****END*****/

#include <AT89X52.h>
#define uchar unsigned char
#define uint unsigned int    //宏定义
#define KEY P3_7      //定义画面切换按键
uchar KY;    //KY作用在后面说明     
uchar disp;     //显示汉字指针
uchar pic=0,num=0;     //pic为按键次数;num为中断次数
//取字模方式请参考取字模文件夹内说明文档
uchar code dis1[] = {
0x04,0x10,0x24,0x08,0x44,0x06,0x84,0x01,0x64,0x82,0x9C,0x4C,0x40,0x20,0x30,0x18,
0x0F,0x06,0xC8,0x01,0x08,0x06,0x08,0x18,0x28,0x20,0x18,0x40,0x00,0x80,0x00,0x00,/*"欢",0*/
0x40,0x00,0x40,0x40,0x42,0x20,0xCC,0x1F,0x00,0x20,0x00,0x40,0xFC,0x4F,0x04,0x44,
0x02,0x42,0x00,0x40,0xFC,0x7F,0x04,0x42,0x04,0x44,0xFC,0x43,0x00,0x40,0x00,0x00,/*"迎",1*/
0x80,0x00,0x60,0x00,0xF8,0xFF,0x07,0x00,0x04,0x80,0xE4,0x81,0x24,0x45,0x24,0x29,
0x24,0x11,0xFF,0x2F,0x24,0x41,0x24,0x41,0x24,0x81,0xE4,0x81,0x04,0x80,0x00,0x00,/*"使",2*/
0x00,0x80,0x00,0x60,0xFE,0x1F,0x22,0x02,0x22,0x02,0x22,0x02,0x22,0x02,0xFE,0x7F,
0x22,0x02,0x22,0x02,0x22,0x42,0x22,0x82,0xFE,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,/*"用",3*/
0x08,0x02,0x08,0x01,0x89,0x00,0xEE,0xFF,0x98,0x00,0x00,0x03,0x00,0x00,0xF8,0x1F,
0x88,0x08,0x88,0x08,0xFF,0xFF,0x88,0x08,0x88,0x08,0xF8,0x1F,0x00,0x00,0x00,0x00,/*"神",4*/
0x40,0x00,0x40,0x00,0x44,0x00,0x44,0x1E,0x64,0x12,0x54,0x12,0x4C,0x12,0x47,0x12,
0x4C,0x1E,0x54,0x40,0x64,0x80,0xC4,0x7F,0x44,0x00,0x40,0x00,0x40,0x00,0x00,0x00,/*"奇",5*/
0x00,0x80,0x00,0x60,0xFE,0x9F,0x4A,0x80,0xAA,0x4F,0xFE,0x4A,0x9A,0x2A,0xEA,0x1A,
0x83,0x6F,0xAA,0xBA,0x9A,0xAA,0xFE,0xBA,0x9A,0xEF,0x2A,0x80,0x42,0xC0,0x00,0x00,/*"魔",6*/
0x40,0x10,0x60,0x38,0x50,0x14,0x4C,0x12,0x43,0x11,0xC0,0x14,0x30,0x38,0x04,0x00,
0x04,0x00,0x04,0x00,0x04,0x40,0x04,0x80,0x04,0x40,0xFC,0x3F,0x00,0x00,0x00,0x00,/*"幻",7*/
0x10,0x02,0x10,0x42,0x10,0x81,0xFF,0x7F,0x90,0x00,0x00,0x00,0x94,0x02,0x64,0x7A,
0x44,0x42,0x54,0x42,0xE2,0x7F,0x42,0x42,0x63,0x42,0x5A,0xFA,0x00,0x02,0x00,0x00,/*"摇",8*/
0x10,0x02,0x10,0x42,0x10,0x81,0xFF,0x7F,0x90,0x00,0x00,0x00,0x94,0x02,0x64,0x7A,
0x44,0x42,0x54,0x42,0xE2,0x7F,0x42,0x42,0x63,0x42,0x5A,0xFA,0x00,0x02,0x00,0x00,/*"摇",9*/
0x10,0x04,0x10,0x03,0xD0,0x00,0xFF,0xFF,0x90,0x00,0x10,0x01,0x44,0x12,0x54,0x11,
0xD4,0x14,0x74,0x14,0x5F,0xFF,0x54,0x14,0xD4,0x14,0x54,0x11,0x44,0x12,0x00,0x00,/*"棒",10*/
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,/*"!",11*/
};
uchar code dis2[] = {
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 dis3[] = {
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 dis4[] = {
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图案*/
};
/*****函数声明*****/
void display1(void);
void display2(void);
void display3(void);
void display4(void);
/*****n(us)延时子程序*****/
void DelayUs(uint N)
{
uint x;
for(x=0; x<=N;x++);
}
/*****中断服务程序*****/
void intersvr0(void) interrupt 0
{
        EA=0;
        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();}  
                }
        }
        EA=1;
}
/*****显示子程序1*****/
void display1(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}     //每中断4次切换一屏显示
        if(disp>=3)        disp=0;                          //一屏4个字,显示几屏disp就大于等于几
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可(括号内数值偏小时,字会偏左)
        for(i=0;i<64;i++)
        {
                P0=~dis1[disp*128+i*2];
                P2=~dis1[disp*128+i*2+1];
                DelayUs(110);  //此延时是字的宽度,一般不用修改
        }
}
/*****显示子程序2*****/
void display2(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}
        if(disp>=1)        disp=0;               
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
        for(i=0;i<64;i++)
        {
                P0=~dis2[disp*128+i*2];
                P2=~dis2[disp*128+i*2+1];
                DelayUs(110);
        }
}
/*****显示子程序3*****/
void display3(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}  
        if(disp>=1)        disp=0;                          
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
        for(i=0;i<64;i++)
        {
                P0=~dis3[disp*128+i*2];
                P2=~dis3[disp*128+i*2+1];
                DelayUs(110);
        }
}
/*****显示子程序4*****/
void display4(void)
{
        uchar i;
        if(num>4)        {disp++;num=0;}     
        if(disp>=1)        disp=0;               
        DelayUs(4000);     //此处延时时间依各硬件差别而各不相同,试着调整使得显示内容居中即可
        for(i=0;i<64;i++)
        {
                P0=~dis4[disp*128+i*2];
                P2=~dis4[disp*128+i*2+1];
                DelayUs(110);
        }
}
/*****主函数*****/
void main(void)
{
IT0=1;
EX0=1;
EA=1;      //开中断,下降沿中断
KY=0;
KEY=1;
while(1)     //主程序中只检测按键
{
  if(KEY==0)     //画面切换键按下
  {
     DelayUs(10000);     //按键去抖
     if(KEY==0);
     pic++;
     while(KEY==0);
  }
  if(pic>3)pic=0;
}
}
/*****END*****/

只有中断函数里面对KY进行了一次取反操作

只有中断函数里面对KY进行了一次取反操作

如果整个程序,只有中断函数里面对KY进行一次取反操作的话,那么就不能保证只有从左往右才会显示画面,你看看程序开头有没有对KY进行定义的。

#include <AT89X52.h>
#define uchar unsigned char
#define uint unsigned int   
#define KEY P3_7      
uchar ZD;         
uchar point=0;     
uchar pcnt=0,num=0;     
unsigned char code wenzi[] = {
0x00,0x01,0x00,0x01,0x90,0x00,0x88,0xFE,0x4C,0x42,0x57,0x42,0x24,0x42,0x24,0x42,
0x54,0x42,0x54,0x42,0x8C,0x42,0x84,0xFE,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,/*"各",0*/
0x00,0x01,0x80,0x00,0x60,0x00,0xF8,0xFF,0x07,0x40,0x10,0x40,0x90,0x41,0x10,0x5E,
0x11,0x40,0x16,0x40,0x10,0x70,0x10,0x4E,0xD0,0x41,0x10,0x40,0x00,0x40,0x00,0x00,/*"位",1*/
0x20,0x10,0x20,0x08,0x24,0x04,0x24,0x02,0x24,0x3F,0x24,0x45,0xBF,0x44,0x64,0x44,
0x24,0x42,0x34,0x42,0x28,0x42,0x24,0x41,0x22,0x78,0x20,0x00,0x20,0x00,0x00,0x00,/*"老",2*/
0x00,0x00,0xFC,0x87,0x00,0x40,0x00,0x30,0xFF,0x0F,0x00,0x00,0x02,0x00,0xE2,0x1F,
0x22,0x00,0x22,0x00,0xFE,0xFF,0x22,0x08,0x22,0x10,0xE2,0x0F,0x02,0x00,0x00,0x00,/*"师",3*/
0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40,0xFF,0x7F,0x40,0x40,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x40,0x00,0x40,0x00,0x00,/*"上",4*/
0x00,0x02,0x40,0x02,0x20,0x02,0x18,0x02,0x0F,0x02,0x08,0x02,0x08,0x02,0xF8,0xFF,
0x08,0x02,0x08,0x02,0x08,0x02,0x08,0x02,0x08,0x02,0x00,0x02,0x00,0x02,0x00,0x00,/*"午",5*/
0x10,0x40,0x10,0x22,0xF0,0x15,0x1F,0x08,0x10,0x16,0xF0,0x61,0x00,0x00,0x80,0x00,
0x82,0x40,0x82,0x80,0xE2,0x7F,0x92,0x00,0x8A,0x00,0x86,0x00,0x80,0x00,0x00,0x00,/*"好",6*/
0xFC,0x0F,0x04,0x04,0xFC,0x0F,0x00,0x00,0xFE,0xFF,0x42,0x10,0xBE,0x0F,0x00,0x00,
0xF2,0x0F,0x12,0x04,0xF2,0x4F,0x02,0x80,0xFE,0x7F,0x02,0x00,0x00,0x00,0x00,0x00,/*"啊",7*/
};
unsigned char code cool[] = {
0xC0,0x07,0x30,0x18,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x10,0x38,0x08,0x00,0x00,/*"C",0*/
0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,/*"O",1*/
0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,/*"O",2*/
0x08,0x20,0xF8,0x3F,0x08,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x30,0x00,0x00,/*"L",3*/
};
unsigned char code smile[] = {
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,0xF0,0x1F,0xF8,0x3F,0xFC,0x67,
0x8E,0x6F,0x8E,0x6F,0x8E,0x6F,0xFE,0x6F,0xFE,0x6F,0x8E,0x6F,0x8E,0x6F,0x8E,0x6F,
0xFC,0x67,0xF8,0x3F,0xF0,0x1F,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,
/*smile*/
};
unsigned char 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*/
};
void display1(void);
void display2(void);
void display3(void);
void display4(void);
void Delay(uint N)
{
uint x;
for(x=0; x<=N;x++);
}
void zhongduan(void) interrupt 0
{
;
  ZD=~ZD;     
if(ZD==0)
{
   num++;     
   switch(pcnt)      
   {
    case 0:{display1();}break;
    case 1:{display2();}break;
    case 2:{display3();}break;
    case 3:{display4();}break;
    default:{display1();}  
   }
}
}
void display1(void)
{
uchar i;
if(num>10){point++;num=0;}     
if(point>1)point=0;
  Delay(5200);     
for(i=0;i<64;i++)
{
     P0=~wenzi[point*128+i*2];
     P2=~wenzi[point*128+i*2+1];
     Delay(100);
}
}
void display2(void)
{
uchar i;
Delay(4000);
for(i=0;i<32;i++)
{
     P0=~cool[i*2];
     P2=~cool[i*2+1];
     Delay(140);
}
}
void display3(void)
{
uchar i;
  Delay(4000);
for(i=0;i<40;i++)
{
     P0=~smile[i*2];
     P2=~smile[i*2+1];
     Delay(120);
}
}
void display4(void)
{
uchar i;
Delay(4000);
for(i=0;i<64;i++)
{
     P0=~love[i*2];
     P2=~love[i*2+1];
     Delay(120);
}
}
void main(void)
{
IT0=1;
EX0=1;
EA=1;      
ZD=0;
KEY=1;
while(1)     
{
  if(KEY==0)     
  {
  Delay(10000);     
  if(KEY==0);
  pcnt++;}
  if(pcnt>3)pcnt=0;
}
}

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,
0xC0,0x07,0x30,0x18,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x10,0x38,0x08,0x00,0x00,/*"C",0*/
0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,/*"O",1*/
0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,/*"O",2*/
0x08,0x20,0xF8,0x3F,0x08,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x30,0x00,0x00,/*"L",3*/
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,

好像还真没有。

那个KEY变量绑定到P3_7引脚,这个变量比较重要,P3_7引脚的高低电平会改变KEY的值,KEY=0调用中断,KEY=1不调用中断,至于ZD的值只是用于判断是否显示画面,这个没有什么好深究的,主要还是KEY。

sorry 发的这个程序和我刚开始给的变量名有点不一样,原来那个KY是下面这个程序的ZD。
p3.7引脚电平变化不会触发中断,水银开关是连p3.2(into)

(⊙﹏⊙),这样的话,就不知道了,只能坐等玩过单片机的坛友回答一下了

还是非常感谢!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top