標(biāo)題:
仿真出來了,然后連上lcd1602只有第二行黑格,其他數(shù)字都沒有呀,咋回事哦。求解...
[打印本頁]
作者:
羊羊22
時間:
2021-1-5 20:15
標(biāo)題:
仿真出來了,然后連上lcd1602只有第二行黑格,其他數(shù)字都沒有呀,咋回事哦。求解...
/**************************************************************************************
* LCD1602電子時鐘 *
實現(xiàn)現(xiàn)象:下載程序后,LCD屏上第一行會顯示出日期,第二行顯示時間和當(dāng)前溫度。按下K1,進(jìn)入時間調(diào)節(jié)
模式,按下K2讓光標(biāo)往左移,按下K3讓光標(biāo)處的數(shù)字增1,按下K4讓光標(biāo)處的數(shù)字減1
注意事項:
***************************************************************************************/
//--頭文件聲明,數(shù)據(jù)類型聲明--//
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
#define key P3
#define no_key 0xff //無按鍵按下
#define key_state0 0 //狀態(tài)定義
#define key_state1 1
#define key_state2 2
//---------端口聲明---------//
sbit lcden = P2^7;
sbit lcdrs = P2^6;
sbit lcdrw = P3^6;
sbit key1 = P3^2;
sbit key2 = P3^3;
sbit key3 = P3^4;
sbit key4 = P3^5;
sbit dq = P3^7; //DS18B20數(shù)據(jù)線
//---------函數(shù)聲明---------//
void lcd_init(); //LCD屏幕初始化
void wcom(); //LCD寫命令
void wdat(); //LCD寫數(shù)據(jù)
void display(); //顯示函數(shù)
void timeshow(); //時間顯示
void dateshow(); //日期顯示
void weekshow(); //星期顯示
void keyscan(); //按鍵掃描
void key_disposal(); //鍵值處理
void day_jud(); //日期處理
void ds18b20_init(); //18B20模塊初始化
uint8 byte_read(); //讀去18B20一個字節(jié)
void byte_write(); //寫入18B20一個字節(jié)
void temp_read(); //溫度讀取函數(shù)
void temp_display(uint8 position,uint16 tvalue1); //溫度顯示函數(shù)
//---------變量定義---------//
uint8 time_1s,time_500ms;
uint8 second,minute = 59,hour = 23;
uint8 day = 1,month = 1,week = 1; //設(shè)定初始
uint16 year = 2019,tvalue;
//uint8 temp;
uint8 cursor; //要調(diào)節(jié)的位置
uint8 key_value;
bit f_twinkle,f_mode,f_pressing,f_set;
uint8 code table1[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00}; //字符℃
//---------長延時---------//
void delayms(uint16 k){
uint16 i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
//---------短延時---------//
void delayus(uint8 time_1us){
while(time_1us--);
}
//---------定時器初始化---------//
void timer_init(){
EA = 0;
TR0 = 0;
TMOD = 0x01;
TH0 = 0x4C; //50ms
TL0 = 0x00;
ET0 = 1; //允許Timer0中斷
ET1 = 1;
EA = 1;
TR0 = 1;
}
//---------Timer0中斷服務(wù)函數(shù)---------//
void isr_timer0 () interrupt 1 using 0 {
TH0 = 0x4C; //50ms
TL0 = 0x00;
keyscan();
if(++time_500ms >= 10){
time_500ms = 0;
f_twinkle = ~f_twinkle;
}
if(!f_set){
if(++time_1s >= 20){
time_1s = 0;
second++;
}
}
}
void ds18b20_init(){ //ds18b20初始化
dq = 1;
_nop_();
dq = 0;
delayus(75);
dq = 1;
delayus(20);
dq = 1;
_nop_();
}
void wcom(uint8 com){ //LCD1602寫命令
lcdrw = 0;
lcdrs = 0;
P0 = com;
lcden = 1;
delayms(3);
lcden = 0;
}
void wdat(uint8 dat){ //LCD1602寫數(shù)據(jù)
lcdrw = 0;
lcdrs = 1;
P0 = dat;
lcden = 1;
delayms(3);
lcden = 0;
}
void lcd_init(){ //LCD初始化
wcom(0x38);
wcom(0x01);
wcom(0x06);
wcom(0x0c);
wcom(0x86);
wdat(0x2d); //"-"
wcom(0x89);
wdat(0x2d); //"-"
wcom(0x80+0x40+0x04);
wdat(0x3a); //":"
wcom(0x80+0x40+0x07);
wdat(0x3a); //":"
}
void timeshow(uint8 position,uint8 time){ //時間顯示,position為顯示位置
uint8 i,j;
i = time/10; //shiwei
j = time%10; //gewei
wcom(0x80+0x40+position);
wdat(0x30+i); //shiwei
wdat(0x30+j); //gewei
}
void dateshow(uint8 position,uint8 date1){ //日期顯示(月、日)
uint8 i,j;
i = date1/10; //shiwei
j = date1%10; //gewei
wcom(0x80+position);
wdat(0x30+i); //shiwei
wdat(0x30+j); //gewei
}
void yearshow(uint8 position,uint16 year1){ //年份顯示
uint8 i,j,m,n;
i = year1/1000; //qianwei
j = year1/100%10; //baiwei
m = year1/10%10;
n = year1%10;
wcom(0x80+position);
wdat(0x30+i); //qianwei
wdat(0x30+j); //baiwei
wdat(0x30+m); //shiwei
wdat(0x30+n); //gewei
}
void weekshow(uint8 position,uint8 week1){ //星期顯示
wcom(0x80+position);
switch(week1){
case 1:wdat(0x4d);wdat(0x4f);wdat(0x4e);break;
case 2:wdat(0x54);wdat(0x55);wdat(0x45);break;
case 3:wdat(0x57);wdat(0x45);wdat(0x44);break;
case 4:wdat(0x54);wdat(0x48);wdat(0x55);break;
case 5:wdat(0x46);wdat(0x52);wdat(0x49);break;
case 6:wdat(0x53);wdat(0x41);wdat(0x54);break;
case 7:wdat(0x53);wdat(0x55);wdat(0x4e);break;
default:break;
}
}
void day_jud(){ //日期處理(秒、分、時、日、月進(jìn)位)
if(second > 59){
second = 0;
if(++minute > 59){
minute = 0;
if(++hour > 23){
hour = 0;
if(++week >= 8) week = 1;
day++;
}
}
}
if(minute > 59){
minute = 0;
if(++hour > 23){
hour = 0;
if(++week > 7) week = 1;
day++;
}
}
if(hour > 23){
hour = 0;
if(++week > 7) week = 1;
day++;
}
if(day > 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)){
day = 1;
month++;
if(month >= 12){
month = 1;
year++;
}
}
else if(day > 30 && (month == 4 || month == 6 || month == 9 || month == 11)){
day = 1;
month++;
}
else if(day > 29 && month == 2 && ((year%4 == 0 && year%100 != 0) || year%400 == 0)){
day = 1;
month++;
}
else if(day > 28 && month == 2){
day = 1;
month++;
}
}
void display(){ //顯示函數(shù)
if(!f_mode){ //時間顯示模式
yearshow(2,year);
dateshow(7,month);
dateshow(0x0a,day);
weekshow(0x0d,week);
timeshow(2,hour);
timeshow(5,minute);
timeshow(0x08,second);
temp_read(); //讀取溫度
temp_display(0x0b,tvalue); //溫度顯示
}
else{ //時間調(diào)節(jié)模式
wcom(0x80+0x40+0x0d);
wdat(0x53); //"S"
wdat(0x45); //"E"
wdat(0x54); //"T"
if(f_twinkle && !f_pressing){ //閃爍標(biāo)志位,0.5秒取反一次,為1則相應(yīng)位置顯示空白
if(cursor == 0) { //按壓標(biāo)志位,按鍵按下時為1,不閃爍
wcom(0x82); //要顯示空白的位置
wdat(0x20); //4位空白(實現(xiàn)閃爍效果)
wdat(0x20);
wdat(0x20);
wdat(0x20);
}
if(cursor == 1) {
wcom(0x87); //要顯示空白的位置
wdat(0x20); //2位空白
wdat(0x20);
}
if(cursor == 2) {
wcom(0x8a);
wdat(0x20);
wdat(0x20);
}
if(cursor == 3) {
wcom(0x8d);
wdat(0x20);
wdat(0x20);
wdat(0x20);
}
if(cursor == 4) {
wcom(0x80+0x42);
wdat(0x20);
wdat(0x20);
}
if(cursor == 5) {
wcom(0x80+0x45);
wdat(0x20);
wdat(0x20);
}
if(cursor == 6) {
wcom(0x80+0x48);
wdat(0x20);
wdat(0x20);
}
}
else { //正常顯示
yearshow(2,year);
dateshow(7,month);
dateshow(0x0a,day);
weekshow(0x0d,week);
timeshow(2,hour);
timeshow(5,minute);
timeshow(0x08,second);
}
}
}
void keyscan(){ //按鍵掃描
static uint8 key_state;
switch(key_state){
case key_state0: //未按下
if(!key1 || !key2 || !key3 || !key4){
key_state = key_state1;
}
break;
case key_state1: //有鍵按下
f_pressing = 1;
if(!key1){
f_set = ~f_set; //秒走時停止
f_mode = ~f_mode; //模式切換
key_value = 1;
key_state = key_state2;
}
else if(!key2){
key_value = 2;
key_state = key_state2;
}
else if(!key3){
key_value = 3;
key_state = key_state2;
}
else if(!key4){
key_value = 4;
key_state = key_state2;
}
else{
key_state = key_state0;
f_pressing = 0; //如果不清零,假如按鍵是無效按下,調(diào)節(jié)模式時就會停止閃爍
}
break;
case key_state2: //松開按鍵
if(key1 && key2 && key3 && key4){
f_pressing = 0;
key_state = key_state0;
}
break;
}
}
void key_disposal(){ //鍵值處理
if(!f_pressing){ //松開按鍵才處理
if(key_value == 1){
key_value = 0;
cursor = 0; //光標(biāo)位置清零
lcd_init(); //清屏
}
else if(key_value == 2){
key_value = 0;
cursor++; //光標(biāo)+(光標(biāo)即閃爍的位置,實際上LCD上并無光標(biāo))
if(cursor > 6){
cursor = 0;
}
}
else if((key_value == 3) && f_mode){ //光標(biāo)所處位置值+
key_value = 0;
if(cursor == 0){
year++;
}
else if (cursor == 1){
if(++month > 12) month = 1;
}
else if(cursor == 2){
if(++day > 31) day = 1;
}
else if(cursor == 3){
if(++week > 7) week = 1;
}
else if(cursor == 4){
if(++hour > 23) hour = 0;
}
else if(cursor == 5){
if(++minute > 59) minute = 0;
}
else if(cursor == 6){
if(++second > 59) second = 0;
}
}
else if((key_value == 4) && f_mode){ //光標(biāo)所處位置值-
key_value = 0;
if(cursor == 0 && year > 0){
year--;
}
else if (cursor == 1 && month > 1){
month--;
}
else if(cursor == 2 && day > 1){
day--;
}
else if(cursor == 3 && week > 1){
week--;
}
else if(cursor == 4 && hour > 0){
hour--;
}
else if(cursor == 5 && minute > 0){
minute--;
}
else if(cursor == 6 && second > 0){
second--;
}
}
}
}
uint8 byte_read(){ //ds18b20 讀字節(jié)
uint8 i,j,dat;
for(i = 0; i < 8 ; i++){
dq = 0;
_nop_();
dq = 1;
_nop_();
j = dq;
delayus(10);
dq = 1;
_nop_();
dat = (j << 7 | dat >> 1);
}
return(dat);
}
void byte_write(uint8 data1){ //ds18b20 寫字節(jié)
uint8 i;
for(i = 0; i < 8 ; i++){
dq = 0;
_nop_();
dq = data1&0x01;
delayus(10);
dq = 1;
_nop_();
data1 >>= 1;
}
}
void temp_read(){ //讀溫度
float data1;
uint8 i,j;
ds18b20_init();
byte_write(0xcc); //向DS18B20發(fā)跳過讀ROM命令
byte_write(0x44); //啟動DS18B20進(jìn)行溫度轉(zhuǎn)換命令,轉(zhuǎn)換結(jié)果存入內(nèi)部RAM
delayus(80);
ds18b20_init();
byte_write(0xcc); //向DS18B20發(fā)跳過讀ROM命令
byte_write(0xbe); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
delayus(80);
i = byte_read(); //內(nèi)部RAM 低位
j = byte_read(); //內(nèi)部RAM 高位
data1 = (j*256 + i) * 6.25; //取出溫度值
tvalue = (uint16)data1; //強(qiáng)制轉(zhuǎn)換
}
void temp_display(uint8 position,uint16 tvalue1){ //溫度顯示
uint8 i,j,m;
i = tvalue1/1000;
j = tvalue1%1000/100;
m = tvalue1%100/10; //取出十位 個位 十分位
wcom(0x80 + 0x40 + position);
wdat(0x30 + i);
wdat(0x30 + j);
wdat(0x2e); //"."
wdat(0x30 + m);
wdat(0x00); //自編字符
}
void main(){
uint8 i;
wcom(0x40);
for(i = 0; i < 8 ; i++){
wdat(table1[i]); //寫入自編字符
}
timer_init();
lcd_init();
while(1){
// if(++temp > 2){
// temp = 0;
// }
// switch(temp){
// case 0:day_jud();break;
// case 1:key_disposal();break;
// case 2:display();break;
// }
day_jud();
key_disposal();
display();
}
}
復(fù)制代碼
作者:
51hei團(tuán)團(tuán)
時間:
2021-1-5 22:41
解決此問題在51hei至少有20個以上的帖子,既有軟件問題也有硬件問題,這個隨便找2個吧,搜索"1602 格":
http://www.torrancerestoration.com/bbs/dpj-187378-1.html
http://www.torrancerestoration.com/bbs/dpj-160754-1.html
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1