}
/*串行输出16位数据函数*/
voidout_16bit(unsignedintnumb)
{unsignedchari;
unsignedintx;
x=numb;
LEDALE=0,LEDCLK=0;/*接口初始化*/
for(i=0;i<16;i++)
{LEDDI=(x&0x8000);/*如果x&0x8000为真LEDDI=1*/
LEDCLK=1;LEDCLK=1;/*送时钟上升沿*/
LEDCLK=0;LEDCLK=0;
x=x<<1;/*数据左移1位*/
}
LEDALE=1;/*送锁存信号上升沿*/
LEDALE=0;
}
voidmain(void)/*测试主函数*/
{unsignedcharxdata*Y0;/*定义外部指针变量*/
unsignedintx;
unsignedchari,j=0;
Y0=0x1000;/*将外部地址1000H赋给Y0*/
x=0x5555;
while
(1)/**在第1-16列上显示x的值,操作U4*/
{x=~x;/*x数值取反*/
for(i=0;i<16;i++)
{out_16bit(x);/*输出x的非*/
j=i;j<<=3;/*计算合理的列值*/
*Y0=j;
delay_l(0x4000);/*延时*/
}
}/*while
(1)结束*/
}/*main结束*/
在本例中读者如果将“delay_l(0x4000)”延时函数中的0x4000,改成0x30后,再观看显示结果有何不同。
2.单片机点阵汉字显示实践
一、硬件电路描述
市面上已有很多有关点矩阵显示器的产品,如广告活动字幕机、股票显示板、活动布告栏等。
它的优点是可按需要的大小、形状、单色或彩色来组合,可与计算机或单片机系统连接,做各种广告性文字或图形显示。
点阵式LED显示器的种类可分为5×7、5×8、6×8、8×8等4种;而按LED发光变化颜色来分,可分为单色、双色、三色;按LED的极性排列方式又可分为共阳极与共阴极。
本实验作为一种原理性的学习,采用4块8×8的共阳极单色点阵式LED组成16×16的点阵来显示汉字或图形。
16×16的点阵电路如图5所示。
16行驱动与16列译码电路如图4所示。
2片74HC138译码输出控制图4的三极管的16列。
而2片74HC595连接起来作为16位数据的输出控制图5的16行。
采用这种电路可以大大减少单片机I/O管脚的使用,而且可扩展更多的点阵驱动。
读者通过基本LED点阵屏的编程练习,可以实现“静态”、“动态”和“滚屏”显示汉字或图形的方法,从中可以获得学习单片机的乐趣。
二、软件编程
根据XD2008综合实验电路,用C实现的汉字静态及动态显示程序如下:
#include/*包含52资源定义头文件*/
#include/*XBYTE的头文件*/
sbitLEDALE=P1^3;/*定义74HC595锁存信号*/
sbitLEDDI=P1^7;/*定义74HC595的串行输入信号*/
sbitLEDCLK=P3^3;/*定义74HC595的时钟信号*/
sbitK1=P1^1;/*k1按键输入脚*/
sbitK2=P2^4;/*k2按键输入脚*/
#defineY0XBYTE[0x1000]/*Y0的口地址*/
unsignedcharled123;/*定义LED灯变量*/
/*-----------有关汉字显示字模---------------*/
unsignedintcodedis_numb1[16]={0x4000,0x4FFE,0x4814,0x4824,
0x4844,0x7F84,0x4804,0x4804,0x4804,0x7F84,0x4844,0x4844,
0x4844,0x4FFE,0x4000,0x0000};/*西*/
unsignedintcodedis_numb2[16]={0x0101,0x0901,0x3101,0x2102,
0x21E2,0x2F14,0xA514,0x6108,0x2114,0x2124,0x21C2,0x2103,
0x2902,0x3100,0x2100,0x0000};/*安*/
unsignedintcodedis_numb3[16]={0x0000,0x0000,0x1FF0,0x1220,
0x1220,0x1220,0x1220,0xFFFC,0x1222,0x1222,0x1222,0x1222,
0x1FF2,0x0002,0x000E,0x0000};/*电*/
unsignedintcodedis_numb4[16]={0x0080,0x0080,0x4080,0x4080,
0x4080,0x4082,0x4081,0x47FE,0x4880,0x5080,0x6080,0x4080,
0x0080,0x0180,0x0080,0x0000};/*子*/
unsignedintcodedis_numb5[16]={0x0820,0x4840,0x4980,0x4E00,
0x7FFF,0x8A00,0x8920,0x0020,0x4420,0x3340,0x0040,0x0040,
0xFFFF,0x0080,0x0080,0x0000};/*科*/
unsignedintcodedis_numb6[16]={0x1080,0x1082,0x1101,0xFFFE,
0x1200,0x1402,0x0002,0x1304,0x12C8,0x1230,0xFE30,0x1248,
0x1384,0x1206,0x1004,0x0000};/*技*/
unsignedintcodedis_numb7[16]={0x0400,0x0401,0x0402,0x0404,
0x0408,0x0430,0x05C0,0xFE00,0x0580,0x0460,0x0410,0x040C,
0x0406,0x0403,0x0402,0x0000};/*大*/
unsignedintcodedis_numb8[16]={0x0200,0x0C40,0x0840,0x4840,
0x3A40,0x2A40,0x0A42,0x8A41,0x7AFE,0x2B40,0x0A40,0x1840,
0xEA40,0x4C40,0x0840,0x0000};/*学*/
unsignedintcodedis_numbz0[]={
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,/*不显示*/
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0FFA,0x0000,0x0000,0x0000,/*!
*/
0x0000,0x4040,0x4042,0x5F43,0x5264,0x5254,0x7E48,0x5248,
0x5248,0x5254,0x7FD4,0x5272,0x5242,0x5F41,0x4041,0x4040,/*要*/
0x0000,0x1002,0x1002,0x1012,0x97D2,0x9552,0x9552,0x9552,
0xFFFE,0x5552,0x5552,0x5552,0x57D2,0x5012,0x1002,0x1002,/*重*/
0x0000,0x0004,0x0046,0x7ECC,0x5258,0x5220,0x53C8,0x5204,
0x5202,0x7FFF,0x0002,0x5800,0xC400,0x23FF,0x1100,0x0880,/*很*/
0x0000,0x0906,0x2941,0x3922,0x491C,0x0970,0xFF88,0x0484,
0x0482,0x0008,0x7C88,0x4488,0x47FC,0x4404,0x7CFC,0x0004,/*践*/
0x0000,0x2040,0x3040,0x2842,0x2047,0x2048,0x2050,0x6FE0,
0xA050,0x2948,0x2DC4,0x3242,0x2042,0x3041,0x0841,0x0000,/*实*/
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
unsignedintcodedis_numbz[]={
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,/*不显示*/
0x0000,0x2040,0x3040,0x2842,0x2047,0x2048,0x2050,0x6FE0,
0xA050,0x2948,0x2DC4,0x3242,0x2042,0x3041,0x0841,0x0000,/*实*/
0x0000,0x0906,0x2941,0x3922,0x491C,0x0970,0xFF88,0x0484,
0x0482,0x0008,0x7C88,0x4488,0x47FC,0x4404,0x7CFC,0x0004,/*践*/
0x0000,0x0004,0x0046,0x7ECC,0x5258,0x5220,0x53C8,0x5204,
0x5202,0x7FFF,0x0002,0x5800,0xC400,0x23FF,0x1100,0x0880,/*很*/
0x0000,0x1002,0x1002,0x1012,0x97D2,0x9552,0x9552,0x9552,
0xFFFE,0x5552,0x5552,0x5552,0x57D2,0x5012,0x1002,0x1002,/*重*/
0x0000,0x4040,0x4042,0x5F43,0x5264,0x5254,0x7E48,0x5248,
0x5248,0x5254,0x7FD4,0x5272,0x5242,0x5F41,0x4041,0x4040,/*要*/
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0FFA,0x0000,0x0000,0x0000,/*!
*/
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
voiddelay_l(unsignedintn)/*长延时*/
{unsignedinti;
for(i=0;i}
voiddelay_s(unsignedcharn)/*短延时*/
{unsignedinti;
for(i=0;i}
/*串行输出16位行数据函数*/
voiddisplay_r(unsignedintnumb)
{unsignedchari;
unsignedintx;
x=~numb;/*数据取反*/
LEDALE=0,LEDCLK=0;
for(i=0;i<16;i++)
{LEDDI=(x&0x8000);
LEDCLK=1;LEDCLK=1;
LEDCLK=0;LEDCLK=0;
x=x<<1;
}
Y0=led123|0x80;/*退出时关显示器D=1*/
LEDALE=1;
LEDALE=0;
}
/*正显示16×16点阵汉字*/
voiddisp_16rz(unsignedintnumb[])
{unsignedchary0data=0,abcd=0,i,j;
for(i=0;i<16;i++)
{display_r(numb[i]);/*送行数据*/
j=abcd<<3;/*计算列的位置*/
y0data=j|(led123&0x07);
Y0=y0data;/*显示一列数据*/
abcd++;/*列加1*/