因為畢業設計初次接觸單片機c語言,要求做一個單片機控制,矩陣鍵盤輸入,LCD顯示,ds18b20測溫的溫度控制系統,軟體調了好久了,之前仿真只有顯示,但ds18b20一直只有高電平,矩陣鍵盤按下去也只有行掃描沒有列掃描程序,按下去也沒有反應,現在東改改西改改連顯示也不顯示了,大佬們幫我看看是哪些地方不對,應該改那些地方了,小白在這謝謝各位大佬了

//LCD顯示
#include<reg52.h>
#ifndef __1602_H__
#define __1602_H__
#define LCD_DATA P0 //LCD的資料口
#define uint unsigned int
#define uchar unsigned char
sbit LCD_BUSY=LCD_DATA^7; //LCD忙信號位
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
void delay(uint ms)
{
uint i;
while(ms--)
{
for(i = 0; i< 125; i++);
}
}
void LCD_check_busy(void) //查忙
{
while(1)
{
LCD_EN=0;
LCD_RS=0; //指令暫存器通信
LCD_RW=1; //read data
LCD_DATA=https://bbs.csdn.net/topics/0xff;
LCD_EN=1;
if(!LCD_BUSY)break;
}
LCD_EN=0;
}
void LCD_cls(void) //LCD清屏
{
LCD_check_busy();
LCD_RS=0;
LCD_RW=0;
LCD_DATA=https://bbs.csdn.net/topics/1;
LCD_EN=1;
LCD_EN=0;
}
void LCD_write_instruction(unsigned char LCD_instruction) //寫指令到LCD
{
LCD_check_busy();
LCD_RS=0;
LCD_RW=0; //寫資料
LCD_DATA=https://bbs.csdn.net/topics/LCD_instruction;
LCD_EN=1;
LCD_EN=0;
}
void LCD_write_data(unsigned char LCD_data) //輸出一個位元組資料到LCD
{
LCD_check_busy();
LCD_RS=1;
LCD_RW=0;
LCD_DATA=https://bbs.csdn.net/topics/LCD_data;
LCD_EN=1;
LCD_EN=0;
}
void LCD_set_position(unsigned char x) //LCD游標定位到x處
{
LCD_write_instruction(0x80+x);
}
void LCD_printc(unsigned char lcd_data) //輸出一個字符到LCD
{
LCD_write_data(lcd_data);
}
void LCD_prints(unsigned char *lcd_string) //輸出一個字串到LCD
{
unsigned char i=0;
while(lcd_string[i]!=0x00)
{
LCD_write_data(lcd_string[i]);
i++;
}
}
void LCD_c(void) //初始化LCD
{
LCD_write_instruction(0x3c);
LCD_write_instruction(0x0c);
LCD_write_instruction(0x06);
LCD_cls();
}
//DS18B20
#ifndef DS18B20_H
#define DS18B20_H
sbit DS=P1^5; //定義IO口
uint temp;
/*DS18B20的復位函式*/
void dsreset() //復位
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
/*DS18B20的讀函式*/
uchar tmpread() //讀取一位元組
{
uchar j,k,dat;
uint i;
for(j=1;j<=8;j++)
{
DS=0;i++; //延時
DS=1;i++;i++;
k=DS;
i=8;while(i>0)i--;
dat=(k<<7)|(dat>>1);//讀出的資料最低位在最前面存一個位元組在DAT里
}
return(dat);
}
/*DS18B20的寫函式*/
void tmpwritebyte(uchar dat) //寫一個位元組
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //寫 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //寫 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
/*DS18B20的資料轉換函式*/
void tmpchange() //DS18B20溫度變換
{
dsreset();
delay(18);
tmpwritebyte(0xcc); //跳過讀取記憶體rom
tmpwritebyte(0x44); //開始轉換
}
/*DS18B20的讀溫度函式*/
uint tmp() //讀取溫度
{
float t;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
delay(10);
a=tmpread(); //a為低位元組8位
b=tmpread(); //b為高位元組8位
temp=b; //temp為溫度值UINT 16bit
temp<<=8; //兩個位元組組合到一起
temp=temp|a;
if(b>127)
{
temp=~temp+1;
}
t=temp*0.0625; // temp/16 則是溫度的真實值tt.7位整數,4位小數
temp=t*10+0.5; // 擴大十倍取出了第一位小數
return(temp);
}
void lcd_DS(void)
{
LCD_set_position(0x02); // 顯示T=的地址
LCD_prints("T=");
LCD_set_position(0x04); //顯示溫度的地址
LCD_printc(temp/100%10+0x30); //顯示溫度的高位
LCD_set_position(0x05);
LCD_printc(temp/10%10+0x30); //顯示溫度的第二位
LCD_set_position(0x06);
LCD_prints("."); //顯示小數點
LCD_set_position(0x07);
LCD_printc(temp%10+0x30); //顯示溫度的最低位
LCD_set_position(0x08); // 顯示“度”
LCD_printc(0xDF);
LCD_set_position(0x09);
LCD_prints("C");
}
#endif
//矩陣鍵盤
#include<reg52.h>
unsigned char GE,SHI,XIAO;
unsigned char wei,ji,Goal;
uchar code tab[]=
{
0x77,0xb7,0xd7,0xe7,
0x7b,0xbb,0xdb,0xeb,
0x7d,0xbd,0xdd,0xed,
0x7e,0xbe,0xde,0xee
};
sbit key_a = P3^0;
sbit key_b = P3^1;
sbit key_c = P3^2;
sbit key_d = P3^3;
sbit key_1 = P3^4;
sbit key_2 = P3^5;
sbit key_3 = P3^6;
sbit key_4 = P3^7;
uchar keyscan(void)
{
uchar c_h,c_l;
{
P3=0x0f;
while(P3 !=0x0f) //識別是否按下
{
delay(100);
while(P3 !=0x0f) //消抖
{
P3=0x0f;
c_h=P3&0x0f;
delay(100);
P3=0xf0;
c_l=P3&0xf0;
delay(100);
return(c_h+c_l); //回傳掃描值
}
delay(1000);
return(0x0f);
}
}
}
void Key_s(void)
{
uchar key;
key=keyscan();
switch(key)
{
case 0xd7:ji=0x01;break;
case 0xdb:ji=0x02;break;
case 0xdd:ji=0x03;break;
case 0xb7:ji=0x04;break;
case 0xbb:ji=0x05;break;
case 0xbd:ji=0x06;break;
case 0x77:ji=0x07;break;
case 0x7b:ji=0x08;break;
case 0x7d:ji=0x09;break;
case 0xeb:ji=0x00;break;
case 0xe7:break;
case 0xed:break;
case 0x7e:wei=0;break;
case 0xde:wei=1;break;
case 0xbe:wei=2;break;
case 0xee:break;
}
}
void Key_z(unsigned char ji,wei)
{
while(wei==0) SHI=ji;
while(wei==1) GE=ji;
while(wei==2) XIAO=ji;
}
void Key_a(void)
{
delay(500);
Goal=SHI*10+GE+XIAO/10;
}
void lcdKEY_DS(void)
{
LCD_set_position(0x42); // 顯示G=的地址
LCD_prints("G=");
LCD_set_position(0x44); //顯示溫度的地址
LCD_printc(SHI+0x30); //顯示溫度的高位
LCD_set_position(0x45);
LCD_printc(GE+0x30); //顯示溫度的第二位
LCD_set_position(0x46);
LCD_prints("."); //顯示小數點
LCD_set_position(0x47);
LCD_printc(XIAO+0x30); //顯示溫度的最低位
LCD_set_position(0x48); // 顯示“度”
LCD_printc(0xDF);
LCD_set_position(0x49);
LCD_prints("C");
}
//加溫
sbit Warning1 = P1^6;
sbit Warning2 = P1^7;
sbit Power = P2^7;
unsigned char PwmA,PwmB;
void System_c(void)
{
EA = 1;
Warning1 = 0;
Warning2 = 0;
Power = 0;
}
void System_main(void)
{
while(temp<Goal)
{
if(Goal-temp>10)
{
PwmA=100;
PwmB=0;
Warning1=0;
Warning2=1;
}
}
while(temp<Goal)
{
if(Goal-temp<10)
{
PwmA=50;
PwmB=50;
Warning1=0;
Warning2=1;
}
}
while(temp<Goal)
{
if(Goal-temp<5)
{
PwmA=25;
PwmB=75;
Warning1=0;
Warning2=1;
}
}
while(temp<Goal)
{
if(Goal-temp<1)
{
PwmA=0;
PwmB=100;
Warning1=1;
Warning2=1;
}
}
while(temp>Goal)
{
if(temp-Goal<1)
{
PwmA=0;
PwmB=100;
Warning1=1;
Warning2=1;
}
}
while(temp>Goal)
{
if(temp-Goal>1)
{
PwmA=0;
PwmB=100;
Warning1=1;
Warning2=0;
}
}
}
void HEATER()
{
uint i,j;
for(i = 0; i< 1250000*PwmA/100; i++)
{
Power=1;
}
for(j = 0;j< 1250000*PwmB/100; j++)
{
Power=0;
}
}
//主程式
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#include<lcd1602.c>
#include<ds18b20.c>
#include<key.c>
#include<others.c>
void main()
{
System_c();
LCD_c();// 初始化LCD
delay(20);
while(1)
{
keyscan();
Key_s();
Key_z();
Key_a();
lcdKEY_DS();
tmpchange();
tmp();
lcd_DS();
System_main();
HEATER();
}
}
uj5u.com熱心網友回復:
頂你一下啊啊啊轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/39506.html
標籤:新手樂園
上一篇:命令列錯誤:無法打開元資料檔案“platform.winmd"
下一篇:能解釋一下第四題的每個選項嘛
