📄 shiyongjixianshi.c
字号:
#include <reg52.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <absacc.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//#define NOP _nop_();_nop_();_nop_()
sbit ZLG7289_CLK=P1^3;
sbit ZLG2_NCS=P2^1; //ZLG驱动第2个
sbit ZLG1_NCS=P2^0;
sbit ZLG7289_DIO=P1^2;
sbit ZLG7289_K=P1^6;
sbit DQ=P3^4; // 定义DS18B20接口
uint temp,temp1; // 温度变量
uchar flag1; // sign of the result positive or negative
//unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f};//不带小数点编码。
//unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//带小数点编码。
/*----------------------------ZLG7289A---------------------上---------------------*/
void ZLG_delay(uint n) //ZLG--delay
{ uint i;for(i=n;i>0;i--);}
void ZLG7289_write8_comm(uchar mm,uchar cData) //向ZLG7289写入命令---纯命令
{
uchar idata cLoop;
if(mm==2){
ZLG2_NCS=1;ZLG7289_CLK=0;ZLG2_NCS=0;ZLG_delay(10);
for(cLoop=0;cLoop<8;cLoop++)
{
if((cData&0x80)==0){ZLG7289_DIO=0;}
else{ZLG7289_DIO=1;}
ZLG_delay(0x01);
ZLG7289_CLK=1; ZLG_delay(0x02);
ZLG7289_CLK=0; ZLG_delay(0x02);
cData<<=1;
}
ZLG2_NCS=1;ZLG7289_CLK=0;
}
else {
ZLG1_NCS=1;ZLG7289_CLK=0;ZLG1_NCS=0;ZLG_delay(0x14);
for(cLoop=0;cLoop<8;cLoop++)
{
if((cData&0x80)==0){ZLG7289_DIO=0;}
else{ZLG7289_DIO=1;}
ZLG_delay(0x01);
ZLG7289_CLK=1; ZLG_delay(0x02);
ZLG7289_CLK=0; ZLG_delay(0x02);
cData<<=1;
}
ZLG1_NCS=1;ZLG7289_CLK=0;
}
}
void ZLG7289_write8_data(uchar pp,uchar comm,uchar cData) //向ZLG7289写入数据
{
uchar idata cLoop;
if(pp==2){
ZLG2_NCS=1;ZLG7289_CLK=0;ZLG2_NCS=0;ZLG_delay(0x14);
for(cLoop=0;cLoop<8;cLoop++)
{
if((comm&0x80)==0){ZLG7289_DIO=0;}
else{ZLG7289_DIO=1;}
ZLG_delay(0x01);
ZLG7289_CLK=1; ZLG_delay(0x02);
ZLG7289_CLK=0; ZLG_delay(0x02);
comm<<=1;
}
ZLG_delay(0x01);
for(cLoop=0;cLoop<8;cLoop++)
{
if((cData&0x80)==0){ZLG7289_DIO=0;}
else{ZLG7289_DIO=1;}
ZLG_delay(0x01);
ZLG7289_CLK=1; ZLG_delay(0x02);
ZLG7289_CLK=0; ZLG_delay(0x02);
cData<<=1;
}
ZLG2_NCS=1;ZLG7289_CLK=0;
}
else {
ZLG1_NCS=1;ZLG7289_CLK=0;;ZLG1_NCS=0;ZLG_delay(0x14);
for(cLoop=0;cLoop<8;cLoop++)
{
if((comm&0x80)==0){ZLG7289_DIO=0;}
else{ZLG7289_DIO=1;}
ZLG_delay(0x01);
ZLG7289_CLK=1; ZLG_delay(0x02);
ZLG7289_CLK=0; ZLG_delay(0x02);
comm<<=1;
}
ZLG_delay(0x01);
for(cLoop=0;cLoop<8;cLoop++)
{
if((cData&0x80)==0){ZLG7289_DIO=0;}
else{ZLG7289_DIO=1;}
ZLG_delay(0x01);
ZLG7289_CLK=1; ZLG_delay(0x02);
ZLG7289_CLK=0; ZLG_delay(0x02);
cData<<=1;
}
ZLG1_NCS=1;ZLG7289_CLK=0;
}}
void ZLG7289_rst(void)
{
ZLG2_NCS=0;
ZLG1_NCS=0;
ZLG_delay(10);
ZLG7289_write8_comm(2,0xa4);
ZLG7289_write8_comm(1,0xa4);
ZLG2_NCS=1;
ZLG1_NCS=1;
}
/*----------------------------ZLG7289A---------------------下---------------------*/
///////功能:串口初始化,波特率9600,方式1///////
/*void Init_Com(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}*/
/*-----------------------------------DS18B20------------------------上-------------*/
void delay(unsigned int i)
{
while(i--);
}
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(4);
}
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay(10); //另加
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //读取温度值低位
b=ReadOneChar(); //读取温度值高位
a=a>>4; //低位右移4位,舍弃小数部分
t=b<<4; //高位左移4位,舍弃符号位
t=t|a;
return(t);
}
void tempLed_display(unsigned char temp) //主程序温度显示函数
{
uchar A1,A2;
//ser=temp/10; //分离出三位要显示的数字
//SBUF=ser;
A1=temp/10;
A2=temp%10;
//A2=A2t/10;
//A3=A2t%10;
//A2=0x80|A2;
//要使选中DIG10-----DIG13。DIG10显示负号,DIG11显示十位,其次,个位,小数位
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -