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 interfacing with adxl-345

Other Parts Discussed in Thread: TM4C1294NCPDT

void InitConsole(void) {
// Enable GPIO port A which is used for UART0 pins.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

// Enable UART0 so that we can configure the clock.
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

// Use the internal 16MHz oscillator as the UART clock source.
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

// Select the alternate (UART) function for these pins.
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

// Initialize the UART for console I/O.
UARTStdioConfig(0, 115200, 16000000);
}

void InitSPI(void) {
// The SSI2 peripheral must be enabled for use.
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2);

// Enable Port D
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

// SETUP SPI CS Pin (to output)
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);

// Set SPI CS to HIGH (active-low)
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6, 0xFF);

GPIOPinConfigure(GPIO_PD3_SSI2CLK);
GPIOPinConfigure(GPIO_PD2_SSI2FSS);
GPIOPinConfigure(GPIO_PD0_SSI2DAT1);
GPIOPinConfigure(GPIO_PD1_SSI2DAT0);

// Configure the GPIO settings for the SSI pins.
GPIOPinTypeSSI(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |
GPIO_PIN_3);

// Configure and enable the SSI port for SPI master mode. Use SSI0,
// system clock supply, idle clock level low and active low clock in
// freescale SPI mode, master mode, 1MHz SSI frequency, and 8-bit data.
// For SPI mode, you can set the polarity of the SSI clock when the SSI
// unit is idle. You can also configure what clock edge you want to
// capture data on. Please reference the datasheet for more information on
// the different SPI modes.
SSIConfigSetExpClk(SSI2_BASE, , SSI_FRF_MOTO_MODE_3,
SSI_MODE_MASTER, 1000000, 8);

// Enable the SSI0 module.
SSIEnable(SSI2_BASE);

// Read any residual data from the SSI port. This makes sure the receive
// FIFOs are empty, so we don't read any unwanted junk. This is done here
// because the SPI SSI mode is full-duplex, which allows you to send and
// receive at the same time. The SSIDataGetNonBlocking function returns
// "true" when data was returned, and "false" when no data was returned.
// The "non-blocking" function checks if there is any data in the receive
// FIFO and does not "hang" if there isn't.
uint32_t scrap;
while(SSIDataGetNonBlocking(SSI2_BASE, &scrap));
}


int main(void) {

// Set the clocking to run directly from the external crystal/oscillator.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);

// Set up UART Serial Output
InitConsole();

// Set up SSI2 for SPI Communication
InitSPI();

UARTprintf("............................................\n");

GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);
SSIDataPut(SSI2_BASE, 0x31); // DATA_FORMAT
while(SSIBusy(SSI2_BASE));
SSIDataPut(SSI2_BASE, 0x01); // +/- 4g
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xFF);

GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);
SSIDataPut(SSI2_BASE, 0x2D); // POWER_CTL
while(SSIBusy(SSI2_BASE));
SSIDataPut(SSI2_BASE, 0x08);
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xFF);

while (1) {

char values[10];
int16_t x, y, z;

char address = 0x80 | DATAX0 | 0x40; //Set MSB for read, start at X0 and enable multi-byte

GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);;

SSIDataPut(SSI2_BASE, address); // ask for data

for(int i=0; i < 6; i++){
SSIDataPut(SSI2_BASE, 0x00);
while(SSIBusy(SSI2_BASE));
SSIDataGet(SSI2_BASE, &values[i]);
}

GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xFF);

for(int i = 0; i < 6; i++){
UARTprintf("%i\t", values[i]);
}
UARTprintf("\n");

//The X value is stored in values[1] and values[2].
x = ((int16_t)values[1]<<8)|(int16_t)values[0];
//The Y value is stored in values[2] and values[3].
y = ((int16_t)values[3]<<8)|(int16_t)values[2];
//The Z value is stored in values[4] and values[5].
z = ((int16_t)values[5]<<8)|(int16_t)values[4];

UARTprintf("%i\t\t%i\t\t%i\n", x, y, z);

SysCtlDelay(50 * (SysCtlClockGet() /1000 /3));
}

return(0); // will never return

when i execute this program i am getting output as "242" for allthe axis.

can any one suggest me  where am i going wrong...

  • Hello Rahul,

    Can you explain what you are trying to do? What is adxl-345? How are you interfacing it with the MCU? What output are you expecting?

    Also, when posting code on the forum please use "Use rich formatting" and then click on the Syntaxhighlighter (with the icon Insert code using Syntaxhighlighter) . This makes the code easier to read and hence might get you a response.

    Thanks,

    Sai

  • Hi Stellaris


    i have interfaced ADXL-345 acclerometer with TM4c1294ncpdt launch pad via SPI communication.
    i am getting the data from each of axis of acclerometer (x,y,z), an di have to convert these raw data to "G's" value.
    i have used the formula to calculate "G" value from raw data of each axis.
    however i am not able to get different values at an output whenever i am moving the ADxl-345 board.
    can u guid me what may be the problem???
  • Hello Rahul,

    Read the manual of the accelerometer to find out if any initialization has to be performed, before it can start sending meaningful data. As part of the initilization routine, you might also have to connect a GPIO from the MCU to the chip select pin on the accelerometer and assert the GPIO to enable the accelerometer.

    Make sure that the accelerometer is still working and not gone bad. Easy way to do this is to try with a spare one. You can also hook up the SPI and chip select (if present) pins of the accelerometer to the oscilloscope and monitor the communication to see if the chip select and the SPI pins are at the correct level.

    Thanks,
    Sai
  • hello sai,

    my ADXl-345 is working fine bcz when i wanted to read device ID of it i got the device iD of ADXL-345.so i gt to know that ADXL-working fine.
    initilizaion i have done perfectly i have interfaced MCU gpio's with Acclerometer,however iam still geting some values.
  • Hi,

    Read also the AN-1077.pdf for minimum initialisation sequence, which is missing from your code (register addresses 0x31, 0x2D, 0x2E with corresponding data).

    Also, in your code, PD5, PD6 are declared as CS, although further PD2 is used for that function; if you think PD2 is the real CS, managed by you, then you should not declare that pin as SSIFSS.

  • hi

    i have declared

    GPIOPinConfigure(GPIO_PD3_SSI2CLK);
    GPIOPinConfigure(GPIO_PD2_SSI2FSS);
    GPIOPinConfigure(GPIO_PD0_SSI2DAT1);
    GPIOPinConfigure(GPIO_PD1_SSI2DAT0);

    other initilization is wrong i deleted it.
    can u tell me what is meaning of this statement "Read also the AN-1077.pdf for minimum initialisation sequence, which is missing from your code (register addresses 0x31, 0x2D, 0x2E with corresponding data)."

    i am not able to enable multibyte.

    and one more thing should i make the acclerometer from "measurement mode" to some othe rmode such as "fifo_ctl" mode etc;
  • Hi,

    There are a lot of info on AD product page, that .pdf application note is just one example.

    Also, see these links below, the third one has initialisations routines written in C - just to use them, but with some care - read it first, look for lowest level SPI routines and only modify them for Tiva micros: