?? sh305.c
字號:
#include <reg51.h>
#include <absacc.h>
#include <stdio.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit P3_5=P3^5;
uchar TEMP; // 溫度值的變量;
uchar flag1; // 結果為負和正的標志位。
void delay (unsigned int count)
{
unsigned int i;
while (count)
{
i =200;
while (i>0) i--;
count--;
}
}
void tmreset (void) // 發送復位和初始化
{
unsigned int i;
P3_5 = 0;
i = 103;
while (i>0) i--; // 延時
P3_5 = 1;
i = 4;
while (i>0) i--;
}
bit tmpread (void) // 讀取數據的一位
{
unsigned int i;
bit dat;
P3_5 = 0; i++;
P3_5 = 1; i++; i++; //延時
dat = P3_5;
i = 8; while (i>0) i--; // 延時
return (dat);
}
unsigned char tmpread2 (void) //讀一個字節
{
unsigned char i,j,dat;
dat = 0;
for (i=1;i<=8;i++)
{
j = tmpread ();
dat = (j << 7) | (dat >> 1);
}
return (dat);
}
void tmpwrite (unsigned char dat) //寫一個字節
{
unsigned int i;
unsigned char j;
bit testb;
for (j=1;j<=8;j++)
{
testb = dat & 0x01;
dat = dat >> 1;
if (testb)
{
P3_5 = 0; // 寫0
i++; i++;
P3_5 = 1;
i = 8; while (i>0) i--;
}
else
{
P3_5 = 0; // 寫0
i = 8; while (i>0) i--;
P3_5 = 1;
i++; i++;
}
}
}
void tmpchange(void) // ds1820 開始轉換
{
tmreset (); // 復位
//tmpre (); // 等待存在脈沖
delay (1); // 延時
tmpwrite (0xcc); // 跳過序列號命令
tmpwrite (0x44); // 發轉換命令 44H,
}
void tmp (void) // 讀取溫度
{
unsigned char a,b;
tmreset (); // 復位
delay (1); // 延時
tmpwrite (0xcc); // 跳過序列號命令
tmpwrite (0xbe); // 發送讀取命令
a = tmpread2 (); // 讀取低位溫度
b = tmpread2 (); //讀取高位溫度
flag1=b; //若b為1則為負溫
if(flag1)
{
TEMP=~a+1; //如果為負溫則去除其補碼
}
else
{
TEMP=a;
}
}
rom() // 讀取器件序列號子程序
{
tmreset (); //復位
delay (1); //延時
tmpwrite(0x33); //發送讀序列號子程序
}
main()
{
do{
tmpchange(); // 開始溫度轉換
delay(200); // 讀取延時
tmp(); //讀取溫度
}while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -