* ------ [Ut_14] Waspmote Using SPI bus Example --------
* Explanation: This example shows how to use some SPI functions and
* how to configure read and configure
* Tips: 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
* Copyright (C) 2018 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
* Implementation: Luis Miguel Martí
// Variable to store function returns
////////////////////////////////////////////////////////////
// 1. Set SPI configuration to communicate with the device
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Connect device Vcc pin to the 3V3 SENSOR POWER pin
// If the device requiers 5V power supply, the 5V SENSOR
// POWER pin may be enabled with:
// PWR.setSensorPower(SENS_5V, SENS_ON);
////////////////////////////////////////////////////////
PWR.setSensorPower(SENS_3V3,SENS_ON);
// Set RESET pin and CS pin as outputs
pinMode(DIGITAL1,OUTPUT);
pinMode(DIGITAL2,OUTPUT);
digitalWrite(DIGITAL1,HIGH);
digitalWrite(DIGITAL2,HIGH);
//////////////////////////////////
// 2. Write bytes into the device
//////////////////////////////////
USB.println(F("/////////////////////"));
USB.println(F("/// Writing bytes ///"));
USB.println(F("/////////////////////"));
USB.println(F("Byte to write 1: 0x6B"));
USB.println(F("Byte to write 2: 0xA0"));
USB.println(F("Byte to write 3: 0x12"));
// Set CS pin to low, enable device
digitalWrite(DIGITAL1,LOW);
// Set register address to write into
// Bit 7 set to 1 to write in register
// Start sending to address
// Enable device with the CS pin
digitalWrite(DIGITAL1,HIGH);
digitalWrite(DIGITAL1,LOW);
// Set register address to write into
// Bit 7 set to 1 to write in register
// Start sending to address
// Enable device with the CS pin
digitalWrite(DIGITAL1,HIGH);
digitalWrite(DIGITAL1,LOW);
// Set register address to write into
// Bit 7 set to 1 to write in register
// Start sending to address
////////////////////////////////////////
// 3. Read stored bytes from the device
////////////////////////////////////////
USB.println(F("///////////////////"));
USB.println(F("// Bytes to read //"));
USB.println(F("///////////////////"));
// Enable device with the CS pin
digitalWrite(DIGITAL1,HIGH);
digitalWrite(DIGITAL1,LOW);
// Set register address to write into
// Bit 7 cleared to read in registers
// Start sending to address
byte1 = SPI.transfer(0x00);
USB.print(F("Byte 1: "));
// Enable device with the CS pin
digitalWrite(DIGITAL1,HIGH);
digitalWrite(DIGITAL1,LOW);
// Set register address to write into
// Bit 7 cleared to read in registers
// Start sending to address
byte2 = SPI.transfer(0x00);
USB.print(F("Byte 2: "));
// Enable device with the CS pin
digitalWrite(DIGITAL1,HIGH);
digitalWrite(DIGITAL1,LOW);
// Set register address to write into
// Bit 7 cleared to read in registers
// Start sending to address
byte3 = SPI.transfer(0x00);
USB.print(F("Byte 3: "));