⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 max232.c

📁 基于proteus的多点温控系统的设计与仿真,该系统能够同时检测多路温度,用DS18B20温度传感器,AT89C51单片机为CPU.
💻 C
字号:
#include<reg51.h>
#include<stdio.h>
#include<intrins.h>
#include "LCD1602.h"



#define uchar unsigned char
#define uint unsigned int

unsigned char TBuffer[15];
sbit menu=P2^3; //按键菜单
sbit key_up=P2^4;   //按键UP
sbit key_down=P2^5; //按键DOWN
sbit enter=P2^6;//按键确定
sbit  DQ=P1^3;        //温度输入口
sbit alarm=P1^0;//蜂鸣器报警接口
sbit led1=P1^1;//状态灯1接口
sbit led2=P1^2;//状态灯2接口

unsigned char aaa,flag1,bbb,ccc,ddd;
unsigned int tvalue;      //16进制温度值
bit tflag;        //温度正负标志
bit Keyset;       //进入温度设定键标志位
bit limit_max;    //温度上限标志
bit limit_min;    //温度下限标志
unsigned int limit_up=0x1A;      //10进制上限温度值
unsigned int limit_down=0x0f;    //10进制下限温度值
unsigned char Keyenter;        //键值
unsigned char temperature;   //10进制温度值

unsigned char buffer[3];
unsigned char flag=1;
unsigned char DataFromPC[3];

unsigned char flag_URT=0;
unsigned char test=0;
unsigned char count=0;

/*------------------------------------------------
Speaker Registers
------------------------------------------------*/
sbit SPEACKER = P3^7;
sbit Send_en = P3^2;
bit show_flag;
          



//uchar open_flag=0;


void IntToStr(unsigned int t, unsigned char *str, unsigned char n) 
{
	unsigned char a[5]; char i, j;                                 	
	a[0]=(t/10000)%10;         //取得整数值到数组         	
	a[1]=(t/1000)%10;                                     	
	a[2]=(t/100)%10;                                      	
	a[3]=(t/10)%10;                                       	
	a[4]=(t/1)%10;                                        	
                                                      
	for(i=0; i<5; i++)         //转成ASCII码              	
		a[i]=a[i]+'0';                                    	
	for(i=0; a[i]=='0' && i<=3; i++);                     	
	for(j=5-n; j<i; j++)       //填充空格                 	
		{ *str=' ';  str++; }                             	
	for(; i<5; i++)                                       	
		{ *str=a[i]; str++; }  //加入有效的数字           	
	*str='\0'; 
} 



void Delay1ms(unsigned int count)
 {
	unsigned int i,j;
	for(i=0;i<count;i++)
	for(j=0;j<120;j++);
 }
/******************************ds1820程序***************************************/
void delay_18B20(unsigned int i)//延时1微秒
{
   while(i--);
}

void ds1820rst()/*ds1820复位*/
{ unsigned char x=0;
DQ = 1;          //DQ复位
delay_18B20(4); //延时
DQ = 0;          //DQ拉低
delay_18B20(100); //精确延时大于480us
DQ = 1;          //拉高
delay_18B20(40); 
   } 

unsigned char ds1820rd()/*读数据*/
{ unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{   DQ = 0; //给脉冲信号
    dat>>=1;
    DQ = 1; //给脉冲信号
    if(DQ)
    dat|=0x80;
    delay_18B20(10);
}
   return(dat);
}

void ds1820wr(uchar wdata)/*写数据*/
{unsigned char i=0;
    for (i=8; i>0; i--)
   { DQ = 0;
     DQ = wdata&0x01;
     delay_18B20(10);
     DQ = 1;
     wdata>>=1;
   }
}



read_temp()/*读取温度值并转换*/
{unsigned char a,b;
ds1820rst();    
ds1820wr(0xcc);//*跳过读序列号*/
ds1820wr(0x44);//*启动温度转换*/
ds1820rst();    
ds1820wr(0xcc);//*跳过读序列号*/ 
ds1820wr(0xbe);//*读取温度*/ 
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
    if(tvalue<0x0fff)
   tflag=0;
    else
   {tvalue=~tvalue+1;
tflag=1;
   }
tvalue=tvalue*(0.625);//温度值扩大10倍,精确到1位小数
return(tvalue);
}

/***********************温度数据处理与显示*********************************/
void ds1820disp(unsigned int t, unsigned char *str, unsigned char n)
{
        unsigned char tp[14]; char i, j;
        if(tflag==1&&Keyset==0){
                  tp[0]=0x2d;       //加一个"-"
                  tp[1]=t/1000+0x30;//百位数
                  tp[2]=t%1000/100+0x30;//十位数
                  tp[3]=t%100/10+0x30;//个位数
                  tp[4]=0x2e;
                  tp[5]=t%10+0x30;//小数位
                  tp[6]=0xdf;//显示"。"
                  tp[7]=0x43;//"C"
                  tp[8]=0xa0;tp[9]=0xa0;tp[10]=0xa0;tp[11]=0xa0;tp[12]=0xa0;
                  temperature=(tp[0]-0x30)*100+(tp[1]-0x30)*10+(tp[2]-0x30)+(tp[4]-0x30)*0.1;
	                for(i=0; tp[i]=='0' && i<=3; i++);
                  	for(j=14-n; j<i; j++){ *str=' ';  str++; }  //填充空格
		            for(; i<14; i++){ *str=tp[i]; str++; }  //加入有效的数字
		        	*str='\0';
                  }
        if(tflag==0&&Keyset==0){
             tp[0]=t/1000+0x30;//百位数
             tp[1]=t%1000/100+0x30;//十位数
             tp[2]=t%100/10+0x30;//个位数
             tp[3]=0x2e;//显示小数点
             tp[4]=t%10+0x30;//小数位
             tp[5]=0xdf;//显示"。"
             tp[6]=0x43;//"C"
             tp[7]=0xa0;tp[8]=0xa0;tp[9]=0xa0;tp[10]=0xa0;tp[11]=0xa0;tp[12]=0xa0;
             temperature=(tp[0]-0x30)*100+(tp[1]-0x30)*10+(tp[2]-0x30)+(tp[4]-0x30)*0.1;
	           for(i=0; tp[i]=='0' && i<=3; i++);
               for(j=13-n; j<i; j++){ *str=' ';  str++; }       //填充空格
		       for(; i<13; i++){ *str=tp[i]; str++; }  //加入有效的数字
		       *str='\0';
             }  
         if(tflag==0&&Keyset==1){
             tp[0]=t/100+0x30;//百位数
             tp[1]=t%100/10+0x30;//十位数
             tp[2]=t%100%10+0x30;//个位数
             tp[3]=0x2e;//显示小数点
             tp[4]=0+0x30;//小数位
             tp[5]=0xdf;//显示"。"
             tp[6]=0x43;//"C"
             tp[7]=0xa0;tp[8]=0xa0;tp[9]=0xa0;tp[10]=0xa0;tp[11]=0xa0;tp[12]=0xa0;
	           for(i=0; tp[i]=='0' && i<=3; i++);
               for(j=13-n; j<i; j++){ *str=' ';  str++; }       //填充空格
		       for(; i<13; i++){ *str=tp[i]; str++; }  //加入有效的数字
		       *str='\0';
              }
  }




 /***********************上下限温度处理*********************************/

void Utdisp(unsigned int t)
{
        unsigned char ut[4]; 
                  ut[0]=t/100;//百位数
                  ut[1]=t%100/10;//十位数
                  ut[2]=t%100%10;//个位数
                //  ut[3]=t%10+0x30;//小数位
                  limit_up=ut[0]*100+ut[1]*10+ut[2];
   }
void Dtdisp(unsigned int t)
{
        unsigned char dt[4]; 
                  dt[0]=t/100;//百位数
                  dt[1]=t%100/10;//十位数
                  dt[2]=t%100%10;//个位数
                //  dt[3]=t%10+0x30;//小数位
                  limit_down=dt[0]*100+dt[1]*10+dt[2];
   }


/********************按键UP子程序**************************************/
KEY_SET_UP()
{
    if(Keyset==1&&limit_max==1){ limit_up=limit_up+1; }
                       
     if(Keyset==1&&limit_min==1){limit_down=limit_down+1;}
}

/********************按键DOWN子程序************************************/

KEY_SET_DOWN()
{
    if(Keyset==1&&limit_max==1){ limit_up=limit_up-1;}
    if(Keyset==1&&limit_min==1){ limit_down=limit_down-1;}
}

/********************按键ENTER子程序**************************************/
KEY_SET_ENTER()
 {
        if(Keyset==0){  }
        if(Keyset==1&&Keyenter<2){Keyenter++;limit_max=0;limit_min=1;}
        if(Keyset==1&&Keyenter>=2){Keyset=0;Keyenter=0;limit_min=0;}
 }

/********************所有按键扫描和显示************************************/
void key_scan()
{
	P2=(P2|0x078);
if((P2&0x078)!=0x078)
  {
   Delay1ms(50);   //延时防抖  按下30ms再测
   P2=(P2|0x078);
   if((P2&0x078)!=0x078)
   {
    if(key_up==0)
	{
         //TempKeyV=1;
         KEY_SET_UP();      
	}
   if(key_down==0)
	{
         //TempKeyV=2;
         KEY_SET_DOWN();   
	}
   if(enter==0)
	{
		//TempKeyV=3;
         KEY_SET_ENTER();

	}
    if(menu==0)
	{
	    //TempKeyV=4; 
        Keyset=1; limit_max=1;
	}
       }
  }
}


 main( )
{
	LCD_Initial();
	GotoXY(0,0);
	Print("The 1602LCD Test");

     	TMOD=0x21;
		TH1=0xfd;
		TL1=0xfd;
        SM0=0;
		SM1=1;
		REN=1;
	    EA=1;
	    ES=1;
	    TR1=1;

	while(1)
	{

    Delay1ms(100);
    key_scan();    //扫描按键子程序
    read_temp();     //读取温度

       if(RI==1)
   		{RI=0;
  		aaa=SBUF;
   		  SBUF=temperature;
 		flag1=1;
  	
  		}	
		
		else{ 
		    if(flag1==1)
		      {  
		 	   ES=0;	  
		      	   flag1=0;
		  		   bbb=temperature/10;
				   ccc=bbb*16;
				   ddd=temperature%10+ccc;
				   
				   SBUF=ddd;

		     while(!TI);
		    
		       TI=0;
			   ES=1;
		
		 }	
		}
    

        if(Keyset==1&&limit_max==1)
           { 
             GotoXY(0,1);
             Print("Max:      ");
             ds1820disp(limit_up,&TBuffer[0],13);//显示
             GotoXY(5,1);
             Print(&TBuffer[0]);
            } 
        if(Keyset==1&&limit_min==1)
            { 
             GotoXY(0,1);
             Print("Min:      ");
             ds1820disp(limit_down,&TBuffer[0],13);//显示
             GotoXY(5,1);
             Print(&TBuffer[0]);
             }


    	if(Keyset==0)
             {
			  Utdisp(limit_up);
              Dtdisp(limit_down);
             if(temperature>=limit_up)
                 { alarm=1;led1=0;Delay1ms(80);led1=1;}
             if(temperature<=limit_down)
                 { alarm=1;led2=0;Delay1ms(80);led2=1;}
             if(temperature<=limit_up&&temperature>=limit_down)
                 { alarm=0;led1=1;led2=1;}

              GotoXY(0,1);
              Print("T=         ");    
              ds1820disp(tvalue,&TBuffer[0],13);//显示
              GotoXY(3,1);
              Print(&TBuffer[0]);
			  }		
      }
 }





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -