Announcement

Collapse
No announcement yet.

I2C

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I2C

    Ok, after the last few messages in this post I've spent some time trying to understand the 4DGL I2C implementation and how it relates to the uLCD-43 touchscreen controller. Here is the code I've come up with:

    Code:
    #platform "uLCD-43PT_GFX2"func main()//A loop counter.var x;var Temp;//This must be called once at the beginning of the program.touch_Set(0);for(x := 0; x < 300; x++)print("No touch loop: ", x, " - ");//We must open the I2C bus before calling 'touch_Get()'pause(1);I2C_Open(I2C_SLOW);if(touch_Get(TOUCH_STATUS))print("Touch   ");elseprint("No touch");endif//Close the bus when we're done.I2C_Close();pause(1);//Make sure we can use the I2C bus for other things while touch is disabled.Temp :=  RTC_Read(0x00);print("\n RTC: ", Temp);//Wait a bit and reset the cursor.pause(50);txt_MoveCursor(0, 0);nextendfuncfunc RTC_Read(var RTCAddress)//A variable to hold the return value.var I2CData;//Disable touch.My_TouchEnable(0);//Open the bus.pause(1);I2C_Open(I2C_SLOW);//Send a start signal.I2C_Start();//Write the address of the RTC chip.I2C_Write(0xDE);//Write the address of the byte within the RTC we want to read from.I2C_Write(LObyte(RTCAddress));//Send a restart.I2C_Restart();//Write the address of the RTC chip with the read bit set.I2C_Write(0xDF);//Read the data.I2CData := I2C_Read();//Send a stop.I2C_Stop();//Close the bus.I2C_Close();pause(1);//Enable touch.My_TouchEnable(1);//Return the data.return(I2CData);endfuncfunc My_TouchEnable(var Enable)var Cmd;var Buf;Buf := mem_AllocZ(6);var Ptr;Ptr := str_Ptr(Buf);//Set up the command bytes.Buf[0] := 0x009A;Buf[1] := 0x0155;//Select enable or disable command.if(Enable)Cmd := 0x0012;elseCmd := 0x0013;endifBuf[2] := Cmd;//Open the I2C bus.pause(1);I2C_Open(I2C_SLOW);//Start.I2C_Start();//Write command.I2C_Putn(Buf, 5);pause(1);//Stop.I2C_Stop();pause(1);//Close bus when we're done.I2C_Close();pause(1);mem_Free(Buf);endfunc
    I'll try and list all I've learned today. The point here is to write a more robust way to turn touch detection on and off so it won't interfere with other I2C communications. using 'touch_Set()' creates the possibility that the system will freeze. touch_Set(0) must be called at least once at the beginning of a program. It must set something in the picasso as well as the touchscreen controller chip to get touch detection working. I found that pause(1) must be called before each I2C_Open() and after each I2C_Close() or the system can lock up when touching. I also learned that, after writing a enable or disable command to the controller, there must be a delay. Somehow, I'm guessing the picasso automatically addresses the controller and reads the response from the enable/disable command sent by my function. If I try to read the response in the function itself, I see four 0x00s and then something else (picasso?) writes 0x9B and clocks out the proper response as five (not four?) data bytes. Anyway, using My_TouchEnable() before and after an I2C transaction seems to be able to keep touches from colliding with other I2C traffic and it doesn't seem to freeze if the bus glitches at all. Hope this can be of use to anyone else having I2C difficulties.

Working...
X