微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于51单片机驱动LCD1602液晶显示thb6064ah细分64

基于51单片机驱动LCD1602液晶显示thb6064ah细分64

时间:11-30 来源:互联网 点击:
#include
#include #define uchar unsigned char#define uint unsigned int #define LCD_DB P0sbit LCD_RS =   P2^4; sbit LCD_RW =   P2^3; sbit LCD_E  =   P2^2; sbit K1 = P1^0;sbit K2 = P1^1;sbit K3 = P1^2;sbit K4 = P1^3;sbit K5 = P1^4;sbit M1 = P1^5;sbit M2 = P1^6;sbit M3 = P1^7;sbit DCY1 = P3^0;sbit DCY2 = P3^1;sbit CLK = P3^2;sbit EN = P2^6;sbit UD = P2^5;sbit UD1 = P2^1;uchar bin2dec(uchar a ,uchar b ,uchar c);uchar bin2dec1(uchar a ,uchar b);void LCD_init(void);void LCD_write_command(uchar command);void LCD_write_date(uchar dat);void LCD_disp_char(uchar x,uchar y,uchar dat);void LCD_disp_str(uchar row,uchar * str);bit LCD_check_busy(void);void delay_n40us(uint n);void StartTime0(void);uchar code dis1[8][9] = {{"div:1/2"},{"div:1/8"},{"div:1/10"},{"div:1/16"},{"div:1/20"},{"div:1/32"},{"div:1/40"},{"div:1/64"}};uchar code hz[8] = {1000,1000,1000,1000,1000,1000,1000,1000};uchar selector = 0;uchar o = 0,m = 0,s = 0,save = 0,save1 = 0;void main(){EN = 1;CLK = 0;StartTime0();UD = 0;UD1 = 1;while(1){M1 = K1;M2 = K2;M3 = K3;DCY1 = K4;DCY2 = K5;o = bin2dec(M1,M2,M3);UD1 = 1;if(o != save){s = 0;save = o;//selector = o;//ET0 = 0;}else{s = 1;save = o;//ET0 = 1;}m = bin2dec1(DCY2,s);if(save1 != m){switch(m){case 0 : {LCD_init();LCD_disp_str(1,dis1[o]);LCD_disp_str(2,"CW:FORWORD");UD1 = 1;}break;case 1 : {LCD_init();LCD_disp_str(1,dis1[o]);LCD_disp_str(2,"CW:FORWORD");UD1 = 1;}break;case 2 : {LCD_init();LCD_disp_str(1,dis1[o]);LCD_disp_str(2,"CW:BACK");UD1 = 0;}break;case 3 : {LCD_init();LCD_disp_str(1,dis1[o]);LCD_disp_str(2,"CW:BACK");UD1 = 0;}break;default :{LCD_init();LCD_disp_str(1,dis1[o]);}}save1 = m;}}}uchar bin2dec(uchar a ,uchar b ,uchar c){uchar i;a = a<2;b = b<1;i = a | b | c;return i;}uchar bin2dec1(uchar a ,uchar b){uchar i;a = a<1;i = a | b;return i;}void LCD_init(void){LCD_write_command(0x38);delay_n40us(100);LCD_write_command(0x0c);delay_n40us(100);LCD_write_command(0x06);delay_n40us(100);LCD_write_command(0x01);delay_n40us(100);}void LCD_write_command(uchar command){while(LCD_check_busy());LCD_RS = 0;LCD_RW = 0;LCD_E=0;_nop_();_nop_();LCD_DB = command;_nop_();_nop_();_nop_();_nop_();LCD_E = 1;_nop_();_nop_();_nop_();_nop_();LCD_E = 0;}void LCD_write_data(uchar dat){while(LCD_check_busy());LCD_RS = 1;LCD_RW = 0;LCD_E = 0;LCD_DB = dat;_nop_();_nop_();_nop_();_nop_();LCD_E = 1;_nop_();_nop_();_nop_();_nop_();LCD_E = 0;delay_n40us(1);}void LCD_disp_char(uchar x,uchar y,uchar dat){uchar address;if(y == 1){address = 0x80 + x;}else{address = 0xc0 + x;}LCD_write_command(address);LCD_write_data(dat);}void LCD_disp_str(uchar row,uchar * str){int i;if(row == 1){for(i=0;str[i]!=;i++){LCD_disp_char(i,1,str[i]);}}else{for(i=0;str[i]!=;i++){LCD_disp_char(i,2,str[i]);}}}bit LCD_check_busy(void){bit result;LCD_RS = 0;LCD_RW = 1;LCD_E = 1;_nop_();_nop_();_nop_();_nop_();delay_n40us(100);result = (bit)(LCD_DB & 0x80);LCD_E = 0;return result;}void delay_n40us(uint n){uint i;uchar j;for(i=n;i>0;i--){for(j=0;j<2;j++){_nop_();_nop_();}}}void StartTime0(void){ET0 = 1;TMOD = 0x01;TH0 = (65536-1000)/256;TL0 = (65536-1000)%256;TR0 = 1;EA = 1;}void CLKG(void) interrupt 1{CLK = ~CLK;TH0 = (65536-1000)/256;TL0 = (65536-1000)%256;}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top