英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
DS18B20读写显示程序
[打印本页]
作者:
winnie
时间:
2009-4-26 11:38
标题:
DS18B20读写显示程序
/*DS18B20,4MHZ晶振读写显示程序PIC16F873A*/
#include<pic.h>
#include<sys.h>
#include <math.h>
#define QD RC7
#define uchar unsigned char
unsigned char s[4];
const unsigned char table[20]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
0x7f,0xbf,0x89,0xff};
void delay(uchar a)
{
while(a--);
}
void H_BCD (unsigned int j)
{
s[0]=j%10;
s[1]=(j/10)%10;
s[2]=(j/100)%10;
s[3]=j/1000;
}
void SPIINIT()
{
PIR1=0;
SSPCON=0x30;
SSPSTAT=0xC0;
TRISC=0x00;
}
void initial()
{
TRISC=0x00;
INTCON=0x00;
RC3=0;
}
void SPILED( data)
{
SSPBUF=data;
do
{
;
}while(SSPIF==0);
SSPIF=0;
}
uchar Readchar()
{
uchar i=0;
uchar dat=0;
TRISC7=0;
for(i=8;i>0;i--)
{
TRISC7=0;
QD=0;
dat=dat>>1;
QD=1;
TRISC7=1;
if(QD)
dat=dat|0x80;
delay(8);
}
return(dat);
}
RESTDS18B20()
{
uchar x=0;
TRISC7=0;
QD=1;
delay(1);
QD=0;
delay(130);
QD=1;
delay(20);
TRISC7=1;
x=QD;
delay(35);
}
void Writechar(uchar dat)
{
uchar h;
TRISC7=0;
for (h=0;h<8;h++)
{
QD=0;
QD=dat&0x01;
delay(8);
QD=1;
dat>>=1;
}
delay(6);
}
unsigned int Readtem()
{
uchar a=0;
uchar b=0;
unsigned int t=0;
RESTDS18B20();
Writechar(0xcc);
Writechar(0x44);
RESTDS18B20();
Writechar(0xcc);
Writechar(0xbe);
a=Readchar();
b=Readchar();
t=b;
t<<=8;
t=t|a;
return(t);
}
main()
{
unsigned int i=0;
unsigned char j,data;
SPIINIT();
while(1)
{
i=Readtem();
i=(unsigned int)(i*0.0625);
H_BCD (i);
for(j=0;j<4;j++)
{
data=table[s[j]];
SPILED(data);
}
RC4=1;
RC4=0;
}
}
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2