- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi..
can anyone help me in interfacing axdl345 accelerometer to psoc5 using SPI
Solved! Go to Solution.
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This thread that may be of some help -
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yeah.. i already posted in that forum also 😞
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Do you have any actual problems with that connections? I used an UDB-based SPI master component to interface with a ADXL375 which is similar to the ADXL345. Its quite straight-forward in the end - just read the manual how to read and write registers.
void waitFinished() { while(0==(SPI_ReadTxStatus()&SPI_STS_SPI_DONE)) { /* Wait while Master completes transfer */ } } void writeRegister(uint8 address, uint8 value) { Reg_CS_Write(0); SPI_WriteTxData(address); SPI_WriteTxData(value); waitFinished(); Reg_CS_Write(1); SPI_ClearRxBuffer(); } int main() { /* Place your initialization/startup code here (e.g. MyInst_Start()) */ CyGlobalIntEnable; LCD_Start(); SPI_Start(); Reg_CS_Write(1); // disable CyDelay(10); Reg_CS_Write(0); CyDelayUs(5); SPI_WriteTxData(0x80); // read register 0 SPI_WriteTxData(0x00); // dummy byte waitFinished(); Reg_CS_Write(1); LCD_Position(1,0); LCD_PrintDecUint16(SPI_GetRxBufferSize()); SPI_ReadRxData(); // read dummy byte uint32 id=SPI_ReadRxData(); LCD_Position(1,0); LCD_PrintString("id="); LCD_PrintHexUint8((uint8)id); CyDelay(2000); LCD_ClearDisplay(); writeRegister(0x2c, 0x07); // set FIFO to streaming writeRegister(0x38, 0x88); // enable measurement writeRegister(0x2D, 0x08);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
is it a 3 axis accelerometer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
// ADXL345 Register Definition
#define _POWER_CTL 0x2D
#define _DATA_FORMAT 0x31
#define _BW_RATE 0x2C
#define _DATAX0 0x32
#define _DATAX1 0x33
#define _DATAY0 0x34
#define _DATAY1 0x35
#define _DATAZ0 0x36
#define _DATAZ1 0x37
#define _FIFO_CTL 0x38
#define _SPI_READ 0x80
#define _SPI_WRITE 0x00
#define _SPEED 0x0F // Buffer Speed - 3200Hz
//
sbit CS_bit at PORTB0_bit;
sbit CS_Direction_bit at DDB0_bit;
unsigned short temp;
char out[16];
int readings[3] = {0, 0, 0}; // X, Y and Z
void ADXL345_Write(unsigned short address, unsigned short data1) {
unsigned short internal = 0;
internal = address | _SPI_WRITE; // Register and Write bit
CS_bit = 0;
SPI1_Write(internal);
SPI1_Write(data1);
CS_bit = 1;
}
unsigned short ADXL345_Read(unsigned short address) {
unsigned short internal = 0;
internal = address | _SPI_READ; // Register and Read bit
CS_bit = 0;
SPI1_Write(internal);
internal = SPI1_Read(0);
CS_bit = 1;
return internal;
}
void main() {
CS_bit = 1; // Deselect Chip before initalization
CS_Direction_bit = 1; // Set direction
// SPI Initialization, Fcy/4, when idle SPI High, Data transmit on low to high edge
SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV4, _SPI_CLK_HI_LEADING);
// UART Initialization
UART1_Init(9600);
UART1_Write_Text("Starting ADXL345 test.");
UART1_Write(13);
UART1_Write(10);
Delay_ms(1000);
// Go into standby mode to configure the device.
ADXL345_Write(_POWER_CTL, 0x00);
// Full resolution, +/-16g, 4mg/LSB.
ADXL345_Write(_DATA_FORMAT, 0x0B);
// Set data rate.
ADXL345_Write(_BW_RATE, _SPEED);
// Measurement mode.
ADXL345_Write(_POWER_CTL, 0x08);
while (1) {
Delay_ms(800);
// Read X Hi
readings[0] = ADXL345_Read(_DATAX0) << 8;
// Read X Lo
readings[0] = readings[0] | ADXL345_Read(_DATAX1);
// Read Y Hi
readings[1] = ADXL345_Read(_DATAY0) << 8;
// Read Y Lo
readings[1] = readings[1] | ADXL345_Read(_DATAY1);
// Read Z Hi
readings[2] = ADXL345_Read(_DATAZ0) << 8;
// Read Z Lo
readings[2] = readings[2] | ADXL345_Read(_DATAZ1);
// Send values via UART
// X
UART1_Write_Text("X: ");
IntToStr(readings[0], out);
UART1_Write_Text(out);
UART1_Write(13);
UART1_Write(10);
Delay_ms(100);
// Y
UART1_Write_Text("Y: ");
IntToStr(readings[1], out);
UART1_Write_Text(out);
UART1_Write(13);
UART1_Write(10);
Delay_ms(100);
// Z
UART1_Write_Text("Z: ");
IntToStr(readings[2], out);
UART1_Write_Text(out);
UART1_Write(13);
UART1_Write(10);
//
UART1_Write(13);
UART1_Write(10);
//
//
}
}
this code doesnt work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is always easier for us to use the help of the IDE to analyze your code. Best is always to post your complete project or at least a reduced project that shows the error. To post use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to define sbit in psoc5?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When sbit means "set bit" you can define it in C-language as
#define sbit(Val,BitNo) (Val |= (0x0001ul << BitNo))
Happy coding
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Each write also reads data from the SPI bus. That means you code has two problems:
- your SPIWriteTxData calls fill the RX buffer, but you don't read the values, so they stay there
- when you actually want to read data from the SPI bus, the master needs to write a dummy byte. YOu don't so your readRxData() call just reads ther value that got read when the adress is written to the bus (which is just garbage)
Look at the place where I read the chip ID in my code, that shows how the reads needs to be done. Also note that after the register writes I clear the RX buffer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
hmmmm......even im getting errors in the UART section..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What kind of errors do you get? Maybe its a good idea to go piece by pieve to get it going, starting with what you need for debugging first (e.g. UART or LCD) and then go to the more difficult parts...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the screenshot I cannot see any error messages, the image is cut off. For compile errors I suggest you open a new thread, since this has nothing to do with the interface to the ADXL345 as such.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Now you are confusing me - what has a dummy variable to do with the UART, and why do you need the UART to control the ADXL? Ajnd what variable are you talking about?
Try to read the ADXL device ID first, using the code I posted above (it writes to a LCD, which is available on the -050 kits, using the CharLCD_MP component. It also uses an UDB-based SPI master). It should be the same for the ADXL345, but check the register IDs first.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Looks like you have an extra brace, "}", at bottom of code view screen
which will produce a lot of errors. Just before the "[] END OF FILE" comment.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
hi..how do u display the values on lcd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Look at the LCD_ functions in my code. Or just look at the API description of the TextLCD component.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
how I can read this data?
#define _DATAX0 0x32
#define _DATAX1 0x33
#define _DATAY0 0x34
#define _DATAY1 0x35
#define _DATAZ0 0x36
#define _DATAZ1 0x37
sekond question how I can test my accelerometer adxl345 with psoc 5 by using debug or UART? the protocol I used is SPI master...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please don't post new questions in a thread thats already one year dead. Especally when you are already asking in another thread.
Please read the data sheet for the SPI master, and the data sheet for the ADXL345. Especially the latter explains how to communicate with it.