📄 新建 文本文档.txt
字号:
#include<reg52.h>
#define ti 0.07
#define td 40
#define kp 12.3
#define uint unsigned int
#define uchar unsigned char
sbit welage=P1^0; //个位位选定义
sbit welashi=P1^1; //十位位选定义
sbit welabai=P1^2; //百位位选定义
sbit keyge=P2^3; //个位键定义
sbit keyshi=P2^4; //十位键定义
sbit keybai=P2^5; //百位键定义
sbit set=P2^6; //设定键定义
sbit start=P2^7; //启动键设定
sbit eoc=P1^3; //转换结束信号端设定,为高时转换结束
sbit clk=P1^4; //输入时钟端设定
sbit din=P1^5; //控制字写入端设定
sbit out=P1^6; //AD输出端设定
sbit cs=P1^7;
sbit speaker=P2^0;
sbit led=P2^1;
sbit heat=P2^3;
uchar code table[10]={0x3f , 0x06 , 0x5b , 0x4f , 0x66 ,
0x6d ,0x7d , 0x07 , 0x7f , 0x6f
};
uchar code tablex[10]={
0,1,2,3,4,
5,6,7,8,9
};
uchar code tablepow[12]={
1,2,4,8,16,32,64,128,
256,512,1024,2048
};
uchar ge,shi,bai;
uchar tmp;//tmp为实际温度值
uchar tmpset;//tmpset为设定温度值
uchar adc[12];
uchar startadc;
uchar setkey;
void delay(uchar z) //延时程序
{ uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*void keyboardset() //键盘设定函数
{ uchar numge,numshi,numbai;
numge=0; //初始化
numshi=0;
numbai=0;
while(1)
{
welage=0; //动态显示当前的设定值
P0=table[numge];
delay(10);
welage=1;
welashi=0;
P0=table[numshi];
delay(10);
welashi=1;
welabai=0;
P0=table[numbai];
delay(10);
welabai=1;
tmpset=tablex[numbai]*100+tablex[numshi]*10+tablex[numge];
if (keyge==0) //个位键按下时开始动作
{
delay(10); //消抖
if (keyge==0)
{
welashi=1; //关闭十位位选
welabai=1; //关闭百位位选
P0=table[numge];
numge++;
if(numge==10)
numge=0;
}
while(!keyge); //检测松键盘
delay(10);
while(!keyge);
}
else if(keyshi==0) //十位键按下时开始动作
{
delay(10);
if (keyshi==0);
{
welage=1; //关闭个位位选
welabai=1; //关闭百位位选
P0=table[numshi];
numshi++;
if(numshi==10)
numshi=0;
}
while(!keyshi); //检测松键盘
delay(10);
while(!keyshi);
}
else if(keybai==0)
{
delay(10);
if (keybai==0);
{
welage=1; //关闭个位位选
welashi=1; //关闭十位位选
P0=table[numbai];
numbai++;
if(numbai==10)
numbai=0;
}
while(!keybai);
delay(10);
while(!keybai);
}
}
}*/
uint tmpconvert() //实际温度值转换程序
{
uchar i,c,b,a,d,result;//a为标度变化系数,当电压值变化1v时,温度变化为a摄氏度
c=0;
b=0;
result=0;
a=0.2; //待测量
for (i=0;i<12;i++)
{
d=11-i;
b=adc[i]*tablepow[d];
c=c+b;
}
result=(4095-c)*0.5;
return (result);
}
void display(uchar shu) //动态显示十进制数
{
uchar a,b,c;
a=shu/100;
b=shu%100/10;
c=shu%10;
welage=0;
P0=table[c];
delay(1);
welage=1;
welashi=0;
P0=table[b];
delay(1);
welashi=1;
welabai=0;
P0=table[a];
delay(1);
welabai=1;
}
void read_bit() //当开始键按下,就开始调用
{
uchar count1,count2;
//cs=1;
cs=0;
for(count2=0;count2<12;count2++)//写入控制字
{
clk=0;
din=0;
clk=1;
}
//while(eoc==1) //转换完成后开始,12个时钟周期后eoc为0
//{
for(count1=0;count1<12;count1++) //将ad数据存入数组中
{
adc[count1]=out;
//din=0; //将下次转换需要的控制字写入,转换后高位先送出
clk=1;
clk=0;
}
//}
cs=1;//eoc为0后将cs置高,开始转换过程
tmp=tmpconvert();
}
uint pid(uchar setv)
{
uchar tempdx,tempdx1,tempdx2;
tempdx=tempdx1=tempdx2=0;
tempdx = setv - tmp;
if(tempdx>0)
{
tempdx2 = tempdx2 + tempdx;
out = kp * Tempdx + Tempdx2 / ti + td * (Tempdx - Tempdx1);
Tempdx1 = Tempdx;
led=0;
return out;
}
else if(tempdx<0)
{
return 0;
speaker=0;
delay(10);
speaker=1
}
}
void main()
{
if (set==0)
{
delay(10);
if(set==0)
{
setkey=1;
startadc=0;
}
while(!set);
delay(10);
while(!set);
}
while(setkey==1)
{
keyboardset();
}
if (start==0) //放入主程序中,做判断
{
delay(10);
if(start==0)
{
din=0;
clk=0;
cs=1;
startadc=1;
//eoc=1;
setkey=0;//AD转换开始后,终止键盘设定
}
while(!start);//不松键盘,择程序停留此处不向下执行
delay(10);
while(!start);
}
while (startadc==1)
{
read_bit();
heatsec=pid(tmpset);
heat=1;
delay(heatsec);
heat=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -