#include <reg51.h>
#include <intrins.h>
#include "ADC0832.h"
#include <DS18B20.h>
#define uint unsigned int
#define uchar unsigned char
#define LCDDATA P1
uint BJ_temp=0,BJ_yw=0;
unsigned char TempBuffer[4];
unsigned char dispbuf[3]=0;
unsigned int getdata;
long uint i,ADC_num,adres_num=0;
int flag_start=0,flag_BJ=0;
int time_over=0;
uint pwm_compare=100,time1=0;
int timenum=0;
char time[6]="00:00";
char f='0',f1='0',g='0',g1='0';
unsigned char t[2],*pt; //用来存放温度值,测温程序就是通过这个数组与主函数通信的
unsigned char TempBuffer1[9]={0x2b,0x31,0x32,0x32,0x2e,0x30,0x30,0x43,'\0'};
unsigned char TempBuffer0[17]={0x54,0x48,0x3a,0x2b,0x31,0x32,0x35,0x20,
0x54,0x4c,0x3a,0x2b,0x31,0x32,0x34,0x43,'\0'};
unsigned char code dotcode[4]={0,25,50,75};
sbit K1=P3^0; //时间+
sbit K2=P3^1;//时间-
sbit K3=P3^2;//开始清洗
sbit K4=P0^0;//电机+
sbit K5=P0^1;//电机-
sbit PWM_OUT=P3^7;
sbit sing=P2^4;
//LCD1602引脚定义
//采用8位并行方式,DB0~DB7连接至LCDDATA0~LCDDATA7
sbit RS=P3^5;
sbit CS=P3^6;
void DelayMS(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<100;j++);
}
void Delay_xms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<1000;j++);
}
void Delay_xus(uint t)
{
for(;t>0;t--)
{
_nop_();_nop_(); _nop_(); _nop_();
_nop_();_nop_(); _nop_(); _nop_();
}
}
void covert1(void) //将温度转换为LCD显示的数据
{
unsigned char x=0x00,y=0x00;
t[0]=*pt;
pt++;
t[1]=*pt;
if(t[1]>0x07) //判断正负温度
{
TempBuffer1[0]=0x2d; //0x2d为"-"的ASCII码
t[1]=~t[1]; /*下面几句把负数的补码*/
t[0]=~t[0]; /* 换算成绝对值*********/
x=t[0]+1; /***********************/
t[0]=x; /***********************/
if(x>255) /**********************/
t[1]++; /*********************/
}
else TempBuffer1[0]=0x2b; //0xfe为变"+"的ASCII码
t[1]<<=4; //将高字节左移4位
t[1]=t[1]&0x70; //取出高字节的3个有效数字位
x=t[0]; //将t[0]暂存到X,因为取小数部分还要用到它
x>>=4; //右移4位
x=x&0x0f; //和前面两句就是取出t[0]的高四位
t[1]=t[1]|x; //将高低字节的有效值的整数部分拼成一个字节
TempBuffer1[1]=t[1]/100+0x30; //+0x30 为变 0~9 ASCII码
if( TempBuffer1[1]==0x30) TempBuffer1[1]=0xfe; //百位数消隐
TempBuffer1[2]=(t[1]%100)/10+0x30; //分离出十位
TempBuffer1[3]=(t[1]%100)%10+0x30; //分离出个位
t[0]=t[0]&0x0c; //取有效的两位小数
t[0]>>=2; //左移两位,以便查表
x=t[0];
y=dotcode[x]; //查表换算成实际的小数
TempBuffer1[5]=y/10+0x30; //分离出十分位
TempBuffer1[6]=y%10+0x30; //分离出百分位
if((TempBuffer1[2]!='0')&&(TempBuffer1[3]!='0'))
{
BJ_temp=(TempBuffer1[2]-'0')*10+TempBuffer1[3]-'0';
TempBuffer[0]=TempBuffer1[2];
TempBuffer[1]=TempBuffer1[3];
}
TempBuffer[2]='C';
TempBuffer[3]='\0';
}
void timer_Init()
{
TMOD=0X11; //定时器0 工作1 定时器1 工作1
TH0= (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
TR0 = 1; //定时器1开始计时
ET0=1;
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
TR1 = 1; //定时器1开始计时
ET1=1;
EA=0;
}
//控制LCD写时序
void LCD_en_write(void)
{
CS=1;
Delay_xus(20);
CS=0;
Delay_xus(20);
}
//写指令函数
void Write_Instruction(uchar command)
{
RS=0;
CS=1;
LCDDATA=command;
LCD_en_write();//写入指令数据
}
//写数据函数
void Write_Data(uchar Wdata)
{
RS=1;
CS=1;
LCDDATA=Wdata;
LCD_en_write();//写入数据
}
//字符显示初始地址设置
void LCD_SET_XY(uchar X,uchar Y)
{
uchar address;
if(Y==0)
address=0x80+X;//Y=0,表示在第一行显示,地址基数为0x80
else
address=0xc0+X;//Y非0时,表时在第二行显示,地址基数为0xC0
Write_Instruction(address);//写指令,设置显示初始地址
}
//在第X行Y列开始显示Wdata所对应的单个字符
void LCD_write_char(uchar X,uchar Y,uchar Wdata)
{
LCD_SET_XY(X,Y);//写地址
Write_Data(Wdata);//写入当前字符并显示
}
/*********************************************************************
*名 称: LCD_write_str()
*功 能:使 LCD1602 显示一个字符串
*入口参数: y:行 0 或 1 x:列, 0~15, str[]: 待显示的字符串
*出口参数:无
*********************************************************************/
void LCD_write_str(uchar X,uchar Y,char str[])
{
uint i=0;
LCD_SET_XY(X,Y);//写地址
while(str[i] != '\0')
{
Write_Data(str[i]);
++i;
Delay_xms(5);
}
}
//清屏函数
void LCD_clear(void)
{
Write_Instruction(0x01);
Delay_xms(5);
}
//显示屏初始化函数
void LCD_init(void)
{
Write_Instruction(0x38); //8bit interface,2line,5*7dots
Delay_xms(5);
Write_Instruction(0x38);
Delay_xms(5);
Write_Instruction(0x38);
Write_Instruction(0x08); //关显示,不显光标,光标不闪烁
Write_Instruction(0x01); //清屏
Delay_xms(5);
Write_Instruction(0x04); //写一字符,整屏显示不移动
//Write_Instruction(0x05); //写一字符,整屏右移
//Write_Instruction(0x06); //写一字符,整屏显示不移动
//Write_Instruction(0x07); //写一字符,整屏左移
Delay_xms(5);
//Write_Instruction(0x0B); //关闭显示(不显示字符,只有背光亮)
Write_Instruction(0x0C); //开显示,光标、闪烁都关闭
//Write_Instruction(0x0D); //开显示,不显示光标,但光标闪烁
//Write_Instruction(0x0E); //开显示,显示光标,但光标不闪烁
//Write_Instruction(0x0F); //开显示,光标、闪烁均显示
}
void ADC()
{
float adc_num=0;
long unsigned int v = 0;
getdata=read0832();
adc_num=(float)(getdata*19.5);
adc_num=(float)(adc_num*0.02); //0--5000 0--100%
i=(int)adc_num;
if(i>=98)
i=100;
BJ_yw=i;
dispbuf[0]=i/100+'0';
dispbuf[1]=i/10%10+'0';
dispbuf[2]=i%10+'0';
LCD_write_str(10,0,dispbuf);
LCD_write_char(13,0,'%');
Delay_xms(20);
}
void time_sacn()
{
int flag=0;
while(flag==0) //十分
{
if(K1==0)
{
while(K1==0);
f++;
if( f==('9')+1)
f='0';
}
if(K2==0)
{
while(K2==0);
f--;
if( f==('0')-1)
f='9';
}
if(K3==0)
{
while(K3==0);
flag=1;
}
time[0]=f;
time[1]=f1;
time[2]=':';
time[3]=g;
time[4]=g1;
LCD_write_str(5,1,time);
}
while(flag==1) //分
{
if(K1==0)
{
while(K1==0);
f1++;
if( f1==('9')+1)
f1='0';
}
if(K2==0)
{
while(K2==0);
f1--;
if( f1==('0')-1)
f1='9';
}
if(K3==0)
{
while(K3==0);
flag=2;
}
time[0]=f;
time[1]=f1;
time[2]=':';
time[3]=g;
time[4]=g1;
time[5]='\0';
flag_start=1;
LCD_write_str(5,1,time);
}
}
void moto()
{
if(K4==0) //加速
{
while(K4==0);
pwm_compare-=20;
if(pwm_compare<=20)
pwm_compare=20;
}
if(K5==0) //减速
{
while(K5==0);
pwm_compare+=20;
if(pwm_compare>=200)
pwm_compare=200;
}
}
void main()
{
PWM_OUT=0;
LCD_init(); //lcd初始化
LCD_clear(); //清屏
time_sacn(); //进行时间设置
timer_Init(); // 初始化
pt=ReadTemperature(0x3f); //上限温度下限
//读取温度,温度值存放在一个两个字节的数组
while(1)
{
if(K3==0) //暂停
{
while(K3==0);
flag_start=~flag_start;
}
if((flag_start==1)||(flag_BJ==1))
EA=1;
if((flag_start!=1)||(flag_BJ!=1)) //停止计数
{ EA=0;PWM_OUT=0; }
moto(); //电机速度
pt=ReadTemperature(0x3f); //上限温度下限
//读取温度,温度值存放在一个两个字节的数组
covert1();
LCD_write_str(2,0,TempBuffer);
ADC(); //ADC采集显示
if((BJ_temp>=80)||(BJ_yw<=10)) //温度高于80 或者 液位低于10% 报警 停止工作
{
time_over=1;
flag_BJ=0;
}
if((BJ_temp<80)&&(BJ_yw>10))
{
time_over=0;
flag_BJ=1;
}
if(time_over==1)
sing=0;
if(time_over==0)
sing=1;
}
}
void t0(void) interrupt 1 using 0 //定时器0 中断服务
{
TH0 = (65536 - 100) / 256;
TL0= (65536 - 1000) % 256;
if(time1<pwm_compare)
{
PWM_OUT=0;
}
else
{
PWM_OUT=1;
}
if(time1==200)
{
time1=0;
PWM_OUT=0;
}
time1++;
}
void timer1_int() interrupt 3
{
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
timenum++;
if(timenum>=20) //1s
{
timenum=0;
g1--;
if(g1==('0')-1)
{ g--;g1='9';}
if(g==('0')-1)
{ f1--;g='5';}
if(f1==('0')-1)
{ f--;f1='9';}
if(f==('0')-1)
{ f='0';}
if((f=='0')&&(f1=='5')&&(g=='0')&&(g1=='0')) //倒计时5min
time_over=1;
if((f=='0')&&(f1=='4')&&(g=='5')&&(g1=='0')) // 报警10s
time_over=0;
if((f=='0')&&(f1=='0')&&(g=='0')&&(g1=='0')) //停止
flag_start=0;
time[0]=f;
time[1]=f1;
time[2]=':';
time[3]=g;
time[4]=g1;
time[5]='\0';
LCD_write_str(5,1,time);
}
}
|