贡献一个8*8的点阵控制代码,比较合适初学者掌握点阵的控制原理。
点阵连接方式很简单,P0接行信号,P2接列信号,可以高位在前,也可以高位在后,主意编码的方式。
硬件电路图,P0直接接行信号,P2直接接列信号,高位在前。
硬件电路图,P0直接接行信号,P2直接接列信号,高位在前。
编码方式
编码方式
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- void delay1ms(unsigned char c) //误差 0us
- {
- unsigned char a,b;
- for(c;c>0;c--)
- for(b=142;b>0;b--)
- for(a=2;a>0;a--);
- }
- unsigned char code taba[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //纵条表
- unsigned char code tabb[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //衡条表
- //unsigned char code a[]={0x18,0x24,0x24,0x42,0x7E,0x42,0x42,0x81};
- unsigned char code a[] = {0xE7,0xDB,0xDB,0xBD,0x81,0xBD,0xBD,0x7E};
- #if 0
- unsigned char number[]= {
- 0xFF,0x9F,0xBF,0xBF,0xBF,0xBF,0x1F,0xFF,/*"1",0*/
- 0xFF,0xC3,0xDB,0xDF,0xEF,0xF7,0xFB,0xC3,/*"2",1*/
- 0xFF,0xC3,0xDB,0xE7,0xDF,0xDF,0xDB,0xE3,/*"3",2*/
- 0xFF,0xDF,0xCF,0xD7,0xDB,0xDB,0xC7,0xDF,/*"4",3*/
- 0xFF,0xC3,0xFB,0xE3,0xDF,0xDF,0xDB,0xE3,/*"5",4*/
- 0xFF,0xE7,0xFB,0xFB,0xC3,0xDB,0xDB,0xE7,/*"6",5*/
- 0xFF,0xC3,0xEF,0xEF,0xF7,0xF7,0xF7,0xF7,/*"7",6*/
- 0xFF,0xC7,0xDB,0xDB,0xE7,0xDB,0xDB,0xE7,/*"8",7*/
- 0xFF,0xE7,0xDB,0xDB,0xC3,0xDF,0xDF,0xE7,/*"9",8*/
- 0xFF,0xE7,0xDB,0xDB,0xDB,0xDB,0xDB,0xE7/*"0",9*/};
- #endif
- #if 0 //高位在后
- unsigned char chara[6][8]={
- {0xFF,0xFF,0xF7,0xEB,0xED,0xC1,0x9C,0xFF},/*"A",0*/
- {0xFF,0xFF,0xC1,0xDD,0xE1,0xDD,0xC1,0xFF},/*"B",1*/
- {0xFF,0xFF,0x83,0xF9,0xFD,0xB9,0x83,0xFF},/*"C",2*/
- {0xFF,0xFF,0xE1,0xDD,0xDD,0xCD,0xE1,0xFF},/*"D",3*/
- {0xFF,0xFF,0xC1,0xFD,0xE1,0xFD,0xC1,0xFF},/*"E",4*/
- {0xFF,0xFF,0xC1,0xFD,0xE1,0xFD,0xFD,0xFF},/*"F",5*/
- };
- #endif
- #if 1 //高位在前
- unsigned char chara[6][8]={
- {0xFF,0xFF,0xEF,0xD7,0xB7,0x83,0x39,0xFF},/*"A",0*/
- {0xFF,0xFF,0x83,0xBB,0x87,0xBB,0x83,0xFF},/*"B",1*/
- {0xFF,0xFF,0xC1,0x9F,0xBF,0x9D,0xC1,0xFF},/*"C",2*/
- {0xFF,0xFF,0x87,0xBB,0xBB,0xB3,0x87,0xFF},/*"D",3*/
- {0xFF,0xFF,0x83,0xBF,0x87,0xBF,0x83,0xFF},/*"E",4*/
- {0xFF,0xFF,0x83,0xBF,0x87,0xBF,0xBF,0xFF},/*"F",5*/
- };
- unsigned char temp[] = {0,0x01,0x3,0x7,0xF,0x1F,0x3F,0x7F,0xFF};
- unsigned char temp2[] = {0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80};
- void main()
- {
- unsigned char i,j,k,z;
- P0 = 1;
- P2 = 0xFE;
- #if 1
- //向左移动字符A
- while(1){
-
- for(k=0;k<16;k++)
- for(j=0;j<30;j++)
- for(i=0;i<8;i++){
- P0 = tabb[i];
- if(k<=7)
- P2 = a[i]>>(8-k) | temp2[k] ;//屏蔽左边没有数据的位
- else
- P2 = ( a[i]<< (k-8) ) | temp[k-8] ; //屏蔽右边移出来的空位,补一,否则右边会亮
- delay1ms(2);
- }
- }
- #endif
- #if 0
- //向左移动所有字符
- while(1)
- {
- for(z=0;z<6;z++)
- {
- for(j=0;j<8;j++) //在可见视窗内,只能显示8列数据,
- {
- for(k=0;k<10;k++) //每一帧数据要显示一定时间,否则看不到要显示的内容
- for(i=0;i<8;i++) //显示一帧,每一帧数据由8行组成
- {
- P0 = tabb[i];
- if(z == 5)
- P2 = chara[z][i] << j;
- else
- P2 = chara[z][i] << j | (chara[z+1][i] >> (8-j));//对数据做重新的组合之后再显示
- delay1ms(2);
- }
- }
- }
- }
- #endif
- #if 0
- while(1){
- //先将二维数组变为一维数组,这样比较容易操作。向上移动显示0-9 ,P0为行扫描,P2为列扫描
- for(k=0;k<sizeof(number)-8;k++)
- for(j=0;j<30;j++)
- for(i=0;i<8;i++){
- P0 = tabb[i];
- P2 = number[k+i];
- delay1ms(2);
- }
- }
- #endif
- #if 0
- //静态显示字母A
- while(1){
- for(i=0;i<8;i++){
- P0 = tabb[i];
- P2 = a[i];
- delay1ms(2);
- }
- }
- #endif
- #if 0
- while(1){
- //循环显示0-9 ,P0为行扫描,P3为列扫描
- for(k=0;k<10;k++)
- for(j=0;j<200;j++)
- for(i=0;i<8;i++){
- P0 = tabb[i];
- P2 = number[k][i];
- delay1ms(2);
- }
- }
- #endif
-
-
- /*
-
- while(1){
- for(i=0;i<8;i++){
- P1 = a[i];
- P3 = taba[i];
- delay1ms();
- P1 = 0;
- P3 = 0;
-
- }
- }
-
-
- while(1){
- //逐行扫描,从上到下
- for(i=0;i<8;i++){
- P0 = tabb[i];
- Delay500ms();
- P2 = 0x00;
- }
- //逐行扫描,从下到上
- for(i=7;i>0;i--){
- P0 = tabb[i];
- Delay500ms();
- P2 = 0x00;
- }
- //逐列扫描 从右到左
- for(i=0;i<8;i++){
- P0 = 0xFF;
- P2 = taba[i];
- Delay500ms();
- }
- //逐列扫描 从左到右
- for(i=7;i>0;i--){
- P0 = 0xFF;
- P2 = taba[i];
- Delay500ms();
- }
- P0 = 0;
- P2 = 0;
- }
- */
- }
复制代码
所有资料51hei提供下载:
dotled.rar
(267.93 KB, 下载次数: 22)
|