c - XMC4400 和 MAX44009 传感器灯 (I2C)

标签 c embedded i2c infineon

我在尝试将光传感器 (MAX44009) 与 I2C 连接时遇到问题。我会解释我所做的,我希望有人能帮助我。

我正在将此连接卡连接到我的 XMC4400 的 80 端口 HMI 端口上。

enter image description here

我已根据此表连接传感器。 SCA-pin37 SCL-pin38 GND - 引脚 80 3.3-XMC4400的3.3V脚

然后我尝试为我的光传感器调整 I2C Master 示例(可在 DAVE 教程中找到)。我创建了一个具有以下设置的 I2C 主应用程序:

enter image description here

enter image description here

我的 main.c 是这样的:

代码:

#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;
 }

我认为我的回调函数不起作用,因为每次我执行一个 I2C 函数时它都会停止。在这种情况下,在第 108 行。另外,有时它会给我一个错误/警告:

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

当我使用 raspberry pi 时,我有这段 Python 代码可以在我的传感器灯上正常工作,我尝试在 XMC 上做同样的事情但没有成功。我希望你可以帮助我。

最佳答案

首先,我建议在您的代码周围构建一个 if 语句 喜欢

DAVE_STATUS_t init_status;
init_status = DAVE_Init();

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

通过这个你可以检查DAVE APP是否初始化正确。如果是这种情况,您可以进一步研究回调问题。如果不是,则说明配置有问题。

您可以通过右键单击应用程序依赖项窗口 -> 应用程序帮助中的 I2C Master 字段来找到更多代码示例。 APP提供的方法都有例子。

关于c - XMC4400 和 MAX44009 传感器灯 (I2C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43068142/

相关文章:

c - 如何交叉编译 ARM 的 libcollections?

c - 是否可以在 C 中伪造文件流,例如 stdin?

c - STM32 HAL I2C 在使用中断驱动方法时返回 OVR(溢出/欠载)错误

c - 在 Raspberry Pi 上为 C 设置 i2c?

python - MPU6050中I2C工作频率与采样率设置的关系

在c中的循环中创建结构体变量

c - 如何继续搜索我的数组?

c - 在选择阻塞时向 fd_Set 添加新的 FD

c - 有没有办法查询串口可用的数据量?

c - ioctl 和执行时间