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

📄 机箱温控.txt

📁 一般机箱的51单片机温控程序设计
💻 TXT
字号:
#include <REG52.H>
#include <INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define LCM_Data P0
#define Busy 0x80 
sbit LCM_RW=P2^1; 
sbit LCM_RS=P2^0;
sbit LCM_E=P2^2;
sbit DQ=P2^7;
sbit P1_0=P1^0;
sbit P3_2=P3^2;
sbit P3_3=P3^3;
unsigned char temper;
unsigned char ReadStatusLCM(void)
{
LCM_Data = 0xff;      
LCM_RS=0;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
while (LCM_Data & Busy);  
return(LCM_Data);
}
void Delay5Ms(void)
{
unsigned int TempCyc = 6555;
while(TempCyc--);
}

void WriteDataLCM(unsigned char WDLCM)
{
ReadStatusLCM(); 
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0;  
LCM_E = 0;  
LCM_E = 1;
}
 
void WriteCommandLCM(unsigned char WCLCM,BuysC) 
{if (BuysC) ReadStatusLCM();  
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0; 
LCM_E = 0;
LCM_E = 0;
LCM_E = 1; 
}

void LCMInit(void)  
{
LCM_Data = 0;
WriteCommandLCM(0x38,0);  
Delay5Ms(); 
WriteCommandLCM(0x38,0);
Delay5Ms(); 
WriteCommandLCM(0x38,0);
Delay5Ms(); 
WriteCommandLCM(0x38,1); 
WriteCommandLCM(0x08,1);  
WriteCommandLCM(0x01,1);  
WriteCommandLCM(0x06,1);  
WriteCommandLCM(0x0F,1); 
 }

void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF;  
if (Y) X |= 0x40; 
X |= 0x80;  
WriteCommandLCM(X, 1);  
WriteDataLCM(DData);
}

void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength,i;

ListLength = 0;
while(DData[ListLength]>31) ListLength=ListLength+1;
for(i=0;i<ListLength;i++)
  {  DisplayOneChar(X++,Y,DData[i]);
    if(X==16)
     {
     X=0;Y=Y^1;
    }
   }
}

void Delay400Ms(void)//400ms延时
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
}
}

bit reset(void)
{    bit s;
    unsigned char i;
    DQ=1;
	_nop_();
    DQ=0;
    i=250;
	while(--i);
    DQ=1;
    i=15;
    while(--i);//100us
    s=DQ;
	DQ=1;
    i=110;
    while(--i); 
	return(s);
 
}

void writecomm(unsigned char command)
{
   unsigned char i;
   unsigned char j;
   for(i=0;i<8;i++)
 {
   DQ=1;
   DQ=0;
   j=5;
   while(--j);
   DQ=(bit)(command&0x01);
   j=35;
   while(--j);
    DQ=1;
	command=command>>1;
	_nop_();
 }
   
}

unsigned char readdata(void)
{
   unsigned char i;
   unsigned char j;
   unsigned char temp;
   temp=0;
   DQ=1;
  for(i=0;i<8;i++)
 {
    temp=temp>>1;
    DQ=0;
     _nop_();
     _nop_();
     DQ=1;
    j=6;
	while(--j);//8us
	_nop_();
 
     if(DQ==1)
        temp=temp|0x80;
     DQ=1;
     j=20;
     while(--j);//60us
    }
   return(temp);
 }

//初始化
void system_init(void)
{
EA=1;
EX1=1;
EX0=1;
IT1=0;
IT0=0;
}

//延时
void delayd(unsigned int N)
{
int i=N;
while(--i);
}

//int0
void INT0_intrupt() interrupt 0 using 1
{
EA=0;
delayd(1000);
if(P3_2==0)
{
while(P3_2==0);
delayd(1000);
temper++;
}
EA=1;
}

//int1
void INT1_intrupt() interrupt 2 using 1
{
EA=0;
delayd(1000);
if(P3_3==0)
{
while(P3_3==0);
delayd(1000);
temper--;
}

EA=1;
}



void main(void)
{
	 unsigned char resultt,simm=0,i;
    int mid;
    unsigned char rddata[2],result,x;//
      bit sflag;
	  temper=38;
	  P1_0=0;      
      reset();  
	  
	       
     writecomm(0xCC);  
	 writecomm(0x44);   
	  Delay400Ms();    
      LCMInit();        
       Delay5Ms();    


 system_init();




	  while(1)
	{  
	 Delay400Ms(); //启动等待,等LCM讲入工作状态





	reset();

	writecomm(0xCC);  //跳过ROM


     writecomm(0xBE);  //读取温度值




    rddata[0]=readdata();  //存放温度低四位
	rddata[1]=readdata();   //存放温度高四位


    sflag=0;               //设定温度为正的



   if((rddata[1] & 0xf8)!=0x00)  //判断高字节的高位是否为1 1表示为负的温度
  {
    sflag=1;   //fu de 
    rddata[1]=~rddata[1]; //负的温度是存放为补码 要得到原码先取反
    rddata[0]=~rddata[0]; //取反
    result=rddata[0]+1;    //低位加一
    rddata[0]=result;     



    if(result>255)         //如果rddata[0]取反大于255,则要进位
       rddata[1]=rddata[1]+1; 
  }




   rddata[1]=rddata[1]<<4;     //去除高五位,低三位为温度值
   rddata[1]=(rddata[1]&0x70);
    x=rddata[0];          //将rddata[0]的高四位和rddata[1]的低三位合并成温度的整数部分
     x=x>>4;          //向低移四位
     x=x&0x0f;       //将高四位置0



     rddata[1]=rddata[1]|x;   //得到温度的整数部分

	if(rddata[1]>temper){  P1_0=0;		i++; 
	 switch(i){
	
	case 0: DisplayListChar(10,0,"      ");	break;
	case 1: DisplayListChar(10,0,"=     ");	break;
	case 2: DisplayListChar(10,0," =    ");	break;
	case 3: DisplayListChar(10,0,"  =   ");	break;
	case 4: DisplayListChar(10,0,"   =  ");	break;
	case 5: DisplayListChar(10,0,"    = ");	break;
	case 6: DisplayListChar(10,0,"     =");	break;
	default: i=0;break;

	}	}
	else if(rddata[1]<temper) {P1_0=1;DisplayListChar(11,0,"||||||");}



    result=rddata[1]/100;  //取得温度整数部分的百位

   DisplayOneChar(0,0,result+48);  //显示



   x=rddata[1]-100*result;       //求余
   result=x/10;           //十位
    DisplayOneChar(1,0,result+48);


	result=x-10*result;      //求余

	 DisplayOneChar(2,0,result+48); //个位

	 DisplayOneChar(3,0,'.');  
	 
    rddata[0]=rddata[0]&0x0f;  //rddata[0]的低四位为小数部分
     mid=rddata[0]*625;       //本应乘0.0625,现在乘0.625,将小数第一位调到个位
     result=mid/1000;         //  取得个位
     mid=mid-result*1000;      //   求得余数
     DisplayOneChar(4,0,result+48);
     result=mid/100;
     DisplayOneChar(5,0,result+48);


	

	 DisplayOneChar(6,0,128);
	 DisplayOneChar(7,0,223);

	 DisplayOneChar(8,0,67);
	  DisplayOneChar(9,0,128);
        DisplayOneChar(13,1,128);
       DisplayOneChar(0,1,128);	 
      DisplayListChar(0,1," Compare TO:    ");	 
   resultt=temper/10;
 DisplayOneChar(14,1,resultt+48);  //显示
  resultt=temper%10;
  DisplayOneChar(15,1,resultt+48);  //显示	 
	
	     reset();

    writecomm(0xCC);

 writecomm(0x44);	//	 启动在线DS1280做温度A/D转换。

}
}

⌨️ 快捷键说明

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