Demo entry 6354096

   

Submitted by anonymous on Apr 04, 2017 at 20:43
Language: C. Code size: 5.4 kB.

#include <reg52.h>  
#include <intrins.h>
#include <stdio.h>  
#include <math.h>

#define uchar unsigned char   
#define uint unsigned int 

uchar code table[]=             //数字对应16进制编码
{
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71
};
uchar hour,minute,second;
uchar t=0,state=0;
uint tem,hum;
uint tem1=0,tem2=0,tem3=0;
uint hum1=0,hum2=0,hum3=0;

sbit led1=P0^5;               //单片机接口设置
sbit led2=P0^4;
sbit led3=P0^3;
sbit led4=P0^2;
sbit led5=P0^1;
sbit led6=P0^0;

sbit inte0=P3^2;
sbit inte1=P3^3;

sbit SCL=P0^6;
sbit SDA=P0^7;

void delay()      //单片机空跑延时
{                 
	_nop_();        
	_nop_();
}

void init()      //设置初始状态
{
     
   TMOD = 0x01;    
   TH0=0x3C;       
   TL0=0x0B0; 

   EA = 1;
   TR0=1;     
   ET0=1; 

   EX0=1;
   IT0=1;
   EX1=1;
   IT1=1;
   
   SCL=1;     
   delay();                   
   SDA=1;
   delay();
}

void delayms (uint x)       //1ms delay
{    
	uint i,j;   
	for(i=x;i>0;i--)  
		for(j=110;j>0;j--); 
}

void printtime()           //显示时间函数
{
	led1=0;
	P2=table[hour/10];
//	P0=0xff;
	delayms(1);
//	led1=1;
	P0=0xff;
   
	led2=0;
	P2=table[hour%10]|0x80;
//	P0=0xff;
	delayms(1);
//	led2=1;
	P0=0xff;
   
	led3=0;
	P2=table[minute/10];
//	P0=0xff;
	delayms(1);
//	led3=1;
	P0=0xff;
   
	led4=0;
	P2=table[minute%10]|0x80;
//	P0=0xff;
	delayms(1);
//	led4=1;
	P0=0xff;
   
	led5=0;
	P2=table[second/10];
//	P0=0xff;
	delayms(1);
//	led5=1;
	P0=0xff;
	
	led6=0;
	P2=table[second%10];
//	P0=0xff;
	delayms(1);
//	led6=1;
	P0=0xff;
}


//I2C数据传输

void start() 
{ 
	SDA=1; 
	delay(); 
	SCL=1; 
	delay();
	SDA=0; 
	delay(); 
}                 

void response()
{
	uchar countres=0;
	SCL=1;
	delay();
	while((SDA==1)&&(countres<255))
		countres++;
	SCL=0;
	delay();
}

void stop()
{ 
	SDA=0; 
	delay();
	SCL=1; 
	delay();
	SDA=1; 
	delay();          
}

void writebyte(uchar value) 
{    
		unsigned char i;     
		for (i=0x80;i>0;i>>=1)  
		{   
			SCL=0;  
			delay();   
			if(i&value)
			{    
				SDA=1;  
			}    
			else
			{   
				SDA=0; 
			} 
			delay();   
			SCL=1; 
			delay();     
		} 
		SCL=0;  
		delay(); 
		SDA=1;   
		delay();                        
} 

char readbyte(bit ack) 
{  
	uchar i,val=0; 
	SDA=1;  
	delay(); 
	for (i=0x80;i>0;i>>=1)   
	{  
		SCL=1;            
		delay();      
		if (SDA)
			val=(val | i);         
		SCL=0;   
		delay();       
	}     
	SDA =! ack;              
	SCL=1;       
	delay();              
	SCL=0;  
	delay();    
	SDA=1;    
	return val; 
} 

void printth()           //输出温度与湿度函数
{
	
	 
	led1=0;
	P2=table[tem1];
//	P0=0xff;
	delayms(1);
	P0=0xff;
   
	led2=0;
	P2=table[tem2]|0x80;
//	P0=0xff;
	delayms(1);
	P0=0xff;
   
	led3=0;
	P2=table[tem3];
//	P0=0xff;
	delayms(1);
	P0=0xff;
   
	led4=0;
	P2=table[hum1];
//	P0=0xff;
	delayms(1);
	P0=0xff;
   
	led5=0;
	P2=table[hum2]|0x80;
//	P0=0xff;
	delayms(1);
	P0=0xff;
	
	led6=0;
	P2=table[hum3];
//	P0=0xff;
	delayms(1);
	P0=0xff;
	
}

void temperature()      //测定温度函数
{
	uchar MSB,LSB;
	uint i=0;   
//	for(i=x;i>0;i--)
	float a;
	start();
	writebyte(0x80);
	response();
	writebyte(0xE3);  
	response();
	start();
	writebyte(0x81);
	response();	
//	delayms(100);
	for(i=0;i<20;i++)
	{
		printth();
	}
	MSB=readbyte(1);
	LSB=readbyte(0);
	stop();
	LSB=(LSB/4)*4;		
	a=-46.85+175.72*(((float)(MSB)*256+(float)(LSB))/65536); 
	tem=(uint)(a*10);
	tem1=tem/100;
	tem2=(tem-tem1*100)/10;
	tem3=tem-tem1*100-tem2*10;
	
}

void humidity()         //测定湿度函数
{
	uchar MSB,LSB;
	uint i=0;
	float a;
	start();
	writebyte(0x80);
	response();
	writebyte(0xE5);  
	response();
	start();
	writebyte(0x81);
	response();	
//	delayms(100);
	for(i=0;i<20;i++)
	{
		printth();
	}
	MSB=readbyte(1);
	LSB=readbyte(0);
	stop();
	LSB=(LSB/4)*4;		
	a=-6+125*(((float)(MSB)*256+(float)(LSB))/65536); 
	hum=(uint)(a*10);
	hum1=hum/100;
	hum2=(hum-hum1*100)/10;
	hum3=hum-hum1*100-hum2*10;
}



void changestate() interrupt 0     //外部中断改变显示状态
{
   delayms(10);
   if(inte0==0)                  //去抖
   {
      state++;
      if(state==5)
      {
	 state=0;
      }
		while(!inte0);
   }
}

void changenumber() interrupt 2     //外部调节时钟
{
   delayms(10);
   if(inte1==0)              //去抖
   {
      switch(state)
      {
				 case 0: 
						break;
				 case 1: 
						second++;
						if(second==60)
							second=0;
						break;
				 case 2:
						minute++;
						if(minute==60)
							minute=0;
						break;
				 case 3:
						hour++;
						if(hour==24)
							hour=0;
						break;
				 case 4:
						break;
      }
			while(!inte1);
   }
}

void timecyc() interrupt 1       //内部定时器记录时间
{
    TH0=0x3C;
    TL0=0x0B0;
    t++;
    if(t==20)
    {
    	t=0;
    	second++;
    	if(second==60)
    	{
    		second=0;
    		minute++;
    		if(minute==60)
    		{
    			minute=0;
    			hour++;
    			if(hour==24)
    			{
    				hour=0;
    			}
    		}

    	}
    }

}

	
void main()         
{
	init();
	while(1)
	{
		if(state==4)
		{
			temperature();
			humidity();
			
		}
		else
		{
			printtime();
		}
	}
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).