基于单片机msp430和温度传感器ds18b20的水温度控制系统的c语言源程序(不是测量,要有加热跟制冷)

2024-12-29 02:41:53
推荐回答(2个)
回答1:

我这是用STC做的,应该很容易移植到MPS430上的给你参考一下。
#include
#include
#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=8;while(i>0)i--;
return (dat);
}

uchar tmpread(void) //读一个字节
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}

void tmpwritebyte(uchar dat) //写一个字节到 ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(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=x;a>0;a--)
for(b=10;b>0;b--);
}
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=0;i<11;i++)
{write_date(table7[i]);
delay(2);}
write_com(0x8b);
for(i=0;i<5;i++)
{write_date(table1[i]);
delay(2);}
write_com(0xc0);
for(i=0;i<16;i++)
{
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=0;y=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=0;y=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=0;a<5;a++)
write_date(table4[a]);
delay(2);
write_com(0x80);
switch(bh)
{
case 1:for(a=0;a<14;a++)write_date(ta1[a]);break;
case 2:for(a=0;a<14;a++)write_date(ta2[a]);break;
case 3:for(a=0;a<14;a++)write_date(ta3[a]);break;
case 4:for(a=0;a<14;a++)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=0;i<8;i++)
{
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=0;i<8;i++)
{
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 = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}

int keyf(int *num,int up,int dn)
{
uint i;
uchar z;
for(i=0;i<600;i++)
{
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=0;z<65;z++)
{
display2(n,*num);
if(key2!=0)
break;
}
while(!key2)
{
for(z=0;z<2;z++)
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=0;z<65;z++)
{
display2(n,*num);
if(key3!=0)
break;
}
while(!key3)
{
for(z=0;z<2;z++)
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=0;i<20;i++)
{
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=0;i<500;i++)
{
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=0;i<6;i++)
{
buf[i]=tmp();
delay1ms(20);
WDT_CONTR=0x35; /////喂狗
}

for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
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;
}
}

回答2:

在百度知道你问纯技术问题很可笑