#include <reg52.h>
/*********外部接口定义**********/
#define out P1 //数据输出端口
sbit led1=P3^0; //第1组LED
sbit led2=P3^1; //第2组LED
sbit p3_3=P3^3;//滚珠开关
sbit key1=P3^7;//切换开关
/*********全局变量定义**********/
unsigned long int TimeCount=0; //定时器单位时间计数
unsigned int DelayN=80, //延时临时计数
DelayTime=80; //每次延时的周期个数
unsigned char key; //按键输入
/*********图片点阵数据表**********/
unsigned char code pic1[]=
{
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xDF,
0xF7,0xDF,0xF7,0xDF,0xF7,0xDF,0x07,0xC0,
0x07,0xC0,0xF7,0xDF,0xF7,0xDF,0xF7,0xDF,
0xFF,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF //I
};
unsigned char code pic2[]=
{
0xFF,0xFF,0xFF,0xFF,0x0F,0xFF,0x07,0xFE,
0x03,0xFC,0x07,0xF8,0x0F,0xF0,0x1F,0xE0,
0x1F,0xE0,0x0F,0xF0,0x07,0xF8,0x03,0xFC,
0x07,0xFE,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF //爱心
};
unsigned char code pic3[]=
{
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x03,0xF0,
0x03,0xE0,0xFF,0xC7,0xFF,0xCF,0xFF,0xCF,
0xFF,0xCF,0xFF,0xCF,0xFF,0xC7,0x03,0xE0,
0x03,0xF0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF //U
};
/*0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xCF,
0xFF,0xCF,0xE7,0xCF,0xE3,0xCF,0x01,0xC0,
0x01,0xC0,0xFF,0xCF,0xFF,0xCF,0xFF,0xCF,
0xFF,0xCF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF*///1
/*0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xFB,0xEF,
0xFB,0xEF,0x1B,0xE0,0xFB,0xEF,0xFB,0xEF,
0x03,0xE0,0x7B,0xEF,0x7B,0xEF,0xFB,0xEF,
0xFF,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF*/ //正
/*0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xCF,
0xFF,0xCF,0xFF,0xCF,0xFF,0xCF,0x01,0xC0,
0x01,0xC0,0xE3,0xCF,0xE7,0xCF,0xFF,0xCF,
0xFF,0xCF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF*///1
/* (16 X 16 ) */
/*********子程序声明**********/
void delay(unsigned char n); //延时子程序
void display_pic(unsigned char code *pic); //图片显示子程序
/********* 主 程 序 **********/
void main()
{
unsigned int i;
EX1=1; //开放外部中断1
IT1=0; //中断1为边沿触发方式
IP=0x08; //设置中断嵌套
TMOD=0x20; //定时器1方式2,8位重装载
TH1=0x80; //设定定时器初值
TL1=0x80;
EA=1; //开放中断
ET1=1; //开放定时器1中断
TR1=1; //启动定时器0,开始计时
key=0; //初始化显示第一组文字
// out=0xff;
//led1=0x00;
//led2=0x00;
while(1)
{
//等待中断
if(key1==0)
{
for(i=0;i<6000;i++); //按键去抖处理
if(key1==0)
{ //有键按下
key++; //更换下一幅图片
if(key==4) key=0; //图片循环显示
while(key1==0); //等待按键释放
}
}
};
}
/*********图片显示子程序**********/
void display_pic(unsigned char code *pic) //显示16*16点阵图
{ unsigned char i,n;
n=0; //初始化数据表计数
for(i=0;i<16;i++){ //共显示16*3=48列
out=0xff; //清显示
led2=1; //关闭第2组LED
led1=0; //开启第1组LED
out=*(pic+n++); //输出数据到LED
delay(1); //延时
out=0xff; //清显示
led1=1; //关闭第1组LED
led2=0; //开启第2组LED
out=*(pic+n++); //输出数据到LED
delay(1); //延时
}
}
/*********显示内容选择子程序**********/
void display(unsigned char i)
{
switch (i){ //通过按键选择显示内容
case 0: display_pic(pic1);break;
case 1: display_pic(pic2);break;
case 2: display_pic(pic3);break;
}
}
/*********延时子程序**********/
void delay(unsigned char n)
{
for(;n>0;n--)
{
DelayN=DelayTime;
while (DelayN);
}
}
/*********中断子程序**********/
void int1() interrupt 2 //使用外部中断1
{
if(TimeCount<800) return; //中断去抖4500
out=0xff; //清显示
DelayTime=TimeCount/200;//设定每次延时时钟周期数=总时间/(16列*2组*1个字)640
TimeCount=0; //延时周期计数清零
delay(70); //延时20列
display(key);
//display_pic(pic1); //显示图片
out=0xff;
while(p3_3==0);
}
void timer1() interrupt 3 //定时器1中断子程序
{
TimeCount++;
if (DelayN!=0)
DelayN--;
}
|