PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Examples
Protocols
MMA7455 Test Z Axis with SPI.c
1
/*
2
MMA7455 Test Z Axis with SPI.c
3
4
Demonstrates using SPI communication to configure and then monitor a
5
Parallax MMA7455 3-Axis Accelerometer Module.
6
7
http://learn.parallax.com/propeller-c-simple-protocols/spi-example
8
*/
9
10
#include "
simpletools.h
"
// Include simpletools lib
11
12
signed
char
z;
// Z-axis value
13
14
int
main
()
// Main function
15
{
16
high
(6);
// CS line high (inactive)
17
low
(8);
// CLK line low
18
low
(6);
// CS -> low start SPI
19
20
shift_out
(7, 8,
MSBFIRST
, 7, 0b1010110);
// Write MCTL register
21
shift_out
(7, 8,
MSBFIRST
, 1, 0b0);
// Send don't-care bit
22
shift_out
(7, 8,
MSBFIRST
, 8, 0b01100101);
// Value for MCTL register
23
24
high
(6);
// CS -> high stop SPI
25
pause
(1);
26
27
while
(1)
// Main loop
28
{
29
low
(6);
// CS low selects chip
30
shift_out
(7, 8,
MSBFIRST
, 7, 0b0001000);
// Send read register address
31
shift_out
(7, 8,
MSBFIRST
, 1, 0b0);
// Send don't-care value
32
33
z =
shift_in
(7, 8,
MSBPRE
, 8);
// Get value from register
34
35
high
(6);
// De-select chip
36
print
(
"%c z = %d%c"
,
HOME
, z,
CLREOL
);
// Display measurement
37
pause
(500);
// Wait 0.5 s before repeat
38
}
39
}
shift_out
void shift_out(int pinDat, int pinClk, int mode, int bits, int value)
Send data to a synchronous serial device.
Definition:
shiftOut.c:19
MSBPRE
#define MSBPRE
For use with shift_in. Stands for most significant bit first, pre-clock.
Definition:
simpletools.h:381
HOME
#define HOME
HOME character (1) sends SimpleIDE Terminal's cursor to top-left "home" position.
Definition:
simpletools.h:233
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition:
libws2812.c:125
low
void low(int pin)
Set an I/O pin to output-low.
Definition:
low.c:19
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
print
int print(const char *format,...) __attribute__((format(printf
Print format "..." args to the default simple terminal device. The output is limited to 256 bytes.
shift_in
int shift_in(int pinDat, int pinClk, int mode, int bits)
Receive data from a synchronous serial device.
Definition:
shiftIn.c:20
main
int main(void)
Definition:
GraphicsTest.c:20
MSBFIRST
#define MSBFIRST
For use with shift_out. Stands for most significant bit first.
Definition:
simpletools.h:417
CLREOL
#define CLREOL
CLREOL character (11) erases all SimpleIDE Terminal characters to the right of the cursor.
Definition:
simpletools.h:323
high
void high(int pin)
Set an I/O pin to output-high.
Definition:
high.c:19
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17