Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Nor Ain Kasmuri
11/25/13 18:26
Read: 1074 times


 
#190165 - LED connection to PCF8575
Responding to: Bert Van Den Berg's previous message
Hi ,

For the PCF8575 pin A0, A1 and A2 connected to ground. So that the slave address (0x40).
The LED connected to the 16 P-Port of PCF8575 (P0-P7) and (P10-P17) with the limiting resistor of 470R.
But i may do wrong in sending the address byte and the data byte?

Thank you.

Regards,
Nor

Below is the code :

...insert code here[#include <REG51F.H>
#include <stdio.h>
#include <INPUT_OUTPUT.h>
#include <delay1.h>

// Function declarations
void initRS(); 												
void initPort();											
void I2C_Init();
void I2C_Start();
void I2C_Restart();
void I2C_Stop();
void I2C_SendAck();
void I2C_SendNack();
unsigned char I2C_Write(unsigned char Data);
unsigned char I2C_Read();


#define SDA p1_0
#define SCL p1_1

//Main function
void main()
{
	initRS();
	initPort();
	I2C_Init();
	
	//unsigned char RxByte = 0;

	I2C_Start();
	I2C_Write(0x40);
	I2C_Write(0xAA);
	I2C_Write(0xF0);
	I2C_Read();
	I2C_SendAck();
	I2C_Stop();

}


//Function : I2C Write Byte	transfer 3 bytes

unsigned char I2C_Write(unsigned char Data)
	{
	unsigned char i;		// Variable to be used in for loop
	
	for(i=0;i<8;i++)		// Repeat for every bit
	{
		SCL = 0;		// Make SCL pin low
		delay1();		  // Data pin should change it's value,
									// when it is confirm that SCL is low

		if ((Data & 0x40) == 0)			  //
			SDA = 1;
		else
			SDA = 0;
			delay1();
		SCL = 1;
		Data<<=1;
	 }

	//Get ACK from slave
	 SCL = 0;
	 SDA = 1;
	 delay1();
	 SCL = 1;
	 SCL = 0;
	 return SDA;

	//

	for(i=0;i<8;i++)		// Repeat for every bit
	{
		SCL = 0;		// Make SCL pin low
		delay1();		  // Data pin should change it's value,
									// when it is confirm that SCL is low

		if ((Data & 0xAA) == 0)			  //10101010
			SDA = 1;
		else
			SDA = 0;
			delay1();
		SCL = 1;
		Data<<=1;
	 }

	//Get ACK from slave
	 SCL = 0;
	 SDA = 1;
	 delay1();
	 SCL = 1;
	 SCL = 0;
	 return SDA;

	 //

	 for(i=0;i<8;i++)		// Repeat for every bit
	{
		SCL = 0;		// Make SCL pin low
		delay1();		  // Data pin should change it's value,
									// when it is confirm that SCL is low

		if ((Data & 0xF0) == 0)			  //11110000
			SDA = 1;
		else
			SDA = 0;
			delay1();
		SCL = 1;
		Data<<=1;
	 }

	//Get ACK from slave
	 SCL = 0;
	 SDA = 1;
	 delay1();
	 SCL = 1;
	 SCL = 0;
	 return SDA	;
	}  
		
		

//	Function : I2C Read Byte
	unsigned char I2C_Read()
	{
	unsigned char i, Data=0;
	for(i=0;i<8;i++){
		SCL = 0;
		SCL = 1;
		delay1();
		if(SDA)
			Data |=1;
		if(i<7)
			Data<<=1;
	}
	SCL = 0;
	SDA = 1;
	return Data;
	}
		


// Function : To set initial values of SDA and SCL
void I2C_Init()
{
	SDA = 1;				// Set SDA pin HIGH
	SCL = 1;			    // Set SCL pin LOW
}

// Function : Master sending START condition, HIGH to LOW transition on SDA while SCL keeping high
void I2C_Start()
{
	SCL = 1;
	SDA = 1;
	delay1();
	SDA = 0;
	SCL =1;
	delay1();
}

void I2C_Restart()
{
	SDA = 1;
	SCL = 1;
	SDA = 0;
}

void I2C_Stop()
{
	SDA = 0;
	SCL = 1;
	SDA = 1;
}

void I2C_Ack()
{
	SDA = 0;
	SCL = 1;
	SCL = 0;
}

void I2C_Nack()
{
	SDA = 1;
	SCL = 1;
	SCL = 0;
}

// Initialize serial port 
void initRS ()
{
    SCON  = 0x50;		        // SCON: mode 1, 8-bit UART, enable rcvr      
    TMOD  = 0x20;               // TMOD: timer 1, mode 2, 8-bit reload        
    TH1   = 253;                // TH1:  reload value for 9600 baud @ 11.0592MHz   FDH
    TR1   = 1;                  // TR1:  timer 1 run                          
    TI    = 1;                 	// TI:   set TI to send first char of UART    
}

//Initialize port
void initPort()
{
	P1 = 0xFF;                 
	P2 = 0x00;
	P0 = 0x00;
}]





List of 6 messages in thread
TopicAuthorDate
i2c : PCF8575 interface with 8051 microcontroller      NOR AIN KASMURI      11/25/13 08:47      
   did you notice      Erik Malund      11/25/13 09:07      
   How did you connect the LED's?      Bert Van Den Berg      11/25/13 10:50      
      LED connection to PCF8575      NOR AIN KASMURI      11/25/13 18:26      
         what happened to MSB of data?      Erik Malund      11/25/13 20:26      
            Write_Byte      NOR AIN KASMURI      11/26/13 02:40      

Back to Subject List