怎么写8×8led点阵屏显示数字0-9的程序?

怎么写8×8led点阵屏显示数字0-9的程序?,第1张

这个程序可以循环显示0~9.#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code Table_of_Digits[]={0x00,0x3e,0x41,0x41,0x41,0x3e,0x00,0x00, //00x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00, //10x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00, //20x00,0x22,0x49,0x49,0x49,0x36,0x00,0x00, //30x00,0x0c,0x14,0x24,0x7f,0x04,0x00,0x00, //40x00,0x72,0x51,0x51,0x51,0x4e,0x00,0x00, //50x00,0x3e,0x49,0x49,0x49,0x26,0x00,0x00, //60x00,0x40,0x40,0x40,0x4f,0x70,0x00,0x00, //70x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00, //80x00,0x32,0x49,0x49,0x49,0x3e,0x00,0x00, //90xff,0x81,0x81,0x81,0x81,0x81,0x81,0xff}uchar code xdat[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}uchar code ydat[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}uchar i=0,j=0,t=0,Num_Index,key,xi,yisbit we1=P1^1sbit we2=P1^3//主程序void main(){//P1=0x80Num_Index=0//从0 开始显示TMOD=0x01//T0 方式0TH0=(65536-2000)/256//2ms 定时TL0=(65536-2000)%256IE=0x82key=0xi=0yi=0EX0=1IT0=1TR0=1//启动T0while(1)}//T0 中断函数void ext_int0() interrupt 0{key++ key&=0x03}void LED_Screen_Display() interrupt 1{TH0=(65536-2000)/256//2ms 定时TL0=(65536-2000)%256switch(key){case 0: P0=0xffwe1=1P0=~Table_of_Digits[Num_Index*8+i]we1=0P0=0xff//输出位码和段码 we2=1P0=xdat[i]we2=0if(++i==8) i=0//每屏一个数字由8 个字节构成 if(++t==250) //每个数字刷新显示一段时间 { t=0if(++Num_Index==10) Num_Index=0//显示下一个数字 } breakcase 1: we1=1P0=~xdat[xi]we1=0we2=1P0=ydat[yi]we2=0if(++t==250) //每个数字刷新显示一段时间 { t=0yi++if(yi>7){yi=0xi++} if(xi>7)xi=0} breakcase 2: we1=1P0=0x00we1=0P0=0xff//输出位码和段码 we2=1P0=xdat[i]we2=0if(++t==250) //每个数字刷新显示一段时间 { if(++i==8) i=0//每屏一个数字由8 个字节构成 t=0} breakdefault:key=0 i=0 j=0 t=0 xi=0 yi=0 Num_Index=0 we1=1 P0=0xff we1=0 we2=1 P1=0x80 we2=0break}}

#include <reg52.h>

sbit ADDR0 = P1^0

sbit ADDR1 = P1^1

sbit ADDR2 = P1^2

sbit ADDR3 = P1^3

sbit ENLED = P1^4

unsigned char code image[11][8] = {

{0xC3, 0x81, 0x99, 0x99, 0x99, 0x99, 0x81, 0xC3}, //数字0

{0xEF, 0xE7, 0xE3, 0xE7, 0xE7, 0xE7, 0xE7, 0xC3}, //数字1

{0xC3, 0x81, 0x9D, 0x87, 0xC3, 0xF9, 0xC1, 0x81}, //数字2

{0xC3, 0x81, 0x9D, 0xC7, 0xC7, 0x9D, 0x81, 0xC3}, //数字3

{0xCF, 0xC7, 0xC3, 0xC9, 0xC9, 0x81, 0xCF, 0xCF}, //数字4

{0x81, 0xC1, 0xF9, 0xC3, 0x87, 0x9D, 0x81, 0xC3}, //数字5

{0xC3, 0x81, 0xF9, 0xC1, 0x81, 0x99, 0x81, 0xC3}, //数字6

{0x81, 0x81, 0x9F, 0xCF, 0xCF, 0xE7, 0xE7, 0xE7}, //数字7

{0xC3, 0x81, 0x99, 0xC3, 0xC3, 0x99, 0x81, 0xC3}, //数字8

{0xC3, 0x81, 0x99, 0x81, 0x83, 0x9F, 0x83, 0xC1}, //数字9

{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //全亮

}

void main()

{

EA = 1 //使能总中断

ENLED = 0 //使能U4,选择LED点阵

ADDR3 = 0

TMOD = 0x01 //设置T0为模式1

TH0 = 0xFC //为T0赋初值0xFC67,定时1ms

TL0 = 0x67

ET0 = 1//使能T0中断

TR0 = 1//启动T0

while (1)

}

/* 定时器0中断服务函数 */

void InterruptTimer0() interrupt 1

{

static unsigned char i = 0 //动态扫描的索引

static unsigned int tmr = 0 //1s软件定时器

static unsigned char index = 9 //图片刷新索引

TH0 = 0xFC //重新加载初值

TL0 = 0x67

//以下代码完成LED点阵动态扫描刷新

P0 = 0xFF //显示消隐

switch (i)

{

case 0: ADDR2=0ADDR1=0ADDR0=0i++P0=image[index][0]break

case 1: ADDR2=0ADDR1=0ADDR0=1i++P0=image[index][1]break

case 2: ADDR2=0ADDR1=1ADDR0=0i++P0=image[index][2]break

case 3: ADDR2=0ADDR1=1ADDR0=1i++P0=image[index][3]break

case 4: ADDR2=1ADDR1=0ADDR0=0i++P0=image[index][4]break

case 5: ADDR2=1ADDR1=0ADDR0=1i++P0=image[index][5]break

case 6: ADDR2=1ADDR1=1ADDR0=0i++P0=image[index][6]break

case 7: ADDR2=1ADDR1=1ADDR0=1i=0P0=image[index][7]break

default: break

}

//以下代码完成每秒改变一帧图像

tmr++

if (tmr >= 1000) //达到1000ms时改变一次图片索引

{

tmr = 0

if (index == 0) //图片索引10~0循环

index = 10

else

index--

}

}

#include<reg52.h>//单片机头文件

void delay1ms(unsigned int ms)//延时ms函数声明(可根据实际情况更改)

unsigned char code led0[]={}//字码表,你自己把你要显示的字码放进去就可以了

void main()

{

unsigned char w,i

while(1)

{

//显示0

w=0x01 //行变量为第一行

for(i=0i<8i++)

{

P1=w //行数据送P1口

P0=led0[i]

delayms(1) //列数据送P0口

w<<=1 //向下移动一行

}

}

}

void delay1ms(unsigned int ms)

{

unsigned int i,j

for(i=0i<msi++)

for(j=0j<110j++)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7765015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存