- LED点阵实验 (显示数字) *
- 行:高电平有效,低16位 列:低电平有效,高16位
- ******************************************************************************************/
- #include <REG51.H>
- #include <intrins.h>
- #define NOP() _nop_() // 定义空指令 ,这个函数在库 <intrins.h> 中
- //SPI IO
- sbit MOSIO = P3^4;
- sbit R_CLK = P3^5;
- sbit S_CLK = P3^6;
- //变量声明
- unsigned long column; //点阵列
- unsigned long row; //点阵行
- unsigned long dt;
- //点阵显示数组
- unsigned char code tab0[] = {0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
- 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00};
- unsigned char code tab1[] = {0,0,0,0,0,0,8,24,14,36,8,66,8,66,8,66,8,66,8,66,8,66,8,36,62,24,0,0,0,0,0,0};
- unsigned char code tab2[] = {0,0,0,0,0,0,24,24,36,36,66,66,66,66,66,66,66,100,66,88,66,64,66,64,36,36,24,28,0,0,0,0} ;
- unsigned char code tab3[] = {0,0,0,0,0,0,24,60,36,66,66,66,66,66,66,36,66,24,66,36,66,66,66,66,36,66,24,60,0,0,0,0};
- unsigned char code tab4[] = {0,0,0,0,0,0,24,126,36,34,66,34,66,16,66,16,66,8,66,8,66,8,66,8,36,8,24,8,0,0,0,0};
- unsigned char code tab5[] = {0,0,0,0,0,0,24,56,36,36,66,2,66,2,66,26,66,38,66,66,66,66,66,66,36,36,24,24,0,0,0,0};
- unsigned char code tab6[] = {0,0,0,0,0,0,24,126,36,2,66,2,66,2,66,26,66,38,66,64,66,64,66,66,36,34,24,28,0,0,0,0};
- unsigned char code tab7[] = {0,0,0,0,0,0,24,32,36,48,66,40,66,36,66,36,66,34,66,34,66,126,66,32,36,32,24,120,0,0,0,0};
- unsigned char code tab8[] = {0,0,0,0,0,0,24,60,36,66,66,66,66,32,66,24,66,32,66,64,66,64,66,66,36,34,24,28,0,0,0,0};
- unsigned char code tab9[] = {0,0,0,0,0,0,24,60,36,66,66,66,66,66,66,32,66,32,66,16,66,8,66,4,36,66,24,126,0,0,0,0};
- unsigned char code tab10[] = {0,0,0,0,0,0,24,8,36,14,66,8,66,8,66,8,66,8,66,8,66,8,66,8,36,8,24,62,0,0,0,0};
- unsigned char code tab11[] = {0,0,0,0,0,0,24,24,36,36,66,66,66,66,66,66,66,66,66,66,66,66,66,66,36,36,24,24,0,0,0,0};
- unsigned char code tab12[] = {0,0,0,0,0,0,60,28,34,34,34,65,1,65,1,65,1,65,113,65,33,65,34,65,34,34,28,28,0,0,0,0};
- //函数声明
- void HC595SendData( unsigned char BT3, unsigned char BT2,unsigned char BT1,unsigned char BT0);
- /*************主函数*****************************/
- void main(void)
- {
- int k,i,ms;
- i = 16; //显示时间
- while(1)
- {
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //亲
- {
- HC595SendData(~tab1[2*k +1],~tab1[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //爱
- {
- HC595SendData(~tab2[2*k +1],~tab2[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //的
- {
- HC595SendData(~tab3[2*k +1],~tab3[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //顾
- {
- HC595SendData(~tab4[2*k +1],~tab4[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //客
- {
- HC595SendData(~tab5[2*k +1],~tab5[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //欢
- {
- HC595SendData(~tab6[2*k +1],~tab6[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //迎
- {
- HC595SendData(~tab7[2*k +1],~tab7[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //使
- {
- HC595SendData(~tab8[2*k +1],~tab8[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //用
- {
- HC595SendData(~tab9[2*k +1],~tab9[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //普
- {
- HC595SendData(~tab10[2*k +1],~tab10[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //中
- {
- HC595SendData(~tab11[2*k +1],~tab11[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- while(1)
- {
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //中
- {
- HC595SendData(~tab12[2*k +1],~tab12[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //中
- {
- HC595SendData(0xff,0xff,0,0);
- }
- }
- } //清屏
- }
- }
- /*********************************************************************************************************
- ** 函数名称: HC595SendData
- ** 功能描述: 向SPI总线发送数据
- *********************************************************************************************************/
- void HC595SendData( unsigned char BT3, unsigned char BT2,unsigned char BT1,unsigned char BT0)
- {
- unsigned char i;
- for(i = 0; i < 32; i++)
- {
- if(i > 23 )
- {
- if((BT0 << (i - 24)) & 0x80)
- {
- MOSIO = 1;
- }
- else
- {
- MOSIO = 0;
- }
- }
- else if(i > 15 & i < 24 )
- {
- if((BT1 << (i - 16)) & 0x80) //set dataline high 0X80000000 最高位与SendVal左移的最高位 进行逻辑运算
- {
- MOSIO = 1; // 如果为真 MOSIO = 1
- }
- else
- {
- MOSIO = 0;
- }
- }
- else if(i < 16 & i > 7 )
- {
- if((BT2 << (i - 8)) & 0x80)
- {
- MOSIO = 1;
- }
- else
- {
- MOSIO = 0;
- }
- }
- else
- {
- if((BT3 << i) & 0x80) //set dataline high 0X80000000 最高位与SendVal左移的最高位 进行逻辑运算
- {
- MOSIO = 1; // 如果为真 MOSIO = 1
- }
- else
- {
- MOSIO = 0;
- }
- }
- S_CLK = 0;
- // NOP(); //产生方形波
- S_CLK = 1;
- }
- R_CLK = 0; //set dataline low
- // NOP(); //产生方形波
- R_CLK = 1; //片选
- }
[color=rgb(51, 102, 153) !important]复制代码
下面是显示汉字的程序:
- /*****************************************************************************************
- * *
- * LED点阵实验 (显示数字) *
- 行:高电平有效,低16位 列:低电平有效,高16位
- ******************************************************************************************/
- #include <REG51.H>
- #include <intrins.h>
- #define NOP() _nop_() // 定义空指令 ,这个函数在库 <intrins.h> 中
- //SPI IO
- sbit MOSIO = P3^4;
- sbit R_CLK = P3^5;
- sbit S_CLK = P3^6;
- //变量声明
- unsigned long column; //点阵列
- unsigned long row; //点阵行
- unsigned long dt;
- //点阵显示数组
- unsigned char code tab0[] = {0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
- 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00};
- unsigned char code tab1[] = {96,2,28,10,16,18,16,2,254,63,16,2,16,18,112,18,24,10,22,10,16,36,16,42,16,49,156,32,0,0,0,0};
- unsigned char code tab2[] = {0,30,252,9,72,8,144,4,254,63,66,32,252,31,32,0,240,15,80,4,136,2,4,1,194,6,56,56,0,0,0,0};
- unsigned char code tab3[] = {144,0,144,0,208,63,72,32,40,16,12,2,10,2,136,10,136,18,72,18,72,34,40,34,8,2,136,3,0,0,0,0};
- unsigned char code tab4[] = {0,0,0,0,60,240,102,156,67,130,0,0,0,0,0,0,0,0,0,0,48,24,96,8,192,7,0,0,0,0,0,0};
- //函数声明
- void HC595SendData( unsigned char BT3, unsigned char BT2,unsigned char BT1,unsigned char BT0);
- /*************主函数*****************************/
- void main(void)
- {
- int k,i,ms;
- i = 16; //显示时间
- while(1)
- {
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //我
- {
- HC595SendData(~tab1[2*k +1],~tab1[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //爱
- {
- HC595SendData(~tab2[2*k +1],~tab2[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = i; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //你
- {
- HC595SendData(~tab3[2*k +1],~tab3[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0); //清屏
- for(ms = 30; ms > 0; ms--)
- {
- for(k = 0; k < 16; k++) //笑脸
- {
- HC595SendData(~tab4[2*k +1],~tab4[2*k],tab0[2*k],tab0[2*k + 1]);
- }
- }
- HC595SendData(0xff,0xff,0,0);
- }
- }
- /*********************************************************************************************************
- ** 函数名称: HC595SendData
- ** 功能描述: 向SPI总线发送数据
- *********************************************************************************************************/
- void HC595SendData( unsigned char BT3, unsigned char BT2,unsigned char BT1,unsigned char BT0)
- {
- unsigned char i;
- for(i = 0; i < 32; i++)
- {
- if(i > 23 )
- {
- if((BT0 << (i - 24)) & 0x80)
- {
- MOSIO = 1;
- }
- else
- {
- MOSIO = 0;
- }
- }
- else if(i > 15 & i < 24 )
- {
- if((BT1 << (i - 16)) & 0x80) //set dataline high 0X80000000 最高位与SendVal左移的最高位 进行逻辑运算
- {
- MOSIO = 1; // 如果为真 MOSIO = 1
- }
- else
- {
- MOSIO = 0;
- }
- }
- else if(i < 16 & i > 7 )
- {
- if((BT2 << (i - 8)) & 0x80)
- {
- MOSIO = 1;
- }
- else
- {
- MOSIO = 0;
- }
- }
- else
- {
- if((BT3 << i) & 0x80) //set dataline high 0X80000000 最高位与SendVal左移的最高位 进行逻辑运算
- {
- MOSIO = 1; // 如果为真 MOSIO = 1
- }
- else
- {
- MOSIO = 0;
- }
- }
- S_CLK = 0;
- // NOP(); //产生方形波
- S_CLK = 1;
- }
- R_CLK = 0; //set dataline low
- // NOP(); //产生方形波
- R_CLK = 1; //片选
- }
|