/****************************************************************************************************************
*************************
*Author:JYW
*Time:2019_04_03
*Describe:LetAllLinesOfCodeSpeak
*****************************************************/
/*************************************************
*Auther:JYW
*Time:2019_04_02
*Describe:03基础实验17_DS18B20温度实验代码学习
Ds18b20.c的练习
******************************/
#include"ds18b20.h"
#define Ds18b20IO P0_7
void Delay_us(unsigned int k)
{
T1CC0L = 0x06;
T1CC0H = 0x00;
T1CTL = 0x02;//模模式,不分频
while(k)
{
while(!(T1CNTL >= 0x04));
k--;
}
T1CTL = 0x00;
}
void Delay_ms(unsigned int k)
{
T1CC0L = 0xe8;
T1CC0H = 0x03;
T1CTL = 0x0A; //0000 1010 32分频 模模式
while(k)
{
while(!((T1CNTL >= 0xE8)&&(T1CNTH >= 0x03)));
k--;
}
T1CTL = 0x00;
}
void Delay_s(unsigned int k)
{
while(k)
{
Delay_ms(1000);
k--;
}
}
void Ds18b20Delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
for(j = 0;j<2;j++);
}
void Ds18b20InputInitial(void)
{
P0DIR &= 0x7F;
}
void Ds18b20OutputInitial(void)
{
P0DIR |= 0x80;
}
unsigned char Ds18b20Initial(void)
{
unsigned char Status = 0x00;
unsigned int CONT_1 = 0;
unsigned char Flag_1 = 1;
Ds18b20OutputInitial();
Ds18b20IO = 1;
Ds18b20Delay(260);
Ds18b20IO = 0;
Ds18b20Delay(750);
Ds18b20IO = 1;
Ds18b20InputInitial();
while((Ds18b20IO != 0)&&(Flag_1 == 1))
{
CONT_1++;
Ds18b20Delay(10);
if(CONT_1 > 8000) Flag = 0;
Status = Ds18b20IO;
}
Ds18b20OutputInitial();
Ds18b20IO = 1;
DS18b20Delay(100);
return Status;//返回初始化状态
}
void Ds18b20Write(unsigned char infor)
{
unsigned int i;
Ds18b20OutputInitial();
for(i=0; i<8; i++)
{
if((infor & 0x01))
{
Ds18b20IO = 0;
Ds18b20Delay(6);
Ds18b20IO = 1;
Ds18b20Delay(50);
}
else
{
Ds18b20IO = 0;
Ds18b20Delay(50);
Ds18b20IO = 1;
Ds18b20Delay(6);
}
infor >>= 1;
}
}
unsigned char Ds18b20Read(void)
{
unsigned char Value = 0x00;
unsigned int i;
Ds18b20OutputInitial();
Ds18b20IO = 1;
Ds18b20Delay(10);
for(i=0; i<8; i++)
{
Value >>= 1;
Ds18b20OutputInitial();
Ds18b20IO = 0;
Ds18b20Delay(3);
Ds18b20IO = 1;
Ds18b20Delay(3);
Ds18b20InputInitial();
if(Ds18b20IO == 1) Value |= 0x80;
Ds18b20Delay(15);
}
return Value;
}
unsigned char ReadDs18b20(void)
{
unsigned char V1,V2;//定义高低8位
unsigned char temp;
Ds18b20Initial();
Ds18b20Write(0xcc);
Ds18b20write(0x44);
Ds18b20Initial();
Ds18b20Write(0xcc);
Ds18b20Write(0xbe);
V1 = Ds18b20Read();//低位
V2 = Ds18b20Read();
temp = ((V1 >> 4) + ((V2 & 0x07)*16));//转换数据
return temp;
}
float floatReadDs18B20(void)
{
unsigned char V1, V2;
unsigned int temp;
float fValue;
Ds18b20Initial();
Ds18b20Write(0xcc);
Ds18b20Write(0x44);
Ds18b20Initial();
Ds18b20Write(0xcc);
Ds18b20Write(0xbe);
V1 = Ds18b20Read();
V2 = Ds18b20Read();//高
//读取顺序为 先低位再高位
temp = V2*0xFF + V1;//temp为int
fValue = temp* 0.0625;
//
return fValue;
}