首页 > 代码库 > 准备用C51做个学生管理系统(2)
准备用C51做个学生管理系统(2)
能够控制矩阵键盘以后,我就直接改代码控制8X8的键盘,用P1和P3口
1 void keyscan() 2 { 3 uchar tmp; 4 P1 = 0xff; 5 P3 = 0x00; 6 delay(5); 7 tmp = P1^0xff; 8 9 //blink(); 10 //delay(500); 11 12 switch(tmp){ 13 case 1: 14 now_num = 0; 15 break; 16 case 2: 17 now_num = 1; 18 break; 19 case 4: 20 now_num = 2; 21 break; 22 case 8: 23 now_num = 3; 24 break; 25 case 16: 26 now_num = 4; 27 break; 28 case 32: 29 now_num = 5; 30 break; 31 case 64: 32 now_num = 6; 33 break; 34 case 128: 35 now_num = 7; 36 break; 37 default: 38 now_num = 44; 39 } 40 41 P1 = 0x00; 42 P3 = 0xff; 43 delay(5); 44 tmp = P3^0xff; 45 switch(tmp){ 46 case 1: 47 now_num += 56; 48 break; 49 case 2: 50 now_num += 48; 51 break; 52 case 4: 53 now_num += 40; 54 break; 55 case 8: 56 now_num += 32; 57 break; 58 case 16: 59 now_num += 24; 60 break; 61 case 32: 62 now_num += 16; 63 break; 64 case 64: 65 now_num += 8; 66 break; 67 case 128: 68 now_num += 0; 69 break; 70 } 71 } 72 73 void show() 74 { 75 if(pre_num != now_num) 76 { 77 display_seg(now_num); 78 pre_num = now_num; 79 } 80 display_seg(now_num); 81 //delay(100); 82 } 83 84 //主函数 85 void main() 86 { 87 int i = 0; 88 int j = 0; 89 90 P0 = 0xff; 91 P2 = 0; 92 init(); 93 TR1 = 1; 94 95 cnt = 00; 96 while(1) 97 { 98 P1 = 0x00; 99 P3 = 0xff;100 101 if(P3 != 0xff)keyscan();102 show();103 } 104 }
差不多就开始做LCD1062显示字符串的功能了。首先画protues电路图
然后在工程里面添加一个LCD1062.c的文件,代码如下:
1 /*************** LCD1062.c ******************/ 2 #include "common.h" 3 4 sbit RS = P2^0; 5 sbit RW = P2^1; 6 sbit EN = P2^2; 7 8 void Delayms(uint ms) 9 {10 uchar i;11 while(ms--)12 {13 for(i=0;i<120;i++);14 }15 }16 17 uchar Busy_Check()18 {19 uchar LCD_Status;20 RS = 0;21 RW = 1;22 EN = 1;23 Delayms(1);24 LCD_Status = P0;25 EN = 0;26 return LCD_Status;27 }28 29 void Write_LCD_Command(uchar cmd)30 {31 while((Busy_Check()&0x80)==0x80);32 RS = 0;33 RW = 0;34 EN = 0;35 P0 = cmd;36 EN = 1;37 Delayms(1);38 EN = 0;39 }40 41 void Write_LCD_Data(uchar dat)42 {43 while((Busy_Check()&0x80)==0x80);44 RS = 1;45 RW = 0;46 EN = 0;47 P0 = dat;48 EN = 1;49 Delayms(1);50 EN = 0;51 }52 53 void Initialize_LCD()54 {55 Write_LCD_Command(0x38);56 Delayms(1); 57 Write_LCD_Command(0x01);58 Delayms(1); 59 Write_LCD_Command(0x06);60 Delayms(1); 61 Write_LCD_Command(0x0c);62 Delayms(1); 63 }64 65 void ShowString(uchar x,uchar y,uchar *str)66 {67 uchar i = 0;68 if(y == 0)69 Write_LCD_Command(0x80 | x);70 if(y == 1)71 Write_LCD_Command(0xc0 | x);72 for(i=0;i<16;i++)73 {74 Write_LCD_Data(str[i]); 75 }76 }
代码的意思我也没细看,是直接从单片机100个例子里面摘抄的,要看明白可能还得了解LCD1062的一些指令,总之调用函数
ShowString(uchar x,uchar y,uchar *str)
大概的意思:第一个参数是***(过些日子再看看)第二个参数是行号,第三个参数是显示的字符串,基本上就能够调用函数让LCD屏显示数据了。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。