1

I'm having trouble trying to connect my light sensor (MAX44009) with I2C. I will explain what I've done and I hope someone can help me.

I am connecting this connection card on HMI port of my XMC4400 with 80 ports.

enter image description here

I've connected the sensor according to this table. SCA - pin37 SCL - pin38 GND - pin 80 3.3 - pin 3.3V of XMC4400

Then I've tried to adapt the I2C Master example (available on DAVE tutorials) for my light sensor. I've created an I2C master app with the following settings:

enter image description here

enter image description here

My main.c is this:

Code:

#include <DAVE.h>        


 #define IO_EXPANDER_ADDRESS  (0x4A)

 uint8_t tx_buffer[4] = {0x00,0x01,0x02,0x03};

 volatile uint8_t tx_completion_0 = 0;
 volatile uint8_t rx_completion_0 = 0;

 /* Transmit callback handling */
 void tx_callback_0(void)
 {
   tx_completion_0 = 1;
 }

/* Receive callback handling */
 void rx_callback_0(void)
 {
   rx_completion_0 = 1;
 }

/* Delay */
 void delay(uint32_t counter)
 {
    volatile uint32_t cnt = counter;
    while(--cnt);
 }

 /*
  * For this demo  the HMI satellite board for the XMC45 CPU board is required.
  * It communicates with the IO expander (U360: PCA9502) found in the mentioned satellite board.
  * The demo implements a binary counter using the LEDs attached to the IO expander.
  *
  */
 int main(void)
 {

  DAVE_Init();

  uint8_t received_data;
  uint8_t counter = 0;

  /* Write data to reset the LEDs through the IO EXPANDER: DIR and 0xFF */
  I2C_MASTER_Transmit(&I2C_MASTER_0,true,IO_EXPANDER_ADDRESS,&tx_buffer[1],2,false);
  while(tx_completion_0 == 0);
  tx_completion_0 = 0;



  while(counter < 255)
  {

    tx_buffer[3] = ~counter;
    counter++;

    /* Write data to set the STATE of the IO EXPANDER */
    I2C_MASTER_Transmit(&I2C_MASTER_0,true,IO_EXPANDER_ADDRESS,&tx_buffer[3],2,false);
    while(tx_completion_0 == 0){

      tx_callback_0();

    }
    tx_completion_0 = 0;

    /* Receive the data from the IO EXPANDER */
    I2C_MASTER_Receive(&I2C_MASTER_0,true,IO_EXPANDER_ADDRESS,&received_data,2,true,true);
    printf("%d", received_data);
    while(rx_completion_0 == 0){
        rx_callback_0();
    }
    rx_completion_0 = 0;
    /* Check if the received  data is correct*/
    if(tx_buffer[3] != received_data)
    {
     // while(1);
    }
    /* Delay to make visible the change */
    delay(0xfffff);
  }
  while(1);
  return 0;
 }

I think my callback functions are not working, since it stops every time I execute one I2C function. on this case is on line 108. Plus, sometimes it gives me an error/warning:

No source available on 0x00.

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# MAX44009 address, 0x4A(74)
# Select configuration register, 0x02(02)
#       0x40(64)    Continuous mode, Integration time = 800 ms
bus.write_byte_data(0x4A, 0x02, 0x40)

time.sleep(0.5)

# MAX44009 address, 0x4A(74)
# Read data back from 0x03(03), 2 bytes
# luminance MSB, luminance LSB
data = bus.read_i2c_block_data(0x4A, 0x03, 2)

# Convert the data to lux
exponent = (data[0] & 0xF0) >> 4
mantissa = ((data[0] & 0x0F) << 4) | (data[1] & 0x0F)
luminance = ((2 ** exponent) * mantissa) * 0.045

# Output data to screen
print "Ambient Light luminance : %.2f lux" %luminance

I have this Python code that works fine on my sensor light when I'm using raspberry pi, I've tried to do the same thing on XMC but without success. I hope you can help me.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • `while(tx_completion_0 == 0){ tx_callback_0(); }` Umm... do you know what a callback function is? It is a function called by the system, not by the programmer. Why do you call your callback functions? These should be triggered by an I2C interrupt, or alternatively you can skip them entirely and poll for a I2C status flag. – Lundin Mar 28 '17 at 13:54
  • Maybe this question fits better in [electronics.SE](http://electronics.stackexchange.com/) – izlin Mar 28 '17 at 14:01
  • Yes, i know it isnt supposed to call the function. Its the system the one who calls, but if do this: while(tx_completion_0 == 0); tx_completion_0 = 0; the prgrams get stuck on the while. i've tried to do this to see if i was receiveing any value on the receive_data variable.. – Ricardo Falcão Mar 28 '17 at 22:14
  • Thanks! i will try it! – Ricardo Falcão Mar 28 '17 at 22:16

1 Answers1

-1

First of all I would recommend to build a if statement arround your code like

DAVE_STATUS_t init_status;
init_status = DAVE_Init();

if(init_status == DAVE_STATUS_SUCCESS)
{
  ... code
}

With this you can check if the DAVE APP is initialized correct. If it is the case you can look further into the callback issue. If not it's a problem with the configuration.

You can find further code exampels by rightlciking on the I2C Master field in teh APP Dependency window -> APP Help. There are examples for the methods provided by the APP.

NeverToLow
  • 141
  • 1
  • 9