ADXL345 Tiva TM4c123 launchpad spi haberleşmesi

Merhaba

Bir projede kullanmak üzere yerli bir siteden ADXL345 siparişi verdik. Kit olarak elimize ulaşan bu ürün ile I2C ile gayet kolay haberleştim fakat iş SPI’a gelince uzun bir süre uğraşmama rağmen bir türlü haberleşme sağlanamadı. Modülü incelediğim zamanda bazı girişlerin I2C’e uygun olarak pull-up yapılmış olduğunu fark ettim. Bunun üzerine bu dirençleri kaldırarak iletişimi sağladım. Texas Instruments in Tiva Tm4c123g serisi launchpad ile aşağıdaki gibi kodlayarak haberleşmeyi sağladım. ilgili kitte SPI haberleşmesi SSI olarak tanımlanmış. İşlemcinin SSI0 spi çıkışını ve  GIPIO PB3 pinini cs çıkışı olarak kullandım

 

 

#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include “inc/hw_memmap.h”
#include “inc/hw_types.h”
#include “driverlib/gpio.h”
#include “driverlib/pin_map.h”
#include “driverlib/sysctl.h”
#include “driverlib/uart.h”
#include “utils/uartstdio.h”
#include “driverlib/ssi.h”
#include “driverlib/ssi.c”

#define ADXL345_DEVID 0x00 // R Device ID
#define ADXL345_THRESH_TAP 0x1D // R/W Tap threshold
#define ADXL345_OFSX 0x1E // R/W X-axis offset
#define ADXL345_OFSY 0x1F // R/W Y-axis offset
#define ADXL345_OFSZ 0x20 // R/W Z-axis offset
#define ADXL345_DUR 0x21 // R/W Tap duration
#define ADXL345_Latent 0x22 // R/W Tap latency
#define ADXL345_Window 0x23 // R/W Tap window
#define ADXL345_THRESH_ACT 0x24 // R/W Activity threshold
#define ADXL345_THRESH_INACT 0x25 // R/W Inactivity threshold
#define ADXL345_TIME_INACT 0x26 // R/W Inactivity time
#define ADXL345_ACT_INACT_CTL 0x27 // R/W Axis enable control for activity and inactivity detection
#define ADXL345_THRESH_FF 0x28 // R/W Free-fall threshold
#define ADXL345_TIME_FF 0x29 // R/W Free-fall time
#define ADXL345_TAP_AXES 0x2A // R/W Axis control for single tap/double tap
#define ADXL345_ACT_TAP_STATUS 0x2B // R Source of single tap/double tap
#define ADXL345_BW_RATE 0x2C // R/W Data Rate and power mode control
#define ADXL345_POWER_CTL 0x2D // R/W Power-saving features control
#define ADXL345_INT_ENABLE 0x2E // R/W Interrupt enable control
#define ADXL345_INT_MAP 0x2F // R/W Interrupt mapping control
#define ADXL345_INT_SOURCE 0x30 // R Source of interrupts
#define ADXL345_DATA_FORMAT 0x31 // R/W Data format control
#define ADXL345_DATAX0 0x32 // R X-Axis Data 0
#define ADXL345_DATAX1 0x33 // R X-Axis Data 1
#define ADXL345_DATAY0 0x34 // R Y-Axis Data 0
#define ADXL345_DATAY1 0x35 // R Y-Axis Data 1
#define ADXL345_DATAZ0 0x36 // R Z-Axis Data 0
#define ADXL345_DATAZ1 0x37 // R Z-Axis Data 1
#define ADXL345_FIFO_CTL 0x38 // R/W FIFO control
#define ADXL345_FIFO_STATUS 0x39 // R FIFO status
#define ADXL345_OUTPUTS 0x32
#define ADXL345ADR 0x53
int32_t ulindex;
void InitConsole()
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 115200, 16000000);
}
void Setup_SSI()
{
// Configures the system clock, the LM3S6965 has a 8MHz crystal onboard
// SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |SYSCTL_XTAL_8MHZ);
// Enable the SSI0 peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// The SSI0 peripheral is on Port A and pins 2,3,4 and 5.
// Enable GPIO port A
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
uint32_t clockk=SysCtlClockGet();
SysCtlClockGet();
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 1000000, 8);
// This function/s configures the pin muxing on port A pins 2,3,4 and 5
// It uses the values DEFINED at the top of the program and takes 1 parameter
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);
SysCtlDelay(4);
// Configures the pins for use by the SSI, takes 2 parameters
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
GPIO_PIN_2);
SysCtlDelay(4);
// Configures the SSI port for SPI master mode, it takes 6 parameters
//SSIConfigSetExpClk(SSI0_BASE, 2000000 , SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 1600 , 8 );

GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xFF);
// Enables the SSI0 module
SSIEnable(SSI0_BASE);
SysCtlDelay(4);
uint32_t scrap;
while(SSIDataGetNonBlocking(SSI0_BASE, &scrap));
}
void SPIWrite(uint32_t SPI_BASE , uint16_t SPI_WAdres ,uint16_t SPI_Wdata ){
//uint32_t send_data=0;
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x00);
SSIDataPut(SPI_BASE, SPI_WAdres);
while(SSIBusy(SPI_BASE));
SSIDataPut(SPI_BASE, SPI_Wdata);
while(SSIBusy(SPI_BASE));
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xff);
}
int32_t SPIRead(uint32_t SPI_BASE , uint32_t SPI_RAdres ){
int32_t receive_data_adress=0,values=0;
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x00);
receive_data_adress=SPI_RAdres|0x80;
SSIDataPut(SPI_BASE, receive_data_adress);
while(SSIBusy(SPI_BASE));
SSIDataPut(SPI_BASE, 0xff);
while(SSIBusy(SPI_BASE));
//SSIDataGet(SPI_BASE, &values);
while(SSIDataGetNonBlocking(SPI_BASE, &values));

GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xff);
return values;
}

int16_t SPIAxesRead(uint32_t SPI_BASE , uint32_t SPI_RAdres){
int32_t receive_data_adress=0,values=0, values2=0;
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x00);
receive_data_adress=SPI_RAdres|0x80;
SSIDataPut(SPI_BASE, receive_data_adress);
//while(SSIBusy(SPI_BASE));
SSIDataPut(SPI_BASE, 0xff);
while(SSIBusy(SPI_BASE));
while(SSIDataGetNonBlocking(SPI_BASE, &values));

GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xff);
SysCtlDelay(3);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x00);
receive_data_adress=SPI_RAdres|0x80;
SSIDataPut(SPI_BASE, receive_data_adress+1);
//while(SSIBusy(SPI_BASE));
SSIDataPut(SPI_BASE, 0xff);
while(SSIBusy(SPI_BASE));
while(SSIDataGetNonBlocking(SPI_BASE, &values2));

GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xff);
values2=values2<<8;
values2=values2+values;
return values2;

}
long int aa = 0x31;
void main(){

SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
InitConsole();
Setup_SSI();

while(1) {
uint8_t values[8]=0;
int16_t datax,datay,dataz=0;
SPIWrite(SSI0_BASE,ADXL345_POWER_CTL,0x08);
SPIWrite(SSI0_BASE,ADXL345_DATA_FORMAT,0x01);
//SPIRead(SSI0_BASE,0x2D,0x08);

while (1) {
SysCtlDelay(10000);
datax=SPIAxesRead(SSI0_BASE,ADXL345_DATAX0);
datay=SPIAxesRead(SSI0_BASE,ADXL345_DATAY0);
dataz=SPIAxesRead(SSI0_BASE,ADXL345_DATAZ0);
UARTprintf(“%i,%i,%i\n”, datax,datay,dataz);
}

UARTprintf(“———– \n”);
SysCtlDelay(10000);
}

}

Leave a Reply