Go Down

Topic: i2C PIC Code 2 Arduino (Read 353 times) previous topic - next topic

jl-p

Apr 20, 2014, 02:44 pm Last Edit: Apr 20, 2014, 03:21 pm by jl-p Reason: 1
I have a 7"display but the only examples i find are pic examples (i think)
I ported the SPI version for the display already from that to arduino and that works as expected.

But now i'm busy with the I2C code so i can choose which connection i want. and make a library for it.
I don't know if its necessary but i will use it on my due

but i have a couple of Wire issues i'm not sure off.

I think the only functions which needs to be corrected are the
master_ACK
slave_ACK
IIC_WriteByte
IIC_ReadByte

so i think start en stop voids can be removed
Start() and stop()  changed for Wire.beginTransmission(); and Wire.endTransmission();





Code: [Select]

sbit  MCU_RST=P2^3;
sbit  SCL=P1^1;
sbit  SDA=P1^0;
sbit next =P2^7; //next
bit ack;

#define write_data_addr  0x0c  //slave addresses with write data
#define read_data_addr  0x0d  //slave addresses with write data
#define write_cmd_addr  0x0e  //slave addresses with write command
#define read_cmd_addr  0x0f  //slave addresses with read status


//I2C_Init()
void SPI_Init(void)
{
// SCL = 1;
// SDA = 1;
        wire.begin();    //         i think only this line instead of the two above
}


//start
void start(void)
{
SDA=1;
delay(1);
SCL=1;
delay(1);
SDA=0;
delay(1);
SCL=0;
delay(1);

}

//stop
void stop(void)
{
SDA=0;
delay(1);
SCL=1;
delay(1);
SDA=1;
delay(1);
SCL=0;
delay(1);

  }

//Host sends an ACK
//ack=0  ACKNOWLEDGE  ack=1 NOT ACKNOWLEDGE
void master_ACK(bit acknow)
{ SDA=acknow;
delay(1);
SCL=1;
delay(1);
SCL=0;
delay(1);
}



//Slave sends ACK
void slave_ACK(void)
{uchar i=0;
delay(1);
SDA=1;
delay(1);
SCL=1;
delay(1);
while((SDA==1)&&i<200)i++;
delay(1);
SCL=0;
delay(1);
}


//*********I2C send byte
void IIC_WriteByte(uchar Byte)
{uchar t;
for(t=0;t<8;t++)
  { delay(1);
    SDA=(bit)(Byte & 0x80) ;
    Byte <<=1;
delay(1);
    SCL=1;
delay(1);
    SCL=0;
delay(1);

    }
slave_ACK();
}

//*********I2C Read byte
uchar IIC_ReadByte(void)
{uchar t,read_data=0;
SCL=0;
delay(1);
SDA=1;
delay(1);
for(t = 0; t < 8; t++)
{ delay(1);
SCL = 1;
delay(1);
read_data<<=1;
if(SDA == 1)
read_data=read_data|0x01;
SCL=0;
}

master_ACK(ack);

return read_data;
}

void LCD_CmdWrite(uchar cmd)
{
Wire.beginTransmission();
IIC_WriteByte(0x0e);
IIC_WriteByte(cmd);
Wire.endTransmission();
}


//////////////SPI Write data or  parameter
void LCD_DataWrite(uchar Data)
{
Wire.beginTransmission();
IIC_WriteByte(0x0c);
IIC_WriteByte(Data);
Wire.endTransmission();
}

///////////////Read data or  parameter
uchar LCD_DataRead(void)
{
uchar Data;

Wire.beginTransmission();
IIC_WriteByte(0x0d);
ack=1;
Data=IIC_ReadByte();
Wire.endTransmission();

return Data;




///////////SPI Read  status
uchar LCD_StatusRead(void)
{
uchar Data;

Wire.beginTransmission();
IIC_WriteByte(0x0f);
ack=1;
Data=IIC_ReadByte();
Wire.endTransmission();

return Data;
}



Already Thanks

Johan

jl-p

#1
Apr 20, 2014, 11:41 pm Last Edit: Apr 20, 2014, 11:43 pm by jl-p Reason: 1
I have this right now.
But no luck with the screen.
first command i send is a 0x00
which should be acknowledged with a 0x75
i see i get a 0 back but when i disconnect the screen i also receive a 0


Code: [Select]
#define write_data_addr  0x0c  //slave addresses with write data
#define read_data_addr  0x0d  //slave addresses with write data
#define write_cmd_addr  0x0e  //slave addresses with write command
#define read_cmd_addr  0x0f  //slave addresses with read status


void LCD_CmdWrite(unsigned char cmd) {
 Wire.beginTransmission(write_cmd_addr);
 Serial.println(cmd,HEX);
 Wire.write(cmd);
 Wire.endTransmission();
}

//////////////SPI Write data or  parameter
void LCD_DataWrite(unsigned char Data) {
 Wire.beginTransmission(write_data_addr);
 Serial.println(Data,HEX);
 Wire.write(Data);
 Wire.endTransmission();
}

///////////////Read data or  parameter
unsigned char LCD_DataRead(void) {
 unsigned char Data;
 Wire.requestFrom(read_data_addr, 1);
 while (Wire.available())  {
   Data =  Wire.read();
   Serial.println(Data, HEX);  }
 return Data;
}

////////////////Write command and parameter
void Write_Dir(unsigned char Cmd, unsigned char Data) {
 LCD_CmdWrite(Cmd);
 LCD_DataWrite(Data);
}

///////////SPI Read  status
unsigned char LCD_StatusRead(void) {
 unsigned char Data;
 Wire.requestFrom(read_data_addr, 1);
 while (Wire.available())  {
   Data =  Wire.read();}
 return Data;
}

jl-p

I think i have it. although the screen isn't working yet but i see responces which looks according the datasheet correct.
So if it work i will update this thread with the solution for other users.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy