?? text1.c
字號:
#include"reg51.h"
#include <absacc.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P1^6;
sbit rw=P1^5;
sbit ep=P1^4;
sbit DQ=P3^6;
unsigned char DS18B20_F=0;
uchar code table[]={"0123456789"};
unsigned char x=0;
void delay(unsigned int i)//延時函數
{
while(i--);
}
void xmjc(void) //閑忙檢測
{
rs=0;
rw=1;
ep=1;
while(P2&0x80)
;
ep=0;
}
void xml(unsigned char comm) //寫命令
{
xmjc();
rs=0;
rw=0;
ep=1;
P2=comm;
ep=0;
}
void xsj(unsigned char dat)
{
xmjc();
rs=1;
rw=0;
ep=1;
P2=dat;
ep=0;
}
void lcd_init(void) //1602初始化
{
xml(0x38);
xml(0x06);//光標和顯示模式設置
xml(0x0c);
xml(0x01);
}
void aa(uchar str,uchar lengch)
{
uchar i;
for(i=0;i<lengch;i++)
{
xsj(str);
}
}
//unsigned char templ=0;
//unsigned char temph=0;
//float temperature ;
//sbit LED1=P1^3;
//sbit LED2=P1^2;
/*********************************************************************
函數功能:延時子程序
入口參數:time
出口參數:
**********************************************************************/
void wait1(unsigned int j)
{ while(j--);
}
/************************
初始化
*************************/
void Init_DS18B20(void) //復位成功時返回1,否則返回0
{ //unsigned char n=255;
DQ=1;
_nop_();
_nop_();
DQ=0; //主機拉低1-Wire總線,延時540us產生復位信號
wait1(60);
// wait1(180);
// wait1(180);
DQ=1; //主機釋放1-Wire總線,延時60us等待DS18B20應答
wait1(2);
DS18B20_F=DQ;
wait1(50);
}
/******************************************************************
函數功能:數碼管顯示子程序
入口參數:
出口參數:
void display1(int k)
{
P1=0xfe;
P2=tab[k/1000];
wait1(100);
P1=0xfd;
P2=tab[k%1000/100];
wait1(100);
P1=0xfb;
P2=tab[k%100/10];
wait1(100);
P1=0xf7;
P2=tab[k%10];
wait1(100);
}
void LedDisp(unsigned char disp)//溫度顯示子程序
{ unsigned int time=0;
while(time<1000)
{
P2=tab[disp/10];
//LED1=1;
//LED2=0;
P1=0xf8;
wait1(30);
P2=tab[disp%10];
P1=0xf9;
//LED1=0;
// LED2=1;
wait1(30);
++time;
}
}
*********************************************************************/
/*********************************************************************
函數功能:向ds18b20讀一個字節數據
入口參數:
出口參數:dat
*********************************************************************/
unsigned char Readonechar(void)
{ unsigned char i=0,c;
c=0;
for(i=8;i>0;i--)
{
DQ=1;
_nop_();_nop_();
DQ=0;
_nop_();_nop_(); //主機拉低1-Wire總線,延時2us
//wait1(2);
c>>=1;
DQ=1;
//wait1(2);
if(DQ)
c|=0x80;
wait1(7);
}
return(c);
}
/**********************************************************************
函數功能:向ds18b20寫一字節的數據
入口參數:dat
出口參數:
**********************************************************************/
void Writeonechar(unsigned char c)
{ unsigned char i;
for(i=8;i>0;i--)
{
DQ=0; //主機拉低1-Wire總線,延時2us
DQ=c&0x01;
wait1(6); //延時60us等待DS18B20采集數據
DQ=1; //主機釋放總線
c>>=1;
}
_nop_();_nop_();
//wait1(7);
}
/***********************************************************************
函數功能:向ds18b20讀溫度值
入口參數:
出口參數:temperature
************************************************************************/
Readtemperature(void)
{ unsigned char temph=0,templ=0;
unsigned int temperature=0;
float temp=0;
Init_DS18B20();
if(!DS18B20_F)
{
//DS18B20_F=0xff;
Writeonechar(0xcc);
Writeonechar(0x44);
//wait1(50000);
//wait1(50000);
Init_DS18B20();
if(!DS18B20_F)
{
Writeonechar(0xcc);
Writeonechar(0xbe);
templ=Readonechar();
temph=Readonechar();
temperature=temph;
temperature<<=8;
temperature=temperature|templ;
temp=temperature*0.0625;
temp=temp+0.5;
//temperature=temp+0.5;
//temperature=temperature/100;
//temperature=((temph*256)+templ)*0.0625;
//return(temperature);
return(temp);
}
//DS18B20_F=Init_DS18B20();
//while(DS18B20_F==0xff);
//DS18B20_F=0xff;
}
}
/***********************************************************************
函數功能:主程序
入口參數:
出口參數:
void main()
{
unsigned char i=0;
while(1)
{
i=Readtemperature();
LedDisp(i);
}
}
************************************************************************/
void main()//主程序
{
unsigned char wendu=0,m, n;
lcd_init();
while(1)
{
wendu=ReadTemperature();//讀溫度
m=wendu/10;
n=wendu%10;
xml(0x82);
aa(table[m],1);
xml(0x83);
aa(table[n],1); //顯示溫度值
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -