This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C123GH6PM: TM4C123GH6PM Configuring 2 light sensors

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hey,

I'm currently working with a team on a project where we take an input from 2 light sensors and based on their values rotate our servo motor.

We were learning how to use CCS and programming the TM4C from the Tiva™ TM4C123G LaunchPad Workshop. However, there isn't enough information regarding this topic since the lab covers only one sensor. 

Any help would be greatly appreciated in how to set up and initialize the sensor. 

Cheers

  • Fred Gebrael said:
    there isn't enough information regarding this topic since the lab covers only one sensor. 

    Perhaps it is fair/proper to note that you/team complain of, "insufficient information" - and then provide absolutely NO information as regards your 2 light sensors!

    Those sensors likely output analog voltage levels or a digital "1/0" based upon the intensity of light reaching the sensor.    Different tactics are required based upon the sensor's output - which (remains) unknown!

  • We are using the same sensors as the ones cited in the lab, which is why we mentioned the workshop we are using .

    No need for aggressive behavior

  • How would any outsider know that? Forcing more work upon your helpers may not make the most sense.

    You may wish to investigate, "Hoist by one's own petard." That's rarely noted as "aggressive."
  • this discussion is going no where. If you wont help us just let one of your colleagues handle it.
    Thanks
  • this discussion is going no where. If you wont help us just let one of your colleagues handle it
    Thanks
  • Fred Gebrael said:
    this discussion is going no where. I

    You are wrong - again.     I've noted that most all light sensors provide analog output - usually more advanced ones may provide digital outputs - which toggle at some known, adjustable level.   (such is "far" from going nowhere.)    (note - nowhere is one word - not two.)

    I wish you well - perhaps others will find you more, "coachable."

  • If you are referring to the lab 14b in this workshop, it uses the sensor hub booster pack. The light sensor on that booster pack is ISL29023 and it interfaces with the microcontroller via the I2C bus. Since the ISL29023 has a 7-bit hardcoded I2C address of 0x44, you cannot put both devices on the same I2C bus. (No problem, the TM4C123GH6PM has 4 I2C modules.) You address the separate light sensors by the base address of the I2C module to which it is attached. That is a function of your hardware design.

  • Thanks!
    So if I want to link sensor 2 to portA, I will use the same code as in lab14 replacing the pins accordingly and using the adress of the I2C that correspond to portA instead of (0x44) correct?

    Can you please point out where I can find the address that is supposed to replace 0x44
  • No, the 0x44 is a hardcoded I2C address in the light sensor. On the sensor hub, the light sensor is connected on I2C bus 3, which is pins 61 and 62. When you create hardware with 2 light sensors, put the other one on I2C bus 0 (pins 47 and 48), I2C bus  1 (pins 23 and 24) or I2C bus 2 (pins 59 and 60). The choice may depend on what other peripherals you are using. Then in the software, like line 520 of the file light_isl29023.c, call the function I2CMInit twice. Once with the base address of one I2C module, and then with the base address of the other populating two structures. For example, let's assume you use I2C2 and I2CC3:

        I2CMInit(&g_sI2CInst1, I2C2_BASE, INT_I2C2, 0xff, 0xff, ROM_SysCtlClockGet());
        I2CMInit(&g_sI2CInst2, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet());
    

  • Hi,

    Again thanks for your help. I tried implementing what I understood from our discussion and was hoping if you could take a look at the code below.

    The device should take the light measurement from both sensors and compare them

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/tm4c123gh6pm.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/gpio.h"
    #include "driverlib/timer.h"
    #define TARGET_IS_BLIZZARD_RB1
    #include "driverlib/rom.h"
    #include "sensorlib/hw_isl29023.h"
    #include "sensorlib/i2cm_drv.h"
    #include "sensorlib/isl29023.h"
    #define DEBUG
    
    // Light sensor 1 (right side):
    // PORTA PIN 6 : I2C1SCL
    // PORTA PIN 7 : I2C1SDA
    
    // Light sensor 2 (left side):
    // PORTB PIN 2 : I2C0SCL
    // PORTB PIN 3 : I2C0SDA
    
    
    #define ISL29023_I2C_ADDRESS 0x44 // ISL29023 I2C address
    
    tI2CMInstance g_sI2CInstSensor1; // I2C master driver structure
    tISL29023 g_sISL29023InsSensor1t; // ISL29023 sensor driver structure
    volatile unsigned long g_vui8DataFlagSensor1; // Data ready flag
    volatile unsigned long g_vui8ErrorFlagSensor1; // Error flag
    
    tI2CMInstance g_sI2CInstSensor2; // I2C master driver structure
    tISL29023 g_sISL29023InsSensor2t; // ISL29023 sensor driver structure
    volatile unsigned long g_vui8DataFlagSensor2; // Data ready flag
    volatile unsigned long g_vui8ErrorFlagSensor2; // Error flag
    
    //*************************************************************************
    void
    ISL29023AppCallback(void *pvCallbackData, uint_fast8_t ui8Status)
    {
        if(ui8Status == I2CM_STATUS_SUCCESS)
        {
            g_vui8DataFlagSensor1 = 1;
        }
        g_vui8ErrorFlagSensor1 = ui8Status;
    }
    //*************************************************************************
    void
    ISL29023I2CIntHandler(void)
    {
        I2CMIntHandler(&g_sI2CInstSensor1);
        I2CMIntHandler(&g_sI2CInstSensor2);
    }
    //*************************************************************************
    void
    ISL29023AppErrorHandler(char *pcFilename, uint_fast32_t ui32Line)
    {
        while(1)
        {
        }
    }
    //*************************************************************************
    void
    ISL29023AppI2CWait(char *pcFilename, uint_fast32_t ui32Line)
    {
        while((g_vui8DataFlagSensor1 == 0) && (g_vui8ErrorFlagSensor1 == 0))
        {
        }
        if(g_vui8ErrorFlagSensor1)
        {
            ISL29023AppErrorHandler(pcFilename, ui32Line);
        }
        g_vui8DataFlagSensor1 = 0;
    }
    //*************************************************************************
    
    
    
    // Function 
    void FredFunction(bool c){
        uint8_t ui8LED = 0; // LED
    
        if(c){
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_3, 0x08);//Green light appears  because direction is Clockwise
        }else{
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x02); //RED light appears since Counter Clockwise
        }
    }
    //END OF FUNCTION
    
    /**
     *\brief initialize all variables
     */
    int main(void) {
        // Varaible declaration
        float fAmbientSensor1;
        float fAmbientSensor2;
        uint8_t ui8Mask;
    
        // Initialize clock to 400/ 2/ 5 = 50MHZ with source PLL, 16MHZ and main oscillations
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    
    /**
    * For phase 1, the motor will be replaced by LED output:
    * GREEN LED (pf03) -> clockwise
    * RED LED (pf011/0 -> counter clockwise
    */
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_3);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_3, 0x00); // clear LEDs
    
    
    // Initialise the sensor 1
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
    
    ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL);
    ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA);
    
    ROM_GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);
    ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);
    
    I2CMInit(&g_sI2CInstSensor1, I2C3_BASE, INT_I2C3, 0xFF, 0xFF, ROM_SysCtlClockGet());
    
    SysCtlDelay(SysCtlClockGet() / 3);
    
    ISL29023Init(&g_sISL29023InsSensor1t, &g_sI2CInstSensor1,
    ISL29023_I2C_ADDRESS,ISL29023AppCallback, &g_sISL29023InsSensor1t);
    ISL29023AppI2CWait(__FILE__, __LINE__);
    ui8Mask = (ISL29023_CMD_I_OP_MODE_M );
    
    ISL29023ReadModifyWrite(&g_sISL29023InsSensor1t, ISL29023_O_CMD_I, ~ui8Mask,
    (ISL29023_CMD_I_OP_MODE_ALS_CONT),
    ISL29023AppCallback, &g_sISL29023InsSensor1t);
    ISL29023AppI2CWait(__FILE__, __LINE__);
    
    /** Initialize sensor 2 */
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
    
    ROM_GPIOPinConfigure(GPIO_PE4_I2C2SCL);
    ROM_GPIOPinConfigure(GPIO_PE5_I2C2SDA);
    
    ROM_GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4);
    ROM_GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5);
    
        I2CMInit(&g_sI2CInstSensor2, I2C2_BASE, INT_I2C2, 0xFF, 0xFF, ROM_SysCtlClockGet());
    
        SysCtlDelay(SysCtlClockGet() / 3);
    
        ISL29023Init(&g_sISL29023InsSensor2t, &g_sI2CInstSensor2,
            ISL29023_I2C_ADDRESS,ISL29023AppCallback, &g_sISL29023InsSensor2t);
            ISL29023AppI2CWait(__FILE__, __LINE__);
            ui8Mask = (ISL29023_CMD_I_OP_MODE_M );
    
    
        ISL29023ReadModifyWrite(&g_sISL29023InsSensor2t, ISL29023_O_CMD_I, ~ui8Mask,
            (ISL29023_CMD_I_OP_MODE_ALS_CONT),
            ISL29023AppCallback, &g_sISL29023InsSensor2t);
            ISL29023AppI2CWait(__FILE__, __LINE__);
    
        ROM_IntMasterEnable();
    
        while(1){
            ISL29023DataRead(&g_sISL29023InstSensor1, ISL29023AppCallback, &g_sISL29023InstSensor1);
            ISL29023AppI2CWait(__FILE__, __LINE__);
            ISL29023DataLightVisibleGetFloat(&g_sISL29023InstSensor1, &fAmbientSensor1);
    
            ISL29023DataRead(&g_sISL29023InstSensor2, ISL29023AppCallback, &g_sISL29023InstSensor2);
                    ISL29023AppI2CWait(__FILE__, __LINE__);
                    ISL29023DataLightVisibleGetFloat(&g_sISL29023InstSensor2, &fAmbientSensor2);
    
            // if( abs(fAmbientSensor1 - fAmbientSensor2 > THRESHOLD)){
            //      Call function
                    //}
        }
    
    return 0;
    }