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.

[参考译文] TM4C1294NCPDT:与 INA219的 I2C 接口

Guru**** 2391415 points
Other Parts Discussed in Thread: INA219

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1006274/tm4c1294ncpdt-i2c-interface-with-ina219

器件型号:TM4C1294NCPDT
主题中讨论的其他器件:INA219

您好!

我想将 INA219电流传感器与 TM4C1294连接。 我已经初始化了 I2C、但它不起作用、我观察到没有生成时钟。 初始化是否有任何问题请帮助。

我还对 RTOS 示例以及 TI 外设驱动程序进行了同样的尝试。 代码在附件中

此致

霍迪达斯

/*
 * Copyright (c) 2014-2015, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *    ======== tcpEcho.c ========
 *    Contains BSD sockets code.
 */
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>

#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART.h>
/* NDK BSD support */
#include <sys/socket.h>

/* Example/Board Header file */
#include "Board.h"

#include "inc/hw_memmap.h"
#include "inc/hw_adc.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/ssi.h"
#include "driverlib/pin_map.h"
#include "driverlib/timer.h"

#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
/*#include <ti/drivers/I2C.h>

I2C_Handle      i2c;
I2C_Params      i2cParams;
I2C_Transaction i2cTransaction;*/
//*********************************************************************//

UART_Handle      handle;
UART_Params myuart;


char rcvd_data;
#define DATBUFFSIZE 13000

#define NUMTCPWORKERS 5
//uint32_t bytesRcvd;

uint32_t adcBuffer[6];
uint32_t adcBuffer2[3];
uint32_t bpf_det[2];


uint32_t ui32SysClock=0;
char buffer[DATBUFFSIZE];

uint32_t adcClock=0, adcDiv=0;


uint32_t counter;
int clientfd;

uint32_t timerclock;
bool pom = 0;


uint16_t adc_eth_buff;

uint8_t buffer_counter;
uint16_t j=0,timecounter,sendcounter;

bool mod=false,BPF=false;

uint8_t         txBuffer[1];
uint8_t         rxBuffer[2];

uint8_t pui32DataTx[2];
uint32_t measuredcurrent;
typedef enum CONFIG_BVOLTAGERANGE {
            CONFIG_BVOLTAGERANGE_16V =        (0x0000),  // 0-16V Range
            CONFIG_BVOLTAGERANGE_32V =        (0x2000),  // 0-32V Range
        } CONFIG_BVOLTAGERANGE;
typedef enum CONFIG_GAIN {
            CONFIG_GAIN_1_40MV        =       (0x0000),  // Gain 1, 40mV Range
            CONFIG_GAIN_2_80MV        =       (0x0800),  // Gain 2, 80mV Range
            CONFIG_GAIN_4_160MV       =       (0x1000),  // Gain 4, 160mV Range
            CONFIG_GAIN_8_320MV       =       (0x1800),  // Gain 8, 320mV Range
          } CONFIG_GAIN;
typedef enum CONFIG_BADCRES {
          CONFIG_BADCRES_9BIT       =       (0x0000),  // 9-bit bus res = 0..511
          CONFIG_BADCRES_10BIT      =       (0x0080),  // 10-bit bus res = 0..1023
          CONFIG_BADCRES_11BIT      =       (0x0100),  // 11-bit bus res = 0..2047
          CONFIG_BADCRES_12BIT       =      (0x0180),  // 12-bit bus res = 0..4097
          } CONFIG_BADCRES;
typedef enum CONFIG_SADCRES {
          CONFIG_SADCRES_9BIT_1S_84US     = (0x0000),  // 1 x 9-bit shunt sample
          CONFIG_SADCRES_10BIT_1S_148US   = (0x0008),  // 1 x 10-bit shunt sample
          CONFIG_SADCRES_11BIT_1S_276US   = (0x0010),  // 1 x 11-bit shunt sample
          CONFIG_SADCRES_12BIT_1S_532US   = (0x0018),  // 1 x 12-bit shunt sample
          CONFIG_SADCRES_12BIT_2S_1060US  = (0x0048),  // 2 x 12-bit shunt samples averaged together
          CONFIG_SADCRES_12BIT_4S_2130US  = (0x0050),  // 4 x 12-bit shunt samples averaged together
          CONFIG_SADCRES_12BIT_8S_4260US  = (0x0058),  // 8 x 12-bit shunt samples averaged together
          CONFIG_SADCRES_12BIT_16S_8510US = (0x0060),  // 16 x 12-bit shunt samples averaged together
          CONFIG_SADCRES_12BIT_32S_17MS   = (0x0068),  // 32 x 12-bit shunt samples averaged together
          CONFIG_SADCRES_12BIT_64S_34MS   = (0x0070),  // 64 x 12-bit shunt samples averaged together
          CONFIG_SADCRES_12BIT_128S_69MS =  (0x0078),  // 128 x 12-bit shunt samples averaged together
      } CONFIG_SADCRES;
      typedef enum CONFIG_MODE {
                 CONFIG_MODE_POWERDOWN          =  (0x0000),
                 CONFIG_MODE_SVOLT_TRIGGERED    =  (0x0001),
                 CONFIG_MODE_BVOLT_TRIGGERED    =  (0x0002),
                 CONFIG_MODE_SANDBVOLT_TRIGGERED = (0x0003),
                 CONFIG_MODE_ADCOFF             =  (0x0004),
                 CONFIG_MODE_SVOLT_CONTINUOUS  =  (0x0005),
                 CONFIG_MODE_BVOLT_CONTINUOUS   =  (0x0006),
                 CONFIG_MODE_SANDBVOLT_CONTINUOUS = (0x0007),
             } CONFIG_MODE;
void Current_init()
{
    #define SLAVE_ADDRESS       0x40
    #define REG_CALIBRATION     0x05
    #define REG_CONFIG          0x00


    uint32_t _currentDivider_mA =25;
    float    powerMultiplier_mW = 0.8;


    int calValue = 10240;

    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    GPIOPinConfigure(GPIO_PB3_I2C0SDA);
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

    I2CMasterEnable(I2C0_BASE);
    I2CMasterInitExpClk(I2C0_BASE, ui32SysClock, true);


    pui32DataTx[0] = REG_CALIBRATION;
    pui32DataTx[1] = calValue;

    I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false); //false means master will write to slave
    I2CMasterDataPut(I2C0_BASE, pui32DataTx[0]);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    I2CMasterDataPut(I2C0_BASE, (calValue >> 8) && 0xFF);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    I2CMasterDataPut(I2C0_BASE, (calValue) && 0xFF);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

//    while(!(I2CSlaveStatus(I2C6_BASE) & I2C_SLAVE_ACT_RREQ));

    uint16_t config = CONFIG_BVOLTAGERANGE_32V |
                        CONFIG_GAIN_8_320MV |
                        CONFIG_BADCRES_12BIT |
                        CONFIG_SADCRES_12BIT_1S_532US |
                        CONFIG_MODE_SANDBVOLT_CONTINUOUS;



    pui32DataTx[0] = REG_CONFIG;
    pui32DataTx[1] = config;
    I2CMasterDataPut(I2C0_BASE,pui32DataTx[0] );
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    I2CMasterDataPut(I2C0_BASE,(config >> 8) && 0xFF);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
    I2CMasterDataPut(I2C0_BASE,(config) && 0xFF);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    I2CMasterDataPut(I2C0_BASE, 0x04);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    while (I2CMasterBusy(I2C0_BASE));

    I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true); //false means master will read from slave
    I2CMasterDataPut(I2C0_BASE, 0x04);
    //I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    while ((I2CMasterBusy(I2C0_BASE)));
    measuredcurrent=I2CSlaveDataGet(I2C0_BASE);

    //I2CMasterDataPut(I2C0_BASE, 0x04);
    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    while ((I2CMasterBusy(I2C0_BASE)));
    pui32DataTx[1]=I2CSlaveDataGet(I2C0_BASE);

/*    I2C_Params_init(&i2cParams);
        i2cParams.bitRate = I2C_400kHz;
        i2c = I2C_open(Board_I2C1, &i2cParams);
        if (i2c == NULL) {
            System_abort("Error Initializing I2C\n");
        }
        else {
            System_printf("I2C Initialized!\n");
        }

        txBuffer[0] = 0x04;
        i2cTransaction.slaveAddress = 0x40;
        i2cTransaction.writeBuf = txBuffer;
        i2cTransaction.writeCount = 1;
        i2cTransaction.readBuf = rxBuffer;
        i2cTransaction.readCount = 2;

        I2C_transfer(i2c, &i2cTransaction);*/
}
void Timer0IntHandler(void)
{
    // Clear the timer interrupt.
   TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
   timecounter++;

}

void timer_init()
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
   // timerclock=TimerClockSourceGet(TIMER0_BASE);
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);

    TimerLoadSet(TIMER0_BASE, TIMER_A, (ui32SysClock / 1000));
    IntEnable(INT_TIMER0A);
    TimerIntRegister(TIMER0_BASE, TIMER_A, Timer0IntHandler);

    // Register Interrupt to NVIC
    IntRegister(INT_TIMER0A, Timer0IntHandler);

    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    IntMasterEnable();
    TimerEnable(TIMER0_BASE, TIMER_A);

}

void ADC0IntHandler(void)
{
    // Clear interrupt Flag
    ADCIntClear(ADC0_BASE, 0);
    ADCSequenceDataGet(ADC0_BASE, 0, adcBuffer);

 //   GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, (!(GPIOPinRead(GPIO_PORTH_BASE, GPIO_PIN_3)))<<3);
}
void ADC1IntHandler(void)
{
    // Clear interrupt Flag
    ADCIntClear(ADC0_BASE, 1);
       ADCSequenceDataGet(ADC0_BASE, 1, adcBuffer2);
 //      GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, (!(GPIOPinRead(GPIO_PORTH_BASE, GPIO_PIN_3)))<<3);
}


void adc1_init()
{
    // Enable the ADC0 peripheral
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    //====================channel change for test purpose with IPMS pcb========================//
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3 | GPIO_PIN_4 |GPIO_PIN_5 );
    // Configure the ADC to use PLL at 480 MHz with Full rate devided by 30 to get 16 MHz
    // Sampling rate is 1.2 MSPS For all channels sofor each channels is Sample Rate/No. Of channels i.e. (1.2 x 10^6 )/6 =200KSPS
    //For Clock rate  (Systemclock/ prescaler )/oversampling i.e using PLL ((120x10^6)/25)/4 =1.2x10^6
    ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 30);   //Actual 250KSPS for MOD
    ADCSequenceDisable(ADC0_BASE, 1);
    // Read the current ADC configuration
    //adcClock=ADCClockConfigGet(ADC0_BASE, &adcDiv);
    // Hardware averageing: by a faktor of 2 -> 2,4,8,16,32,64
    ADCHardwareOversampleConfigure(ADC0_BASE, 2);
    // ADC voltage-lvl reference set to intern
    ADCReferenceSet(ADC0_BASE, ADC_REF_INT);
    // ADC Sequencer config: Source ADC0, Sequencer 0, Trigger: always, priority: 0
    ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_ALWAYS, 0);
     // ADC Sequencer step
    // 1. Source-ADC -> ADC0_BASE
    // 2. Source-Sequencer -> 0
    // 3. Sample-Value depends in the depth of the FIFO, by Sequencer 0 it is up to 7 (0-7)
    // 4. Config-> select input-channel AINx, interrupt specification
    ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH8);
    ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_CH9);
    ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_CH0|ADC_CTL_IE|ADC_CTL_END);
    IntPrioritySet(INT_ADC0SS1, 0x00);
    // ADC0SS0 Interrupt source
    ADCIntRegister(ADC0_BASE, 1, ADC1IntHandler);
    // Register Interrupt to NVIC
    IntRegister(INT_ADC0SS1, ADC1IntHandler);
    // ADC0 enable
    ADCIntEnable(ADC0_BASE, 1);
    // Interrupt ADC0SS0 enable
    IntEnable(INT_ADC0SS1);
    // Enable Global Interrupts
    IntMasterEnable();
    ADCSequenceEnable(ADC0_BASE, 1);

}

void adc2_init(void)
 {

        // Enable the ADC0 peripheral
        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
        //====================channel change for test purpose with IPMS pcb========================//

        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
        GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_1 | GPIO_PIN_0 | GPIO_PIN_7 | GPIO_PIN_6 | GPIO_PIN_5 | GPIO_PIN_4);

        // Configure the ADC to use PLL at 480 MHz with Full rate devided by 30 to get 16 MHz
        // Sampling rate is 1.2 MSPS For all channels sofor each channels is Sample Rate/No. Of channels i.e. (1.2 x 10^6 )/6 =200KSPS
        //For Clock rate  (Systemclock/ prescaler )/4 i.e using PLL ((120x10^6)/25)/4 =1.2x10^6
        ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 15);
        ADCSequenceDisable(ADC0_BASE, 0);
        // Read the current ADC configuration
        //adcClock=ADCClockConfigGet(ADC0_BASE, &adcDiv);
        // Hardware averageing: by a faktor of 2 -> 2,4,8,16,32,64
        ADCHardwareOversampleConfigure(ADC0_BASE, 2);
        // ADC voltage-lvl reference set to intern
        ADCReferenceSet(ADC0_BASE, ADC_REF_INT);
        // ADC Sequencer config: Source ADC0, Sequencer 0, Trigger: always, priority: 0
        ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_ALWAYS, 0);
        // ADC Sequencer step
        // 1. Source-ADC -> ADC0_BASE
        // 2. Source-Sequencer -> 0
        // 3. Sample-Value depends in the depth of the FIFO, by Sequencer 0 it is up to 7 (0-7)
        // 4. Config-> select input-channel AINx, interrupt specification

        ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH15);
        ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH14);
        ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH7);
        ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH6);
        ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_CH5);
        ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_CH4|ADC_CTL_IE|ADC_CTL_END);


        IntPrioritySet(INT_ADC0SS0, 0x00);
        // ADC0SS0 Interrupt source
        ADCIntRegister(ADC0_BASE, 0, ADC0IntHandler);
        // Register Interrupt to NVIC
        IntRegister(INT_ADC0SS0, ADC0IntHandler);
        // ADC0 enable
        ADCIntEnable(ADC0_BASE, 0);
        // Interrupt ADC0SS0 enable
        IntEnable(INT_ADC0SS0);
        // Enable Global Interrupts
        IntMasterEnable();
        ADCSequenceEnable(ADC0_BASE, 0);
}

//        bytesRcvd = recv(clientfd, buffer, TCPPACKETSIZE, 0);
//        bytesSent = send(clientfd, buffer, bytesRcvd, 0);

/*
 *  ======== tcpHandler ========
 *  Creates new Task to handle new TCP connections.
 */
Void tcpHandler(UArg arg0, UArg arg1)
{
    int                status;

    int                server;
    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    int                optval;
    int                optlen = sizeof(optval);
    socklen_t          addrlen = sizeof(clientAddr);
    Task_Handle        taskHandle;
    Task_Params        taskParams;
    Error_Block        eb;

    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (server == -1)
    {
        System_printf("Error: socket not created.\n");
        goto shutdown;
    }


    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(arg0);

    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status == -1) {
        System_printf("Error: bind failed.\n");
        goto shutdown;
    }

    status = listen(server, NUMTCPWORKERS);
    if (status == -1) {
        System_printf("Error: listen failed.\n");
        goto shutdown;
    }

    optval = 1;
    if (setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
        System_printf("Error: setsockopt failed\n");
        goto shutdown;
    }
    Current_init();
    adc2_init();
    timer_init();
    while ((clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1)
    {
        timecounter=0;
        System_printf("tcpHandler: Creating thread clientfd = %d\n", clientfd);

        /* Init the Error_Block */
        Error_init(&eb);



        GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_3);
        GPIODirModeSet(GPIO_PORTH_BASE,GPIO_PIN_3,GPIO_DIR_MODE_OUT);

        while(1)
        {

                if(j<DATBUFFSIZE )
                    {

                    if(timecounter<10000)
                    {

                            if(timecounter<5000)
                            {
                                buffer[j]=0x41;
                                buffer[j+1]=((adcBuffer[0] >> 8) & 0xFF);
                                buffer[j+2]=(adcBuffer[0] & 0xFF);
                                buffer[j+3]=((adcBuffer[1] >> 8) & 0xFF);
                                buffer[j+4]=(adcBuffer[1] & 0xFF);
                                buffer[j+5]=((adcBuffer[2] >> 8) & 0xFF);
                                buffer[j+6]=(adcBuffer[2] & 0xFF);
                            }
                            else
                            {
                                //ADCSequenceDisable(ADC0_BASE, 0);
                                if(mod==false)
                                {
                                 mod=true;
                                 ADCSequenceDisable(ADC0_BASE, 0);
                                 SysCtlPeripheralDisable(SYSCTL_PERIPH_GPIOD);
                                 adc1_init();

                                }
                                buffer[j]=0x41;
                                buffer[j+1]=((adcBuffer2[0] >> 8) & 0xFF);
                                buffer[j+2]=(adcBuffer2[0] & 0xFF);
                                buffer[j+3]=((adcBuffer2[1] >> 8) & 0xFF);
                                buffer[j+4]=(adcBuffer2[1] & 0xFF);
                                buffer[j+5]=((adcBuffer2[2] >> 8) & 0xFF);
                                buffer[j+6]=(adcBuffer2[2] & 0xFF);
                            }
                        //buffer[j+3]=((adcBuffer[1] >> 8) & 0xFF);
                        //buffer[j+4]=(adcBuffer[1] & 0xFF);
                    }
                        buffer[j+7]=((adcBuffer[3] >> 8) & 0xFF);
                        buffer[j+8]=(adcBuffer[3] & 0xFF);
                        buffer[j+9]=((adcBuffer[4] >> 8) & 0xFF);
                        buffer[j+10]=(adcBuffer[4] & 0xFF);
                        buffer[j+11]=((adcBuffer[5] >> 8) & 0xFF);
                        buffer[j+12]=(adcBuffer[5] & 0xFF);
                        j+=13;
                      //  pom=1;

                    }
                else
                  {

                       send(clientfd, buffer, sizeof(buffer), 0);
                       memset(buffer,NULL,sizeof(buffer));
                       sendcounter++;
                       j=0;
                  }


        }


        /* addrlen is a value-result param, must reset for next accept call */

    }
    addrlen = sizeof(clientAddr);



    System_printf("Error: accept failed.\n");

shutdown:
    if (server > 0)
    {
        close(server);
    }
}




/*
 *  ======== main ========
 */
int main(void)
{
    /* Call board init functions */
    ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |SYSCTL_OSC_MAIN |SYSCTL_USE_PLL |SYSCTL_CFG_VCO_480), 120000000);
    Board_initGeneral();
    Board_initGPIO();
    Board_initEMAC();
    Board_initUART();
    //Board_initI2C();

    UART_Params_init(&myuart);
    handle = UART_open(0, &myuart);
   // UARTStdioConfig(0, 115200, ui32SysClock);
    UART_write(handle, "HELLO", 5);

    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    /* Start BIOS */
    BIOS_start();

    return (0);
}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您说没有时钟、SCL 线路是高电平还是低电平? 您的外部上拉电阻器的值是多少?  

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    最后、我发现其中一个模块出现故障、它抑制了时钟和数据线、然后另一个模块正常、但仍然没有获得电流测量结果、因此 I2C 初始化或 INA219配置中是否存在任何错误。

    上拉电阻安装在 INA219模块上、为10K。

    此致

    霍迪达斯

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

     您需要将寄存器地址和数据作为突发的一部分、而不是使用 I2C_MASTER_CMD_SINGLE_SEND 写入寄存器。 这样、您就不会在写入之间生成"停止"条件。 以下是 MPU9150的一些示例代码:

    void MPU9150WriteRegister(uint8_t reg, uint8_t value)
    {
        I2CMasterSlaveAddrSet(MPU9150_I2C_BASE, MPU9150_I2C_ADDRESS, false);
        I2CMasterDataPut(MPU9150_I2C_BASE, reg);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        if (I2CMasterErr(MPU9150_I2C_BASE) != I2C_MASTER_ERR_NONE)
        {
            //Turn Red LED on.
            GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, GPIO_PIN_2);
        }
        I2CMasterDataPut(MPU9150_I2C_BASE, value);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        if (I2CMasterErr(MPU9150_I2C_BASE) != I2C_MASTER_ERR_NONE)
        {
            //Turn Red LED on.
            GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, GPIO_PIN_2);
        }
    
    }
    
    uint8_t MPU9150ReadRegister(uint8_t reg)
    {
        I2CMasterSlaveAddrSet(MPU9150_I2C_BASE, MPU9150_I2C_ADDRESS, false);
        I2CMasterDataPut(MPU9150_I2C_BASE, reg);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        I2CMasterSlaveAddrSet(MPU9150_I2C_BASE, MPU9150_I2C_ADDRESS, true);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        return (uint8_t)I2CMasterDataGet(MPU9150_I2C_BASE);
    }
    
    void MPU9150BurstRead(uint8_t reg, unsigned int count, uint8_t *p8Data)
    {
        unsigned int i;
    
        if(count < 2)
        {
            return;
        }
        I2CMasterSlaveAddrSet(MPU9150_I2C_BASE, MPU9150_I2C_ADDRESS, false);
        I2CMasterDataPut(MPU9150_I2C_BASE, reg);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        I2CMasterSlaveAddrSet(MPU9150_I2C_BASE, MPU9150_I2C_ADDRESS, true);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        *p8Data++ = (uint8_t)I2CMasterDataGet(MPU9150_I2C_BASE);
        for(i = 0; i < (count - 2); i++)
        {
            I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
            while(I2CMasterBusy(MPU9150_I2C_BASE));
            *p8Data++ = (uint8_t)I2CMasterDataGet(MPU9150_I2C_BASE);
        }
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        *p8Data++ = (uint8_t)I2CMasterDataGet(MPU9150_I2C_BASE);
    }
    
    void MPU9150BurstWrite(uint8_t reg, unsigned int count, uint8_t *p8Data)
    {
        unsigned int i;
    
        I2CMasterSlaveAddrSet(MPU9150_I2C_BASE, MPU9150_I2C_ADDRESS, false);
        I2CMasterDataPut(MPU9150_I2C_BASE, reg);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
        for(i = 0; i < (count - 1); i++)
        {
            I2CMasterDataPut(MPU9150_I2C_BASE, *p8Data++);
            I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
            while(I2CMasterBusy(MPU9150_I2C_BASE));
        }
        I2CMasterDataPut(MPU9150_I2C_BASE, *p8Data);
        I2CMasterControl(MPU9150_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
        while(I2CMasterBusy(MPU9150_I2C_BASE));
    
    }
    

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    我将尝试并相应地返回。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    最终解决了。 问题是发送命令过程

    对于信号发送模式或突发发送模式下的每个发送通信、都需要发送地址、共享用于32V 1A 校准的 INA 219电流测量代码。

    float measuredcurrent;
    uint8_t pui32DataTx[2];
    uint8_t pui32DataRx[2];
    #define SLAVE_ADDRESS 0x40
    typedef enum CONFIG_BVOLTAGERANGE {
                CONFIG_BVOLTAGERANGE_16V =        (0x0000),  // 0-16V Range
                CONFIG_BVOLTAGERANGE_32V =        (0x2000),  // 0-32V Range
            } CONFIG_BVOLTAGERANGE;
    typedef enum CONFIG_GAIN {
                CONFIG_GAIN_1_40MV        =       (0x0000),  // Gain 1, 40mV Range
                CONFIG_GAIN_2_80MV        =       (0x0800),  // Gain 2, 80mV Range
                CONFIG_GAIN_4_160MV       =       (0x1000),  // Gain 4, 160mV Range
                CONFIG_GAIN_8_320MV       =       (0x1800),  // Gain 8, 320mV Range
              } CONFIG_GAIN;
    typedef enum CONFIG_BADCRES {
              CONFIG_BADCRES_9BIT       =       (0x0000),  // 9-bit bus res = 0..511
              CONFIG_BADCRES_10BIT      =       (0x0080),  // 10-bit bus res = 0..1023
              CONFIG_BADCRES_11BIT      =       (0x0100),  // 11-bit bus res = 0..2047
              CONFIG_BADCRES_12BIT       =      (0x0180),  // 12-bit bus res = 0..4097
              } CONFIG_BADCRES;
    typedef enum CONFIG_SADCRES {
              CONFIG_SADCRES_9BIT_1S_84US     = (0x0000),  // 1 x 9-bit shunt sample
              CONFIG_SADCRES_10BIT_1S_148US   = (0x0008),  // 1 x 10-bit shunt sample
              CONFIG_SADCRES_11BIT_1S_276US   = (0x0010),  // 1 x 11-bit shunt sample
              CONFIG_SADCRES_12BIT_1S_532US   = (0x0018),  // 1 x 12-bit shunt sample
              CONFIG_SADCRES_12BIT_2S_1060US  = (0x0048),  // 2 x 12-bit shunt samples averaged together
              CONFIG_SADCRES_12BIT_4S_2130US  = (0x0050),  // 4 x 12-bit shunt samples averaged together
              CONFIG_SADCRES_12BIT_8S_4260US  = (0x0058),  // 8 x 12-bit shunt samples averaged together
              CONFIG_SADCRES_12BIT_16S_8510US = (0x0060),  // 16 x 12-bit shunt samples averaged together
              CONFIG_SADCRES_12BIT_32S_17MS   = (0x0068),  // 32 x 12-bit shunt samples averaged together
              CONFIG_SADCRES_12BIT_64S_34MS   = (0x0070),  // 64 x 12-bit shunt samples averaged together
              CONFIG_SADCRES_12BIT_128S_69MS =  (0x0078),  // 128 x 12-bit shunt samples averaged together
          } CONFIG_SADCRES;
          typedef enum CONFIG_MODE {
                     CONFIG_MODE_POWERDOWN          =  (0x0000),
                     CONFIG_MODE_SVOLT_TRIGGERED    =  (0x0001),
                     CONFIG_MODE_BVOLT_TRIGGERED    =  (0x0002),
                     CONFIG_MODE_SANDBVOLT_TRIGGERED = (0x0003),
                     CONFIG_MODE_ADCOFF             =  (0x0004),
                     CONFIG_MODE_SVOLT_CONTINUOUS  =  (0x0005),
                     CONFIG_MODE_BVOLT_CONTINUOUS   =  (0x0006),
                     CONFIG_MODE_SANDBVOLT_CONTINUOUS = (0x0007),
                 } CONFIG_MODE;
    
    
    
    
    int16_t I2CReadRegister(uint8_t reg)
     {
         I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
         I2CMasterDataPut(I2C0_BASE, reg);
         I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
         while(I2CMasterBusy(I2C0_BASE));
    
         I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
         I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
         while(I2CMasterBusy(I2C0_BASE));
         pui32DataRx[0] = I2CMasterDataGet(I2C0_BASE);
    
         I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
         I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
         while(I2CMasterBusy(I2C0_BASE));
         pui32DataRx[1] = I2CMasterDataGet(I2C0_BASE);
         //measuredcurrent =(uint16_t)( pui32DataRx[1] << 8) |  pui32DataRx[0];
         return (((pui32DataRx[0] << 8) ) | pui32DataRx[1]);
     }
    
    
    void Current_init()
    {
    
    
    
    
        #define REG_CALIBRATION     0x05
        #define REG_CONFIG          0x00
        #define READ_CURRENT_MA     0x04
    
        uint32_t _currentDivider_mA =25;
    
        PSU_WRITE(24.00);
        SysCtlDelay(ui32SysClock/10);
        PSU_ON();
    
        VBAT_ON();
    
        //int calValue = 4096;
        int calValue = 10240;
    
        SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    
        GPIOPinConfigure(GPIO_PB2_I2C0SCL);
        GPIOPinConfigure(GPIO_PB3_I2C0SDA);
        GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
        GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
    
        I2CMasterInitExpClk(I2C0_BASE, ui32SysClock, true);
        I2CMasterEnable(I2C0_BASE);
    
    //***************************************************************
    
        pui32DataTx[1] = ((calValue) & 0xFF);
        pui32DataTx[0] = ((calValue >>8) & 0xFF);
    
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false); //false means master will write to slave
        I2CMasterDataPut(I2C0_BASE, REG_CALIBRATION);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        while(I2CMasterBusy(I2C0_BASE));
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
        I2CMasterDataPut(I2C0_BASE, pui32DataTx[0]);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
        while(I2CMasterBusy(I2C0_BASE));
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
        I2CMasterDataPut(I2C0_BASE, pui32DataTx[1]);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
        while(I2CMasterBusy(I2C0_BASE));
    
         uint16_t config = CONFIG_BVOLTAGERANGE_32V |
                                CONFIG_GAIN_8_320MV |
                                CONFIG_BADCRES_12BIT |
                                CONFIG_SADCRES_12BIT_128S_69MS |
                                CONFIG_MODE_SANDBVOLT_CONTINUOUS;
    
        pui32DataTx[1] = ((config) & 0xFF);
        pui32DataTx[0] = ((config >>8) & 0xFF);
    
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false); //false means master will write to slave
        I2CMasterDataPut(I2C0_BASE, REG_CONFIG);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    
        while(I2CMasterBusy(I2C0_BASE));
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
        I2CMasterDataPut(I2C0_BASE,pui32DataTx[0]);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
    
        while(I2CMasterBusy(I2C0_BASE));
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
        I2CMasterDataPut(I2C0_BASE, pui32DataTx[1]);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    
        while(I2CMasterBusy(I2C0_BASE));
    
    
    
           pui32DataTx[1] = ((calValue) & 0xFF);
            pui32DataTx[0] = ((calValue >>8) & 0xFF);
    
            I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false); //false means master will write to slave
            I2CMasterDataPut(I2C0_BASE, REG_CALIBRATION);
            I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
            while(I2CMasterBusy(I2C0_BASE));
            I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
            I2CMasterDataPut(I2C0_BASE, pui32DataTx[0]);
            I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
            while(I2CMasterBusy(I2C0_BASE));
            I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
            I2CMasterDataPut(I2C0_BASE, pui32DataTx[1]);
            I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
            while(I2CMasterBusy(I2C0_BASE));
    
    
            measuredcurrent=I2CReadRegister(READ_CURRENT_MA);
            measuredcurrent /= _currentDivider_mA ;
    
    
    }