C learning SPI example with MMA7455 only gives values with all bits set??
twm47099
Posts: 867
Today,
I tried duplicating the SPI Example using the spiasm library (other thread), It seemed to work, but I only printed values of 3,7,15, 31, 63.127 and -1. so i tried running the unmodified Learn SPI Example. (from the Learn, Examples, Protocols folder. It gave the same results. (the example says that the result should be z = 64 when the board is set flat and -64 when inverted. My results were 127 when flat and =-1 when inverted. When printing in binary format it was clear that no bits in the number (except leading bits were zero.
This is the first time using the MAA7455 so , I'm not sure if it is working correctly.
I also tried reading the x and y axies, they gave the same type of results.
thanks
tom
See the code below (C Learning example code).
I tried duplicating the SPI Example using the spiasm library (other thread), It seemed to work, but I only printed values of 3,7,15, 31, 63.127 and -1. so i tried running the unmodified Learn SPI Example. (from the Learn, Examples, Protocols folder. It gave the same results. (the example says that the result should be z = 64 when the board is set flat and -64 when inverted. My results were 127 when flat and =-1 when inverted. When printing in binary format it was clear that no bits in the number (except leading bits were zero.
This is the first time using the MAA7455 so , I'm not sure if it is working correctly.
I also tried reading the x and y axies, they gave the same type of results.
thanks
tom
See the code below (C Learning example code).
/* MMA7455 Test Z Axis with SPI.c Demonstrates using SPI communication to configure and then monitor a Parallax MMA7455 3-Axis Accelerometer Module. http://learn.parallax.com/propeller-c-simple-protocols/spi-example */ #include "simpletools.h" // Include simpletools lib signed char z; // Z-axis value int main() // Main function { high(6); // CS line high (inactive) low(8); // CLK line low low(6); // CS -> low start SPI shift_out(7, 8, MSBFIRST, 7, 0b1010110); // Write MCTL register shift_out(7, 8, MSBFIRST, 1, 0b0); // Send don't-care bit shift_out(7, 8, MSBFIRST, 8, 0b01100101); // Value for MCTL register pause(5); high(6); // CS -> high stop SPI pause(1); while(1) // Main loop { low(6); // CS low selects chip shift_out(7, 8, MSBFIRST, 7, 0b0001000); // Send read register address shift_out(7, 8, MSBFIRST, 1, 0b0); // Send don't-care value z = shift_in(7, 8, MSBPRE, 8); // Get value from register pause(5); high(6); // De-select chip print("%c z = %b%c", HOME, z, CLREOL); // Display measurement pause(500); // Wait 0.5 s before repeat } }
Comments
Tom
I did check and second time through I changed my wiring to match the pin numbers in each code example. (First time through I changed pin numbers in code to match my wiring.)
I got the same results. The strange thing is that the leading binary digits are correct, but after the highest bit that is "1" all of the lower order bits are also 1 as opposed to mix of 1's and 0's. So 64 shows up as 127, -64 shows up as -1 (all bits set), etc.
I can't find anything in the data sheets that say the device has a mode that would do that, and I can't visualize a mis-wiring that would give those results (correct except all lower order bits set.) So I'm wondering if the part might be defective?
Thanks
Tom
Andy
Andy,
I got the same results with all three methods: the Spin example on the product page, the C program in the tutorial, and my spiasm library.
I'll check the link you posted above.
Tom
On page 16 of the Freescale document is a strong recommendation to disable I2C when using SPI because of a possible interference from the I2C interface while using SPI. The CS pin is the only determinant as to which protocol is used, and when CS is high the chip is in I2C mode .
from page 16:
Note: It is recommended to disable I2C during SPI communication to avoid communication errors between devices using a different
SPI communication protocol. To disable I2C, set the I2CDIS bit in I2C Device Address register using SPI.
Hal,
Thanks for the suggestion. Unfortunately, I got the same results.
Tom
Andy,
I tried the SPI Example tutorial with the simpletools library from the link you posted, but I got the same results.
Tom
It will take me some time to get a picture and post it. I'm also not at my computer that has the code with the i2cdis modification. I'll post everything tomorrow.
I appreciate your help. .
Tom
Ok so I ran the simple devices code (after I rewired the device to match its directions and changed all the pin definitions in the other programs to match.) But first I reran my program and the SPI example from the protocols tutorial.
Those 2 programs gave me the same bad results. Then I tried the simple devices version (this uses a special MMA7455 library and simplifies all the calls).
Well, the simple version worked (so it's not a bad device.)
Then I tried my library version - it worked, almost -- I got the correct values divided by 4 (64 showed up as 16).
Then I tried the SPI example from the simple protocols tutorial. It gave the correct readings (64 = 64). Then I retried my code and it gave the correct values.?!?
If I disconnected the prop board (I'm using a quickstart board) from power, when I reconnected, the problems started again, but were resolved in the same way.
So now I need To figure out what the simple devices library is doing, how the simple protocol code is affected by that, and what's going in my code. (I also have to figure out if version 0.98.1 of Simpletools library has anything to do with it.)
It is progress.
Tom
What was wrong - I thought I had fixed the MSBFIRST routine in the assembly language o the SPI driver, but I didn't save it properly. Also in the C program, I had to add some pauses after the write commands and before the "high(CS);" command. As little as pause(1) was sufficient.
Here's the program listing that works along with a zip of the library.
Thanks for all the help
Tom