Simple demo for exercising the board
Dependencies: MAX11300 MAX4822 OneWire Terminal ds3231 mbed
main.cpp
- Committer:
- j3
- Date:
- 2016-07-29
- Revision:
- 1:03c04017a728
- Parent:
- 0:24b8a6f0a563
- Child:
- 2:0c0994f70fd2
File content as of revision 1:03c04017a728:
/********************************************************************** * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Maxim Integrated * Products, Inc. shall not be used except as stated in the Maxim Integrated * Products, Inc. Branding Policy. * * The mere transfer of this software does not imply any licenses * of trade secrets, proprietary technology, copyrights, patents, * trademarks, maskwork rights, or any other form of intellectual * property whatsoever. Maxim Integrated Products, Inc. retains all * ownership rights. **********************************************************************/ #include "mbed.h" #include "ds3231.h" #include "MAX4822.h" #include "MAX11300.h" #include "Terminal.h" #include "OneWire.h" using namespace OneWire; using namespace RomCommands; void set_rtc(Terminal & term, Ds3231 & rtc); void get_time_date(Terminal & term, Ds3231 & rtc); void search_ow_bus(Terminal & term, DS2484 & owm); void print_rom_id(Terminal & term, RomId & romId); int main(void) { Terminal term(USBTX, USBRX); term.baud(57600); term.cls(); term.home(); term.printf("Starting Demo\n"); SPI spi_bus(D11, D12, D13); MAX4822 rly_drvr(spi_bus, D10); DigitalOut rly_drvr_set(D7, 1); DigitalOut rly_drvr_reset(D6, 1); rly_drvr.reset_all_relays(rly_drvr_reset); MAX11300 pixi(spi_bus, D8, NC, D9); //set all analog outs to 0 for(uint8_t idx = 0; idx < 9; idx++) { pixi.single_ended_dac_write(static_cast<MAX11300::MAX11300_Ports>(idx), 0); } //ensure latching relays are reset for(uint8_t idx = 0; idx < 3; idx++) { pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(idx + 9), 1); wait_ms(100); pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(idx + 9), 0); } I2C i2c_bus(D14, D15); i2c_bus.frequency(400000); Ds3231 rtc(i2c_bus); DS2484 owm(i2c_bus); OneWireMaster::CmdResult owm_result = owm.OWInitMaster(); while(owm_result != OneWireMaster::Success) { wait_ms(100); term.printf("Failed to initialize OneWire master\n"); owm_result = owm.OWInitMaster(); } term.printf("OneWire master initialized\n\n"); int32_t user_entry = 0; int32_t relay = 0; uint16_t analog_out, analog_val; float aio, aii; const int32_t QUIT = 14; MAX11300::CmdResult pixi_result; MAX4822::CmdResult rly_drvr_result; while(user_entry != QUIT) { term.printf("1. Set RTC\n"); term.printf("2. Get Time/Date\n"); term.printf("3. Search OneWire bus\n"); term.printf("4. Set non-latching relay\n"); term.printf("5. Reset non-latching relay\n"); term.printf("6. Set all non-latching relays\n"); term.printf("7. Reset all non-latching relays\n"); term.printf("8. Set latching relay\n"); term.printf("9. Reset latching relay\n"); term.printf("10. Set 0-10 analog out\n"); term.printf("11. Set 4-20 out\n"); term.printf("12. Get 4-20 in\n"); term.printf("%d. Clear Screen\n", (QUIT - 1)); term.printf("%d. Quit\n\n", QUIT); user_entry = term.get_int32("Please select an option above: ", 1, QUIT); //get non-latching relay if((user_entry == 4) || (user_entry == 5)) { relay = term.get_int32("Select a relay from 1 to 8: ", 1, 8); } //get latching relay if((user_entry == 8) || (user_entry == 9)) { relay = (8 + term.get_int32("Select a relay from 1 to 3: ", 1, 3)); } switch(user_entry) { case 1: set_rtc(term, rtc); break; case 2: get_time_date(term, rtc); break; case 3: search_ow_bus(term, owm); break; case 4: rly_drvr_result = rly_drvr.set_relay(static_cast<MAX4822::RelayChannel>(relay)); if(rly_drvr_result != MAX4822::Success) { term.printf("Failed to set relay\n"); } break; case 5: rly_drvr_result = rly_drvr.reset_relay(static_cast<MAX4822::RelayChannel>(relay)); if(rly_drvr_result != MAX4822::Success) { term.printf("Failed to reset relay\n"); } break; case 6: rly_drvr.set_all_relays(rly_drvr_set); term.printf("\n"); break; case 7: rly_drvr.reset_all_relays(rly_drvr_reset); term.printf("\n"); break; case 8: pixi_result = pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(relay), 1); if(pixi_result != MAX11300::Success) { term.printf("Failed to set relay\n"); } break; case 9: pixi_result = pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(relay), 0); if(pixi_result != MAX11300::Success) { term.printf("Failed to reset relay\n"); } break; case 10: analog_out = (term.get_int32("Please select a 0-10V output; 1-8: ", 1, 8) - 1); analog_val = term.get_int32("Please select a value from 0-4095: ", 0, 4095); pixi_result = pixi.single_ended_dac_write(static_cast<MAX11300::MAX11300_Ports>(analog_out), analog_val); if(pixi_result != MAX11300::Success) { term.printf("Failed to set 0-10 out\n"); } break; case 11: analog_val = term.get_int32("Please select a value from 0-4095: ", 0, 4095); pixi_result = pixi.single_ended_dac_write(MAX11300::PORT8, analog_val); if(pixi_result != MAX11300::Success) { term.printf("Failed to set 4-20 out\n"); } break; case 12: pixi_result = pixi.single_ended_adc_read(MAX11300::PORT12, analog_val); aii = ((9.7e-6 *analog_val) + 2.3e-5); if(pixi_result == MAX11300::Success) { term.printf("4-20 in = %.2e\n", aii); } else { term.printf("Failed to read 4-20 in\n"); } break; case (QUIT - 1): term.cls(); term.home(); break; case QUIT: term.printf("\nEnding Program\n"); break; default: mbed_die(); break; } } } //********************************************************************* void set_rtc(Terminal & term, Ds3231 & rtc) { //default, use bit masks in ds3231.h for desired operation ds3231_cntl_stat_t rtc_control_status = {0,0}; ds3231_time_t rtc_time; ds3231_calendar_t rtc_calendar; rtc.set_cntl_stat_reg(rtc_control_status); //get day from user rtc_calendar.day = term.get_int32("\nPlease enter day of week, 1 for Sunday (1-7): ", 1, 7); //get day of month from user rtc_calendar.date = term.get_int32("\nPlease enter day of month (1-31): ", 1, 31); //get month from user rtc_calendar.month = term.get_int32("\nPlease enter the month, 1 for January (1-12): ", 1, 12); //get year from user rtc_calendar.year = term.get_int32("\nPlease enter the year (0-99): ", 0, 99); //Get time mode rtc_time.mode = term.get_int32("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1); if(rtc_time.mode) { //Get AM/PM status rtc_time.am_pm = term.get_int32("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1); //Get hour from user rtc_time.hours = term.get_int32("\nPlease enter the hour (1-12): ", 1, 12); } else { //Get hour from user rtc_time.hours = term.get_int32("\nPlease enter the hour (0-23): ", 0, 23); } //Get minutes from user rtc_time.minutes = term.get_int32("\nPlease enter the minute (0-59): ", 0, 59); //Get seconds from user rtc_time.seconds = term.get_int32("\nPlease enter the second (0-59): ", 0, 59); term.printf("\n"); rtc.set_time(rtc_time); rtc.set_calendar(rtc_calendar); } //********************************************************************* void get_time_date(Terminal & term, Ds3231 & rtc) { time_t epoch_time; epoch_time = rtc.get_epoch(); term.printf("\n%s\n", ctime(&epoch_time)); } //********************************************************************* void search_ow_bus(Terminal & term, DS2484 & owm) { OneWireMaster::CmdResult result = owm.OWReset(); SearchState search_state; if(result == OneWireMaster::Success) { term.printf("\nOWReset success, starting search\n"); result = OWFirst(owm, search_state); if(result == OneWireMaster::Success) { while(result == OneWireMaster::Success) { //print current devices rom id print_rom_id(term, search_state.romId); //find the next device if any result = OWNext(owm, search_state); } term.printf("\n"); } else { term.printf("\nSearch failed\n"); term.printf("\nError code = %d\n", result); } } else { term.printf("\nFailed to find any 1-wire devices on bus\n"); term.printf("\nError code = %d\n", result); } } //********************************************************************* void print_rom_id(Terminal & term, RomId & romId) { //print the rom number term.printf("\n"); int8_t idx = (RomId::byteLen - 1); do { if(romId[idx] < 16) { term.printf("0x0%x ", romId[idx--]); } else { term.printf("0x%2x ", romId[idx--]); } } while(idx >= 0); term.printf("\n"); }