Denwis La / Mbed OS mDot_Send_Data

Dependencies:   libmDot-dev-mbed5-deprecated ISL29011

Fork of mdot-examples by 3mdeb

Committer:
SDesign2018
Date:
Fri Dec 01 21:08:13 2017 +0000
Revision:
7:5f2b3d1a9b0b
Parent:
6:3b41238872a8
Child:
8:efab0e415826
Tried scanf but same results. D:; PLEASE HELP

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 5:c9ab5062cfc3 101 "9: SPI Write to a register 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 5:c9ab5062cfc3 376 case 57: // 9
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 6:3b41238872a8 382 pc.printf("What register do you want(2 digit hex form)? \n\r"
SDesign2018 6:3b41238872a8 383 "Ex: Register 0x01 enter 01\n\r");
SDesign2018 7:5f2b3d1a9b0b 384 pc.scanf("%s\n\r", input);
SDesign2018 6:3b41238872a8 385 intRegister = atoi(input);
SDesign2018 6:3b41238872a8 386 itoa(intRegister, passRegister, 16); // Convert number(input), into hexadecimal(16), and save in char buffer(passVariable)
SDesign2018 6:3b41238872a8 387 passRegister[0] = atoi(passRegister);
SDesign2018 5:c9ab5062cfc3 388 pc.printf("0x%x register\n\r", passRegister[0]);
SDesign2018 6:3b41238872a8 389
SDesign2018 6:3b41238872a8 390 pc.printf("What is the hex representation of your data?\n\r");
SDesign2018 7:5f2b3d1a9b0b 391 pc.scanf("%s\n\r", input);
SDesign2018 6:3b41238872a8 392 intData = atoi(input); // Convert input to integer
SDesign2018 6:3b41238872a8 393 itoa(intRegister, passData, 16); //Convert number(input), into hexadecimal(16), and save in char buffer(passVariable)
SDesign2018 6:3b41238872a8 394 passData[0] = atoi(passData);
SDesign2018 5:c9ab5062cfc3 395 pc.printf("0x%x is your data\n\r", passData[0]);
SDesign2018 6:3b41238872a8 396 break;
SDesign2018 4:b0ce6385d008 397 default:
SDesign2018 4:b0ce6385d008 398 printMenu();
SDesign2018 4:b0ce6385d008 399 break;
SDesign2018 4:b0ce6385d008 400 }
SDesign2018 4:b0ce6385d008 401 }
SDesign2018 4:b0ce6385d008 402
SDesign2018 4:b0ce6385d008 403 }
SDesign2018 4:b0ce6385d008 404
SDesign2018 4:b0ce6385d008 405 return 0;
SDesign2018 4:b0ce6385d008 406 }
SDesign2018 4:b0ce6385d008 407 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 408
SDesign2018 4:b0ce6385d008 409
SDesign2018 4:b0ce6385d008 410 /*******************************************************************************
SDesign2018 4:b0ce6385d008 411 *
SDesign2018 4:b0ce6385d008 412 * I2C function for the the ADXL372 accelerometer for a write sequence
SDesign2018 4:b0ce6385d008 413 * Param:
SDesign2018 4:b0ce6385d008 414 * hexAddress: Pass the hexadecimal value for what register you want
SDesign2018 4:b0ce6385d008 415 * hexData: Pass the hexadecimal value for what data you want to send
SDesign2018 4:b0ce6385d008 416 * i.e. hexadecimal represenatation of certain bits
SDesign2018 4:b0ce6385d008 417 * Returns:
SDesign2018 4:b0ce6385d008 418 * 1: Write was a complete success
SDesign2018 4:b0ce6385d008 419 * 2: Writing data to register failed
SDesign2018 4:b0ce6385d008 420 * 3: Writing to register Address failed
SDesign2018 4:b0ce6385d008 421 ******************************************************************************/
SDesign2018 4:b0ce6385d008 422 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 423 int accelerometerI2CWrite(int hexAddress, int hexData){
SDesign2018 4:b0ce6385d008 424 //--------- One full writing cycle for ADXL372 for X enable ------------------//
SDesign2018 4:b0ce6385d008 425 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 426 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 427 * '2' - timeout
SDesign2018 4:b0ce6385d008 428 */
SDesign2018 4:b0ce6385d008 429 int flag;
SDesign2018 4:b0ce6385d008 430 int registerAddress = hexAddress;
SDesign2018 4:b0ce6385d008 431 int data = hexData;
SDesign2018 4:b0ce6385d008 432
SDesign2018 4:b0ce6385d008 433 ADXL372.start();
SDesign2018 4:b0ce6385d008 434 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 435 if(flag == 1)
SDesign2018 4:b0ce6385d008 436 {
SDesign2018 4:b0ce6385d008 437 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 438 wait(0.1);
SDesign2018 4:b0ce6385d008 439 flag = ADXL372.write(registerAddress);
SDesign2018 4:b0ce6385d008 440 if(flag == 1)
SDesign2018 4:b0ce6385d008 441 {
SDesign2018 4:b0ce6385d008 442 pc.printf("Write to register 0x%x address success\n\r", registerAddress);
SDesign2018 4:b0ce6385d008 443 flag = ADXL372.write(data);
SDesign2018 4:b0ce6385d008 444 if(flag == 1)
SDesign2018 4:b0ce6385d008 445 {
SDesign2018 4:b0ce6385d008 446 pc.printf("Writing data 0x%x to register address success\n\r", data);
SDesign2018 4:b0ce6385d008 447 ADXL372.stop();
SDesign2018 4:b0ce6385d008 448 return 1;
SDesign2018 4:b0ce6385d008 449 }else {ADXL372.stop(); return 2;}
SDesign2018 4:b0ce6385d008 450 }else {ADXL372.stop(); return 3;}
SDesign2018 4:b0ce6385d008 451 }else ADXL372.stop();
SDesign2018 4:b0ce6385d008 452
SDesign2018 4:b0ce6385d008 453 return 0;
SDesign2018 4:b0ce6385d008 454 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 455 }
SDesign2018 4:b0ce6385d008 456 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 457
SDesign2018 4:b0ce6385d008 458
SDesign2018 4:b0ce6385d008 459 /*******************************************************************************
SDesign2018 4:b0ce6385d008 460 * I2C read sequence for the accelerometer
SDesign2018 4:b0ce6385d008 461 * Param:
SDesign2018 4:b0ce6385d008 462 * hexAddress: pass the hexadecimal representation of desired Register address
SDesign2018 4:b0ce6385d008 463 * Return:
SDesign2018 4:b0ce6385d008 464 * Char pointer to the array of read data.
SDesign2018 4:b0ce6385d008 465 *
SDesign2018 4:b0ce6385d008 466 * Right now it works only for the XData, YData, ZData because I wrote it to read
SDesign2018 4:b0ce6385d008 467 * 6 bytes(6 registers).
SDesign2018 4:b0ce6385d008 468 * Should change it to be 1 byte at a time
SDesign2018 4:b0ce6385d008 469 ******************************************************************************/
SDesign2018 4:b0ce6385d008 470 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 471 char * accelerometerI2CRead(int hexAddress){
SDesign2018 4:b0ce6385d008 472 char accelData[6];
SDesign2018 4:b0ce6385d008 473 char registerAddress[1];
SDesign2018 4:b0ce6385d008 474 registerAddress[0] = hexAddress;
SDesign2018 4:b0ce6385d008 475
SDesign2018 4:b0ce6385d008 476 // Perform mbed's way sending a start bit, then device address[r/~w], and then the register address
SDesign2018 4:b0ce6385d008 477 // Also if it succeeds, continue to the next operation
SDesign2018 4:b0ce6385d008 478 if(ADXL372.write(ADXL372_Address_8bit, registerAddress, 1) == 0){
SDesign2018 4:b0ce6385d008 479
SDesign2018 4:b0ce6385d008 480 // If previous sequence works, get 6 bytes into char array accelData
SDesign2018 4:b0ce6385d008 481 // Char array because it uses 1 byte(8bits)
SDesign2018 4:b0ce6385d008 482 // Should probably change it to uint8_t type
SDesign2018 4:b0ce6385d008 483 if(ADXL372.read(ADXL372_Address_8bit, accelData, 6) == 0){
SDesign2018 4:b0ce6385d008 484 return accelData;
SDesign2018 4:b0ce6385d008 485 }else pc.printf("Failed to read\n\r");
SDesign2018 4:b0ce6385d008 486 }else pc.printf("Failed to write\n\r");
SDesign2018 4:b0ce6385d008 487 return 0; // Only if it fails
SDesign2018 4:b0ce6385d008 488 }
SDesign2018 4:b0ce6385d008 489 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 490
SDesign2018 4:b0ce6385d008 491
SDesign2018 4:b0ce6385d008 492
SDesign2018 4:b0ce6385d008 493 /*******************************************************************************
SDesign2018 4:b0ce6385d008 494 * Initializes whatever settings you want for the accelerometer
SDesign2018 4:b0ce6385d008 495 * Can change it to use the previous I2C write function instead of all this mess
SDesign2018 4:b0ce6385d008 496 *
SDesign2018 4:b0ce6385d008 497 ******************************************************************************/
SDesign2018 4:b0ce6385d008 498 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 499 void ADXL372Initialize(void){
SDesign2018 4:b0ce6385d008 500 int flag;
SDesign2018 4:b0ce6385d008 501
SDesign2018 4:b0ce6385d008 502 //--------- Change mode to full Bandwidth Measurement Mode ------------------//
SDesign2018 4:b0ce6385d008 503 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 504 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 505 * '2' - timeout
SDesign2018 4:b0ce6385d008 506 */
SDesign2018 4:b0ce6385d008 507 pc.printf("Initializing Single Byte write\n\r");
SDesign2018 4:b0ce6385d008 508 ADXL372.start(); // Send start
SDesign2018 4:b0ce6385d008 509 flag = ADXL372.write(ADXL372_Address_8bit | 0); // Send Device Address
SDesign2018 4:b0ce6385d008 510 if(flag == 1)
SDesign2018 4:b0ce6385d008 511 {
SDesign2018 4:b0ce6385d008 512 pc.printf("Write to I2C address for write success\n\r");
SDesign2018 4:b0ce6385d008 513 wait(0.1);
SDesign2018 4:b0ce6385d008 514 flag = ADXL372.write(0x3F); // Send Register Address
SDesign2018 4:b0ce6385d008 515 if(flag == 1)
SDesign2018 4:b0ce6385d008 516 {
SDesign2018 4:b0ce6385d008 517
SDesign2018 4:b0ce6385d008 518 pc.printf("Write to 0x3F register address success\n\r");
SDesign2018 4:b0ce6385d008 519 flag = ADXL372.write(0x03); // Send Data that represents Full BandWidth mode
SDesign2018 4:b0ce6385d008 520 if(flag == 1)
SDesign2018 4:b0ce6385d008 521 {
SDesign2018 4:b0ce6385d008 522 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 523 ADXL372.stop();
SDesign2018 4:b0ce6385d008 524 }
SDesign2018 4:b0ce6385d008 525 }
SDesign2018 4:b0ce6385d008 526 }
SDesign2018 4:b0ce6385d008 527 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 528 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 529
SDesign2018 4:b0ce6385d008 530 //--------- One full writing cycle for ADXL372 for X enable ------------------//
SDesign2018 4:b0ce6385d008 531 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 532 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 533 * '2' - timeout
SDesign2018 4:b0ce6385d008 534 */
SDesign2018 4:b0ce6385d008 535 ADXL372.start();
SDesign2018 4:b0ce6385d008 536 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 537 if(flag == 1)
SDesign2018 4:b0ce6385d008 538 {
SDesign2018 4:b0ce6385d008 539 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 540 wait(0.1);
SDesign2018 4:b0ce6385d008 541 flag = ADXL372.write(0x24);
SDesign2018 4:b0ce6385d008 542 if(flag == 1)
SDesign2018 4:b0ce6385d008 543 {
SDesign2018 4:b0ce6385d008 544 pc.printf("Write to register 0x24 address success\n\r");
SDesign2018 4:b0ce6385d008 545 flag = ADXL372.write(0x01);
SDesign2018 4:b0ce6385d008 546 if(flag == 1)
SDesign2018 4:b0ce6385d008 547 {
SDesign2018 4:b0ce6385d008 548 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 549 pc.printf("X Enable should be set now\n\r");
SDesign2018 4:b0ce6385d008 550 ADXL372.stop();
SDesign2018 4:b0ce6385d008 551 }
SDesign2018 4:b0ce6385d008 552 }
SDesign2018 4:b0ce6385d008 553 }
SDesign2018 4:b0ce6385d008 554 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 555 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 556
SDesign2018 4:b0ce6385d008 557 //--------- One full writing cycle for ADXL372 for Y Enable ------------------//
SDesign2018 4:b0ce6385d008 558 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 559 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 560 * '2' - timeout
SDesign2018 4:b0ce6385d008 561 */
SDesign2018 4:b0ce6385d008 562 ADXL372.start();
SDesign2018 4:b0ce6385d008 563 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 564 if(flag == 1)
SDesign2018 4:b0ce6385d008 565 {
SDesign2018 4:b0ce6385d008 566 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 567 wait(0.1);
SDesign2018 4:b0ce6385d008 568 flag = ADXL372.write(0x25);
SDesign2018 4:b0ce6385d008 569 if(flag == 1)
SDesign2018 4:b0ce6385d008 570 {
SDesign2018 4:b0ce6385d008 571 pc.printf("Write to 0x25 register address success\n\r");
SDesign2018 4:b0ce6385d008 572 flag = ADXL372.write(0x01);
SDesign2018 4:b0ce6385d008 573 if(flag == 1)
SDesign2018 4:b0ce6385d008 574 {
SDesign2018 4:b0ce6385d008 575 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 576 pc.printf("Y Enable should be set now\n\r");
SDesign2018 4:b0ce6385d008 577
SDesign2018 4:b0ce6385d008 578 ADXL372.stop();
SDesign2018 4:b0ce6385d008 579 }
SDesign2018 4:b0ce6385d008 580 }
SDesign2018 4:b0ce6385d008 581 }
SDesign2018 4:b0ce6385d008 582 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 583 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 584
SDesign2018 4:b0ce6385d008 585 //--------- One full writing cycle for ADXL372 for Z Enable ------------------//
SDesign2018 4:b0ce6385d008 586 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 587 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 588 * '2' - timeout
SDesign2018 4:b0ce6385d008 589 */
SDesign2018 4:b0ce6385d008 590 ADXL372.start();
SDesign2018 4:b0ce6385d008 591 flag = ADXL372.write(ADXL372_Address_8bit);
SDesign2018 4:b0ce6385d008 592 if(flag == 1)
SDesign2018 4:b0ce6385d008 593 {
SDesign2018 4:b0ce6385d008 594 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 595 wait(0.1);
SDesign2018 4:b0ce6385d008 596 flag = ADXL372.write(0x26);
SDesign2018 4:b0ce6385d008 597 if(flag == 1)
SDesign2018 4:b0ce6385d008 598 {
SDesign2018 4:b0ce6385d008 599 pc.printf("Write to 0x26 register address success\n\r");
SDesign2018 4:b0ce6385d008 600 flag = ADXL372.write(0x01); // Set bit 0
SDesign2018 4:b0ce6385d008 601 if(flag == 1)
SDesign2018 4:b0ce6385d008 602 {
SDesign2018 4:b0ce6385d008 603 pc.printf("Write data to register address success\n\r");
SDesign2018 4:b0ce6385d008 604 pc.printf("Z Enable should be set now\n\r");
SDesign2018 4:b0ce6385d008 605
SDesign2018 4:b0ce6385d008 606 ADXL372.stop();
SDesign2018 4:b0ce6385d008 607 }
SDesign2018 4:b0ce6385d008 608 }
SDesign2018 4:b0ce6385d008 609 }
SDesign2018 4:b0ce6385d008 610 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 611 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 612
SDesign2018 4:b0ce6385d008 613 }
SDesign2018 4:b0ce6385d008 614 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 615
SDesign2018 4:b0ce6385d008 616
SDesign2018 4:b0ce6385d008 617
SDesign2018 4:b0ce6385d008 618 /*******************************************************************************
SDesign2018 4:b0ce6385d008 619 * BitBangSPIWrite for ADXL372 if you wire it up as SPI
SDesign2018 4:b0ce6385d008 620 *
SDesign2018 4:b0ce6385d008 621 * Sends the 6-0bits of desired register with LSB of transmission bit R/!W
SDesign2018 4:b0ce6385d008 622 *
SDesign2018 4:b0ce6385d008 623 ******************************************************************************/
SDesign2018 4:b0ce6385d008 624 ///////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 625 void BitBangSPIWrite(const unsigned char regAddr, const unsigned char regData)
SDesign2018 4:b0ce6385d008 626 {
SDesign2018 4:b0ce6385d008 627 unsigned char SPICount; // Counter used to clock out the data
SDesign2018 4:b0ce6385d008 628
SDesign2018 4:b0ce6385d008 629 unsigned char SPIData; // Define a data structure for the SPI data
SDesign2018 4:b0ce6385d008 630 SPI_CS = 0; // Make sure we start with active-low CS high
SDesign2018 4:b0ce6385d008 631 SPI_CLK = 0; // and CK low
SDesign2018 4:b0ce6385d008 632
SDesign2018 4:b0ce6385d008 633 SPIData = regAddr; // Preload the data to be sent with Address
SDesign2018 4:b0ce6385d008 634 SPI_CS = 1; // Set active-low CS low to start the SPI cycle
SDesign2018 4:b0ce6385d008 635
SDesign2018 4:b0ce6385d008 636 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address byte
SDesign2018 4:b0ce6385d008 637 {
SDesign2018 4:b0ce6385d008 638 if (SPIData & 0x80) // Check for a 1 at MSB
SDesign2018 4:b0ce6385d008 639 MOSI = 1; // and set the MOSI line appropriately
SDesign2018 4:b0ce6385d008 640 else
SDesign2018 4:b0ce6385d008 641 MOSI = 0;
SDesign2018 4:b0ce6385d008 642 SPI_CLK = 1; // Toggle the clock line
SDesign2018 4:b0ce6385d008 643 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 644 SPIData <<= 1; // Rotate to get the next bit
SDesign2018 4:b0ce6385d008 645 } // and loop back to send the next bit
SDesign2018 4:b0ce6385d008 646
SDesign2018 4:b0ce6385d008 647 // Repeat for the Data byte
SDesign2018 4:b0ce6385d008 648 SPIData = regData; // Preload the data to be sent with Data
SDesign2018 4:b0ce6385d008 649 for (SPICount = 0; SPICount < 8; SPICount++)
SDesign2018 4:b0ce6385d008 650 {
SDesign2018 4:b0ce6385d008 651 if (SPIData & 0x80)
SDesign2018 4:b0ce6385d008 652 MOSI = 1;
SDesign2018 4:b0ce6385d008 653 else
SDesign2018 4:b0ce6385d008 654 MOSI = 0;
SDesign2018 4:b0ce6385d008 655 SPI_CLK = 1;
SDesign2018 4:b0ce6385d008 656 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 657 SPIData <<= 1; // After each bit, move next bit one to left
SDesign2018 4:b0ce6385d008 658 }
SDesign2018 4:b0ce6385d008 659 SPI_CS = 0;
SDesign2018 4:b0ce6385d008 660 MOSI = 0;
SDesign2018 4:b0ce6385d008 661 }
SDesign2018 4:b0ce6385d008 662 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 663
SDesign2018 4:b0ce6385d008 664
SDesign2018 4:b0ce6385d008 665
SDesign2018 4:b0ce6385d008 666 /*******************************************************************************
SDesign2018 4:b0ce6385d008 667 * BitBangSPIRead for ADXL372 if you wire it up as SPI
SDesign2018 4:b0ce6385d008 668 *
SDesign2018 4:b0ce6385d008 669 *
SDesign2018 4:b0ce6385d008 670 *
SDesign2018 4:b0ce6385d008 671 ******************************************************************************/
SDesign2018 4:b0ce6385d008 672 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 5:c9ab5062cfc3 673 uint8_t BitBangSPIRead (uint8_t regAddr)
SDesign2018 4:b0ce6385d008 674 {
SDesign2018 4:b0ce6385d008 675
SDesign2018 4:b0ce6385d008 676 unsigned char SPICount; // Counter used to clock out the data
SDesign2018 4:b0ce6385d008 677
SDesign2018 5:c9ab5062cfc3 678 uint8_t SPIData;
SDesign2018 4:b0ce6385d008 679
SDesign2018 4:b0ce6385d008 680 SPI_CS = 0; // Make sure we start with active-low CS high
SDesign2018 4:b0ce6385d008 681 SPI_CLK = 0; // and CK low
SDesign2018 4:b0ce6385d008 682 SPIData = regAddr; // Preload the data to be sent with Address and Data
SDesign2018 4:b0ce6385d008 683
SDesign2018 4:b0ce6385d008 684 SPI_CS = 1; // Set active-low CS low to start the SPI cycle
SDesign2018 4:b0ce6385d008 685 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock out the Address and Data
SDesign2018 4:b0ce6385d008 686 {
SDesign2018 4:b0ce6385d008 687 if (SPIData & 0x80)
SDesign2018 4:b0ce6385d008 688 MOSI = 1;
SDesign2018 4:b0ce6385d008 689 else
SDesign2018 4:b0ce6385d008 690 MOSI = 0;
SDesign2018 4:b0ce6385d008 691 SPI_CLK = 1;
SDesign2018 4:b0ce6385d008 692 SPI_CLK = 0;
SDesign2018 4:b0ce6385d008 693 SPIData <<= 1;
SDesign2018 4:b0ce6385d008 694 }// and loop back to send the next bit
SDesign2018 4:b0ce6385d008 695
SDesign2018 4:b0ce6385d008 696 MOSI = 0; // Reset the MOSI data line
SDesign2018 4:b0ce6385d008 697
SDesign2018 4:b0ce6385d008 698 SPIData = 0;
SDesign2018 4:b0ce6385d008 699 for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock in the data to be read
SDesign2018 4:b0ce6385d008 700 {
SDesign2018 4:b0ce6385d008 701 SPIData <<=1; // Rotate the data, keep previous bit value
SDesign2018 4:b0ce6385d008 702 SPI_CLK = 1; // Raise the clock to clock the data out of the MAX7456
SDesign2018 4:b0ce6385d008 703 SPIData += SPI_MISO; // Read the data one bit at a time, starting from MISO MSB
SDesign2018 4:b0ce6385d008 704 SPI_CLK = 0; // Drop the clock ready for the next bit
SDesign2018 4:b0ce6385d008 705 }// and loop back for next bit
SDesign2018 4:b0ce6385d008 706 SPI_CS = 0; // Raise CS
SDesign2018 4:b0ce6385d008 707
SDesign2018 5:c9ab5062cfc3 708 return ((uint8_t)SPIData); // Finally return the read data
SDesign2018 4:b0ce6385d008 709 }
SDesign2018 4:b0ce6385d008 710
SDesign2018 4:b0ce6385d008 711 /*******************************************************************************
SDesign2018 4:b0ce6385d008 712 * Not really used at the moment
SDesign2018 4:b0ce6385d008 713 * Not really needed. But keep just in case because I don't want to rewrite it
SDesign2018 4:b0ce6385d008 714 ******************************************************************************/
SDesign2018 4:b0ce6385d008 715 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 716 unsigned char twosComplementConversion(unsigned char value)
SDesign2018 4:b0ce6385d008 717 {
SDesign2018 4:b0ce6385d008 718 /*
SDesign2018 4:b0ce6385d008 719 * Go from bit 0 to bit 7 and invert them
SDesign2018 4:b0ce6385d008 720 * Then finally add 1
SDesign2018 4:b0ce6385d008 721 */
SDesign2018 4:b0ce6385d008 722 char mask = value & 0x80;
SDesign2018 4:b0ce6385d008 723 if(mask == 0x80){ // Check for sign
SDesign2018 4:b0ce6385d008 724 value = ~value + 1;
SDesign2018 4:b0ce6385d008 725 return value;
SDesign2018 4:b0ce6385d008 726 }
SDesign2018 4:b0ce6385d008 727 return value;
SDesign2018 4:b0ce6385d008 728 }
SDesign2018 4:b0ce6385d008 729 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 730
SDesign2018 4:b0ce6385d008 731
SDesign2018 4:b0ce6385d008 732 /*******************************************************************************
SDesign2018 4:b0ce6385d008 733 * Performs one byte write I2C protocol
SDesign2018 4:b0ce6385d008 734 * PARAM:
SDesign2018 4:b0ce6385d008 735 * registerAddress: register you want access to in device, one byte char hex format
SDesign2018 4:b0ce6385d008 736 * data: one byte data that you want to write to device register
SDesign2018 4:b0ce6385d008 737 * Return:
SDesign2018 4:b0ce6385d008 738 * 0: failure at writing i2c address
SDesign2018 4:b0ce6385d008 739 * 1: successful write
SDesign2018 4:b0ce6385d008 740 * 2: failure at writing data
SDesign2018 4:b0ce6385d008 741 * 3: failure at writing register address
SDesign2018 4:b0ce6385d008 742 ******************************************************************************/
SDesign2018 4:b0ce6385d008 743 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 744 int ADT7410Write(unsigned char registerAddress, unsigned char data){
SDesign2018 4:b0ce6385d008 745 int flag;
SDesign2018 4:b0ce6385d008 746 ADT7410.start();
SDesign2018 4:b0ce6385d008 747 flag = ADT7410.write(ADT7410_Address_8BIT);
SDesign2018 4:b0ce6385d008 748 if(flag == 1)
SDesign2018 4:b0ce6385d008 749 {
SDesign2018 4:b0ce6385d008 750 pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 751 wait(0.1);
SDesign2018 4:b0ce6385d008 752 flag = ADT7410.write(registerAddress);
SDesign2018 4:b0ce6385d008 753 if(flag == 1)
SDesign2018 4:b0ce6385d008 754 {
SDesign2018 4:b0ce6385d008 755 pc.printf("Write to register 0x%x address success\n\r", registerAddress);
SDesign2018 4:b0ce6385d008 756 flag = ADT7410.write(data);
SDesign2018 4:b0ce6385d008 757 if(flag == 1)
SDesign2018 4:b0ce6385d008 758 {
SDesign2018 4:b0ce6385d008 759 pc.printf("Writing data 0x%x to register address success\n\r", data);
SDesign2018 4:b0ce6385d008 760 ADT7410.stop();
SDesign2018 4:b0ce6385d008 761 return 1;
SDesign2018 4:b0ce6385d008 762 }else {ADT7410.stop(); return 2;}
SDesign2018 4:b0ce6385d008 763 }else {ADT7410.stop(); return 3;}
SDesign2018 4:b0ce6385d008 764 }else ADT7410.stop();
SDesign2018 4:b0ce6385d008 765
SDesign2018 4:b0ce6385d008 766 return 0;
SDesign2018 4:b0ce6385d008 767 }
SDesign2018 4:b0ce6385d008 768 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 769
SDesign2018 4:b0ce6385d008 770 /*******************************************************************************
SDesign2018 4:b0ce6385d008 771 * I2C Read function for ADT7410 Temperature sensor
SDesign2018 4:b0ce6385d008 772 * Param:
SDesign2018 4:b0ce6385d008 773 * hex: hexadecimal representation for desired register
SDesign2018 4:b0ce6385d008 774 * Return:
SDesign2018 4:b0ce6385d008 775 * Char pointer to the array of data values.
SDesign2018 4:b0ce6385d008 776 * Could also change from a char pointer to a uint8_t pointer.
SDesign2018 4:b0ce6385d008 777 *
SDesign2018 4:b0ce6385d008 778 ******************************************************************************/
SDesign2018 4:b0ce6385d008 779 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 780 char * ADT7410Read(int hex){
SDesign2018 4:b0ce6385d008 781 //short int convertedVal;
SDesign2018 4:b0ce6385d008 782 char data[2] = {0, 0};
SDesign2018 4:b0ce6385d008 783 char cmd[1];
SDesign2018 4:b0ce6385d008 784 cmd[0] = hex;
SDesign2018 4:b0ce6385d008 785 //pc.printf("Register Addres is: %x \n\r", cmd[0]);
SDesign2018 4:b0ce6385d008 786 if(ADT7410.write(ADT7410_Address_8BIT, cmd,1) == 0){
SDesign2018 4:b0ce6385d008 787 if(ADT7410.read(ADT7410_Address_8BIT, data, 2) == 0){
SDesign2018 4:b0ce6385d008 788
SDesign2018 4:b0ce6385d008 789 return data;
SDesign2018 4:b0ce6385d008 790 //return (data[0] << 8 | data[1])>>3; // Explained here: https://stackoverflow.com/a/141576 SOOO GREAT
SDesign2018 4:b0ce6385d008 791
SDesign2018 4:b0ce6385d008 792 }else {pc.printf("Failed to read \n\r"); return data;}
SDesign2018 4:b0ce6385d008 793 }else {pc.printf("Failed to write \n\r"); return data;}
SDesign2018 4:b0ce6385d008 794
SDesign2018 4:b0ce6385d008 795 }
SDesign2018 4:b0ce6385d008 796 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 797
SDesign2018 4:b0ce6385d008 798
SDesign2018 4:b0ce6385d008 799 /*******************************************************************************
SDesign2018 4:b0ce6385d008 800 * ADXL372 reset function
SDesign2018 4:b0ce6385d008 801 * Resets all registers and settings back to default
SDesign2018 4:b0ce6385d008 802 * Basically the same as the previous ADXL372 I2C write function
SDesign2018 4:b0ce6385d008 803 *
SDesign2018 4:b0ce6385d008 804 ******************************************************************************/
SDesign2018 4:b0ce6385d008 805 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 4:b0ce6385d008 806 void ADXL372Reset(void){
SDesign2018 4:b0ce6385d008 807 int flag;
SDesign2018 4:b0ce6385d008 808 //--------- One full writing cycle for ADXL372 for Z Enable ------------------//
SDesign2018 4:b0ce6385d008 809 /* '0' - NAK was received
SDesign2018 4:b0ce6385d008 810 * '1' - ACK was received, <---- This good
SDesign2018 4:b0ce6385d008 811 * '2' - timeout
SDesign2018 4:b0ce6385d008 812 */
SDesign2018 4:b0ce6385d008 813 ADXL372.start();
SDesign2018 4:b0ce6385d008 814 flag = ADXL372.write(ADXL372_Address_8bit | 0);
SDesign2018 4:b0ce6385d008 815 if(flag == 1)
SDesign2018 4:b0ce6385d008 816 {
SDesign2018 4:b0ce6385d008 817 //pc.printf("Write to I2C address success\n\r");
SDesign2018 4:b0ce6385d008 818
SDesign2018 4:b0ce6385d008 819 flag = ADXL372.write(0x41);
SDesign2018 4:b0ce6385d008 820 if(flag == 1)
SDesign2018 4:b0ce6385d008 821 {
SDesign2018 4:b0ce6385d008 822 //pc.printf("Write to 0x41 register address success\n\r");
SDesign2018 4:b0ce6385d008 823 flag = ADXL372.write(0x52); // Set bit 0
SDesign2018 4:b0ce6385d008 824 if(flag == 1)
SDesign2018 4:b0ce6385d008 825 {
SDesign2018 4:b0ce6385d008 826 pc.printf("Everything has been reset\n\r");
SDesign2018 4:b0ce6385d008 827 ADXL372.stop();
SDesign2018 4:b0ce6385d008 828 }
SDesign2018 4:b0ce6385d008 829 }
SDesign2018 4:b0ce6385d008 830 }
SDesign2018 4:b0ce6385d008 831 else ADXL372.stop();
SDesign2018 4:b0ce6385d008 832 // ---------------- End of writing cycle --------------------------//
SDesign2018 4:b0ce6385d008 833 }
SDesign2018 4:b0ce6385d008 834 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 6:3b41238872a8 835
SDesign2018 6:3b41238872a8 836 // Used to clear serial buffer in beginning
SDesign2018 6:3b41238872a8 837 ////////////////////////////////////////////////////////////////////////////////
SDesign2018 6:3b41238872a8 838 void flushSerialBuffer(void) { char char1 = 0; while (pc.readable()) { char1 = pc.getc(); } return; }