#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P1^3
sbit sda=P1^4
sbit key1=P1^6
sbit key2=P1^7
sbit key3=P2^0
sbit key4=P2^1
sbit lcrs=P3^7//数据/命令
sbit lcwr=P3^5//读/写
sbit lcden=P3^4//使能
sbit DS=P2^2
/*sbit lcrs=P3^4//数据/命令
sbit lcwr=P3^7//读/写
sbit lcden=P3^5//使能
*/
sbit jrk=P2^2
sbit cyk=P2^3
sbit xhk=P2^4
bit flag=0,rsg=0,not=0,he=0,in=0
int acon=0,bcon=0,dcon=0,econ=0,
temp=0,y=0,j=0,l=0,cfj=0,ec=0,dc=0,at
uchar code table[]={48,49,50,51,52,53,54,55,56,57}
uchar code ta1[]={"Temperature UP"}
uchar code ta2[]={"Temperature DN"}
uchar code ta3[]={"Inflator Cycle"}
uchar code ta4[]={"Inflator Time "}
uchar code ta5[]={" Heating UP "}
uchar code ta6[]={"Inflator"}
uchar code table7[]={"Temperature"}
uchar table1[]={0,0,0,'.',0}
uchar table3[]={"AptitudeAquarium"}
uchar table4[]={0,0,0,0,0}
uchar n,c=0
void delay(uchar)
void wen_kong()
void xh()
void rso()
void weno()
void Init_Com(void)
{
TMOD = 0x11
PCON = 0x00
TH1=0x61
TL1=0x99
EA=1
ET1=1
TR1=1
}
void delay(uchar count) //delay
{
uint i
while(count)
{
i=200
while(i>0)
i--
count--
}
}
////初始化18B20/////////
bit init18b20(void)
{
uint i
bit no
DS=0
i=103
while(i>0)i--
DS=1
i=4
while(i>0)i--
no=DS
if(no==0)
{
DS=1
i=100
while(i>0)i--
no=DS
if(no==1)
not=0
else
not=1
}
else
not=1
return (not)
}
bit tmpreadbit(void) //读一位
{
uint i
bit dat
DS=0
i++
DS=1
i++i++
dat=DS
i=8while(i>0)i--
return (dat)
}
uchar tmpread(void) //读一个字节
{
uchar i,j,dat
dat=0
for(i=1i<=8i++)
{
j=tmpreadbit()
dat=(j<<7)|(dat>>1)//读出的数据最低位在最前面,这样刚好一个字节仿袜悔在好没DAT里
}
return(dat)
}
void tmpwritebyte(uchar dat) //写一备正个字节到 ds18b20
{
uint i
uchar j
bit testb
for(j=1j<=8j++)
{
testb=dat&0x01
dat=dat>>1
if(testb) //write 1
{
DS=0
i++i++
DS=1
i=8while(i>0)i--
}
else
{
DS=0 //write 0
i=8
while(i>0)i--
DS=1
i++i++
}
}
}
int tmp() //DS18B20温度读取
{
float tt
int a,b
if(init18b20()==0)
{
WDT_CONTR=0x36 /////喂狗
EA=0
delay(1)
tmpwritebyte(0xcc) // 跳过读ROM *** 作
tmpwritebyte(0x44) // 启动温度转换
delay(10)
init18b20()
delay(1)
tmpwritebyte(0xcc)
tmpwritebyte(0xbe)
a=tmpread()
b=tmpread()
temp=b
temp<<=8 //将高字节温度数据与低字节温度数据整合
temp=temp|a
c=b>>4
tt=temp*0.0625
temp=tt*10+0.5//放大10倍输出并四舍五入
EA=1
return temp
}
else
not=1
}
//////1062/////////
void ydelay(uint x)
{
uint a,b
for(a=xa>0a--)
for(b=10b>0b--)
}
void write_com(uchar com)
{
P0=com
lcwr=0
lcrs=0
lcden=0
ydelay(10)
lcden=1
ydelay(10)
lcden=0
lcwr=1
}
void write_date(uchar date)//写数据
{
P0=date
lcwr=0
lcrs=1
lcden=0
ydelay(10)
lcden=1
ydelay(10)
lcden=0
lcwr=1
}
void init1602()//初始化
{
write_com(0x38)//设置显示模式
ydelay(20)
write_com(0x0c)//开显示
ydelay(20)
write_com(0x06)//指针和光标自动加一
ydelay(20)
write_com(0x01)//清屏指令
ydelay(20)
}
///////显示程序//////
void display(int num)
{
uint i,A1,A2
WDT_CONTR=0x35 /////喂狗
if(c!=0)
num=~num+1
A1=num/1000
A2=num%1000/100
if(not==0)
{
if(c!=0)
{
c=0
table1[0]='-'
}
else if(A1==0)
table1[0]=' '
else
table1[0]=table[A1]
if(A1==0)
if(A2==0)
table1[1]=' '
else
table1[1]=table[A2]
table1[2]=table[num%1000%100/10]
table1[4]=table[num%1000%100%10]
}
else
{
table1[0]='?'
table1[1]='?'
table1[2]='?'
table1[4]='?'
}
write_com(0x80)
for(i=0i<11i++)
{write_date(table7[i])
delay(2)}
write_com(0x8b)
for(i=0i<5i++)
{write_date(table1[i])
delay(2)}
write_com(0xc0)
for(i=0i<16i++)
{
if(he==1)
write_date(ta5[i])
else if(in==1)
write_date(ta6[i])
else
write_date(table3[i])
}
c=0
WDT_CONTR=0x35 /////喂狗
}
////显示2////////////////////
display2(uchar bh,int dat)
{
uchar a,A,B
WDT_CONTR=0x35 /////喂狗
//write_com(0x01)//清屏指令
y=dat
y=y&0x8000
if(y!=0)
dat=~dat+1
A=dat/1000
B=dat%1000/100
if((bh!=4)&&(bh!=5))
{
if(A!=0)
table4[0]=table[dat/1000]
else if((c!=0)||(y!=0))
{
c=0y=0
table4[0]='-'
}
else
table4[0]=' '
if(B!=0)
table4[1]=table[B]
else
table4[1]=' '
table4[2]=table[dat%1000%100/10]
table4[3]='.'
table4[4]=table[dat%1000%100%10]
}
else
{
table4[0]=' '
if((c!=0)||(y!=0))
{
c=0y=0
table4[1]='-'
}
else
table4[1]=' '
table4[2]=' '
table4[3]=table[dat%1000%100/10]
table4[4]=table[dat%1000%100%10]
}
write_com(0xc4)
delay(2)
for(a=0a<5a++)
write_date(table4[a])
delay(2)
write_com(0x80)
switch(bh)
{
case 1:for(a=0a<14a++)write_date(ta1[a])break
case 2:for(a=0a<14a++)write_date(ta2[a])break
case 3:for(a=0a<14a++)write_date(ta3[a])break
case 4:for(a=0a<14a++)write_date(ta4[a])break
default:break
}
}
///////////x24c02//////////////////
void delay24()
{ }
void init24c02() //初始化
{
sda=1
delay24()
scl=1
delay24()
}
void start() //开始信号
{
sda=1
delay24()
scl=1
delay24()
sda=0
delay24()
}
void stop() //停止
{
sda=0
delay24()
scl=1
delay24()
sda=1
delay24()
}
void respons() //应答
{
uchar i
scl=1
delay24()
while((sda==1)&&(i<250))i++
scl=0
delay24()
}
void write_byte(uchar date) // 写数据子函数
{
uchar i,temp
temp=date
for(i=0i<8i++)
{
temp=temp<<1
scl=0
delay24()
sda=CY
delay24()
scl=1
delay24()
}
scl=0
delay24()
sda=1
delay24()
}
uchar read_byte() // 读数据子函数
{
uchar i,k
scl=0
delay24()
sda=1
delay24()
for(i=0i<8i++)
{
scl=1
delay24()
k=(k<<1)|sda
scl=0
delay24()
}
return k
}
///////写数据函数///////////////////
void write_add(uchar address,uint date)
{
start()
write_byte(0xa0)
respons()
write_byte(address)
respons()
write_byte(date/256)
respons()
write_byte(date%256)
respons()
stop()
}
uchar read_add(uchar address) //读数据函数
{
uchar date
start()
write_byte(0xa0)
respons()
write_byte(address)
respons()
start()
write_byte(0xa1)
respons()
date=read_byte()
stop()
return date
}
void delay1ms(uchar ms)
{
uchar i
while(ms--)
{
for(i = 0i<250i++)
{
_nop_()
_nop_()
_nop_()
_nop_()
}
}
}
int keyf(int *num,int up,int dn)
{
uint i
uchar z
for(i=0i<600i++)
{
display2(n,*num)
if(key1==0)
{
delay1ms(30)
if(key1==0)
{
i=0
n++
if(n>=9)
n=0
while(!key1)
display2(n,*num)
break
}
}
if(key2==0)
{
delay1ms(10)
if(key2==0)
{
i=0
if(*num>=up)
*num=up
else if(n!=4)
*num=*num+1
else if(*num<100)
*num=*num+5
else
*num=*num+10
for(z=0z<65z++)
{
display2(n,*num)
if(key2!=0)
break
}
while(!key2)
{
for(z=0z<2z++)
display2(n,*num)
if(*num>=up)
*num=up
else if(n!=4)
*num=*num+1
else if(*num<100)
*num=*num+5
else
*num=*num+10
}
}
}
if(key3==0)
{
delay1ms(10)
if(key3==0)
{
i=0
if(*num<=dn)
*num=dn
else if(n!=4)
*num=*num-1
else if(*num<100)
*num=*num-5
else
*num=*num-10
for(z=0z<65z++)
{
display2(n,*num)
if(key3!=0)
break
}
while(!key3)
{
for(z=0z<2z++)
display2(n,*num)
if(*num<=dn)
*num=dn
else if(n!=4)
*num=*num-1
else if(*num<100)
*num=*num-5
else
*num=*num-10
}
}
}
}
return(*num)
}
void keyjc()
{
uchar i=0
if(key1==0)
{
delay1ms(10)
if(key1==0)
{
EA=0
for(i=0i<20i++)
{
display(tmp())
}
if(key1==0)
{
write_com(0x01)//清屏指令
n++
if(n>=5)
n=0
while(!key1)
{
switch(n)
{
case 1:display2(n,acon)break
case 0:break
}
}
if(n==1)
{
keyf(&acon,1250,-530)
if((acon-bcon)<3)
bcon=acon-3
}
if(n==2)
{
keyf(&bcon,1240,-550)
if((acon-bcon)<3)
acon=bcon+3
}
write_add(1,acon)//A
delay1ms(15)
write_add(3,bcon)//B
n=0
write_com(0x01)//清屏指令
}
EA=1
}
}
}
key()
{
uint i
if(key4==0)
delay1ms(50)
if(key4==0)
{
write_com(0x01)//清屏指令
for(i=0i<500i++)
{
if(key4==0)
{
delay1ms(15)
if(key4==0)
{
i=0
n++
if(n>=5)
n=0
while(!key4)
{
switch(n)
{
case 1: display2(1,acon)break
case 2: display2(2,bcon)break
default: break
}
}
}
}
switch(n)
{
case 1: display2(1,acon)break
case 2: display2(2,bcon)break
default: break
}
}
n=0
}
}
///////滤波////////
int filter()
{
int tm,buf[6]
uchar i,j
EA=0
for(i=0i<6i++)
{
buf[i]=tmp()
delay1ms(20)
WDT_CONTR=0x35 /////喂狗
}
for(j=0j<5j++)
for(i=0i<5-ji++)
if(buf[i]>buf[i+1])
{
tm=buf[i]
buf[i]=buf[i+1]
buf[i+1]=tm
}
tm=((buf[2]+buf[3])/2)
EA=1
return (tm)
}
void main()
{
uchar b,c
Init_Com()
init1602()
init24c02()
b=read_add(1)
delay1ms(15)
c=read_add(2)
delay1ms(15)
acon=b*256+c
b=read_add(3)
delay1ms(15)
c=read_add(4)
delay1ms(15)
bcon=b*256+c
AUXR=0x01// 禁止ALE输出
WDT_CONTR=0x35//启动看门狗
write_com(0x01)//清屏指令
while(1)
{
at=filter()
display(at)
keyjc()
key()
wen_kong()
weno()
}
}
//////温度控制//////////////
void wen_kong()
{
if((flag==0)&&(not==0))
{
at=filter()
if(at<=bcon)
{
flag=1
jrk=0
xhk=0
he=1
}
}
}
void weno()
{
if(flag)
{
at=filter()
if(at>=acon)
{
flag=0
jrk=1
if(rsg)
xhk=0
else
xhk=1
he=0
}
}
if(not==1)
{
flag=0
jrk=1
if(rsg)
xhk=0
else
xhk=1
he=0
}
}
我做过DS18B20温控设备,单总线传感这是念衫一个不错的选择哦。下面是C语言程序,硬件接线如下:(1)把“单片机系统”区域中的P0.0-P0.7用8芯排线连接到“动态数码显示”区域中的ABCDEFGH端子上。
(2)把“单片机系统”区域中的P2.0-P2.7用8芯排线连接到“动态数码显示”区域中的S1S2S3S4S5S6S7S8端子上。
(3)把DS18B20芯片插入“四路单总线”区域中的任一个插座中,注意电源与地信号不要接反。
(4)把“四路单总线”行蔽区域中的对应的DQ端子连接到“单片机系仔带腔统”区域中的P3.7/RD端子上。
#include <REG52.h>//at89c52
#include <INTRINS.h>
unsigned char code displaybit[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f}
unsigned char code displaycode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00,0x40}
unsigned char code dotcode[32]={0,3,6,9,12,16,19,22,
25,28,31,34,38,41,44,48,
50,53,56,59,63,66,69,72,
75,78,81,84,88,91,94,97}
unsigned char displaycount
unsigned char displaybuf[8]={16,16,16,16,16,16,16,16}
unsigned char timecount
unsigned char readdata[8]
sbit DQ=P3^7
bit sflag
bit resetpulse(void)
{
unsigned char i
DQ=0
for(i=255i>0i--)
DQ=1
for(i=60i>0i--)
return(DQ)
for(i=200i>0i--)
}
void writecommandtods18b20(unsigned char command)
{
unsigned char i
unsigned char j
for(i=0i<8i++)
{
if((command &0x01)==0)
{
DQ=0
for(j=35j>0j--)
DQ=1
}
else
{
DQ=0
for(j=2j>0j--)
DQ=1
for(j=33j>0j--)
}
command=_cror_(command,1)
}
}
unsigned char readdatafromds18b20(void)
{
unsigned char i
unsigned char j
unsigned char temp
temp=0
for(i=0i<8i++)
{
temp=_cror_(temp,1)
DQ=0
_nop_()
_nop_()
DQ=1
for(j=10j>0j--)
if(DQ==1)
{
temp=temp | 0x80
}
else
{
temp=temp | 0x00
}
for(j=200j>0j--)
}
return(temp)
}
void main(void)
{
TMOD=0x01
TH0=(65536-4000)/256
TL0=(65536-4000)%256
ET0=1
EA=1
while(resetpulse())
writecommandtods18b20(0xcc)
writecommandtods18b20(0x44)
TR0=1
while(1)
{
}
}
void t0(void) interrupt 1 using 0
{
unsigned char x
unsigned int result
TH0=(65536-4000)/256
TL0=(65536-4000)%256
if(displaycount==2)
{
P0=displaycode[displaybuf[displaycount]] | 0x80
}
else
{
P0=displaycode[displaybuf[displaycount]]
}
P2=displaybit[displaycount]
displaycount++
if(displaycount==8)
{
displaycount=0
}
timecount++
if(timecount==150)
{
timecount=0
while(resetpulse())
writecommandtods18b20(0xcc)
writecommandtods18b20(0xbe)
readdata[0]=readdatafromds18b20()
readdata[1]=readdatafromds18b20()
for(x=0x<8x++)
{
displaybuf[x]=16
}
sflag=0
if((readdata[1] &0xf8)!=0x00)
{
sflag=1
readdata[1]=~readdata[1]
readdata[0]=~readdata[0]
result=readdata[0]+1
readdata[0]=result
if(result>255)
{
readdata[1]++
}
}
readdata[1]=readdata[1]<<4
readdata[1]=readdata[1] &0x70
x=readdata[0]
x=x>>4
x=x &0x0f
readdata[1]=readdata[1] | x
x=2
result=readdata[1]
while(result/10)
{
displaybuf[x]=result%10
result=result/10
x++
}
displaybuf[x]=result
if(sflag==1)
{
displaybuf[x+1]=17
}
x=readdata[0] &0x0f
x=x<<1
displaybuf[0]=(dotcode[x])%10
displaybuf[1]=(dotcode[x])/10
while(resetpulse())
writecommandtods18b20(0xcc)
writecommandtods18b20(0x44)
}
}
Good luck!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)