2

我和我的兄弟几天来一直在努力解决这个问题,但我们无法弄清楚我们做错了什么,我们真的可以请一些帮助!

我们试图完成的是通过我们自己用 C 语言编写的程序,使用 BCM2835 库从 RPI 扩展板上的光传感器读取数据。

这是我们正在使用的光传感器:TSL2561 https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf

我们使用的是安装了 Raspbian 的树莓派 B+ 模型(通过新手)。

这是我们正在使用的 C 库: http ://www.airspayce.com/mikem/bcm2835/

我通过 raspian 配置激活了 I2c。

我通过 i2ctools 检测到光传感器,它正确显示为地址 0x29。

我们通过读取得到 0 值,我们用示波器测试了我们的命令,似乎他确实确认了写入命令。他只是没有寄回任何东西......

有人可以帮助我们吗?

#include <bcm2835.h>
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <stdint.h>

uint16_t clk_div = BCM2835_I2C_CLOCK_DIVIDER_148;
uint8_t slave_address = 0x29; //0101001 - this is the sensor address
uint64_t delay = 70000;


int main(int argc, char **argv)
{
    /*
    DATA0LOW    Ch  7:0 ADC channel 0 lower byte    
    DATA0HIGH   Dh  7:0 ADC channel 0 upper byte
    */



    char buffer[10]={0};
    char addr;
    uint16_t data;
    uint8_t data2;
    int i =0;

    char writeBuff[1] = {0x8C}; ////Address the Ch0 lower data register
    char readBuff[10];
    char writeBuff2[2] = {0x8D}; ////Address the Ch0 upper data register
    char readBuff2[10];


    char *wb_ptr,*r_ptr,*wb_ptr2,*r_ptr2;
    wb_ptr = writeBuff;
    wb_ptr2 = writeBuff2;
    r_ptr = readBuff;
    r_ptr2 = readBuff2;

    printf("Running ... \n");

    bcm2835_init():
    bcm2835_i2c_begin();
    bcm2835_i2c_setSlaveAddress(slave_address); //0x29

    printf("Clock divider set to: %d\n", clk_div);
    printf("Slave address set to: %d or %X\n",slave_address,slave_address);

    //needed according to datasheet to read although unsure if it needs to be sent in two writes or in one ? 0x83 instead of 0x80 + 0x03 ?
    bcm2835_i2c_write(0x80, 1); //command register
    bcm2835_i2c_write(0x03, 1); //command itself
    bcm2835_delayMicroseconds(delay);
    //--------------------------


    while (1)
    {

    printf("reading data from light sensor\n");

    bcm2835_i2c_write(wb_ptr, 1); // 0x8C
    bcm2835_delayMicroseconds(delay);
    data = bcm2835_i2c_read(readBuff,1);
    bcm2835_delayMicroseconds(delay);

    printf("Read Result 1 = %d\n", data);

    bcm2835_i2c_write(wb_ptr2, 1); //0x8D
    bcm2835_delayMicroseconds(delay);
    data2 = bcm2835_i2c_read(readBuff2,1);
    bcm2835_delayMicroseconds(delay);

    printf("Read Result 2 = %d\n", data);

    bcm2835_delay(1000);

    }
    bcm2835_i2c_end();
    bcm2835_close();
    printf("... done\n");
    return 0;
}

这是一个快速编辑

#include <bcm2835.h>
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <stdint.h>

uint16_t clk_div = BCM2835_I2C_CLOCK_DIVIDER_148;
uint8_t slave_address = 0x29; //0101001 - this is the sensor address
uint64_t delay = 70000;


int main(int argc, char **argv)
{
    /*
    DATA0LOW    Ch  7:0 ADC channel 0 lower byte    
    DATA0HIGH   Dh  7:0 ADC channel 0 upper byte
    */

    /*
    enum    bcm2835I2CReasonCodes { BCM2835_I2C_REASON_OK = 0x00, BCM2835_I2C_REASON_ERROR_NACK = 0x01, BCM2835_I2C_REASON_ERROR_CLKT = 0x02, BCM2835_I2C_REASON_ERROR_DATA = 0x04 }
    */


    char buffer[10]={0};
    char addr;
    uint16_t data;
    uint8_t data2;
    uint8_t error = 0xff;
    int i =0;

    char writeBuff[1] = {0x8C}; ////Address the Ch0 lower data register
    char readBuff[10];
    char writeBuff2[2] = {0x8D}; ////Address the Ch0 upper data register
    char readBuff2[10];
    char writeBuff3[2] = {0x80}; 
    char writeBuff4[2] = {0x03}; 


    char *wb_ptr,*r_ptr,*wb_ptr2,*r_ptr2,*r_ptr3,*r_ptr4;
    wb_ptr = writeBuff;
    wb_ptr2 = writeBuff2;
    r_ptr = readBuff;
    r_ptr2 = readBuff2;
    r_ptr3 = writeBuff3;
    r_ptr4 = writeBuff4;

    printf("Running ... \n");

    bcm2835_init();
    bcm2835_i2c_begin();
    bcm2835_i2c_setSlaveAddress(slave_address); //0x29

    printf("Clock divider set to: %d\n", clk_div);
    printf("Slave address set to: %d or %X\n",slave_address,slave_address);

    //needed according to datasheet to read although unsure if it needs to be sent in two writes or in one ? 0x83 instead of 0x80 + 0x03 ?
    bcm2835_i2c_write(r_ptr3, sizeof(r_ptr3)); //command register
    bcm2835_i2c_write(r_ptr4, sizeof(r_ptr4)); //command itself
    bcm2835_delayMicroseconds(delay);
    //--------------------------


    // Blink
    while (1)
    {

    printf("reading data from light sensor\n");

    error = bcm2835_i2c_write(wb_ptr, sizeof(wb_ptr)); // 0x8C
    bcm2835_delayMicroseconds(delay);
    data = bcm2835_i2c_read(readBuff,sizeof(readBuff));
    bcm2835_delayMicroseconds(delay);

    printf("readbuff1 = 0x%02X \n",readBuff);
    printf("error result = 0x%02X\n", error);
    printf("Read Result 1 = 0x%02X\n", data);

    error = bcm2835_i2c_write(wb_ptr2, sizeof(wb_ptr2)); //0x8D
    bcm2835_delayMicroseconds(delay);
    data2 = bcm2835_i2c_read(readBuff2,sizeof(readBuff2));
    bcm2835_delayMicroseconds(delay);

    printf("readbuff2 = 0x%02X \n",readBuff2);
    printf("error result = 0x%02X\n", error);
    printf("Read Result 2 = 0x%02X\n", data2);

    bcm2835_delay(1000);

    }
    bcm2835_i2c_end();
    bcm2835_close();
    printf("... done\n");
    return 0;
}
4

1 回答 1

1

我设法修复它,我遇到了几个问题,其中一个是我发送命令的地址错误,它必须是 0xa0 而不是 0x80。

#include <bcm2835.h>
#include <stdio.h>



int main(void)
{
    /*
    DATA0LOW    Ch  7:0 ADC channel 0 lower byte    
    DATA0HIGH   Dh  7:0 ADC channel 0 upper byte
    */

    /*
    enum    bcm2835I2CReasonCodes { BCM2835_I2C_REASON_OK = 0x00, BCM2835_I2C_REASON_ERROR_NACK = 0x01, BCM2835_I2C_REASON_ERROR_CLKT = 0x02, BCM2835_I2C_REASON_ERROR_DATA = 0x04 }
    */

      if (!bcm2835_init())
    return 1;
    char uitgelezenTempWaarde[1];           
    int totalTemp[2];
    int error =0;


    bcm2835_i2c_begin();                    // start i2c
    bcm2835_i2c_setSlaveAddress(0x29);      // slave address
    bcm2835_i2c_set_baudrate(1000);         // default

    //----------- turn channels on for measurement ------

    uitgelezenTempWaarde[0] = 0xa0;             
    error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }

    uitgelezenTempWaarde[0] = 0x03;             
    error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }

    bcm2835_delay(500);
    error = bcm2835_i2c_read(uitgelezenTempWaarde,1);
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }

    //----------- read data ------

        uitgelezenTempWaarde[0] = 0xac;             //DATA0LOW  Ch  7:0 ADC channel 0 lower byte
    error = bcm2835_i2c_write(uitgelezenTempWaarde,1);      
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }
    error = bcm2835_i2c_read(uitgelezenTempWaarde,1);
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }

        totalTemp[1]= (int)uitgelezenTempWaarde[0];

        uitgelezenTempWaarde[0] = 0xad;             //DATA0HIGH Dh  7:0 ADC channel 0 upper byte
    error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }
    error = bcm2835_i2c_read(uitgelezenTempWaarde,1);
    if(error != 0x00)
    {
        printf("i2c error! : 0x%02X \n",error);
    }   

        totalTemp[0] = (int)uitgelezenTempWaarde[0]; //

        totalTemp[0] *= 256; //hex conversion for the highest byte so it is seen as a high number (16 bits)
        printf("The light value is :%d\n",totalTemp[0]+totalTemp[1]);


    bcm2835_i2c_end();
    bcm2835_close();

    return 0;
}
于 2016-08-27T13:52:09.253 回答