当前位置:文档之家› 温度反馈控制程序

温度反馈控制程序

#include
#include
#include






void TP();
void tempint();
void tempwrite();
void tempread();
void delayus(unsigned int i);
void delayms_t(unsigned int i);
void close_i();
void open_i();




#define temp_in DDRB=DDRB&(0b11110111)
#define temp_out DDRB=DDRB|(0b00001000)
#define set_tempio PORTB=PORTB|(0b00001000)
#define clr_tempio PORTB=PORTB&(0b11110111)
#define temp_io 0b00001000

unsigned int temp;
unsigned char error1,comd,finish,templ;





#define date PORTA
int MODE=0,a=0;
unsigned int L,H,T1,T0,T,R;
unsigned char bf;



unsigned int c=0,b=0;



void intite();
void address();
void write();
void delay();
void comw();
void busy();
void busyr();
void chu();
void cal();
void comp();
void TP();
void TP_w();
//void output();




int main(void)
{
unsigned int i=0;
temp_in;
clr_tempio;
intite();
chu();
DDRA=0xff;
MCUCR=10;
GICR |= (1<
DDRD=(0b10000000)|DDRD;

MCUCR=10; //中断使能
GICR &= (1<
TCCR0=4;
TIMSK=1;
TCNT0=0;


sei();
while(1)
{
if(MODE==0)
{
TIMSK=1;
TP_w();


c=(T1*10+T0)*10;
b=c*R/100;
a=a%c;

if((a>=0)&&(a{
PORTD=PORTD|(0b10000000);
}
else if((a>=b)&&(a{
PORTD=PORTD&(0b01111111);
}
else
a=0;
cal();

}
else
{
TIMSK=0;
PORTD=(0b01111111)&PORTD;
}
}
}




SIGNAL(SIG_INTERRUPT0)
{
unsigned int i,j;

for(j=0;j<60000;j++);
for(j=0;j<40000;j++);
GIFR |=(0<MODE=(MODE+1)%7;
if(MODE==0)
{
date=0b00001100; //指令4 显示开关控制
comw();
for(i=0;i<100;i++)
{
}
date=0b10000000;
comw();
cal();
}
else if(MODE==1)
{
date=130;
comw();
date=0b00001111; //指令4 显示开关控制
comw();
for(i=0;i<100;i++)
{
}

}
else if(MODE==2)
{
date=131;
comw();
date=0b00001111; //指令4 显示开关控制
comw();
for(i=0;i<100;i++)
{
}
}

else if(MODE==3)
{
date=135;
comw();
date=0b00001111; //指令4 显示开关控制

comw();
for(i=0;i<100;i++)
{
}
}

else if(MODE==4)
{
date=136;
comw();
date=0b00001111; //指令4 显示开关控制
comw();
for(i=0;i<100;i++)
{
}
}

else if(MODE==5)
{
date=141;
comw();
date=0b00001111; //指令4 显示开关控制
comw();
for(i=0;i<100;i++)
{
}
}

else if(MODE==6)
{
date=143;
comw();
date=0b00001111; //指令4 显示开关控制
comw();
for(i=0;i<100;i++)
{
}
}
else;
}



SIGNAL(SIG_INTERRUPT1)
{
unsigned int j;
for(j=0;j<60000;j++);
for(j=0;j<40000;j++);
GIFR |=(0<if(MODE==0)
{
return;
}
else if(MODE==1)
{
L=(L+10)%50;
date=130;
comw();
date=L/10+'0';
write();
date=130;
comw();
comp();
cal();
}
else if(MODE==2)
{
L=(L+1)%50;
date=131;
comw();
date=L%10+'0';
write();
date=131;
comw();
comp();
cal();
}

else if(MODE==3)
{
H=(H+10)%100;
date=135;
comw();
date=H/10+'0';
write();
date=135;
comw();
comp();
cal();
}

else if(MODE==4)
{
H=(H+1)%100;
date=136;
comw();
date=H%10+'0';
write();
date=136;
comw();
comp();
cal();
}

else if(MODE==5)
{
T1=(T1)%9+1;
date=141;
comw();
date=T1+'0';
write();
date=141;
comw();
cal();
}

else if(MODE==6)
{
date=143;
comw();
T0=(T0+1)%10;
date=T0+'0';
write();
date=143;
comw();
cal();
}
else;
}


void intite()
{
unsigned int i;
DDRA=0xff;
DDRB=0b11111111;
PORTB=0;

date=1; //指令1 清显示
comw();
for(i=0;i<800;i++)
{
}


date=6; //指令3 光标和显示模式设置
comw();
for(i=0;i<800;i++)
{
}

date=0b00001100; //指令4 显示开关控制
comw();
for(i=0;i<800;i++)
{
}

date=0b00111000; //指令6 功能设置
comw();
for(i=0;i<800;i++)
{
}


date=0x17; //指令5 光标和显示移位,此时用光标
comw();
for(i=0;i<800;i++)
{
}




}







void delay()
{

return;
}


void write() //write() 数据写入时三个控制端口电平变化
{
unsigned int j;
PORTB=(PORTB)|(0b00000101);
PORTB=(PORTB)&(0b11111101);
for(j=0;j<40;j++)
delay();
PORTB=(PORTB)&(0b11111011);
delay();
delay();
}


void comw() //comw() 命令和地址写入时三个控制端口电平变化
{
unsigned int j;
PORTB=(PORTB)&(0b11111100);
PORTB=(PORTB)|(0b00000100);
for(j=0;j<40;j++)
delay();
PORTB=(PORTB)&(0b11111011);
delay();
delay();
}


void busy()
{

busyr();

while(bf==1)
{
busyr();
}
return ;
}

void busyr() //busyr() 读忙时三个控制端口电平变化
{
unsigned int j;
DDRA=0;
date=255;
PORTB=PORTB|(0b00000100);
PORTB=PORTB|(0b00000010);
PORTB=PORTB&(0b11111110);
for(j=0;j<40;j++)
delay();
bf=PINA&(0b10000000);
PORTB=PORTB&(0b11111011);
DDRA=255;
date=255;
}



void chu()
{
date=0b00001111; //指令4 显示开关控制
unsigned char temp0[16]="L:10 H:20 T:1.0";
unsigned char temp1[16]="T: . C R: %";
L=10;
H=20;
T1=1;
T0=0;

unsigned int i;
busy();
date=0b10000000;
comw();
for(i=0;i<16;i++)
{
busy();
date=temp0[i];
write();
delay();
}
busy();
date=0b11000000;
comw();
for(i=0;i<16;i++)
{
busy();
date=temp1[i];
write();
delay();
}


cal();
}


void cal() //计算出R T=t*10 以避免用浮点数
{

TP_w();
if(T>(H*10))
{
R=0;
}
else if(T<(L*10))
{
R=100;
}
else
{
R=(H*10-T)*10/(H-L);
}

//显示出T,R

busy();
date=204;
comw();
busy();
date=R/100+'0';
write();
busy();
date=205;
comw();
busy();
date=(R%100)/10+'0';
write();
busy();
date=206;
comw();
busy();
date=(R%10)+'0';
write();
busy();
date=128;
comw();


if(MODE==0)
{
busy();
date=128;
comw();
}
else if(MODE==1)
{
busy();
date=130;
comw();
}
else if(MODE==2)
{
busy();
date=131;
comw();
}

else if(MODE==3)
{
busy();
date=135;
comw();
}

else if(MODE==4)
{

busy();
date=136;
comw();
}

else if(MODE==5)
{
busy();
date=141;
comw();
}

else if(MODE==6)
{
busy();
date=143;
comw();
}
}


void comp() //防止出现H{
if(H=L)
{
H=(L+1)%100;
busy();
date=135;
comw();
date=H/10+'0';
write();

busy();
date=136;
comw();
busy();
date=H%10+'0';
write();

if(MODE==1)
{
busy();
date=130;
comw();
}
else if(MODE==2)
{
busy();
date=131;
comw();
}
else if(MODE==3)
{
busy();
date=135;
comw();
}
else if(MODE==4)
{
busy();
date=136;
comw();
}
else;
}
else;
}



void TP_w()
{
TP();
T=temp*25/40;
busy();
date=194;
comw();
busy();
date=T/100+'0';
write();

busy();
date=195;
comw();
busy();
date=(T%100)/10+'0';
write();
busy();
date=197;
comw();
busy();
date=(T%10)+'0';
write();
busy();
date=128;
comw();
}

/*
void output()
{
unsigned int i=0,j=0;
i=(T1*10+T0)*10;
j=i*R/100;
a=a%i;

if((a>=0)&&(a{
PORTD=PORTD|(0b10000000);
}
else if((a>=j)&&(a{
PORTD=PORTD&(0b01111111);
}
else;
}
*/

SIGNAL(SIG_OVERFLOW0)
{
a=a+1;

}









void TP()
{
tempint();
if(error1)
return;




comd=0xcc;
tempwrite();



comd=0x44;
tempwrite();

delayms_t(20);

tempint();
if(error1)
return;

comd=0xcc;
tempwrite();


comd=0xbe;
tempwrite();


temp=0;

tempread();
temp=templ;

tempread();
temp=temp+256*templ;

}


void tempint()
{


temp_out;


delayus(200);


temp_in;

delayus(25);





if((PINB&temp_io)==0)
{
if((PINB&temp_io)==0)
{
error1=0;
}
else
{
error1=1;

}
}
else
{
error1=1;

}



/* TCCR0=1;//delay240us
TCNT0=15;
TIMSK=

1;
finish=1;
while(finish);

set_tempio;


TCCR0=1;//delay240us
TCNT0=15;
TIMSK=1;
finish=1;
while(finish);*/


delayus(150);





}





void tempwrite()
{
unsigned char j;

for(j=0;j<8;j++)
{
temp_out;
delayus(1);
if(comd&(0b00000001))
{
temp_in;
}
else
{
temp_out;
}



delayus(17);

temp_in;
comd=comd>>1;
delayus(1);
}



}





void tempread()
{
unsigned char j;
TCCR0=0;
for(j=0;j<8;j++)
{
templ=templ>>1;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_out;
temp_in;
temp_in;


if(PINB&(temp_io))
templ=templ+128;

else;


delayus(18);

}
TCCR0=4;
}











void delayus(unsigned int i)
{
unsigned int j;
for(j=0;j{
}
}




void delayms_t(unsigned int i)
{
unsigned int j,k;

for(j=0;j{
for(k=0;k<1000;k++)
{
}

相关主题
文本预览
相关文档 最新文档