1、独立键盘动态显示接口电路键盘显示电路图与程序程序1 74595_4位LED#include#include#define uchar unsigned char#define uint unsigned intunsigned char code ledk10=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;sbit DS=P10; /串行数据输入sbit SHCP =P11; /移位时钟脉冲sbit STCP =P12; /输出锁存器控制脉冲void delay (unsigned int n)unsigned int i,j; for(i
2、=0;in;i+) for(j=0;jn ;j+); void shift(unsigned char d) unsigned char j=0; while(j8) if(d&0x80)!=0) DS=1; else DS=0; SHCP=0;_nop_();SHCP=1; d=1;j+; void OUT_595(void) STCP=0; _nop_(); STCP=1; _nop_(); /上升沿将数据送到输出锁存器 STCP=0;main() uchar i,mask=0xef; while(1) for (i=0; i10; i+) shift(ledki); OUT_595();
3、 P1=(mask&0xf0)|(P1&0x0f); mask=_crol_(mask,1); if(mask=0xfe)mask=0xef; delay(200); /程序2 74595_8位LED#include#include#define uchar unsigned char#define uint unsigned intunsigned char code ledk10=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;sbit SDATA=P10; /串行数据输入sbit SCLK =P11; /移位时钟脉冲sbit RCK =
4、P12; /输出锁存器控制脉冲uchar temp; void delay (unsigned int n)unsigned int i,j; for(i=0;in;i+) for(j=0;jn ;j+); void shift(unsigned char d) unsigned char j=0; while(j8) if(d&0x80)!=0) SDATA=1; else SDATA=0; SCLK=0;_nop_();SCLK=1; d=1;j+; void OUT_595(void) RCK=0; _nop_(); RCK=1; _nop_(); /上升沿将数据送到输出锁存器 RCK=
5、0;main() uchar i,mask=0xfe; while(1) for (i=0; i10; i+) shift(ledki); OUT_595(); P2=mask; mask=_crol_(mask,1); delay(200); /程序3 LCD#include #include void main(void)unsigned char i,a5=5,4,3,2,1; lcd_init(); while(1)wr_comd(0x80); for(i=0;i0;t-) for(j=125;j0;j-);void wr_comd(unsigned char x)P0=x;rs=0;
6、rw=0;e=1;delay_ms(1);e=0;void wr_data(unsigned char x)P0=x;rs=1;rw=0;e=1;delay_ms(1);e=0;void lcd_init(void) wr_comd(0x01);delay_ms(5); wr_comd(0x38);delay_ms(5);wr_comd(0x38);delay_ms(5); wr_comd(0x06);delay_ms(5);wr_comd(0x0c);/程序4 按键功能1#include #include LCD1602.hunsigned int Count,Count1;unsigned
7、 char a5,b5;unsigned char dispbuf6,key,dispbuf16;sbit P10=P10;sbit P11=P11;sbit P12=P12;sbit P13=P13;/本程序与按键直接输入_共阴程序不同之处是本程序不允许两个或以上键按下,否则,不承认void hextodec(unsigned int t) a0=(t/10000)%10+0; /取得整数值到数组,转成ASCII码 a1=(t/1000)%10+0; a2=(t/100)%10+0; a3=(t/10)%10+0; a4=(t/1)%10+0; void hextodec1(unsigned
8、 int t) b0=(t/10000)%10+0; /取得整数值到数组,转成ASCII码 b1=(t/1000)%10+0; b2=(t/100)%10+0; b3=(t/10)%10+0; b4=(t/1)%10+0; void Delay1ms(unsigned int count)unsigned int i,j; for(i=0;icount;i+) for(j=0;j120;j+);unsigned char get_key(void)unsigned char key_code; unsigned char mask=0x01,low_num=0,i; for(i=0;i4;i+
9、) if(!(mask&P1)low_num+; mask=mask1; /mask,low_num都必须设为局部变量,不能是全局变量 hextodec1(low_num); /显示按键按下数if(low_num=1) if (P10=0)key_code=0; else if (P11=0)key_code=1; else if (P12=0)key_code=2; else if (P13=0)key_code=3; else key_code=0xff; return(key_code);main()unsigned int i; LCD_Initial(); GotoXY(2,0);
10、Print(1602LCD Disp); EA=1;EX0=1;IT0=1,EX1=1;IT1=1; TMOD=0x05; while(1) key=get_key( ); hextodec(key); for(i=0; i5; i+)dispbufi=ai; dispbuf5=0; GotoXY(1,1); Print(&dispbuf0);for(i=0; i5; i+)dispbuf1i=bi; dispbuf15=0; GotoXY(10,1); Print(&dispbuf10); Delay1ms(100); 程序4.1 LCD1602头文件#ifndef LCD_CHAR_160
11、2_2005_4_9#define LCD_CHAR_1602_2005_4_9#include sbit LcdRs = P20; /口定义 sbit LcdRw = P21;sbit LcdEn = P22;sfr DBPort = 0x80; /P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口unsigned char LCD_Wait(void) /内部等待函数LcdRs=0; LcdRw=1; _nop_(); LcdEn=1; _nop_(); LcdEn=0; return DBPort; /向LCD写入命令或数据#define LCD_COMMAND 0
12、 / Command#define LCD_DATA 1 / Data#define LCD_CLEAR_SCREEN 0x01 / 清屏#define LCD_HOMING 0x02 / 光标返回原点void LCD_Write(bit style, unsigned char input) LcdEn=0; LcdRs=style; LcdRw=0; _nop_(); DBPort=input; _nop_();/注意顺序 LcdEn=1; _nop_();/注意顺序 LcdEn=0; _nop_(); LCD_Wait(); /设置显示模式#define LCD_SHOW 0x04 /显
13、示开#define LCD_HIDE 0x00 /显示关 #define LCD_CURSOR 0x02 /显示光标#define LCD_NO_CURSOR 0x00 /无光标 #define LCD_FLASH 0x01 /光标闪动#define LCD_NO_FLASH 0x00 /光标不闪动void LCD_SetDisplay(unsigned char DisplayMode) LCD_Write(LCD_COMMAND, 0x08|DisplayMode); /设置输入模式*#define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 / defa
14、ult#define LCD_MOVE 0x01 / 画面可平移#define LCD_NO_MOVE 0x00 /defaultvoid LCD_SetInput(unsigned char InputMode) LCD_Write(LCD_COMMAND, 0x04|InputMode);/初始化LCDvoid LCD_Initial() LcdEn=0; LCD_Write(LCD_COMMAND,0x38); /8位数据端口,2行显示,5*7点阵 LCD_Write(LCD_COMMAND,0x38); LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); /
15、开启显示, 无光标 LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); /清屏 LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); /AC递增, 画面不动/*void GotoXY(unsigned char x, unsigned char y) if(y=0) LCD_Write(LCD_COMMAND,0x80|x); if(y=1) LCD_Write(LCD_COMMAND,0x80|(x-0x40);void Print(unsigned char *str) while(*str!=0) LCD_Write(LCD_DATA,*
16、str); str+; #endif/程序5 按键直接输入共阴#include #include unsigned char low_num,ledk11=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00;sbit P10=P10;sbit P11=P11;sbit P12=P12;sbit P13=P13;unsigned char get_key(void)unsigned char key_code; if (P10=0)key_code=0; else if (P11=0)key_code=1; else if (P12=0)
17、key_code=2; else if (P13=0)key_code=3; else key_code=0xff; /当没有按键按下 return(key_code);void key_func(key) switch (key) case 0: P2=ledk0;break; case 1: P2=ledk1;break; case 2: P2=ledk2;break; case 3: P2=ledk3;break; default: P2=ledk10;break; /当没有按键按下,不显示 void main(void)unsigned char key; while(1) key=g
18、et_key( ); key_func(key); /程序6 按键_74165#include #include sbit sh_ld=P10;sbit clk=P11;sbit in=P12;unsigned char disp8,xx;void delay(unsigned int n)unsigned int i; for(i=0;in;i+) ;hextobin(xx)disp0=xx/128; disp1=(xx%128)/64; disp2=(xx%64)/32; disp3=(xx%32)/16; disp4=(xx%16)/8; disp5=(xx%8)/4; disp6=(x
19、x%4)/2; disp7=xx%2; unsigned char shift165( )unsigned char j=0,d=0; sh_ld=1;delay(2);sh_ld=0;delay(2);sh_ld=1; while(j8) if(in=1)d=d*2+1; else d=d*2; clk=0;clk=1; j+; return d;void main(void) unsigned char i; lcd_init();while(1) xx=shift165(); hextobin(xx); wr_comd(0xc4); for(i=0;i0;t-) for(j=125;j0
20、;j-);void wr_comd(unsigned char x)P0=x;rs=0;rw=0;e=1;delay_ms(1);e=0;void wr_data(unsigned char x)P0=x;rs=1;rw=0;e=1;delay_ms(1);e=0;void lcd_init(void) wr_comd(0x01);delay_ms(5); wr_comd(0x38);delay_ms(5);wr_comd(0x38);delay_ms(5); wr_comd(0x06);delay_ms(5);wr_comd(0x0c);/程序7 点阵#include#include#def
21、ine uchar unsigned char#define uint unsigned int/unsigned char code a8=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80;void delay (unsigned int n)unsigned int i,j; for(i=0;in;i+) for(j=0;jn ;j+); main()unsigned int i,mask=0xfe,mask1=0x01; while(1) for(i=0;i8;i+) P1=0XFF; P2=mask; mask=_crol_(mask,1); delay(
22、200); for(i=0;i8;i+) P2=0;P1=mask1; mask1=_crol_(mask1,1); delay(200); / 程序8 点阵_74595 #include#include#define uchar unsigned char#define uint unsigned intsbit SDATA=P10; /串行数据输入sbit SCLK =P11; /移位时钟脉冲sbit RCK =P12; /输出锁存器控制脉冲void delay (unsigned int n)unsigned int i,j; for(i=0;in;i+) for(j=0;jn ;j+)
23、; void shift(unsigned char d) unsigned char j=0; while(j8) if(d&0x80)!=0) SDATA=1; else SDATA=0; SCLK=0;_nop_();SCLK=1; d=1;j+; void OUT_595(void) RCK=0; _nop_(); RCK=1; _nop_(); /上升沿将数据送到输出锁存器 RCK=0;main() uchar i,mask=0xfe,mask1=0x01; while(1) for(i=0;i8;i+) shift(0xff); OUT_595(); P2=mask; mask=_crol_(mask,1); delay(100); for(i=0;i
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1