大神给看下 这个是电动机还有温度传感器控制的 但是有两个提醒
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
sbit dianji=P1^7; //控制电机I/O口定义
sbit ds=P2^2; //温度传感器信号线
sbit dula=P2^6; //数码管段选线
sbit wela=P2^7; //数码管位选线
uchar num=0,gao_num=1,di_num=3;
void init_DS18B20(void)
{
ds=1;
delay(8);
ds=0;
delay(75);
ds=1;
delay(40);
}
//单片机向DS18B20写一个字节
uchar readonechar(void)
{
uchar i=0;
uchar dat=0;
for(i=0;i<8;i++)
{
ds=1;
delay(1);
ds=0;
dat>>=1;
ds=1;
delay(1);
if(ds)
dat|=0x80;
delay(4);
}
return (dat);
}
//单片机读DS18B20一个字节
void writeonechar(unsigned char dat)
{
uchar i=0;
for(i=0;i<8;i++)
{
ds=0;
ds=dat&0x01;
delay(5);
ds=1;
dat>>=1;
}
delay(4);
}
//单片机读取DS18B20的温度
float readtemperature(void)
{
uchar templ=0;
uchar temph=0;
float temperature;
init_DS18B20();
writeonechar(0xcc);
writeonechar(0xbe);
templ=readonechar();
temph=readonechar();
temperature=((temph*256)+templ)*0.0625;
delay(200);
return(temperature);
}
void distemperature(float m)
{
int n;
n=m*10;
num=(m-25)/2;
}
void dispose()
{
switch(num)
{
case 0:
gao_num=1; //PWM信号中高电平持续时间标志为1
di_num=3; //PWM信号中低电平持续时间标志为3,此时速度最慢
break;
case 1:
gao_num=2;
di_num=2;
break;
case 2:
gao_num=3;
di_num=1;
break;
case 3:
gao_num=4;
di_num=0; //此时速度最快
break;
default:
gao_num=0;
di_num=0;
}
}
void qudong() //控制电机程序
{
uchar i;
if(di_num!=0)
{
for(i=0;i<di_num;i++)
{
dianji=0; //实现PWM信号低电平输出
delay(50);
}
}
for(i=0;i<gao_num;i++)
{
dianji=1;
delay(50);
}
}
void main()
{
while(1)
{
dianji=0;
dispose();
qudong();
}
}
这是编译结果:
Build target 'Target 1'
assembling STARTUP.A51...
compiling 电动机1.c...
linking...
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
SEGMENT: ?PR?READTEMPERATURE?电禵籣1
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
SEGMENT: ?PR?_DISTEMPERATURE?电禵籣1
Program Size: data=22.0 xdata=0 code=1402
"电动机1" - 0 Error(s), 2 Warning(s).
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
sbit dianji=P1^7; //控制电机I/O口定义
sbit ds=P2^2; //温度传感器信号线
sbit dula=P2^6; //数码管段选线
sbit wela=P2^7; //数码管位选线
uchar num=0,gao_num=1,di_num=3;
void init_DS18B20(void)
{
ds=1;
delay(8);
ds=0;
delay(75);
ds=1;
delay(40);
}
//单片机向DS18B20写一个字节
uchar readonechar(void)
{
uchar i=0;
uchar dat=0;
for(i=0;i<8;i++)
{
ds=1;
delay(1);
ds=0;
dat>>=1;
ds=1;
delay(1);
if(ds)
dat|=0x80;
delay(4);
}
return (dat);
}
//单片机读DS18B20一个字节
void writeonechar(unsigned char dat)
{
uchar i=0;
for(i=0;i<8;i++)
{
ds=0;
ds=dat&0x01;
delay(5);
ds=1;
dat>>=1;
}
delay(4);
}
//单片机读取DS18B20的温度
float readtemperature(void)
{
uchar templ=0;
uchar temph=0;
float temperature;
init_DS18B20();
writeonechar(0xcc);
writeonechar(0xbe);
templ=readonechar();
temph=readonechar();
temperature=((temph*256)+templ)*0.0625;
delay(200);
return(temperature);
}
void distemperature(float m)
{
int n;
n=m*10;
num=(m-25)/2;
}
void dispose()
{
switch(num)
{
case 0:
gao_num=1; //PWM信号中高电平持续时间标志为1
di_num=3; //PWM信号中低电平持续时间标志为3,此时速度最慢
break;
case 1:
gao_num=2;
di_num=2;
break;
case 2:
gao_num=3;
di_num=1;
break;
case 3:
gao_num=4;
di_num=0; //此时速度最快
break;
default:
gao_num=0;
di_num=0;
}
}
void qudong() //控制电机程序
{
uchar i;
if(di_num!=0)
{
for(i=0;i<di_num;i++)
{
dianji=0; //实现PWM信号低电平输出
delay(50);
}
}
for(i=0;i<gao_num;i++)
{
dianji=1;
delay(50);
}
}
void main()
{
while(1)
{
dianji=0;
dispose();
qudong();
}
}
这是编译结果:
Build target 'Target 1'
assembling STARTUP.A51...
compiling 电动机1.c...
linking...
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
SEGMENT: ?PR?READTEMPERATURE?电禵籣1
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
SEGMENT: ?PR?_DISTEMPERATURE?电禵籣1
Program Size: data=22.0 xdata=0 code=1402
"电动机1" - 0 Error(s), 2 Warning(s).
感觉一个是普通的超温保护,一个是极限超温故障提示