블루 브리지 컵 단편기 CT107D18_모듈 변환기 PCF8591의 기본 응용

#include
#include"IIC.h"

sbit S4 = P3^3;               //      
unsigned char dat_Rd1 = 0;	  //  Rd1  
unsigned char dat_Rb2 = 0;	  //  Rb2  
unsigned char state = 0;	  //   
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 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
			break;
	}
}

void Delay_tube(unsigned char 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 channel,unsigned char dat)
{
     
	Show_tube(0,SEG_code[16]);
	Delay_tube(100);
	Show_tube(1,SEG_code[channel]);	      //    
	Delay_tube(100);
	Show_tube(2,SEG_code[16]);
	Delay_tube(100);

	Show_tube(5,SEG_code[dat/100]);       //       
	Delay_tube(100);
	Show_tube(6,SEG_code[(dat/10)%10]);	  //       
	Delay_tube(100);
	Show_tube(7,SEG_code[dat%10]);        //       
	Delay_tube(100);

	Show_all();
}

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)
		{
     
			if(state == 0)
			{
     
				state = 1;        //  Rb2
			}
			
			else if(state == 1)
			{
     
				state = 0;		  //  Rd1
			}
		}
		while(S4 == 0);
	}
}

void Read_Rd1()         //      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();            //      
	dat_Rd1 = IIC_RecByte();  //  PCF8591  1   
	IIC_Ack(0);               //       
	IIC_Stop();               //IIC      
}

void Read_Rb2()			//     Rb2   
{
     
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);
	IIC_WaitAck();
	IIC_Stop();

	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	dat_Rb2 = IIC_RecByte();
	IIC_Ack(0);
	IIC_Stop();
}

void main()
{
     
	Select_HC138(5);
	P0 = 0x00;       //         
	while(1)
	{
     
		Press_keys();
		switch(state)
		{
     
			case 0:
				Read_Rd1();
				Display_tube(1,dat_Rd1);
				break;
			case 1:
				Read_Rb2();
				Display_tube(3,dat_Rb2);
				break;
		}
	}
}
		

좋은 웹페이지 즐겨찾기