블루 브리지 컵 단편기 CT107D20_지능 조명 컨트롤러

구동:
//IIC.h
#include
sbit SDA = P2^1;      //   
sbit SCL = P2^0;      //   

void Delay_IIC(unsigned char t);        //    
void IIC_Start(void);                   //    
void IIC_Stop(void);                    //    
void IIC_Ack(unsigned char ackbit);     //    
bit IIC_WaitAck(void);                  //    
void IIC_SendByte(unsigned char byte);	//    
unsigned char IIC_RecByte(void);        //    

//IIC.c
#include

void Delay_IIC(unsigned char time)
{
     
	while(time--);
}

void IIC_Start(void)
{
     
	SDA = 1;
	SCL = 1;
	Delay_IIC(5);
	SDA = 0;        // SCL     ,SDA    
	Delay_IIC(5);
	SCL = 0;	
}

void IIC_Stop(void)
{
     
	SDA = 0;
	SCL = 1;
	Delay_IIC(5);
	SDA = 1;	// SCL     ,SDA    
	Delay_IIC(5);
}

void IIC_Ack(unsigned char ackbit)
{
     
	if(ackbit) 	
		SDA = 0;	//      
	else 
		SDA = 1;	//       
	Delay_IIC(5);
	SCL = 1;
	Delay_IIC(5);	        // 9     
        SCL = 0;
	SDA = 1; 		//  SDA 
	Delay_IIC(5);
}

bit IIC_WaitAck(void)
{
     
	SDA = 1;
	Delay_IIC(5);
	SCL = 1;
	Delay_IIC(5);	
 
	if(SDA)    		// SCL     ,SDA    ,     。
	{
        
		SCL = 0;
		IIC_Stop();
		return 0;
	}
	else 			// SCL     ,SDA    ,     。
	{
      
		SCL = 0;
		return 1;
	}	
}

void IIC_SendByte(unsigned char byt)
{
     
	unsigned char i;
	for(i=0;i<8;i++)		//    8   
	{
        
		if(byt & 0x80) 		//       
		{
     	
			SDA = 1;
		}
		else 			//       
		{
     
			SDA = 0;
		}
 
		Delay_IIC(5);
		SCL = 1;		//SCL     ,SDA        
		byt <<= 1;		//       ,       
		Delay_IIC(5);		//  SDA      
		SCL = 0;
	}
}

unsigned char IIC_RecByte(void)
{
     
	unsigned char da;
	unsigned char i;
	for(i=0;i<8;i++)
	{
        
		SCL = 1;
		Delay_IIC(5);		// SCL     ,  SDA   
		da <<= 1;
		if(SDA) 
			da |= 0x01;
		SCL = 0;
		Delay_IIC(5);
	}
	return da;
}


마스터 프로그램:
#include
#include"IIC.h"

sbit S4 = P3^3;
sbit S5 = P3^2;          //      
unsigned char level;  	 //    
unsigned char voltage;   //Rd1  
unsigned char record_l;  //          
unsigned char record_v;  //  Rd1      
unsigned char code SEG_code[18] = 
	{
     0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
	 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
	 0xbf,0x7f};	     //           :0~F,—,.

void Select_HC138(unsigned char n)
{
     
	switch(n)
	{
     
		case 0:
			P2 = (P2 & 0x1f) | 0x00;
			break;
		case 4:
			P2 = (P2 & 0x1f) | 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
			break;
	}
}

void Delay(unsigned int t)
{
     
	while(t--);
}

void Show_tube(unsigned char position,unsigned char value)
{
     
	Select_HC138(7);
	P0 = 0xff;
	Select_HC138(6);
	P0 = 0x01 << position;
	Select_HC138(7);
	P0 = value;
}

void Show_all()
{
     
	Select_HC138(7);
	P0 = 0xff;
	Select_HC138(6);
	P0 = 0xff;
}

void Display_tube(unsigned char led,unsigned char rd1)
{
     
	Show_tube(0,SEG_code[16]);
	Delay(100);
	Show_tube(1,SEG_code[led]);
	Delay(100);
	Show_tube(2,SEG_code[16]);
	Delay(100);

	Show_tube(5,SEG_code[rd1/100]);
	Delay(100);
	Show_tube(6,SEG_code[(rd1/10)%10]);
	Delay(100);
	Show_tube(7,SEG_code[rd1%10]);
	Delay(100);

	Show_all();
}

void Read_Rd1()
{
     
	IIC_Start();             //IIC      
	IIC_SendByte(0x90);		 //     
	IIC_WaitAck();           //      
	IIC_SendByte(0x01);      //    
	IIC_WaitAck();           //      
	IIC_Stop();              //IIC      

	IIC_Start();               //IIC      
	IIC_SendByte(0x91);        //     
	IIC_WaitAck();             //      
	voltage = IIC_RecByte();   //  PCF8591  1   
	IIC_Ack(0);                //         
	IIC_Stop();                //IIC      
}

void Write_24C02(unsigned char addr,unsigned char dat)
{
     
	IIC_Start();      		 //IIC      
	IIC_SendByte(0xa0);      //     
	IIC_WaitAck();           //      
	IIC_SendByte(addr);      //       
	IIC_WaitAck();           //      
	IIC_SendByte(dat);       //      
	IIC_WaitAck();           //      
	IIC_Stop();              //IIC      
}  

unsigned char Read_24C02(unsigned char addr)
{
     
	unsigned char temp;
	//        
	IIC_Start();   			 //IIC      
	IIC_SendByte(0xa0);      //     
	IIC_WaitAck();           //      
	IIC_SendByte(addr);      //       
	IIC_WaitAck();           //      
	
	IIC_Start();             //IIC      
	IIC_SendByte(0xa1);      //     
	IIC_WaitAck();           //      
	temp = IIC_RecByte();    //      
	IIC_Ack(0);              //         
	IIC_Stop();              //IIC      
	return temp;
}

void Led_light()
{
     
	Read_Rd1();
	if(voltage>200 && voltage<=255)
	{
     
		level = 1;
		Select_HC138(4);
		P0 = 0xfe;	         //  L1
		Select_HC138(0);
	}
	else if(voltage>150 && voltage<=200)
	{
     
		level = 2;
		Select_HC138(4);
		P0 = 0xfc;			 //  L1、L2
		Select_HC138(0);
	}
	else if(voltage>100 && voltage<=150)
	{
     
		level = 3;
		Select_HC138(4);
		P0 = 0xf0;			 //  L1~L4
		Select_HC138(0);
	}
	else if(voltage<=100)
	{
     
		level = 4;
		Select_HC138(4);
		P0 = 0x00;			 //  L1~L8
		Select_HC138(0);
	}
}

void Access()
{
     
	Write_24C02(0x01,level);      //  level
	Delay(1000);
	Write_24C02(0x02,voltage);	  //  voltage
	Delay(1000);
}

void Show()
{
     
	record_l = Read_24C02(0x01);
	record_v = Read_24C02(0x02);
}

void Delay_keys()
{
     
	unsigned char i,j;
	i = 108;
	j = 145;
	do
	{
     
		while(--j);
	}while(--i);
}

void Press_keys()
{
     
	if(S4 == 0)
	{
     
		Delay_keys();
		if(S4 == 0)
		{
     
			Access();	  //  
			while(S4 == 0)
			{
     
				Display_tube(level,voltage);
			}
		}
	}

	else if(S5 == 0)
	{
     
		Delay_keys();
		if(S5 == 0)
		{
     
			Show();
			while(S5 == 0)
			{
     
				Display_tube(record_l,record_v);
			}
		}
	}
}

void main()
{
     
	Select_HC138(5);
	P0 = 0x00;           //         
	Select_HC138(0);
	while(1)
	{
     
		Led_light();
		Display_tube(level,voltage);
		Press_keys();
	}
}

좋은 웹페이지 즐겨찾기