0

How to connect nRF51822 using adxl345 SPI? needed help

mantap12 gravatar image

asked 2017-12-07 12:20:50 +0100

updated 2017-12-07 12:24:12 +0100

Well, I have question for this SPI connection problem. anyway,I have nRF51822 core board using adxl345 with SPI connection. that connect SDA to P5, SDO to P6, SCL to P7, CS to P8. this program works fine till connect over BLE but there's no data receive from nRF UART app but there is RX event. so in my opinion, are there wrong pin or can those default SPI nRF51 pin change to using other pin? this is my mbed program

this code:

#include "mbed.h"
#include "ble/BLE.h"
#include "ble/services/UARTService.h"
#include "Serial.h"
#include "ADXL345.h"
#include "mbed.h"

#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                           * it will have an impact on code-size and power consumption. */

#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

ADXL345 accelerometer(p5, p6, p7, p8); // (SDA, SDO, SCL, CS);
BLEDevice  ble;
DigitalOut led1(LED1);
Serial uart1(USBTX,USBRX);
UARTService *uartServicePtr;


void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 
 {

   DEBUG("Connected!\n\r");

 }


 uint8_t b[40]={'a','d','q','w'};
 void onDataWritten1(const GattWriteCallbackParams *params)
{


    if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
    uint16_t bytesRead = params->len;
    DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
   // if(sFlag == 1)
   //     ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, 4/*bytesRead*/);
    }
   }


    void periodicCallback(void)
    {
        led1 = !led1;

    }


int main() 
{
uart1.baud(9600);
int readings[3] = {0, 0, 0};
uart1.printf("Starting ADXL345 test...\n");
uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());

//Go into standby mode to configure the device.
accelerometer.setPowerControl(0x00);

//Full resolution, +/-16g, 4mg/LSB.
accelerometer.setDataFormatControl(0x0B);

//3.2kHz data rate.
accelerometer.setDataRate(ADXL345_3200HZ);

//Measurement mode.
accelerometer.setPowerControl(0x08);

char buffer [20];

// Test Daata
memset(&buffer, 0, sizeof(buffer));
int16_t reading_1 = 54;
int16_t reading_2 = 42;
int16_t reading_3 = 32;

snprintf(buffer,20,"data: %d, %d, %d\n", reading_1, reading_2, reading_3);

led1 = 1;
uart1.baud(9600);
Ticker ticker;
ticker.attach(periodicCallback, 1);

DEBUG("Initialising the nRF51822\n\r");
ble.init();
ble.onDisconnection(disconnectionCallback);
ble.onConnection(connectionCallback);
ble.onDataWritten(onDataWritten1);

/* setup advertising */
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));

ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
ble.startAdvertising();

UARTService uartService(ble);
uartServicePtr = &uartService;

while (1) {
    ble.waitForEvent();
    wait(0.1);
    accelerometer.getOutput(readings);

    //13-bit, sign extended values.
    uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);

    memset(&buffer, 0, sizeof(buffer));
    snprintf(buffer, 20, "data: %d, %d, %d\n", (int16_t)readings[0],(int16_t)readings[1], (int16_t)readings[2]);
    ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer, sizeof(buffer), false);

  }

 }
edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
1
sigurdon gravatar image

answered 2017-12-08 15:20:48 +0100

mantap12 gravatar image

updated 2017-12-09 00:23:01 +0100

Hi,

The nRF5x have flexible GPIO mapping, so all pins can be individually mapped to interface blocks for layout flexibility. You can therefore use any free GPIO pin you have on your board for SPI.

So make sure that the SPI pins you are using is not being used by anything else on your board. E.g. on the nRF51-DK, pin 8,9,10 and 11 is used for UART.

edit flag offensive delete publish link more

Comments

thank you for your answer mr. Sigurd.. I'm using ADXL345 connection between SDA to P5, SDO to P6, SCL to P7, CS to P8. so CS connected to P8 must be change another pin or CS must be active High through VCC? and I'm using pin 9 & 11 for UART.

mantap ( 2017-12-09 00:35:49 +0100 )editconvert to answer

Yes, try to use another pin for CS, and see if that solves the issue.

Sigurd ( 2017-12-12 13:30:20 +0100 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

Question Tools

1 follower

Stats

Asked: 2017-12-07 12:20:50 +0100

Seen: 30 times

Last updated: des. 07