int main(void)
{
IOX_Init(0); //Board Initializations
StartCondition();
// WriteByteToIOX(0x62); //The writing commands which have been successful
// halMcuWaitUs(30);
WriteIntoPortB(); //Got a free port on the controller, connected it with the outputs of the PCF8574A
ReadByteFromIOX(); //The problematic scheme
StopCondition();
while(1);
}
void IOX_Init(S_DB_Interface_t *interface)
{
//Whatever
}
void StartCondition(void)
{
//Whatever
}
void StopCondition(void)
{
//Whatever
}
void WriteIntoPortB(void)
{
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_PIN_1);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_PIN_5);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6, 0);
}
void ReadByteFromIOX(void)
{
static unsigned char NewByte;
unsigned char x;
char i;
for(i = 0; i < 8; i++)
{
WriteBitToIOX(ReadSlaveAdd & 0x80); //Send the read add, 0x79, the A2 is one, Both A1 and A0 are zeros
ReadSlaveAdd <<= 1;
}
//Drive SCL LOW
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0);
halMcuWaitUs(10);
//Drive SDA HIGH
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
halMcuWaitUs(30);
//Drive SCL HIGH
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, GPIO_PIN_6);
halMcuWaitUs(30);
//Change the direction of SDA pin into input
GPIODirModeSet(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_DIR_MODE_IN);
for (i = 0; i < 8; i++)
{
x = ReadBitFromIOX();
if(x)
{
NewByte <<= 1;
NewByte |= 0x01;
}
else
NewByte <<= 1;
}
//Drive SCL LOW
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0);
halMcuWaitUs(10);
//Drive SDA LOW
//As a receiver, the master should pull the SDA LOW to inform the ack
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, 0);
halMcuWaitUs(30);
//Drive SCL HIGH
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, GPIO_PIN_6);
halMcuWaitUs(30);
}
unsigned char ReadBitFromIOX(void)
{
unsigned char NewBit;
//Drive SCL LOW
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0);
halMcuWaitUs(30);
NewBit = GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) & 0x08;
halMcuWaitUs(60);
//Drive SCL HIGH
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, GPIO_PIN_6);
return NewBit;
}