EP2C5T144C8 EP2C5T144C8
关注数: 6 粉丝数: 28 发帖数: 725 关注贴吧数: 17
新手,求大神帮我修改程序,加一个PID控制 如题,程序: #include <reg52.h> #define uchar unsigned char #define uint unsigned int uint temp; uchar flag1; uint count; sbit DS=P2^2; sbit heat=P1^5; sbit plus=P3^4 ; sbit minus=P3^5; sbit dula=P2^6; sbit wela=P2^7; unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; void delay(uint z) //延迟 { uint x,y; for (x=z;x>0;x--) for(y=110;y>0;y--); } void dsreset(void) //初始化DS18B20 { uint i; DS=0; i=103; while(i>0)i--; DS=1; i=4; while(i>0)i--; } 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); //读出的数据最低位在最前面 } return(dat); } void tmpwritebyte(uchar dat) //写一个字节数据 { uint i; uchar j; bit testb; for(j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if(testb) { 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++; } } } void tmpchange(void) //DS18B20 开始温度转换 { dsreset(); delay(1); tmpwritebyte(0xcc); tmpwritebyte(0x44); } uint tmp() //读取寄存器中数据 { float tt; uchar a,b; dsreset(); delay(1); tmpwritebyte(0xcc); tmpwritebyte(0xbe); a=tmpread(); b=tmpread(); temp=b; temp<<=8; //两个字节合为一个字节 temp=temp|a; tt=temp*0.0625; temp=tt*10+0.5; return temp; } void key() { if(plus==0) { delay(10); if(plus==0) { count++; if(count>=90) count=90; while(!plus); } } if(minus==0) { delay(10); if(minus==0) { count--; if(count<=20) count=20; while(!minus); } } } void display(uint temp)//显示程序 { uchar A1,A2,A2t; uchar a,b; a=count/10; b=count%10; dula=0; P0=table[a];//显示十位 dula=1; dula=0; wela=0; P0=0xef; wela=1; wela=0; delay(1); P0=table[b];//显示个位 dula=1; dula=0; P0=0xdf; wela=1; wela=0; delay(1); A1=temp/100; A2t=temp%100; A2=A2t/10; dula=0; P0=table[A1];//显示十位 dula=1; dula=0; wela=0; P0=0xfe; wela=1; wela=0; delay(1); P0=table[A2];//显示个位 dula=1; dula=0; P0=0xfd; wela=1; wela=0; delay(1); } void heating() { uint t; t=temp/10; if(t<count) {heat=1;} else {heat=0;} } void main() { uchar a; count=25; heat=0; while(1) { key(); heating(); tmpchange(); for(a=10;a>0;a--) { display(tmp()); } } }
1 下一页