|
发表于 2022-8-16 12:10:55
|
显示全部楼层
#include<reg52.h>
#include<18b20.h>
#define uchar unsigned char
#define uint unsigned int
sbit D1=P0^0;//电机
sbit D2=P0^1;
sbit E1=P0^2;
sbit xun1=P3^2;//
sbit xun2=P3^3;
sbit xun3=P3^4;//
sbit xun4=P3^5;
uchar right1 =0;
uchar Z_right=0;// 右电机占空比N/10
bit Right_stop=1;
uint time=0;
void delayms(uint z)
{uint i,j;
for(i=110;i>0;i--)
for(j=z;j>0;j--);
}
void fast(void)//前进函数
{
Z_right =100; //PWM 调节参数1-100 1为最慢,10是最快 改这个值可以改变其速度
D1=1;D2=0;
}
void middle()
{
Z_right =55; //PWM 调节参数1-100 1为最慢,10是最快 改这个值可以改变其速度
D1=1;D2=0;
}
void low()
{
Z_right =35; //PWM 调节参数1-100 1为最慢,10是最快 改这个值可以改变其速度
D1=1;D2=0;
}
void close()
{
Z_right =0; //PWM 调节参数1-100 1为最慢,10是最快 改这个值可以改变其速度
D1=0;D2=0;
}
void jiance()
{
if((xun1==0&&xun2==0)||(xun3==0&&xun4==0)||(xun2==0&&xun3==0))
{delayms(10);
if((xun1==0&&xun2==0)||(xun3==0&&xun4==0)||(xun2==0&&xun3==0))
{
if((tvalue>=240)&&(tvalue<=260))
{
low();
}
if((tvalue<280)&&(tvalue>260))
{
middle();
}
if(tvalue>=280)
{
fast();
}
if(tvalue<240)
{
close();
}
}
}
else
{
close();
}
}
/* 右电机调速 */
void pwm_out_right_moto(void)
{
if(Right_stop)
{
if(right1<=Z_right)
E1=1;
else
E1=0;
if(right1>=100)//与占空比100相对应
right1=0;
}
else
E1=0;
}
void main()
{
lcd_init();
init_play();//初始化显示
ReadTemp();//读取温度
TMOD=0X01;
TH0=(65536-917)/256; //1ms定时
TL0= (65536-917)%256;
TR0= 1;
ET0= 1;
EA = 1;
while(1)
{
ReadTemp();//读取温度
ds1820disp();//显示
boot=0;
jiance();
}
}
void timer0()interrupt 1 using 2
{
TH0=(65536-917)/256; //1ms定时
TL0= (65536-917)%256;
time++;
right1++;
pwm_out_right_moto();
} |
|