Announcement

Collapse
No announcement yet.

Gen4-uLCD-43DT I2C MCP342x

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

  • Gen4-uLCD-43DT I2C MCP342x

    Hi,
    I would like to use Gen4 touch screen for oven control. I use a MCP3428 for reading thermocouples but I'm unable to read the data by I2C.
    I run I2C sweep to find the address of the MCP.
    I get the communication acknowledgement but the data i receive are random.
    Have anyone some experience on that?
    Thk


    Code:
    #platform "Gen4-uLCD-43DT"
    
    
    // Program Skeleton 1.4 generated 07/10/2021 14:21:31
    
    #inherit "4DGL_16bitColours.fnc"
    
    #inherit "VisualConst.inc"
    
    
    #inherit "sI2CMCP3424Const.inc"
    
    var buffer[20];
    var buffer2[2];
    var data[4];
    var BUFFERLENGTH:=10;
    var rxBuffer[10];
    var rxBufferIndex := 0;
    var rxBufferLength;
    var b:=0;
    
    
    #constant MCP3428 0xD8
    #constant WR 1
    
    
    func Wire_requestFrom(var address, var quantity)
    if(quantity > BUFFERLENGTH)
    quantity := BUFFERLENGTH;
    endif
    I2C1_Start();
    I2C1_Write((address << 1) | 1);
    I2C1_AckStatus();
    var i;
    for(i := 0; i < quantity; i++)
    rxBuffer[i] := I2C1_Read();
    if(i < quantity-1) I2C1_Ack();
    next
    I2C1_Nack();
    I2C1_Stop();
    rxBufferIndex := 0;
    rxBufferLength := quantity;
    return 1;
    endfunc
    
    
    
    
    
    
    
    
    
    
    
    func main()
    gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change
    
    print("Hello World\n") ; // replace with your code
    
    I2C1_Open(I2C_SLOW,PA2,PA3);
    
    I2C1_Idle();
    I2C1_Start(); // Generate Start condition
    I2C1_Write(MCP3428 + 0); // send control byte for Read
    print(I2C1_AckStatus() );
    I2C1_Write(0x98);
    print(I2C1_AckStatus() );
    
    I2C1_Stop();
    
    repeat
    
    
    gfx_CircleFilled(200,100,10, YELLOW);
    pause(950);
    gfx_CircleFilled(200,100,10, BLACK);
    pause(50);
    gfx_MoveTo(0, 80);
    
    
    Wire_requestFrom(0x6C, 8) ;
    print(rxBuffer[0] ,"\n");
    print(rxBuffer[1] ,"\n");
    print(rxBuffer[2] ,"\n");
    print(rxBuffer[3] ,"\n");
    print(rxBuffer[4] ,"\n");
    print(rxBuffer[5] ,"\n");
    print(rxBuffer[6] ,"\n");
    print(rxBuffer[7] ,"\n");
    
    /*I2C1_Idle();
    I2C1_Start(); // Generate Start condition
    I2C1_Write(MCP3428 + 0); // send control byte for Read
    print(I2C1_AckStatus() );
    I2C1_Write(0x98);
    print(I2C1_AckStatus() );
    
    I2C1_Stop();
    
    
    I2C1_Idle();
    I2C1_Start(); // Generate Start condition
    I2C1_Write(MCP3428 + 0); // send control byte
    print(I2C1_AckStatus() );
    I2C1_Write(0x38); // set condition
    print(I2C1_AckStatus() );
    pause(500);
    I2C1_Stop();
    I2C1_Idle();
    I2C1_Start();
    I2C1_Write(MCP3428 + 1); // read
    print(I2C1_AckStatus() );
    pause(500);
    b := I2C1_Read(); // read the byte
    I2C1_Stop();
    print("received: ",[HEX2]b, "\n");
    
    pause(500);
    
    */
    gfx_CircleFilled(200,100,10, RED);
    pause(950);
    gfx_CircleFilled(200,100,10, BLACK);
    pause(50);
    
    
    
    
    
    forever
    
    endfunc
Working...
X