Denwis La / Mbed OS mDot_Send_Data

Dependencies:   libmDot-dev-mbed5-deprecated ISL29011

Fork of mdot-examples by 3mdeb

Committer:
SDesign2018
Date:
Tue Dec 05 23:33:16 2017 +0000
Revision:
8:efab0e415826
Parent:
7:5f2b3d1a9b0b
Child:
9:df0c4e8a3097
Changed option 9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SDesign2018 5:c9ab5062cfc3 1 #include <stdlib.h>
SDesign2018 6:3b41238872a8 2 #include <iostream>
SDesign2018 5:c9ab5062cfc3 3 #include <string.h>
SDesign2018 5:c9ab5062cfc3 4 #include <mbed.h>
SDesign2018 4:b0ce6385d008 5 #include "dot_util.h"
SDesign2018 4:b0ce6385d008 6 #include "RadioEvent.h"
SDesign2018 5:c9ab5062cfc3 7 #include "itoa.h"
SDesign2018 5:c9ab5062cfc3 8
SDesign2018 4:b0ce6385d008 9
SDesign2018 4:b0ce6385d008 10 /////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 11 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
SDesign2018 4:b0ce6385d008 12 // * Because these example programs can be used for both mDot and xDot //
SDesign2018 4:b0ce6385d008 13 // devices, the LoRa stack is not included. The libmDot library should //
SDesign2018 4:b0ce6385d008 14 // be imported if building for mDot devices. The libxDot library //
SDesign2018 4:b0ce6385d008 15 // should be imported if building for xDot devices. //
SDesign2018 4:b0ce6385d008 16 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
SDesign2018 4:b0ce6385d008 17 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
SDesign2018 4:b0ce6385d008 18 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
SDesign2018 4:b0ce6385d008 19 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
SDesign2018 4:b0ce6385d008 20 /////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 21
SDesign2018 4:b0ce6385d008 22 /////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 23 // * these options must match between the two devices in //
SDesign2018 4:b0ce6385d008 24 // order for communication to be successful
SDesign2018 4:b0ce6385d008 25 //-------------------MDOT variables------------------------//
SDesign2018 4:b0ce6385d008 26 /////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 27 static uint8_t network_address[] = { 0x00, 0x11, 0x22, 0x33 };
SDesign2018 4:b0ce6385d008 28 static uint8_t network_session_key[] = { 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33 };
SDesign2018 4:b0ce6385d008 29 static uint8_t data_session_key[] = { 0x33, 0x22, 0x11, 0x00, 0x33, 0x22, 0x11, 0x00, 0x33, 0x22, 0x11, 0x00, 0x33, 0x22, 0x11, 0x00 };
SDesign2018 4:b0ce6385d008 30
SDesign2018 4:b0ce6385d008 31 mDot* dot = NULL;
SDesign2018 4:b0ce6385d008 32 lora::ChannelPlan* plan = NULL;
SDesign2018 4:b0ce6385d008 33 //--------------End of MDOT variables-------------------//
SDesign2018 4:b0ce6385d008 34
SDesign2018 4:b0ce6385d008 35 Serial pc(USBTX, USBRX);
SDesign2018 4:b0ce6385d008 36
SDesign2018 4:b0ce6385d008 37 // ADXL372 Slave SPI
SDesign2018 4:b0ce6385d008 38 DigitalOut MOSI(D11);
SDesign2018 4:b0ce6385d008 39 DigitalIn MISO(D12);
SDesign2018 4:b0ce6385d008 40 DigitalOut SPI_CLK(D13);
SDesign2018 4:b0ce6385d008 41 DigitalOut SPI_CS(D10);
SDesign2018 4:b0ce6385d008 42 //InterruptIn INT1();
SDesign2018 4:b0ce6385d008 43 //InterruptIn INT2();
SDesign2018 5:c9ab5062cfc3 44 //DigitalOut CS(D10); // Used for CS chip select
SDesign2018 4:b0ce6385d008 45
SDesign2018 4:b0ce6385d008 46 // ADXL372 Slave I2C
SDesign2018 4:b0ce6385d008 47 I2C ADXL372(I2C_SDA, I2C_SCL); // (D14,D15) (MISO, CS)
SDesign2018 4:b0ce6385d008 48
SDesign2018 4:b0ce6385d008 49 // ADT7410 Temperature
SDesign2018 4:b0ce6385d008 50 I2C ADT7410(I2C_SDA, I2C_SCL); // Attempt at making I2C connection to slaves (D14,D15)
SDesign2018 4:b0ce6385d008 51 InterruptIn ADT7410_Int(D2); // Allow this pin for ADT7410 Interrupt critical temperature notice
SDesign2018 4:b0ce6385d008 52
SDesign2018 4:b0ce6385d008 53 // DS7505s Temperature
SDesign2018 4:b0ce6385d008 54 I2C DS7505(I2C_SDA, I2C_SCL); // Attempt at making I2C connection to slaves (D14,D15)
SDesign2018 4:b0ce6385d008 55
SDesign2018 4:b0ce6385d008 56 // Create reocurring interrupt function that could be used to periodically take temperatures
SDesign2018 4:b0ce6385d008 57 // Not working right now due to some mutex initialize error
SDesign2018 4:b0ce6385d008 58 // Suspect that it is due to it having be a RTOS task thing
SDesign2018 4:b0ce6385d008 59 // Should probably go back to using an in processor timer interrupt instead of mbed
SDesign2018 4:b0ce6385d008 60 Ticker interruptEverything;
SDesign2018 4:b0ce6385d008 61
SDesign2018 4:b0ce6385d008 62 const int ADT7410_Address_7BIT = 0x49; // A0 set HIGH and A1 set LOW
SDesign2018 4:b0ce6385d008 63 const int ADT7410_Address_8BIT = ADT7410_Address_7BIT << 1; // Shift 1 bit to left for R/~W bit, and basic I2C format
SDesign2018 4:b0ce6385d008 64
SDesign2018 4:b0ce6385d008 65 const int ADXL372_Address_7bit = 0x1D; // Address for the I2C if MISO pulled low, 0x53 if pulled high
SDesign2018 4:b0ce6385d008 66 const int ADXL372_Address_8bit = ADXL372_Address_7bit << 1; // Same
SDesign2018 4:b0ce6385d008 67
SDesign2018 4:b0ce6385d008 68 const int DS7505s_Address_7bit = 0x48; // A0 set LOR, A1 set LOW, A2 set LOW
SDesign2018 4:b0ce6385d008 69 const int DS7505s_Address_8bit = DS7505s_Address_7bit << 1; // Same
SDesign2018 4:b0ce6385d008 70
SDesign2018 4:b0ce6385d008 71
SDesign2018 4:b0ce6385d008 72 //-------------------All prototype functions-----------------------//
SDesign2018 4:b0ce6385d008 73 void ADXL372Initialize(void);
SDesign2018 4:b0ce6385d008 74
SDesign2018 4:b0ce6385d008 75 int accelerometerI2CWrite(int hexAddress, int hexData);
SDesign2018 4:b0ce6385d008 76 char * accelerometerI2CRead(int hexAddress);
SDesign2018 4:b0ce6385d008 77 void ADXL372Reset(void);
SDesign2018 4:b0ce6385d008 78 void BitBangSPIWrite(const unsigned char regAddr, const unsigned char regData);
SDesign2018 5:c9ab5062cfc3 79 uint8_t BitBangSPIRead (const unsigned char regAddr);
SDesign2018 4:b0ce6385d008 80
SDesign2018 4:b0ce6385d008 81 int ADT7410Write(unsigned char registerAddress, unsigned char data);
SDesign2018 4:b0ce6385d008 82 char * ADT7410Read(int hex);
SDesign2018 4:b0ce6385d008 83
SDesign2018 4:b0ce6385d008 84 int DS7505sWrite(unsigned char registerAddress, unsigned char data);
SDesign2018 4:b0ce6385d008 85 char * DS7505sRead(int hex);
SDesign2018 4:b0ce6385d008 86
SDesign2018 4:b0ce6385d008 87 unsigned char * twosComplementConversion(unsigned char *value);
SDesign2018 6:3b41238872a8 88 void flushSerialBuffer(void);
SDesign2018 4:b0ce6385d008 89 //---------------------End of prototype functions-----------------------------//
SDesign2018 4:b0ce6385d008 90
SDesign2018 4:b0ce6385d008 91 void printMenu(){
SDesign2018 4:b0ce6385d008 92 pc.printf("Please eneter a debug option: \n\r"
SDesign2018 5:c9ab5062cfc3 93 "1: I2C Read converted values from Accelerometer ADXL372\n\r"
SDesign2018 4:b0ce6385d008 94 "2: Read converted values from Temperature ADT7410\n\r"
SDesign2018 4:b0ce6385d008 95 "3: Read raw values from Accelerometer ADXL372\n\r"
SDesign2018 4:b0ce6385d008 96 "4: Read raw values from Temperature ADT7410\n\r"
SDesign2018 4:b0ce6385d008 97 "5: Initialize Accelerometer\n\r"
SDesign2018 4:b0ce6385d008 98 "6: Reset Accelerometer\n\r"
SDesign2018 5:c9ab5062cfc3 99 "7: Send Temperature data\n\r"
SDesign2018 5:c9ab5062cfc3 100 "8: SPI Read values from Accelerometer ADXL372\n\r"
SDesign2018 8:efab0e415826 101 "9: SPI reset for ADXL372\n\r");
SDesign2018 4:b0ce6385d008 102 }
SDesign2018 4:b0ce6385d008 103
SDesign2018 4:b0ce6385d008 104
SDesign2018 4:b0ce6385d008 105 /*******************************************************************************
SDesign2018 4:b0ce6385d008 106 * Function to be called by the ticker interrupt
SDesign2018 4:b0ce6385d008 107 * Read temperatures and send it
SDesign2018 4:b0ce6385d008 108 *
SDesign2018 4:b0ce6385d008 109 *
SDesign2018 4:b0ce6385d008 110 ******************************************************************************/
SDesign2018 4:b0ce6385d008 111 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 112 void interruptReadTemperature(void){
SDesign2018 4:b0ce6385d008 113 std::vector<uint8_t> tx_data;
SDesign2018 4:b0ce6385d008 114 uint16_t temperatures;
SDesign2018 4:b0ce6385d008 115 char data[2] = {0, 0};
SDesign2018 4:b0ce6385d008 116 char cmd[1];
SDesign2018 4:b0ce6385d008 117 cmd[0] = 0x00;
SDesign2018 4:b0ce6385d008 118 //pc.printf("Register Addres is: %x \n\r", cmd[0]);
SDesign2018 4:b0ce6385d008 119 if(ADT7410.write(ADT7410_Address_8BIT, cmd,1) == 0){
SDesign2018 4:b0ce6385d008 120 if(ADT7410.read(ADT7410_Address_8BIT, data, 2) == 0){
SDesign2018 4:b0ce6385d008 121 temperatures = ((data[0] << 8) | data[1]) >> 3;
SDesign2018 4:b0ce6385d008 122 tx_data.push_back((temperatures >> 8) & 0xFF);
SDesign2018 4:b0ce6385d008 123 tx_data.push_back(temperatures & 0xFF);
SDesign2018 4:b0ce6385d008 124 logInfo("light: %lu [0x%04X]", temperatures, temperatures);
SDesign2018 4:b0ce6385d008 125 send_data(tx_data);
SDesign2018 4:b0ce6385d008 126 //return (data[0] >> 8 | data[1])>>3; // Explained here: https://stackoverflow.com/a/141576 SOOO GOOOOODDD
SDesign2018 4:b0ce6385d008 127
SDesign2018 4:b0ce6385d008 128 }
SDesign2018 4:b0ce6385d008 129 }
SDesign2018 4:b0ce6385d008 130 }
SDesign2018 4:b0ce6385d008 131 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 132
SDesign2018 4:b0ce6385d008 133 int main() {
SDesign2018 4:b0ce6385d008 134 // Custom event handler for automatically displaying RX data
SDesign2018 5:c9ab5062cfc3 135 //interruptEverything.attach(&interruptReadTemperature, 7.0);
SDesign2018 4:b0ce6385d008 136 RadioEvent events;
SDesign2018 4:b0ce6385d008 137 uint32_t tx_frequency;
SDesign2018 4:b0ce6385d008 138 uint8_t tx_datarate;
SDesign2018 4:b0ce6385d008 139 uint8_t tx_power;
SDesign2018 4:b0ce6385d008 140 uint8_t frequency_band;
SDesign2018 4:b0ce6385d008 141
SDesign2018 4:b0ce6385d008 142 // Points to the returned char pointer from called functions
SDesign2018 4:b0ce6385d008 143 char * rawTempValues; // Could change to uint8_t, same for other char pointers
SDesign2018 4:b0ce6385d008 144
SDesign2018 4:b0ce6385d008 145 // Save converted values here
SDesign2018 4:b0ce6385d008 146 uint16_t convertedTempValue; // Data values must be uint16_t for conversion and send prep
SDesign2018 4:b0ce6385d008 147 char *accelValues;
SDesign2018 4:b0ce6385d008 148 uint16_t XData;
SDesign2018 4:b0ce6385d008 149 uint16_t YData;
SDesign2018 4:b0ce6385d008 150 uint16_t ZData;
SDesign2018 4:b0ce6385d008 151 int regAddress;
SDesign2018 4:b0ce6385d008 152
SDesign2018 4:b0ce6385d008 153 // Change baud rate in serial terminal to this value
SDesign2018 4:b0ce6385d008 154 pc.baud(115200);
SDesign2018 4:b0ce6385d008 155
SDesign2018 4:b0ce6385d008 156
SDesign2018 4:b0ce6385d008 157 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
SDesign2018 4:b0ce6385d008 158
SDesign2018 4:b0ce6385d008 159 // Sometimes when calling this, it creates error: type specifier expected
SDesign2018 4:b0ce6385d008 160 // Even with identical include files I would get this in another workspace.
SDesign2018 4:b0ce6385d008 161 plan = new lora::ChannelPlan_US915();
SDesign2018 4:b0ce6385d008 162
SDesign2018 4:b0ce6385d008 163 logInfo("Now asserting");
SDesign2018 4:b0ce6385d008 164 assert(plan);
SDesign2018 4:b0ce6385d008 165
SDesign2018 4:b0ce6385d008 166 // Careful when using this. The production ready libmdot-mbed5 has a void constructor
SDesign2018 4:b0ce6385d008 167 // Therefore, can only use the libmDot-dev-mbed5 version, for now.
SDesign2018 4:b0ce6385d008 168 dot = mDot::getInstance(plan);
SDesign2018 4:b0ce6385d008 169 assert(dot);
SDesign2018 4:b0ce6385d008 170
SDesign2018 4:b0ce6385d008 171 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
SDesign2018 4:b0ce6385d008 172
SDesign2018 4:b0ce6385d008 173 // start from a well-known state
SDesign2018 4:b0ce6385d008 174 logInfo("defaulting Dot configuration");
SDesign2018 4:b0ce6385d008 175 dot->resetConfig();
SDesign2018 4:b0ce6385d008 176
SDesign2018 4:b0ce6385d008 177 // make sure library logging is turned on
SDesign2018 4:b0ce6385d008 178 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
SDesign2018 4:b0ce6385d008 179
SDesign2018 4:b0ce6385d008 180 // attach the custom events handler
SDesign2018 4:b0ce6385d008 181 dot->setEvents(&events);
SDesign2018 4:b0ce6385d008 182
SDesign2018 4:b0ce6385d008 183 // update configuration if necessary
SDesign2018 4:b0ce6385d008 184 if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
SDesign2018 4:b0ce6385d008 185 logInfo("changing network join mode to PEER_TO_PEER");
SDesign2018 4:b0ce6385d008 186 if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
SDesign2018 4:b0ce6385d008 187 logError("failed to set network join mode to PEER_TO_PEER");
SDesign2018 4:b0ce6385d008 188 }
SDesign2018 4:b0ce6385d008 189 }
SDesign2018 4:b0ce6385d008 190
SDesign2018 4:b0ce6385d008 191 /*
SDesign2018 4:b0ce6385d008 192 * Get the Frequency and then choose transfer frequency, datarate, and power accordingly
SDesign2018 4:b0ce6385d008 193 *
SDesign2018 4:b0ce6385d008 194 */
SDesign2018 4:b0ce6385d008 195 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 196 frequency_band = dot->getFrequencyBand();
SDesign2018 4:b0ce6385d008 197 switch (frequency_band) {
SDesign2018 4:b0ce6385d008 198 case lora::ChannelPlan::EU868_OLD:
SDesign2018 4:b0ce6385d008 199 case lora::ChannelPlan::EU868:
SDesign2018 4:b0ce6385d008 200 // 250kHz channels achieve higher throughput
SDesign2018 4:b0ce6385d008 201 // DR_6 : SF7 @ 250kHz
SDesign2018 4:b0ce6385d008 202 // DR_0 - DR_5 (125kHz channels) available but much slower
SDesign2018 4:b0ce6385d008 203 tx_frequency = 869850000;
SDesign2018 4:b0ce6385d008 204 tx_datarate = lora::DR_6;
SDesign2018 4:b0ce6385d008 205 // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
SDesign2018 4:b0ce6385d008 206 tx_power = 4;
SDesign2018 4:b0ce6385d008 207 break;
SDesign2018 4:b0ce6385d008 208
SDesign2018 4:b0ce6385d008 209 case lora::ChannelPlan::US915_OLD:
SDesign2018 4:b0ce6385d008 210 case lora::ChannelPlan::US915:
SDesign2018 4:b0ce6385d008 211 case lora::ChannelPlan::AU915_OLD:
SDesign2018 4:b0ce6385d008 212 case lora::ChannelPlan::AU915:
SDesign2018 4:b0ce6385d008 213 // 500kHz channels achieve highest throughput
SDesign2018 4:b0ce6385d008 214 // DR_8 : SF12 @ 500kHz
SDesign2018 4:b0ce6385d008 215 // DR_9 : SF11 @ 500kHz
SDesign2018 4:b0ce6385d008 216 // DR_10 : SF10 @ 500kHz
SDesign2018 4:b0ce6385d008 217 // DR_11 : SF9 @ 500kHz
SDesign2018 4:b0ce6385d008 218 // DR_12 : SF8 @ 500kHz
SDesign2018 4:b0ce6385d008 219 // DR_13 : SF7 @ 500kHz
SDesign2018 4:b0ce6385d008 220 // DR_0 - DR_3 (125kHz channels) available but much slower
SDesign2018 4:b0ce6385d008 221 tx_frequency = 915500000;
SDesign2018 4:b0ce6385d008 222 tx_datarate = lora::DR_13;
SDesign2018 4:b0ce6385d008 223 // 915 bands have no duty cycle restrictions, set tx power to max
SDesign2018 4:b0ce6385d008 224 tx_power = 20;
SDesign2018 4:b0ce6385d008 225 break;
SDesign2018 4:b0ce6385d008 226
SDesign2018 4:b0ce6385d008 227 case lora::ChannelPlan::AS923:
SDesign2018 4:b0ce6385d008 228 case lora::ChannelPlan::AS923_JAPAN:
SDesign2018 4:b0ce6385d008 229 // 250kHz channels achieve higher throughput
SDesign2018 4:b0ce6385d008 230 // DR_6 : SF7 @ 250kHz
SDesign2018 4:b0ce6385d008 231 // DR_0 - DR_5 (125kHz channels) available but much slower
SDesign2018 4:b0ce6385d008 232 tx_frequency = 924800000;
SDesign2018 4:b0ce6385d008 233 tx_datarate = lora::DR_6;
SDesign2018 4:b0ce6385d008 234 tx_power = 16;
SDesign2018 4:b0ce6385d008 235 break;
SDesign2018 4:b0ce6385d008 236
SDesign2018 4:b0ce6385d008 237 case lora::ChannelPlan::KR920:
SDesign2018 4:b0ce6385d008 238 // DR_5 : SF7 @ 125kHz
SDesign2018 4:b0ce6385d008 239 tx_frequency = 922700000;
SDesign2018 4:b0ce6385d008 240 tx_datarate = lora::DR_5;
SDesign2018 4:b0ce6385d008 241 tx_power = 14;
SDesign2018 4:b0ce6385d008 242 break;
SDesign2018 4:b0ce6385d008 243
SDesign2018 4:b0ce6385d008 244 default:
SDesign2018 4:b0ce6385d008 245 while (true) {
SDesign2018 4:b0ce6385d008 246 logFatal("no known channel plan in use - extra configuration is needed!");
SDesign2018 4:b0ce6385d008 247 wait(5);
SDesign2018 4:b0ce6385d008 248 }
SDesign2018 4:b0ce6385d008 249 break;
SDesign2018 4:b0ce6385d008 250 }
SDesign2018 4:b0ce6385d008 251
SDesign2018 4:b0ce6385d008 252 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 253
SDesign2018 4:b0ce6385d008 254 // in PEER_TO_PEER mode there is no join request/response transaction
SDesign2018 4:b0ce6385d008 255 // as long as both Dots are configured correctly, they should be able to communicate
SDesign2018 4:b0ce6385d008 256 update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
SDesign2018 4:b0ce6385d008 257
SDesign2018 4:b0ce6385d008 258 // save changes to configuration
SDesign2018 4:b0ce6385d008 259 logInfo("saving configuration");
SDesign2018 4:b0ce6385d008 260 if (!dot->saveConfig()) {
SDesign2018 4:b0ce6385d008 261 logError("failed to save configuration");
SDesign2018 4:b0ce6385d008 262 }
SDesign2018 4:b0ce6385d008 263
SDesign2018 4:b0ce6385d008 264 // Display configuration
SDesign2018 4:b0ce6385d008 265 // It's gonna output a lot of information onto the Serial Terminal
SDesign2018 4:b0ce6385d008 266 display_config();
SDesign2018 4:b0ce6385d008 267
SDesign2018 4:b0ce6385d008 268 /*
SDesign2018 4:b0ce6385d008 269 *
SDesign2018 4:b0ce6385d008 270 * From here on is my own code
SDesign2018 4:b0ce6385d008 271 * Can add more options to choose from
SDesign2018 4:b0ce6385d008 272 *
SDesign2018 4:b0ce6385d008 273 */
SDesign2018 4:b0ce6385d008 274 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 275 printMenu();
SDesign2018 6:3b41238872a8 276 flushSerialBuffer();
SDesign2018 4:b0ce6385d008 277 pc.printf("\n\rChoose what you want to do: \n\r");
SDesign2018 4:b0ce6385d008 278
SDesign2018 4:b0ce6385d008 279 char userInput = pc.getc();
SDesign2018 4:b0ce6385d008 280 while(1){
SDesign2018 4:b0ce6385d008 281 // Create a vector of uint8_t elements to be sent later
SDesign2018 4:b0ce6385d008 282 std::vector<uint8_t> tx_data;
SDesign2018 4:b0ce6385d008 283
SDesign2018 4:b0ce6385d008 284 // Checks if a character has been pressed;
SDesign2018 4:b0ce6385d008 285 // Works right now for 1 digit numbers :(
SDesign2018 4:b0ce6385d008 286 // Change to work with larger inputs
SDesign2018 4:b0ce6385d008 287 if(pc.readable())
SDesign2018 4:b0ce6385d008 288 {
SDesign2018 4:b0ce6385d008 289 userInput = pc.getc();
SDesign2018 4:b0ce6385d008 290 switch(userInput){
SDesign2018 4:b0ce6385d008 291 case 49: // 1
SDesign2018 4:b0ce6385d008 292 pc.printf("Reading converted values from accelerometer\n\r");
SDesign2018 4:b0ce6385d008 293 for(int i = 0; i < 15; ++i){
SDesign2018 4:b0ce6385d008 294 regAddress = 0x08; // This is the register address for XData
SDesign2018 4:b0ce6385d008 295 accelValues = accelerometerI2CRead(regAddress);
SDesign2018 5:c9ab5062cfc3 296 XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short int, remove last 4 flag bits
SDesign2018 4:b0ce6385d008 297 YData = ((*(accelValues + 2) << 8) | *(accelValues + 3)) >> 4;
SDesign2018 4:b0ce6385d008 298 ZData = ((*(accelValues + 4) << 8) | *(accelValues + 5)) >> 4;
SDesign2018 4:b0ce6385d008 299 pc.printf("\n %d: X: 0x%x | Y: 0x%x | Z: 0x%x \n\r", i+1, XData, YData, ZData);
SDesign2018 4:b0ce6385d008 300 wait(0.2);
SDesign2018 4:b0ce6385d008 301 }
SDesign2018 4:b0ce6385d008 302 break;
SDesign2018 4:b0ce6385d008 303 case 50: // 2
SDesign2018 4:b0ce6385d008 304 pc.printf("Reading converted values from temperature\n\r");
SDesign2018 4:b0ce6385d008 305 for(int i = 0; i < 10; ++i){
SDesign2018 4:b0ce6385d008 306 regAddress = 0x00;
SDesign2018 4:b0ce6385d008 307 rawTempValues = ADT7410Read(regAddress);
SDesign2018 4:b0ce6385d008 308 convertedTempValue = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes into
SDesign2018 5:c9ab5062cfc3 309 // a short int variable(16 bits), remove last 3 bits
SDesign2018 4:b0ce6385d008 310
SDesign2018 4:b0ce6385d008 311 pc.printf("\n %d: Temperature is: 0x%x \n\r", i+1, convertedTempValue);
SDesign2018 4:b0ce6385d008 312 }
SDesign2018 4:b0ce6385d008 313
SDesign2018 4:b0ce6385d008 314 break;
SDesign2018 4:b0ce6385d008 315 case 51: // 3
SDesign2018 4:b0ce6385d008 316 pc.printf("Reading raw values from accelerometer\n\r");
SDesign2018 4:b0ce6385d008 317 for(int i = 0; i < 15; ++i){
SDesign2018 4:b0ce6385d008 318 regAddress = 0x08;
SDesign2018 4:b0ce6385d008 319 accelValues = accelerometerI2CRead(regAddress);
SDesign2018 5:c9ab5062cfc3 320 XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short int(16 bits), remove last 4 flag bits
SDesign2018 4:b0ce6385d008 321 YData = ((*(accelValues + 2) << 8) | *(accelValues + 3)) >> 4;
SDesign2018 4:b0ce6385d008 322 ZData = ((*(accelValues + 4) << 8) | *(accelValues + 5)) >> 4;
SDesign2018 4:b0ce6385d008 323 pc.printf("\n %d: X:: H: %x | L: %x | Y:: H: %x | L: %x | Z: H: %x | L: %x \n\r", i+1, *(accelValues + 0), *(accelValues + 1), *(accelValues + 2), *(accelValues + 3), *(accelValues + 4), *(accelValues + 5));
SDesign2018 4:b0ce6385d008 324 wait(0.2);
SDesign2018 4:b0ce6385d008 325 }
SDesign2018 4:b0ce6385d008 326 break;
SDesign2018 4:b0ce6385d008 327 case 52: // 4
SDesign2018 4:b0ce6385d008 328 pc.printf("Reading raw values from temperature\n\r");
SDesign2018 4:b0ce6385d008 329 for(int i = 0; i < 10; ++i){
SDesign2018 4:b0ce6385d008 330 regAddress = 0x00;
SDesign2018 4:b0ce6385d008 331 rawTempValues = ADT7410Read(regAddress);
SDesign2018 4:b0ce6385d008 332 pc.printf("\n %d: Temperature is: HIGH BYTE: %x | LOW BYTE: %x \n\r", i+1, *(rawTempValues + 0), *(rawTempValues + 1));
SDesign2018 4:b0ce6385d008 333 }
SDesign2018 4:b0ce6385d008 334 break;
SDesign2018 4:b0ce6385d008 335 case 53: // 5
SDesign2018 4:b0ce6385d008 336 ADXL372Initialize();
SDesign2018 4:b0ce6385d008 337 break;
SDesign2018 4:b0ce6385d008 338 case 54: // 6
SDesign2018 4:b0ce6385d008 339 ADXL372Reset();
SDesign2018 4:b0ce6385d008 340 break;
SDesign2018 4:b0ce6385d008 341
SDesign2018 4:b0ce6385d008 342 case 55: // 7
SDesign2018 4:b0ce6385d008 343 regAddress = 0x00;
SDesign2018 4:b0ce6385d008 344 rawTempValues = ADT7410Read(regAddress);
SDesign2018 4:b0ce6385d008 345 convertedTempValue = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes
SDesign2018 4:b0ce6385d008 346
SDesign2018 4:b0ce6385d008 347 tx_data.push_back((convertedTempValue >> 8) & 0xFF);
SDesign2018 4:b0ce6385d008 348 tx_data.push_back(convertedTempValue & 0xFF);
SDesign2018 4:b0ce6385d008 349 logInfo("light: %lu [0x%04X]", convertedTempValue, convertedTempValue);
SDesign2018 4:b0ce6385d008 350 send_data(tx_data);
SDesign2018 5:c9ab5062cfc3 351 break;
SDesign2018 4:b0ce6385d008 352
SDesign2018 5:c9ab5062cfc3 353 case 56: // 8
SDesign2018 5:c9ab5062cfc3 354 uint8_t MSB;
SDesign2018 5:c9ab5062cfc3 355 uint8_t LSB;
SDesign2018 5:c9ab5062cfc3 356
SDesign2018 5:c9ab5062cfc3 357
SDesign2018 5:c9ab5062cfc3 358 for(int i = 0; i < 15; ++i){
SDesign2018 5:c9ab5062cfc3 359
SDesign2018 5:c9ab5062cfc3 360 MSB = BitBangSPIRead(0x08); // XData MSB
SDesign2018 5:c9ab5062cfc3 361 LSB = BitBangSPIRead(0x09); // XData LSB
SDesign2018 5:c9ab5062cfc3 362 XData = ((MSB << 8) | LSB) >> 4;
SDesign2018 5:c9ab5062cfc3 363
SDesign2018 5:c9ab5062cfc3 364 MSB = BitBangSPIRead(0x0A); // YData MSB
SDesign2018 5:c9ab5062cfc3 365 LSB = BitBangSPIRead(0x0B); // YData LSB
SDesign2018 5:c9ab5062cfc3 366 YData = ((MSB << 8) | LSB) >> 4;
SDesign2018 5:c9ab5062cfc3 367
SDesign2018 5:c9ab5062cfc3 368 MSB = BitBangSPIRead(0x0C); // ZData MSB
SDesign2018 5:c9ab5062cfc3 369 LSB = BitBangSPIRead(0x0D); // ZData LSB
SDesign2018 5:c9ab5062cfc3 370 ZData = ((MSB << 8 ) | LSB) >> 4;
SDesign2018 5:c9ab5062cfc3 371
SDesign2018 5:c9ab5062cfc3 372 pc.printf("\n %d: X: 0x%x | Y: 0x%x | Z: 0x%x \n\r", i+1, XData, YData, ZData);
SDesign2018 5:c9ab5062cfc3 373 wait(0.2);
SDesign2018 5:c9ab5062cfc3 374 }
SDesign2018 5:c9ab5062cfc3 375 break;
SDesign2018 8:efab0e415826 376 case 57: // 9 Reset Accelerometer with SPI
SDesign2018 6:3b41238872a8 377 int intRegister;
SDesign2018 7:5f2b3d1a9b0b 378 char input[4];
SDesign2018 5:c9ab5062cfc3 379 char passRegister[1];
SDesign2018 6:3b41238872a8 380 int intData;
SDesign2018 5:c9ab5062cfc3 381 char passData[1];
SDesign2018 8:efab0e415826 382 BitBangSPIWrite(0x41, 0x52);
SDesign2018 8:efab0e415826 383 pc.printf("Done \n\r");
SDesign2018 6:3b41238872a8 384 break;
SDesign2018 4:b0ce6385d008 385 default:
SDesign2018 4:b0ce6385d008 386 printMenu();
SDesign2018 4:b0ce6385d008 387 break;
SDesign2018 4:b0ce6385d008 388 }
SDesign2018 4:b0ce6385d008 389 }
SDesign2018 4:b0ce6385d008 390
SDesign2018 4:b0ce6385d008 391 }
SDesign2018 4:b0ce6385d008 392
SDesign2018 4:b0ce6385d008 393 return 0;
SDesign2018 4:b0ce6385d008 394 }
SDesign2018 4:b0ce6385d008 395 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 396
SDesign2018 4:b0ce6385d008 397
SDesign2018 4:b0ce6385d008 398 /*******************************************************************************
SDesign2018 4:b0ce6385d008 399 *
SDesign2018 4:b0ce6385d008 400 * I2C function for the the ADXL372 accelerometer for a write sequence
SDesign2018 4:b0ce6385d008 401 * Param:
SDesign2018 4:b0ce6385d008 402 * hexAddress: Pass the hexadecimal value for what register you want
SDesign2018 4:b0ce6385d008 403 * hexData: Pass the hexadecimal value for what data you want to send
SDesign2018 4:b0ce6385d008 404 * i.e. hexadecimal represenatation of certain bits
SDesign2018 4:b0ce6385d008 405 * Returns:
SDesign2018 4:b0ce6385d008 406 * 1: Write was a complete success
SDesign2018 4:b0ce6385d008 407 * 2: Writing data to register failed
SDesign2018 4:b0ce6385d008 408 * 3: Writing to register Address failed
SDesign2018 4:b0ce6385d008 409 ******************************************************************************/
SDesign2018 4:b0ce6385d008 410 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 411 int accelerometerI2CWrite(int hexAddress, int hexData){
SDesign2018 4:b0ce6385d008 412 //--------- One full writing cycle for ADXL372 for X enable ------------------//
SDesign2018 4:b0ce6385d008 413 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 414 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 415 * '2' - timeout
SDesign2018 4:b0ce6385d008 416 */
SDesign2018 4:b0ce6385d008 417 int flag;
SDesign2018 4:b0ce6385d008 418 int registerAddress = hexAddress;
SDesign2018 4:b0ce6385d008 419 int data = hexData;
SDesign2018 4:b0ce6385d008 420
SDesign2018 4:b0ce6385d008 421 ADXL372.start();
SDesign2018 4:b0ce6385d008 422 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 423 if(flag == 1)
SDesign2018 4:b0ce6385d008 424 {
SDesign2018 4:b0ce6385d008 425 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 426 wait(0.1);
SDesign2018 4:b0ce6385d008 427 flag = ADXL372.write(registerAddress);
SDesign2018 4:b0ce6385d008 428 if(flag == 1)
SDesign2018 4:b0ce6385d008 429 {
SDesign2018 4:b0ce6385d008 430 pc.printf("Write to register 0x%x address success\n\r", registerAddress);
SDesign2018 4:b0ce6385d008 431 flag = ADXL372.write(data);
SDesign2018 4:b0ce6385d008 432 if(flag == 1)
SDesign2018 4:b0ce6385d008 433 {
SDesign2018 4:b0ce6385d008 434 pc.printf("Writing data 0x%x to register address success\n\r", data);
SDesign2018 4:b0ce6385d008 435 ADXL372.stop();
SDesign2018 4:b0ce6385d008 436 return 1;
SDesign2018 4:b0ce6385d008 437 }else {ADXL372.stop(); return 2;}
SDesign2018 4:b0ce6385d008 438 }else {ADXL372.stop(); return 3;}
SDesign2018 4:b0ce6385d008 439 }else ADXL372.stop();
SDesign2018 4:b0ce6385d008 440
SDesign2018 4:b0ce6385d008 441 return 0;
SDesign2018 4:b0ce6385d008 442 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 443 }
SDesign2018 4:b0ce6385d008 444 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 445
SDesign2018 4:b0ce6385d008 446
SDesign2018 4:b0ce6385d008 447 /*******************************************************************************
SDesign2018 4:b0ce6385d008 448 * I2C read sequence for the accelerometer
SDesign2018 4:b0ce6385d008 449 * Param:
SDesign2018 4:b0ce6385d008 450 * hexAddress: pass the hexadecimal representation of desired Register address
SDesign2018 4:b0ce6385d008 451 * Return:
SDesign2018 4:b0ce6385d008 452 * Char pointer to the array of read data.
SDesign2018 4:b0ce6385d008 453 *
SDesign2018 4:b0ce6385d008 454 * Right now it works only for the XData, YData, ZData because I wrote it to read
SDesign2018 4:b0ce6385d008 455 * 6 bytes(6 registers).
SDesign2018 4:b0ce6385d008 456 * Should change it to be 1 byte at a time
SDesign2018 4:b0ce6385d008 457 ******************************************************************************/
SDesign2018 4:b0ce6385d008 458 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 459 char * accelerometerI2CRead(int hexAddress){
SDesign2018 4:b0ce6385d008 460 char accelData[6];
SDesign2018 4:b0ce6385d008 461 char registerAddress[1];
SDesign2018 4:b0ce6385d008 462 registerAddress[0] = hexAddress;
SDesign2018 4:b0ce6385d008 463
SDesign2018 4:b0ce6385d008 464 // Perform mbed's way sending a start bit, then device address[r/~w], and then the register address
SDesign2018 4:b0ce6385d008 465 // Also if it succeeds, continue to the next operation
SDesign2018 4:b0ce6385d008 466 if(ADXL372.write(ADXL372_Address_8bit, registerAddress, 1) == 0){
SDesign2018 4:b0ce6385d008 467
SDesign2018 4:b0ce6385d008 468 // If previous sequence works, get 6 bytes into char array accelData
SDesign2018 4:b0ce6385d008 469 // Char array because it uses 1 byte(8bits)
SDesign2018 4:b0ce6385d008 470 // Should probably change it to uint8_t type
SDesign2018 4:b0ce6385d008 471 if(ADXL372.read(ADXL372_Address_8bit, accelData, 6) == 0){
SDesign2018 4:b0ce6385d008 472 return accelData;
SDesign2018 4:b0ce6385d008 473 }else pc.printf("Failed to read\n\r");
SDesign2018 4:b0ce6385d008 474 }else pc.printf("Failed to write\n\r");
SDesign2018 4:b0ce6385d008 475 return 0; // Only if it fails
SDesign2018 4:b0ce6385d008 476 }
SDesign2018 4:b0ce6385d008 477 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 478
SDesign2018 4:b0ce6385d008 479
SDesign2018 4:b0ce6385d008 480
SDesign2018 4:b0ce6385d008 481 /*******************************************************************************
SDesign2018 4:b0ce6385d008 482 * Initializes whatever settings you want for the accelerometer
SDesign2018 4:b0ce6385d008 483 * Can change it to use the previous I2C write function instead of all this mess
SDesign2018 4:b0ce6385d008 484 *
SDesign2018 4:b0ce6385d008 485 ******************************************************************************/
SDesign2018 4:b0ce6385d008 486 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 487 void ADXL372Initialize(void){
SDesign2018 4:b0ce6385d008 488 int flag;
SDesign2018 4:b0ce6385d008 489
SDesign2018 4:b0ce6385d008 490 //--------- Change mode to full Bandwidth Measurement Mode ------------------//
SDesign2018 4:b0ce6385d008 491 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 492 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 493 * '2' - timeout
SDesign2018 4:b0ce6385d008 494 */
SDesign2018 4:b0ce6385d008 495 pc.printf("Initializing Single Byte write\n\r");
SDesign2018 4:b0ce6385d008 496 ADXL372.start(); // Send start
SDesign2018 4:b0ce6385d008 497 flag = ADXL372.write(ADXL372_Address_8bit | 0); // Send Device Address
SDesign2018 4:b0ce6385d008 498 if(flag == 1)
SDesign2018 4:b0ce6385d008 499 {
SDesign2018 4:b0ce6385d008 500 pc.printf("Write to I2C address for write success\n\r");
SDesign2018 4:b0ce6385d008 501 wait(0.1);
SDesign2018 4:b0ce6385d008 502 flag = ADXL372.write(0x3F); // Send Register Address
SDesign2018 4:b0ce6385d008 503 if(flag == 1)
SDesign2018 4:b0ce6385d008 504 {
SDesign2018 4:b0ce6385d008 505
SDesign2018 4:b0ce6385d008 506 pc.printf("Write to 0x3F register address success\n\r");
SDesign2018 4:b0ce6385d008 507 flag = ADXL372.write(0x03); // Send Data that represents Full BandWidth mode
SDesign2018 4:b0ce6385d008 508 if(flag == 1)
SDesign2018 4:b0ce6385d008 509 {
SDesign2018 4:b0ce6385d008 510 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 511 ADXL372.stop();
SDesign2018 4:b0ce6385d008 512 }
SDesign2018 4:b0ce6385d008 513 }
SDesign2018 4:b0ce6385d008 514 }
SDesign2018 4:b0ce6385d008 515 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 516 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 517
SDesign2018 4:b0ce6385d008 518 //--------- One full writing cycle for ADXL372 for X enable ------------------//
SDesign2018 4:b0ce6385d008 519 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 520 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 521 * '2' - timeout
SDesign2018 4:b0ce6385d008 522 */
SDesign2018 4:b0ce6385d008 523 ADXL372.start();
SDesign2018 4:b0ce6385d008 524 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 525 if(flag == 1)
SDesign2018 4:b0ce6385d008 526 {
SDesign2018 4:b0ce6385d008 527 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 528 wait(0.1);
SDesign2018 4:b0ce6385d008 529 flag = ADXL372.write(0x24);
SDesign2018 4:b0ce6385d008 530 if(flag == 1)
SDesign2018 4:b0ce6385d008 531 {
SDesign2018 4:b0ce6385d008 532 pc.printf("Write to register 0x24 address success\n\r");
SDesign2018 4:b0ce6385d008 533 flag = ADXL372.write(0x01);
SDesign2018 4:b0ce6385d008 534 if(flag == 1)
SDesign2018 4:b0ce6385d008 535 {
SDesign2018 4:b0ce6385d008 536 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 537 pc.printf("X Enable should be set now\n\r");
SDesign2018 4:b0ce6385d008 538 ADXL372.stop();
SDesign2018 4:b0ce6385d008 539 }
SDesign2018 4:b0ce6385d008 540 }
SDesign2018 4:b0ce6385d008 541 }
SDesign2018 4:b0ce6385d008 542 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 543 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 544
SDesign2018 4:b0ce6385d008 545 //--------- One full writing cycle for ADXL372 for Y Enable ------------------//
SDesign2018 4:b0ce6385d008 546 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 547 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 548 * '2' - timeout
SDesign2018 4:b0ce6385d008 549 */
SDesign2018 4:b0ce6385d008 550 ADXL372.start();
SDesign2018 4:b0ce6385d008 551 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 552 if(flag == 1)
SDesign2018 4:b0ce6385d008 553 {
SDesign2018 4:b0ce6385d008 554 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 555 wait(0.1);
SDesign2018 4:b0ce6385d008 556 flag = ADXL372.write(0x25);
SDesign2018 4:b0ce6385d008 557 if(flag == 1)
SDesign2018 4:b0ce6385d008 558 {
SDesign2018 4:b0ce6385d008 559 pc.printf("Write to 0x25 register address success\n\r");
SDesign2018 4:b0ce6385d008 560 flag = ADXL372.write(0x01);
SDesign2018 4:b0ce6385d008 561 if(flag == 1)
SDesign2018 4:b0ce6385d008 562 {
SDesign2018 4:b0ce6385d008 563 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 564 pc.printf("Y Enable should be set now\n\r");
SDesign2018 4:b0ce6385d008 565
SDesign2018 4:b0ce6385d008 566 ADXL372.stop();
SDesign2018 4:b0ce6385d008 567 }
SDesign2018 4:b0ce6385d008 568 }
SDesign2018 4:b0ce6385d008 569 }
SDesign2018 4:b0ce6385d008 570 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 571 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 572
SDesign2018 4:b0ce6385d008 573 //--------- One full writing cycle for ADXL372 for Z Enable ------------------//
SDesign2018 4:b0ce6385d008 574 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 575 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 576 * '2' - timeout
SDesign2018 4:b0ce6385d008 577 */
SDesign2018 4:b0ce6385d008 578 ADXL372.start();
SDesign2018 4:b0ce6385d008 579 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 580 if(flag == 1)
SDesign2018 4:b0ce6385d008 581 {
SDesign2018 4:b0ce6385d008 582 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 583 wait(0.1);
SDesign2018 4:b0ce6385d008 584 flag = ADXL372.write(0x26);
SDesign2018 4:b0ce6385d008 585 if(flag == 1)
SDesign2018 4:b0ce6385d008 586 {
SDesign2018 4:b0ce6385d008 587 pc.printf("Write to 0x26 register address success\n\r");
SDesign2018 4:b0ce6385d008 588 flag = ADXL372.write(0x01); // Set bit 0
SDesign2018 4:b0ce6385d008 589 if(flag == 1)
SDesign2018 4:b0ce6385d008 590 {
SDesign2018 4:b0ce6385d008 591 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 592 pc.printf("Z Enable should be set now\n\r");
SDesign2018 4:b0ce6385d008 593
SDesign2018 4:b0ce6385d008 594 ADXL372.stop();
SDesign2018 4:b0ce6385d008 595 }
SDesign2018 4:b0ce6385d008 596 }
SDesign2018 4:b0ce6385d008 597 }
SDesign2018 4:b0ce6385d008 598 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 599 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 600
SDesign2018 4:b0ce6385d008 601 }
SDesign2018 4:b0ce6385d008 602 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 603
SDesign2018 4:b0ce6385d008 604
SDesign2018 4:b0ce6385d008 605
SDesign2018 4:b0ce6385d008 606 /*******************************************************************************
SDesign2018 4:b0ce6385d008 607 * BitBangSPIWrite for ADXL372 if you wire it up as SPI
SDesign2018 4:b0ce6385d008 608 *
SDesign2018 4:b0ce6385d008 609 * Sends the 6-0bits of desired register with LSB of transmission bit R/!W
SDesign2018 4:b0ce6385d008 610 *
SDesign2018 4:b0ce6385d008 611 ******************************************************************************/
SDesign2018 4:b0ce6385d008 612 ///////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 613 void BitBangSPIWrite(const unsigned char regAddr, const unsigned char regData)
SDesign2018 4:b0ce6385d008 614 {
SDesign2018 4:b0ce6385d008 615 unsigned char SPICount; // Counter used to clock out the data
SDesign2018 4:b0ce6385d008 616
SDesign2018 4:b0ce6385d008 617 unsigned char SPIData; // Define a data structure for the SPI data
SDesign2018 4:b0ce6385d008 618 SPI_CS = 0; // Make sure we start with active-low CS high
SDesign2018 4:b0ce6385d008 619 SPI_CLK = 0; // and CK low
SDesign2018 4:b0ce6385d008 620
SDesign2018 4:b0ce6385d008 621 SPIData = regAddr; // Preload the data to be sent with Address
SDesign2018 4:b0ce6385d008 622 SPI_CS = 1; // Set active-low CS low to start the SPI cycle
SDesign2018 4:b0ce6385d008 623
SDesign2018 4:b0ce6385d008 624 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address byte
SDesign2018 4:b0ce6385d008 625 {
SDesign2018 4:b0ce6385d008 626 if (SPIData & 0x80) // Check for a 1 at MSB
SDesign2018 4:b0ce6385d008 627 MOSI = 1; // and set the MOSI line appropriately
SDesign2018 4:b0ce6385d008 628 else
SDesign2018 4:b0ce6385d008 629 MOSI = 0;
SDesign2018 4:b0ce6385d008 630 SPI_CLK = 1; // Toggle the clock line
SDesign2018 4:b0ce6385d008 631 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 632 SPIData <<= 1; // Rotate to get the next bit
SDesign2018 4:b0ce6385d008 633 } // and loop back to send the next bit
SDesign2018 4:b0ce6385d008 634
SDesign2018 4:b0ce6385d008 635 // Repeat for the Data byte
SDesign2018 4:b0ce6385d008 636 SPIData = regData; // Preload the data to be sent with Data
SDesign2018 4:b0ce6385d008 637 for (SPICount = 0; SPICount < 8; SPICount++)
SDesign2018 4:b0ce6385d008 638 {
SDesign2018 4:b0ce6385d008 639 if (SPIData & 0x80)
SDesign2018 4:b0ce6385d008 640 MOSI = 1;
SDesign2018 4:b0ce6385d008 641 else
SDesign2018 4:b0ce6385d008 642 MOSI = 0;
SDesign2018 4:b0ce6385d008 643 SPI_CLK = 1;
SDesign2018 4:b0ce6385d008 644 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 645 SPIData <<= 1; // After each bit, move next bit one to left
SDesign2018 4:b0ce6385d008 646 }
SDesign2018 4:b0ce6385d008 647 SPI_CS = 0;
SDesign2018 4:b0ce6385d008 648 MOSI = 0;
SDesign2018 4:b0ce6385d008 649 }
SDesign2018 4:b0ce6385d008 650 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 651
SDesign2018 4:b0ce6385d008 652
SDesign2018 4:b0ce6385d008 653
SDesign2018 4:b0ce6385d008 654 /*******************************************************************************
SDesign2018 4:b0ce6385d008 655 * BitBangSPIRead for ADXL372 if you wire it up as SPI
SDesign2018 4:b0ce6385d008 656 *
SDesign2018 4:b0ce6385d008 657 *
SDesign2018 4:b0ce6385d008 658 *
SDesign2018 4:b0ce6385d008 659 ******************************************************************************/
SDesign2018 4:b0ce6385d008 660 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 5:c9ab5062cfc3 661 uint8_t BitBangSPIRead (uint8_t regAddr)
SDesign2018 4:b0ce6385d008 662 {
SDesign2018 4:b0ce6385d008 663
SDesign2018 4:b0ce6385d008 664 unsigned char SPICount; // Counter used to clock out the data
SDesign2018 4:b0ce6385d008 665
SDesign2018 5:c9ab5062cfc3 666 uint8_t SPIData;
SDesign2018 4:b0ce6385d008 667
SDesign2018 4:b0ce6385d008 668 SPI_CS = 0; // Make sure we start with active-low CS high
SDesign2018 4:b0ce6385d008 669 SPI_CLK = 0; // and CK low
SDesign2018 4:b0ce6385d008 670 SPIData = regAddr; // Preload the data to be sent with Address and Data
SDesign2018 4:b0ce6385d008 671
SDesign2018 4:b0ce6385d008 672 SPI_CS = 1; // Set active-low CS low to start the SPI cycle
SDesign2018 4:b0ce6385d008 673 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address and Data
SDesign2018 4:b0ce6385d008 674 {
SDesign2018 4:b0ce6385d008 675 if (SPIData & 0x80)
SDesign2018 4:b0ce6385d008 676 MOSI = 1;
SDesign2018 4:b0ce6385d008 677 else
SDesign2018 4:b0ce6385d008 678 MOSI = 0;
SDesign2018 4:b0ce6385d008 679 SPI_CLK = 1;
SDesign2018 4:b0ce6385d008 680 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 681 SPIData <<= 1;
SDesign2018 4:b0ce6385d008 682 }// and loop back to send the next bit
SDesign2018 4:b0ce6385d008 683
SDesign2018 4:b0ce6385d008 684 MOSI = 0; // Reset the MOSI data line
SDesign2018 4:b0ce6385d008 685
SDesign2018 4:b0ce6385d008 686 SPIData = 0;
SDesign2018 4:b0ce6385d008 687 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock in the data to be read
SDesign2018 4:b0ce6385d008 688 {
SDesign2018 4:b0ce6385d008 689 SPIData <<=1; // Rotate the data, keep previous bit value
SDesign2018 4:b0ce6385d008 690 SPI_CLK = 1; // Raise the clock to clock the data out of the MAX7456
SDesign2018 4:b0ce6385d008 691 SPIData += SPI_MISO; // Read the data one bit at a time, starting from MISO MSB
SDesign2018 4:b0ce6385d008 692 SPI_CLK = 0; // Drop the clock ready for the next bit
SDesign2018 4:b0ce6385d008 693 }// and loop back for next bit
SDesign2018 4:b0ce6385d008 694 SPI_CS = 0; // Raise CS
SDesign2018 4:b0ce6385d008 695
SDesign2018 5:c9ab5062cfc3 696 return ((uint8_t)SPIData); // Finally return the read data
SDesign2018 4:b0ce6385d008 697 }
SDesign2018 4:b0ce6385d008 698
SDesign2018 4:b0ce6385d008 699 /*******************************************************************************
SDesign2018 4:b0ce6385d008 700 * Not really used at the moment
SDesign2018 4:b0ce6385d008 701 * Not really needed. But keep just in case because I don't want to rewrite it
SDesign2018 4:b0ce6385d008 702 ******************************************************************************/
SDesign2018 4:b0ce6385d008 703 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 704 unsigned char twosComplementConversion(unsigned char value)
SDesign2018 4:b0ce6385d008 705 {
SDesign2018 4:b0ce6385d008 706 /*
SDesign2018 4:b0ce6385d008 707 * Go from bit 0 to bit 7 and invert them
SDesign2018 4:b0ce6385d008 708 * Then finally add 1
SDesign2018 4:b0ce6385d008 709 */
SDesign2018 4:b0ce6385d008 710 char mask = value & 0x80;
SDesign2018 4:b0ce6385d008 711 if(mask == 0x80){ // Check for sign
SDesign2018 4:b0ce6385d008 712 value = ~value + 1;
SDesign2018 4:b0ce6385d008 713 return value;
SDesign2018 4:b0ce6385d008 714 }
SDesign2018 4:b0ce6385d008 715 return value;
SDesign2018 4:b0ce6385d008 716 }
SDesign2018 4:b0ce6385d008 717 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 718
SDesign2018 4:b0ce6385d008 719
SDesign2018 4:b0ce6385d008 720 /*******************************************************************************
SDesign2018 4:b0ce6385d008 721 * Performs one byte write I2C protocol
SDesign2018 4:b0ce6385d008 722 * PARAM:
SDesign2018 4:b0ce6385d008 723 * registerAddress: register you want access to in device, one byte char hex format
SDesign2018 4:b0ce6385d008 724 * data: one byte data that you want to write to device register
SDesign2018 4:b0ce6385d008 725 * Return:
SDesign2018 4:b0ce6385d008 726 * 0: failure at writing i2c address
SDesign2018 4:b0ce6385d008 727 * 1: successful write
SDesign2018 4:b0ce6385d008 728 * 2: failure at writing data
SDesign2018 4:b0ce6385d008 729 * 3: failure at writing register address
SDesign2018 4:b0ce6385d008 730 ******************************************************************************/
SDesign2018 4:b0ce6385d008 731 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 732 int ADT7410Write(unsigned char registerAddress, unsigned char data){
SDesign2018 4:b0ce6385d008 733 int flag;
SDesign2018 4:b0ce6385d008 734 ADT7410.start();
SDesign2018 4:b0ce6385d008 735 flag = ADT7410.write(ADT7410_Address_8BIT);
SDesign2018 4:b0ce6385d008 736 if(flag == 1)
SDesign2018 4:b0ce6385d008 737 {
SDesign2018 4:b0ce6385d008 738 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 739 wait(0.1);
SDesign2018 4:b0ce6385d008 740 flag = ADT7410.write(registerAddress);
SDesign2018 4:b0ce6385d008 741 if(flag == 1)
SDesign2018 4:b0ce6385d008 742 {
SDesign2018 4:b0ce6385d008 743 pc.printf("Write to register 0x%x address success\n\r", registerAddress);
SDesign2018 4:b0ce6385d008 744 flag = ADT7410.write(data);
SDesign2018 4:b0ce6385d008 745 if(flag == 1)
SDesign2018 4:b0ce6385d008 746 {
SDesign2018 4:b0ce6385d008 747 pc.printf("Writing data 0x%x to register address success\n\r", data);
SDesign2018 4:b0ce6385d008 748 ADT7410.stop();
SDesign2018 4:b0ce6385d008 749 return 1;
SDesign2018 4:b0ce6385d008 750 }else {ADT7410.stop(); return 2;}
SDesign2018 4:b0ce6385d008 751 }else {ADT7410.stop(); return 3;}
SDesign2018 4:b0ce6385d008 752 }else ADT7410.stop();
SDesign2018 4:b0ce6385d008 753
SDesign2018 4:b0ce6385d008 754 return 0;
SDesign2018 4:b0ce6385d008 755 }
SDesign2018 4:b0ce6385d008 756 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 757
SDesign2018 4:b0ce6385d008 758 /*******************************************************************************
SDesign2018 4:b0ce6385d008 759 * I2C Read function for ADT7410 Temperature sensor
SDesign2018 4:b0ce6385d008 760 * Param:
SDesign2018 4:b0ce6385d008 761 * hex: hexadecimal representation for desired register
SDesign2018 4:b0ce6385d008 762 * Return:
SDesign2018 4:b0ce6385d008 763 * Char pointer to the array of data values.
SDesign2018 4:b0ce6385d008 764 * Could also change from a char pointer to a uint8_t pointer.
SDesign2018 4:b0ce6385d008 765 *
SDesign2018 4:b0ce6385d008 766 ******************************************************************************/
SDesign2018 4:b0ce6385d008 767 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 768 char * ADT7410Read(int hex){
SDesign2018 4:b0ce6385d008 769 //short int convertedVal;
SDesign2018 4:b0ce6385d008 770 char data[2] = {0, 0};
SDesign2018 4:b0ce6385d008 771 char cmd[1];
SDesign2018 4:b0ce6385d008 772 cmd[0] = hex;
SDesign2018 4:b0ce6385d008 773 //pc.printf("Register Addres is: %x \n\r", cmd[0]);
SDesign2018 4:b0ce6385d008 774 if(ADT7410.write(ADT7410_Address_8BIT, cmd,1) == 0){
SDesign2018 4:b0ce6385d008 775 if(ADT7410.read(ADT7410_Address_8BIT, data, 2) == 0){
SDesign2018 4:b0ce6385d008 776
SDesign2018 4:b0ce6385d008 777 return data;
SDesign2018 4:b0ce6385d008 778 //return (data[0] << 8 | data[1])>>3; // Explained here: https://stackoverflow.com/a/141576 SOOO GREAT
SDesign2018 4:b0ce6385d008 779
SDesign2018 4:b0ce6385d008 780 }else {pc.printf("Failed to read \n\r"); return data;}
SDesign2018 4:b0ce6385d008 781 }else {pc.printf("Failed to write \n\r"); return data;}
SDesign2018 4:b0ce6385d008 782
SDesign2018 4:b0ce6385d008 783 }
SDesign2018 4:b0ce6385d008 784 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 785
SDesign2018 4:b0ce6385d008 786
SDesign2018 4:b0ce6385d008 787 /*******************************************************************************
SDesign2018 4:b0ce6385d008 788 * ADXL372 reset function
SDesign2018 4:b0ce6385d008 789 * Resets all registers and settings back to default
SDesign2018 4:b0ce6385d008 790 * Basically the same as the previous ADXL372 I2C write function
SDesign2018 4:b0ce6385d008 791 *
SDesign2018 4:b0ce6385d008 792 ******************************************************************************/
SDesign2018 4:b0ce6385d008 793 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 794 void ADXL372Reset(void){
SDesign2018 4:b0ce6385d008 795 int flag;
SDesign2018 4:b0ce6385d008 796 //--------- One full writing cycle for ADXL372 for Z Enable ------------------//
SDesign2018 4:b0ce6385d008 797 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 798 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 799 * '2' - timeout
SDesign2018 4:b0ce6385d008 800 */
SDesign2018 4:b0ce6385d008 801 ADXL372.start();
SDesign2018 4:b0ce6385d008 802 flag = ADXL372.write(ADXL372_Address_8bit | 0);
SDesign2018 4:b0ce6385d008 803 if(flag == 1)
SDesign2018 4:b0ce6385d008 804 {
SDesign2018 4:b0ce6385d008 805 //pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 806
SDesign2018 4:b0ce6385d008 807 flag = ADXL372.write(0x41);
SDesign2018 4:b0ce6385d008 808 if(flag == 1)
SDesign2018 4:b0ce6385d008 809 {
SDesign2018 4:b0ce6385d008 810 //pc.printf("Write to 0x41 register address success\n\r");
SDesign2018 4:b0ce6385d008 811 flag = ADXL372.write(0x52); // Set bit 0
SDesign2018 4:b0ce6385d008 812 if(flag == 1)
SDesign2018 4:b0ce6385d008 813 {
SDesign2018 4:b0ce6385d008 814 pc.printf("Everything has been reset\n\r");
SDesign2018 4:b0ce6385d008 815 ADXL372.stop();
SDesign2018 4:b0ce6385d008 816 }
SDesign2018 4:b0ce6385d008 817 }
SDesign2018 4:b0ce6385d008 818 }
SDesign2018 4:b0ce6385d008 819 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 820 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 821 }
SDesign2018 4:b0ce6385d008 822 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 6:3b41238872a8 823
SDesign2018 6:3b41238872a8 824 // Used to clear serial buffer in beginning
SDesign2018 6:3b41238872a8 825 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 6:3b41238872a8 826 void flushSerialBuffer(void) { char char1 = 0; while (pc.readable()) { char1 = pc.getc(); } return; }