想用温度传感器反馈一个pwm信号控制电机转速(程序如下),程序在34.5之前输出电压在2V以下,在34.5之后输出为12v,小白搞不定他,有没有大神解解惑,万分感谢。
单片机源程序:
#include "reg52.h"
#include"temp.h"
#include<intrins.h>
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit beep=P1^5;
sbit PWM1=P1^1;//
sbit PWM2=P1^2;
sbit ENA=P1^3;
char num=0;
uchar time=0;
uchar ting; uchar zou;
u8 DisplayData[8];
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
u8 start1=0;
u8 start2=0;
int flag=0;
void delay(u16 i)
{ while(i--); }
void sound1()
{ u8 i=100;
while(i--)
{ beep=~beep;
delay(10);
}
start1=0;
beep=1;
}
void sound2()
{ u8 i=100;
while(i--)
{ beep=~beep;
delay(100);
}
start2=0;
beep=1; }
void datapros(int temp)
{ float tp;
if(temp< 0)
{ DisplayData[0] = 0x40; //
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
}
else
{ DisplayData[0] = 0x00;
tp=temp;
temp=tp*0.0625*100+0.5; }
DisplayData[1] = smgduan[temp / 10000];
DisplayData[2] = smgduan[temp % 10000 / 1000];
DisplayData[3] = smgduan[temp % 1000 / 100] | 0x80;
DisplayData[4] = smgduan[temp % 100 / 10];
DisplayData[5] = smgduan[temp % 10];}void
baojing(int temp)
{ float
mmp,tempH,tempL,tempM1,tmp,tempM2,tempM3,tempM4,tempM5,tempM6,tempM7,tempM8;
tempH=35;
tempM1=31;
tempM2=31.5;
tempM3=32;
tempM4=32.5;
tempM5=33;
tempM6=33.5;
tempM7=34;
tempM8=34.5;
tempL=28;
mmp=(float)temp;
tmp=(mmp*0.0625*100+0.5)/100; if(tmp>=tempH)
{ start2=1;
ting=100; }
else if(tmp<=tempL)
{ start1=1;
ting=95;
}
else if (tmp>=tempM8)
{ start1=1;
ting=89;
}
else if (tmp>=tempM7)
{ start1=1;
ting=87;
}
else if (tmp>=tempM6)
{ start1=1;
ting=85;
}
else if (tmp>=tempM5)
{ start1=1;
ting=83;
}
else if (tmp>=tempM4)
{ start1=1;
ting=80;
}
else if (tmp>=tempM3)
{ start1=1;
ting=78;
}
else if (tmp>=tempM2)
{ start1=1;
ting=75;
}
else if (tmp>=tempM1)
{ start1=1;
ting=70;
}
else
{ beep=1;
ting=65;
}}
void DigDisplay()
{ u8 i; for(i=0;i<6;i++)
{ switch(i) { case(0): LSA=0;LSB=0;LSC=0; break; case(1): LSA=1;LSB=0;LSC=0; break; case(2): LSA=0;LSB=1;LSC=0; break; case(3): LSA=1;LSB=1;LSC=0; break; case(4): LSA=0;LSB=0;LSC=1; break; case(5): LSA=1;LSB=0;LSC=1; break; }
P0=DisplayData[5-i];
delay(10);
P0=0x00; }
}
void Timer0Init()
{ TMOD=0x01;
TH0=0xff;
TL0=0xf7;
EA=1;
ET0=1;
TR0=1;}
void main()
{ ting=40;
Timer0Init();
while(1) {
if(start1==1)
{ sound1();
}
else if(start2==1)
{
sound2(); }
else { beep=1;
} }
}
void tim0() interrupt 1
{ if(flag==0)
{
datapros(Ds18b20ReadTemp()); DigDisplay();
baojing(Ds18b20ReadTemp()); flag=1;
}
TR0=0;
TH0=0xff;
TL0=0xf7;
TR0=1;
time++;
if(time>=100)
{datapros(Ds18b20ReadTemp()); DigDisplay();
baojing(Ds18b20ReadTemp()); time=0; }
if(time<=ting)
{ PWM1=1;
PWM2=0;
// ENA=1; }
else
{ PWM1=0;
PWM2=0;
// ENA=0; }
} |