|
TM1640芯片:- #include <STC12C5A.H>
- #include "intrins.h" //包含_nop_()指令头文件
- /********************定义控制端口**********************/
- sbit DIN=P4^1; //定义DIN数据端口
- sbit CLK=P3^7; //定义CLK数据端口
- sbit HH1 = P2^4;
- sbit HH2 = P2^3;
- sbit HH3 = P2^2;
- sbit HH4 = P2^1;
- sbit LL1 = P2^0;
- sbit LL2 = P0^7;
- sbit LL3 = P0^6;
- sbit LL4 = P0^5;
- unsigned char KeyValue = 20;//按键值 显示-
- /********************定义数据*************************/
- //共阴数码管段选表
- unsigned code tabel[]= {
- // 1 2 3 4 5 6 7 8
- 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
- //9 0 A B C D E F H L
- 0x6F, 0x3F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x76, 0x38,
- //n u - 熄灭
- 0x37, 0x3E, 0x40, 0x00 };
- /********************延时函数,延时nms******************/
- void delay(unsigned z)
- {
- unsigned x,y;
- for(x=z;x>0;x--)
- for(y=300;y>0;y--);
- }
- /*====================================
- 函数 :KeyScan()
- 参数 :无
- 返回值 :无
- 描述 :4*4矩阵键盘与独立键盘扫描
- 按键按下KeyValue全局变量值发生相应变化
- ====================================*/
- char KeyScan(void) //行列扫描
- {
- HH1 = 0;
- if (!LL1) KeyValue =0;
- if (!LL2) KeyValue =1;
- if (!LL3) KeyValue =2;
- if (!LL4) KeyValue =3;
- HH1 = 1;
- HH2 = 0;
- if (!LL1) KeyValue =4;
- if (!LL2) KeyValue =5;
- if (!LL3) KeyValue =6;
- if (!LL4) KeyValue =7;
- HH2 = 1;
- HH3 = 0;
- if (!LL1) KeyValue = 8;
- if (!LL2) KeyValue = 9;
- if (!LL3) KeyValue = 10;
- if (!LL4) KeyValue = 11;
- HH3 = 1;
- HH4 = 0;
- if (!LL1) KeyValue =12;
- if (!LL2) KeyValue =13;
- if (!LL3) KeyValue =14;
- if (!LL4) KeyValue =15;
- HH4 = 1;
- HH1=1;
- if(!HH1) KeyValue =16;
- HH2=2;
- if(!HH2) KeyValue =17;
- HH3=3;
- if(!HH3) KeyValue =18;
- HH4=4;
- if(!HH4) KeyValue =19;
- return 255; //没有按键,就返回 255
- }
- /********************Start函数*************************/
- void I2CStart()
- {
- DIN=1;
- CLK=1;
- delay(2);
- DIN=1;
- delay(2);
- DIN=0;
- delay(4);
- CLK=0;
- delay(2);
- }
- /********************Stop函数*************************/
- void I2CStop()
- {
- CLK=1;
- delay(2);
- DIN=0;
- delay(2);
- DIN=1;
- delay(2);
- CLK=0;
- DIN=0;
- }
- /***************发送8bit数据,从低位开始**************/
- void I2CWritebyte(unsigned char oneByte)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- CLK=0;
- if(oneByte&0x01)
- DIN=1;
- else
- DIN=0;
- delay(3);
- CLK=1;
- oneByte=oneByte>>1;
- }
- CLK=0;
- delay(4);
- DIN=0;
- delay(2);
- }
- void main()
- {
- while(1){
- unsigned char i;
- I2CStart();
- I2CWritebyte(0x40); //数据命令设置:普通模式,地址自动加一
- I2CStop();
- I2CStart();
- I2CWritebyte(0xc0); //地址命令设置:初始地址00H
- for(i=0;i<1;i++) //发送16位显示数据
- {
- KeyScan();//20个按键键盘扫描
- I2CWritebyte(tabel[KeyValue]); //显示按键值
- }
- I2CStop();
- I2CStart();
- I2CWritebyte(0x8c); //显示控制:显示开,脉冲宽度设为11/16
- I2CStop();
- }
- }
复制代码
74HC573芯片:
- #include<reg51.h>
- #include <intrins.h>//包含移位标准库函数头文件
- #define uint unsigned int
- #define uchar unsigned char
- sbit DU = P2^6;//数码管段选
- sbit WE = P2^7;//数码管段选
- uchar num;//数码管显示的值
- uchar KeyValue = 20;//按键值 显示-
- //共阴数码管段选表
- uchar code tabel[]= {
- //0 1 2 3 4 5 6 7 8
- 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
- //9 A B C D E F H L
- 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x76, 0x38,
- //n u - 熄灭
- 0x37, 0x3E, 0x40, 0x00 };
- /*====================================
- 函数 : delay(uint z)
- 参数 :z 延时毫秒设定,取值范围0-65535
- 返回值 :无
- 描述 :12T/Fosc11.0592M毫秒级延时
- ====================================*/
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
- /*====================================
- 函数 :KeyScan()
- 参数 :无
- 返回值 :无
- 描述 :4*4矩阵键盘与独立键盘扫描
- 按键按下KeyValue全局变量值发生相应变化
- ====================================*/
- sbit HH1 = P3^0;
- sbit HH2 = P3^1;
- sbit HH3 = P3^2;
- sbit HH4 = P3^3;
- sbit LL1 = P3^4;
- sbit LL2 = P3^5;
- sbit LL3 = P3^6;
- sbit LL4 = P3^7;
- char KeyScan(void) //行列扫描
- {
- HH1 = 0;
- if (!LL1) KeyValue =0;
- if (!LL2) KeyValue =1;
- if (!LL3) KeyValue =2;
- if (!LL4) KeyValue =3;
- HH1 = 1;
- HH2 = 0;
- if (!LL1) KeyValue =4;
- if (!LL2) KeyValue =5;
- if (!LL3) KeyValue =6;
- if (!LL4) KeyValue =7;
- HH2 = 1;
- HH3 = 0;
- if (!LL1) KeyValue = 8;
- if (!LL2) KeyValue = 9;
- if (!LL3) KeyValue = 10;
- if (!LL4) KeyValue = 11;
- HH3 = 1;
- HH4 = 0;
- if (!LL1) KeyValue =12;
- if (!LL2) KeyValue =13;
- if (!LL3) KeyValue =14;
- if (!LL4) KeyValue =15;
- HH4 = 1;
- return 255; //没有按键,就返回 255
- }
- void main()//main函数自身会循环
- {
- WE = 1;//打开位选锁存器
- P0 = 0XFE; //1111 1110
- WE = 0;//锁存位选数据
- DU = 1;//打开段选锁存器
- while(1)
- {
- KeyScan();//20个按键键盘扫描
- P0 = tabel[KeyValue];//显示按键值
- }
- }
复制代码
|
|