2017-03-28 43 views
1

我在尝试将我的光传感器(MAX44009)与I2C连接时出现问题。我会解释我所做的,我希望有人能帮助我。XMC4400和MAX44009传感器指示灯(I2C)

我在XMC4400的HMI端口上连接了这个连接卡和80个端口。

enter image description here

我已经根据该表将传感器连接。 SCA - pin37 SCL - pin38 GND - 销80 3.3 - 的XMC4400

然后我试着去适应I2C主机例子(DAVE教程可用)我光传感器引脚3.3V。我创建了一个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.另外,有时它给了我一个错误/警告:

没有来源可用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 

我有这样的Python代码,当我使用树莓派,我试着做XMC但没有成功同样的事情在我的感应灯工作正常。我希望你能帮助我。

+0

'while(tx_completion_0 == 0){tx_callback_0(); '嗯......你知道什么是回调函数吗?它是由系统调用的函数,而不是程序员。你为什么要调用你的回调函数?这些应该由I2C中断触发,或者您可以完全跳过它们并轮询I2C状态标志。 – Lundin

+0

也许这个问题更适合[electronics.SE](http://electronics.stackexchange.com/) – izlin

+0

是的,我知道它不应该调用该函数。它的系统调用者,但如果这样做:while(tx_completion_0 == 0); tx_completion_0 = 0;这些方案暂时陷入困境。我试图做到这一点,看看我是否收到receive_data变量的任何值.. –

回答

-1

首先,我会建议建立一个if语句以防万一您的代码 像

DAVE_STATUS_t init_status; 
init_status = DAVE_Init(); 

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

有了这个,你可以检查DAVE APP初始化正确。如果是这种情况,您可以进一步查看回调问题。如果不是,这是配置问题。

您可以在APP Dependency窗口中的I2C Master字段右侧找到更多代码示例 - > APP帮助。这些APP提供的方法有一些例子。