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

📄 hygs_v4.c

📁 恒压供水控制器恒压供水控制 器V4.0 恒压供水控制器V4.0
💻 C
字号:
#include <AT89X52.h>
#include <math.h>
#include <intrins.h>

#define	CLK P1_4
#define	DIN P1_3
#define	SCL P1_1
#define	SDA P1_0

unsigned char	XSCS=0,XSCS1=0;
unsigned char	xsdh;
bit	temp0,temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp10;
bit	temp0_0,temp0_1,temp0_2,temp0_3,temp0_4,temp0_5,temp0_6,temp0_7,temp0_10;
bit	temp1_0,temp1_1,temp1_2,temp1_3,temp1_4,temp1_5,temp1_6,temp1_7,temp1_10;
//bit	temp_aj1,temp_aj2;    
//bit	temp1_aj1,temp1_aj2;
//bit	temp2_aj1,temp2_aj2;
             
void			display(void);
void			DELAY(unsigned int dltime);
void 			timer0(void);
void 			i2c_write(unsigned char,unsigned char);
void 			i2c_send8bit(unsigned char); 
unsigned char 	i2c_read(unsigned char); 
unsigned char 	i2c_receive8bit(void); 
void 			i2c_start(void); 
void 			i2c_stop(void); 
bit 			i2c_ack(void);
void 			jidianqi(void);
  

void display(void)//将DISP1改为dispaly
{	
	unsigned char i=0;
	P3_6=0;//显示用八上升沿D触发器的时钟输入端 
	if (P3_5==0) //如果有键按下,则使所有LED点亮,这里是按键2	
		xsdh=0xff;//dispout,xsdh是如何跟硬件联系的?它们是间接联系的,
	else if (XSCS==0)//实质是通过后面的while语句控制LED的移位   
		xsdh=0x01;
	else if (XSCS==1)
		xsdh=0x02;
	else if (XSCS==2)
		xsdh=0x04; 
	else if (XSCS==3)
		xsdh=0x08;
	else if (XSCS==4)
		xsdh=0x10;
	else if (XSCS==5)
		xsdh=0x20;
	else if (XSCS==6)
		xsdh=0x40;
	else if (XSCS==7)
	 	xsdh=0x80;
	else 
		xsdh=0;
		
		i=8;   
	while(i>0)
	{
		CLK=0;
        DELAY(3);
		DELAY(20);
	    if((xsdh&0x01)==0)
			DIN=0;
	    else
			DIN=1;
			CLK=1;
			DELAY(3);
			DELAY(20);
			i--;
			xsdh=xsdh>>=1;//右移是干吗?右移其实就是实现了外部LED的移位显示
	}
		P3_6=1;
}
   
void DELAY(unsigned int dltime)
{
	unsigned int m;
	for(m=0;m<dltime;m++)
	{
		;
	}
}

void timer0(void) interrupt 1 //中断源为定时器0
{
	TL0=0X58;//定时器的定时时间为50ms
	TH0=0X9e;
	if (XSCS1<20) 
		XSCS1++;   
	else XSCS1=0;
	if (XSCS1==3) 
		{
		if (XSCS<16) 
		XSCS++;	      
		else XSCS=0;  
		}				  
	if (P3_4==0) //如果有键按下,则使所有LED点亮,这里是按键1*******
		{		 //为什么对按键2的写法和按键1的写法不一样?因为按键1是通过单片机 	
		SBUF=0xff; //内部专用的串行口进行移位控制的
		while (TI==0)
		;
		}			   
	else if (XSCS==8)
		{
		SBUF=0x1;
		while (TI==0)
		;
		}
	else if (XSCS==9)
		{
		SBUF=0x2;
		while (TI==0)
		;
		}
	else if (XSCS==10)
		{
		SBUF=0x4;
		while (TI==0)
		;
		}
	else if (XSCS==11)
		{
		SBUF=0x8;
		while (TI==0)
		;
		}		      
	else if (XSCS==12)
		{
		SBUF=0x10;
		while (TI==0)
		;
		}
	else if (XSCS==13)
		{
		SBUF=0x20;
		while (TI==0)
		;
		}
	else if (XSCS==14)
		{
		SBUF=0x40;
		while (TI==0)
		;
		}
	else if (XSCS==15)
		{
		SBUF=0x80;
		while (TI==0)
		;
		} 
		else SBUF=0;
}			  
				
void i2c_write(unsigned char Address,unsigned char Data) 
{ 
	do
		{ 
			i2c_start();//发送开始信号
			i2c_send8bit(0xA0); //送8位数据,A0表示写

		}	while(i2c_ack()); //当有发送接收确认信号时
			i2c_send8bit(Address); // Address=0x00
			i2c_ack();    
			i2c_send8bit(Data);   
			i2c_ack(); 
			i2c_stop(); 
			return; 
}

unsigned char i2c_read(unsigned char Address) 
{ 
	unsigned char c;
	do
	{ 
		i2c_start(); 
		i2c_send8bit(0xA0); 
  	}while(i2c_ack()); //=1,表示无确认,再次发送 
		i2c_send8bit(Address); 
		i2c_ack();  
	do
	{ 
		i2c_start(); 
		i2c_send8bit(0xA1); 
	}while(i2c_ack()); 
	c=i2c_receive8bit();
	i2c_ack(); 
	i2c_stop(); 
	return(c); 
} 

//发送开始信号 
void i2c_start(void) 
{ 
	SDA = 1; 
	SCL = 1; 
	SDA = 0; 
	SCL = 0; //SCL下降信号比SDA来的晚一些
	return; 
} 

//发送结束信号 
void i2c_stop(void) 
{ 
	SDA = 0; 
	SCL = 1; 
	SDA = 1; 
	return; 
}
	 
//发送接收确认信号 
bit i2c_ack(void) 
{ 
	bit ack; 
	SDA = 1; 
	SCL = 1; 
	if (SDA==1) 
		ack = 1; 
	else 
		ack = 0; 
		SCL = 0; 
	return (ack);//返回ack值为1说明已确认 
} 

//送八位数据 
void i2c_send8bit(unsigned char b) 
{ 
	unsigned char a; 
	for(a=0;a<8;a++) 
	{ 
		if ((b << a ) & 0x80) 
			SDA = 1; 
		else 
			SDA = 0; 
		SCL = 1; 
		SCL = 0; 
	} 
	return; 
} 

//接收八位数据 
unsigned char i2c_receive8bit(void) 
{ 
	unsigned char a; 
	unsigned char b=0; 
	for(a=0;a<8;a++) 
	{ 
		SCL = 1; 
		b=b<<1; 
		if (SDA==1) 
		b=b|0x01; //按位或 
		SCL = 0; 
	} 
	return (b); 
}
	
void jidianqi(void)
{
	temp0=P2_0;
	temp1=P2_1;
	temp2=P2_2;
	temp3=P2_3;
	temp4=P2_4;
	temp5=P2_5;
	temp6=P2_6;
	temp7=P2_7;
	temp10=P3_2;
	DELAY(200);
	temp0_0=P2_0;
	temp0_1=P2_1;
	temp0_2=P2_2;
	temp0_3=P2_3;
	temp0_4=P2_4;
	temp0_5=P2_5;
	temp0_6=P2_6;
	temp0_7=P2_7;
	temp0_10=P3_2;
	DELAY(2000);
	temp1_0=P2_0;
	temp1_1=P2_1;
	temp1_2=P2_2;
	temp1_3=P2_3;
	temp1_4=P2_4;
	temp1_5=P2_5;
	temp1_6=P2_6;
	temp1_7=P2_7;
	temp1_10=P3_2;
	DELAY(2000);
	P3_7=0;
	DELAY(200);
    if	((temp0==temp0_0)&&(temp0==temp1_0)
			&&(temp1==temp0_1)&&(temp1==temp1_1)
			&&(temp2==temp0_2)&&(temp2==temp1_2)
			&&(temp3==temp0_3)&&(temp3==temp1_3)
			&&(temp4==temp0_4)&&(temp4==temp1_4)
			&&(temp5==temp0_5)&&(temp5==temp1_5)
			&&(temp6==temp0_6)&&(temp6==temp1_6)
			&&(temp7==temp0_7)&&(temp7==temp1_7)
		)
	{
		P0_4=temp0;
		P0_6=temp1;
		P0_5=temp2;
		P0_3=temp3;
		P0_2=temp4;
		P0_7=temp5;
		P0_1=temp6;
		P0_0=temp7;
		if (temp10==0x00)
		{   
			P0=0x00;
			P3_7=0;
			DELAY(2000);
			P3_7=1;
			display();
		}
	} 
	P3_7=1;
	display();
}

//$$$$$$$$$$$$$$$$主程序$$$$$$$$$$$$$$$
void main(void)	   
{
	char lunliu;
	TMOD=0x01;//选择定时器T0方式1
	TL0=0xAF;//延时0.1秒
	TH0=0x3C;
	EA=1;
	ET0=1;
	TR0=1;
	P0=0x00;
	SCON=0x00;
	while (1)
	{ 
 		jidianqi();	 
		//如果按键1和按键2一起按下,就对EEPROM进行写和读的操作,
		//并通过8个继电器的轮流闭开一次体现
		if ((P3_4==0x00)&&(P3_5==0x00))//EEPROM的写入不能太频繁,当有按键按下时才写入数据											   
		{			   
			unsigned char i; 
			for (i=1;i>0;i--)
 			{
				i2c_write(0x00,0x66);
				_nop_();
				lunliu=i2c_read(0x00);
    		}
			if (lunliu==0x66)
			{	
				P0=0x00;
				DELAY(1000);
				P3_7=0;
				DELAY(1000);
				P3_7=1;
	       		display();
				DELAY(38000);

		   		P0=0xff;
				DELAY(1000);
            	P3_7=0;
		    	DELAY(1000);
		    	P3_7=1;
            	display();
				DELAY(38000);
			}
		} 
	}
}

⌨️ 快捷键说明

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