UT 14: SPI
This example shows how to use some SPI functions and how to configure read and configure

Required Materials

1 x Waspmote 1 x MiniUSB wire 1 x Battery

Notes

This example was developed with a LoRa module. Open the Waspmote technical guide, page 59, section I/O to see how digital pins and bus pins are located
This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [Ut_14] Waspmote Using SPI bus Example --------
3
*
4
* Explanation: This example shows how to use some SPI functions and
5
* how to configure read and configure
6
*
7
* Tips: This example was developed with a LoRa module
8
* Open the Waspmote technical guide, page 59, section I/O
9
* to see how digital pins and bus pins are located
10
*
11
* Copyright (C) 2018 Libelium Comunicaciones Distribuidas S.L.
12
* http://www.libelium.com
13
*
14
* This program is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program. If not, see .
26
*
27
* Version: 3.0
28
* Design: David Gascón
29
* Implementation: Luis Miguel Martí
30
*/
31
32
// Variable to store function returns
33
uint32_t value = 0;
34
uint8_t byte1 = 0;
35
uint8_t byte2 = 0;
36
uint8_t byte3 = 0;
37
uint8_t address = 0;
38
39
void setup()
40
{
41
////////////////////////////////////////////////////////////
42
// 1. Set SPI configuration to communicate with the device
43
////////////////////////////////////////////////////////////
44
SPI.begin();
45
46
////////////////////////////////////////////////////////
47
// Power on the socket
48
////////////////////////////////////////////////////////
49
// Connect device Vcc pin to the 3V3 SENSOR POWER pin
50
// If the device requiers 5V power supply, the 5V SENSOR
51
// POWER pin may be enabled with:
52
// PWR.setSensorPower(SENS_5V, SENS_ON);
53
////////////////////////////////////////////////////////
54
PWR.setSensorPower(SENS_3V3,SENS_ON);
55
56
// Set RESET pin and CS pin as outputs
57
pinMode(DIGITAL1,OUTPUT);
58
pinMode(DIGITAL2,OUTPUT);
59
60
// Set both pins to high
61
digitalWrite(DIGITAL1,HIGH);
62
digitalWrite(DIGITAL2,HIGH);
63
64
delay(10);
65
66
//////////////////////////////////
67
// 2. Write bytes into the device
68
//////////////////////////////////
69
USB.println(F("/////////////////////"));
70
USB.println(F("/// Writing bytes ///"));
71
USB.println(F("/////////////////////"));
72
USB.println(F("Byte to write 1: 0x6B"));
73
USB.println(F("Byte to write 2: 0xA0"));
74
USB.println(F("Byte to write 3: 0x12"));
75
76
// Set CS pin to low, enable device
77
digitalWrite(DIGITAL1,LOW);
78
delay(10);
79
80
// Set register address to write into
81
address = 0x06;
82
// Bit 7 set to 1 to write in register
83
bitSet(address, 7);
84
// Start sending to address
85
SPI.transfer(address);
86
// Send data
87
SPI.transfer(0x6B);
88
89
// Enable device with the CS pin
90
delay(10);
91
digitalWrite(DIGITAL1,HIGH);
92
delay(10);
93
digitalWrite(DIGITAL1,LOW);
94
95
// Set register address to write into
96
address = 0x07;
97
// Bit 7 set to 1 to write in register
98
bitSet(address, 7);
99
// Start sending to address
100
SPI.transfer(address);
101
// Send data
102
SPI.transfer(0xA0);
103
104
// Enable device with the CS pin
105
delay(10);
106
digitalWrite(DIGITAL1,HIGH);
107
delay(10);
108
digitalWrite(DIGITAL1,LOW);
109
110
// Set register address to write into
111
address = 0x08;
112
// Bit 7 set to 1 to write in register
113
bitSet(address, 7);
114
// Start sending to address
115
SPI.transfer(address);
116
// Send data
117
SPI.transfer(0x12);
118
119
delay(100);
120
121
}
122
123
124
void loop()
125
{
126
////////////////////////////////////////
127
// 3. Read stored bytes from the device
128
////////////////////////////////////////
129
130
USB.println(F("///////////////////"));
131
USB.println(F("// Bytes to read //"));
132
USB.println(F("///////////////////"));
133
134
// Enable device with the CS pin
135
digitalWrite(DIGITAL1,HIGH);
136
delay(10);
137
digitalWrite(DIGITAL1,LOW);
138
delay(10);
139
140
// Set register address to write into
141
address = 0x06;
142
// Bit 7 cleared to read in registers
143
bitClear(address, 7);
144
// Start sending to address
145
SPI.transfer(address);
146
// Start reading
147
byte1 = SPI.transfer(0x00);
148
149
USB.print(F("Byte 1: "));
150
USB.println(byte1,HEX);
151
152
153
// Enable device with the CS pin
154
digitalWrite(DIGITAL1,HIGH);
155
delay(10);
156
digitalWrite(DIGITAL1,LOW);
157
delay(10);
158
159
// Set register address to write into
160
address = 0x07;
161
// Bit 7 cleared to read in registers
162
bitClear(address, 7);
163
// Start sending to address
164
SPI.transfer(address);
165
// Start reading
166
byte2 = SPI.transfer(0x00);
167
168
USB.print(F("Byte 2: "));
169
USB.println(byte2,HEX);
170
171
// Enable device with the CS pin
172
digitalWrite(DIGITAL1,HIGH);
173
delay(10);
174
digitalWrite(DIGITAL1,LOW);
175
delay(10);
176
177
// Set register address to write into
178
address = 0x08;
179
// Bit 7 cleared to read in registers
180
bitClear(address, 7);
181
// Start sending to address
182
SPI.transfer(address);
183
// Start reading
184
byte3 = SPI.transfer(0x00);
185
186
USB.print(F("Byte 3: "));
187
USB.println(byte3,HEX);
188
delay(10000);
189
190
191
192
}
Copied!
Last modified 1yr ago