Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20703 Discussions

DE0-nano's adxl345 accelerometer via SPI-3 wire

Altera_Forum
Honored Contributor II
2,120 Views

Hello, 

 

Recently I started a project in wich I have to get the datas from the 3 axis of the accelerometer ADXL345 of the DE0-nano board. 

I only have some skills in VHDL but i can understand verilog. 

So I started from the project DE0_Nano_Gsensor that is in the CD included with the board. This project only give the datas in the X-axis, and i tried to analyze the code (spi_controller.v, spi_ee_config.v) in order to change it to add the 2 other axis, but i really don't understand them.  

 

The fact is that i don't understand the communication between the ADXL and the Verilog program. 

 

So my question is, what do i have to change in the original program to get the 3 axis datas, or if you have some advices to help me to start a VHDL/verilog program in order to get the 3 axis datas with SPI communication, cause i am kinda stuck :(.
0 Kudos
9 Replies
Altera_Forum
Honored Contributor II
841 Views

 

--- Quote Start ---  

Hello, 

 

Recently I started a project in wich I have to get the datas from the 3 axis of the accelerometer ADXL345 of the DE0-nano board. 

I only have some skills in VHDL but i can understand verilog. 

So I started from the project DE0_Nano_Gsensor that is in the CD included with the board. This project only give the datas in the X-axis, and i tried to analyze the code (spi_controller.v, spi_ee_config.v) in order to change it to add the 2 other axis, but i really don't understand them.  

 

The fact is that i don't understand the communication between the ADXL and the Verilog program. 

 

So my question is, what do i have to change in the original program to get the 3 axis datas, or if you have some advices to help me to start a VHDL/verilog program in order to get the 3 axis datas with SPI communication, cause i am kinda stuck :(. 

--- Quote End ---  

 

 

I'm actually not comfort with Verilog. I just made a code that can access the ADXL345, I know because a I read the first register and it gave me the right code, but I’m still having some problems with the axis reading, they oscillate a lot =/. But about your problem. I don't know if you code does it, but before the SPI-3 (SPI-4 on DE0-nano is impossible) you must change a register value, because the default communication SPI is 4, as you don't have 4 pins, you must communicate through I2C, reconfigure the register, so the SPI-4 become SPI-3.
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

 

--- Quote Start ---  

.... I don't know if you code does it, but before the SPI-3 (SPI-4 on DE0-nano is impossible) you must change a register value, because the default communication SPI is 4, as you don't have 4 pins, you must communicate through I2C, reconfigure the register, so the SPI-4 become SPI-3. 

--- Quote End ---  

 

 

No, this should not be necessary. If you look at the SPI-3 vs SPI-4 protocol, the Write would be the same so you should be able to configure 0x31 to 3-wire SPI via SPI and then do all your reads in 3-wire mode. The only trick is that there is another I2C device on the same bus on the DE0 Nano board, so you must structure your SPI communications so that you do not trigger an I2C Start or Stop frame. 

 

I have done this and can successfully read the DEVID register via SPI. 

 

I am not familiar with Verilog, so I don't know if all Verilog is like the sample or they are just particular bad coders, but I couldn't really follow the sample either, so I wrote my own version from scratch in VHDL. I have generic modules to read/write via SPI so it now should be a relatively short matter to configure all the registers I need to read all 3-axis and grab the data.
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

I have now working sample. (Actually during the process of debugging, I also ended up coding up the I2C version as well). At any rate, I have some VHDL that strictly uses SPI-3 wire mode to configure the ADXL345 and is able to configure any register and read back the results. I'm not sure if it is strictly necessary, but I did have to be careful to encode the SPI communication to make sure it does not initiate any I2C start/stop frame. 

 

My test program sets the range to smallest range then reads the result back continuously on the z-axis and shows the results on the 8 LED. It flashes left half for negative G and right for positive. Obviously, I have scaled the 16-bit result down to 4-bits in the process. 

 

You can contact me if you need more info, not sure if I should post full code here.
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

 

--- Quote Start ---  

I have now working sample. (Actually during the process of debugging, I also ended up coding up the I2C version as well). At any rate, I have some VHDL that strictly uses SPI-3 wire mode to configure the ADXL345 and is able to configure any register and read back the results. I'm not sure if it is strictly necessary, but I did have to be careful to encode the SPI communication to make sure it does not initiate any I2C start/stop frame. 

 

My test program sets the range to smallest range then reads the result back continuously on the z-axis and shows the results on the 8 LED. It flashes left half for negative G and right for positive. Obviously, I have scaled the 16-bit result down to 4-bits in the process. 

 

You can contact me if you need more info, not sure if I should post full code here. 

--- Quote End ---  

 

 

Hi Issac 

I am working with accelerometer FPGA DE0-Nano and searching help the VHDL. As do contact you? 

How see your code? 

 

Thank you 

Nouara 

Would
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

Well, where exactly are you running into problems? I have no problem sending you my code, but I am also not going to do your work for you if you've not put in any effort yet. Maybe you can post some of your code or just describe where you are running into problems. 

 

BTW, I would advise that you write generic blocks that can handle SPI or I2C communication to the ADXL345, not hardcoding specific bit sequences as I see some people on this forum try to do.
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

hello.. Can you please help us on the verilog code to obtain the two axis of the accelerometer.. i.e. X and Y axis?we dont have any idea how to change the demo code from having two axis readings.,Help please.,Thankyou

0 Kudos
Altera_Forum
Honored Contributor II
841 Views

 

--- Quote Start ---  

hello.. Can you please help us on the verilog code to obtain the two axis of the accelerometer.. i.e. X and Y axis?we dont have any idea how to change the demo code from having two axis readings.,Help please.,Thankyou 

--- Quote End ---  

 

 

Well I'm sorry, but I'm not so familiar with Verilog, only VHDL. If you want to work with that, I can maybe help. 

What do you mean you don't have any idea how to proceed? Can you change the demo code to read Y instead of X axis? Or Z axis? You have to be able to at least do that, or else you are in way over your head and you need to step back and either bone up on Verilog or figure out how to read a datasheet. 

 

You ought to be able to describe, in words, what registers you need to set and which ones to read in order to poll multiple axis. Yes? If you can post your pseudo-code then we can maybe work on translating that to Verilog or VHDL.
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

 

--- Quote Start ---  

Well I'm sorry, but I'm not so familiar with Verilog, only VHDL. If you want to work with that, I can maybe help. 

What do you mean you don't have any idea how to proceed? Can you change the demo code to read Y instead of X axis? Or Z axis? You have to be able to at least do that, or else you are in way over your head and you need to step back and either bone up on Verilog or figure out how to read a datasheet. 

 

You ought to be able to describe, in words, what registers you need to set and which ones to read in order to poll multiple axis. Yes? If you can post your pseudo-code then we can maybe work on translating that to Verilog or VHDL. 

--- Quote End ---  

 

 

 

Yes I can change the demo code to read other axes. But to combine the two axes..I cant
0 Kudos
Altera_Forum
Honored Contributor II
841 Views

Hi, i'm currently working on to get the 3-axis data from accelerometer to drive my robot 

is there anyone can share the code to me??? 

i have to use the data to drive my quadcopter to fly in much higher stable mode 

tq
0 Kudos
Reply