#include<c8051f020.h>
#include < intrins.h >
#include <absacc.h>
#include <stdio.h>
#define SYSCLK 22118400 // SYSCLK frequency in Hz
sbit DataPortDS1820=P0^4;
void SYSCLK_Init (void);
void PORT_Init (void);
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.118400crystal
for (i=0; i < 256; i++) ; // Wait for osc. to start up
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
} // source and enable missing clock
void PORT_Init (void)
{
XBR2=0x40; // Enable crossbar and weak pull-ups
P0MDOUT |=0xf0;
P1MDOUT |=0xff;
P2MDOUT |=0x0f; //P2低位弱上拉
// P3MDOUT |=0xff;
}
void Delay1us(unsigned char us)
{
while (us)
{
_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
--us;
}
}
void Delay15us(void)
{
Delay1us(15);
}
void Delay10us(void)
{
Delay1us(10);
}
bit RstDS1820(void) //返回0-有设备连接1-无设备连接
{
unsigned char i;
bit RstFlag;
RstFlag=1;
DataPortDS1820=0;
for (i=0;i<100/*40*/;i++) //480us
Delay15us();
DataPortDS1820=1;
for (i=0;i<4;i++) //15us-60us
{
Delay15us();
}
for (i=0;i<16;i++) //60us-240us
{
Delay15us();
if (DataPortDS1820==0) RstFlag=0;
}
for (i=0;i<16;i++) //240us
{
Delay15us();
}
return RstFlag;
}
void WriteDS1820(unsigned char ch)
{
unsigned char i;
DataPortDS1820=1;
Delay1us(1);
for (i=0;i<8;i++)
{
EA=0;
DataPortDS1820=0;
Delay15us();
DataPortDS1820=ch&0x1;
EA=1;
Delay15us();Delay15us();Delay15us();
DataPortDS1820=1;
ch=ch>>1;
Delay1us(1);
}
}
unsigned char ReadDS1820(void)
{
unsigned char i,ch;
ch=0;
DataPortDS1820=1;
Delay1us(1);
for (i=0;i<8;i++)
{
EA=0;
DataPortDS1820=0;
Delay10us();
DataPortDS1820=1;
Delay1us(2);
ch=ch>>1;
if (DataPortDS1820==1)
{
ch=ch+0x80;
}
EA=1;
Delay15us();Delay15us();Delay15us();
}
return ch;
}
void SkipROMCode(void)
{
RstDS1820();
WriteDS1820(0xcc);
}
void StartADC(void)
{
RstDS1820(); //复位
WriteDS1820(0xcc); //广播
WriteDS1820(0x44); //启动AD转换 12bit700ms
}
unsigned int GetTempValue(void)
{
unsigned char i,j;
unsigned int T;
SkipROMCode();
WriteDS1820(0xBE);
i=ReadDS1820();
j=ReadDS1820();
StartADC();
T=i+j*256;
if (T==0xffff) return 0xffff;
if (T>0x8000)
{
T=-T;
return (0x8000+T*5/8);
}
else
return (T*5/8);
}
void main(void)
{
unsigned int w;
WDTCN = 0xde;
WDTCN = 0xad;
SYSCLK_Init();
PORT_Init();
// w= GetTempValue();
while(1)
{
Delay1us(100);
w= GetTempValue();
}
}
点击此处下载
ourdev_625495FHXCE4.txt(文件大小:3K) (原文件名:temp_display.txt)
一周热门 更多>