The preloaded firmware shipped on the PowerMate

Dependencies:   USBDevice mbed

Committer:
Experiment626
Date:
Thu Dec 04 19:10:52 2014 +0000
Revision:
1:ea25641678f7
Parent:
0:c0f091562db4
Changed the voltage output precision to two places.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Experiment626 0:c0f091562db4 1 /*
Experiment626 0:c0f091562db4 2 ** This software can be freely used, even comercially, as highlighted in the license.
Experiment626 0:c0f091562db4 3 **
Experiment626 0:c0f091562db4 4 ** Copyright 2014 GHI Electronics, LLC
Experiment626 0:c0f091562db4 5 **
Experiment626 0:c0f091562db4 6 ** Licensed under the Apache License, Version 2.0 (the "License");
Experiment626 0:c0f091562db4 7 ** you may not use this file except in compliance with the License.
Experiment626 0:c0f091562db4 8 ** You may obtain a copy of the License at
Experiment626 0:c0f091562db4 9 **
Experiment626 0:c0f091562db4 10 ** http://www.apache.org/licenses/LICENSE-2.0
Experiment626 0:c0f091562db4 11 **
Experiment626 0:c0f091562db4 12 ** Unless required by applicable law or agreed to in writing, software
Experiment626 0:c0f091562db4 13 ** distributed under the License is distributed on an "AS IS" BASIS,
Experiment626 0:c0f091562db4 14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Experiment626 0:c0f091562db4 15 ** See the License for the specific language governing permissions and
Experiment626 0:c0f091562db4 16 ** limitations under the License.
Experiment626 0:c0f091562db4 17 **
Experiment626 0:c0f091562db4 18 **/
Experiment626 0:c0f091562db4 19
Experiment626 0:c0f091562db4 20 #include "mbed.h"
Experiment626 0:c0f091562db4 21 #include "character_display.h"
Experiment626 0:c0f091562db4 22
Experiment626 0:c0f091562db4 23 // Outrageous Circuits PowerMate
Experiment626 0:c0f091562db4 24
Experiment626 0:c0f091562db4 25 #define TIME_BETWEEN_SAMPLES_ms 300
Experiment626 0:c0f091562db4 26
Experiment626 0:c0f091562db4 27 void led_flash();
Experiment626 0:c0f091562db4 28 void print_float(float value, char* units, int fraction_digits);
Experiment626 0:c0f091562db4 29
Experiment626 0:c0f091562db4 30 character_display lcd(P0_14, P0_13, P0_12, P0_11, P0_22, P0_10); // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=HD44780
Experiment626 0:c0f091562db4 31
Experiment626 0:c0f091562db4 32 main()
Experiment626 0:c0f091562db4 33 {
Experiment626 0:c0f091562db4 34 Ticker blink;
Experiment626 0:c0f091562db4 35
Experiment626 0:c0f091562db4 36 AnalogIn voltage(P0_23);
Experiment626 0:c0f091562db4 37 AnalogIn current(P0_15);
Experiment626 0:c0f091562db4 38
Experiment626 0:c0f091562db4 39 DigitalIn btn1(P0_1,PullUp);
Experiment626 0:c0f091562db4 40 DigitalIn btn2(P1_19,PullUp);
Experiment626 0:c0f091562db4 41
Experiment626 0:c0f091562db4 42 const int DISPLAY_VOLTAGE = 1;
Experiment626 0:c0f091562db4 43 const int DISPLAY_CURRENT = 2;
Experiment626 0:c0f091562db4 44 int display = 0;
Experiment626 0:c0f091562db4 45
Experiment626 0:c0f091562db4 46 Timer clock;
Experiment626 0:c0f091562db4 47 uint32_t start_time_ms = 0;
Experiment626 0:c0f091562db4 48
Experiment626 0:c0f091562db4 49 blink.attach(&led_flash, 0.5);
Experiment626 0:c0f091562db4 50 clock.start();
Experiment626 0:c0f091562db4 51
Experiment626 0:c0f091562db4 52 while(1) {
Experiment626 0:c0f091562db4 53 if (btn1 == 0) {
Experiment626 0:c0f091562db4 54 display = DISPLAY_VOLTAGE;
Experiment626 0:c0f091562db4 55 start_time_ms = clock.read_ms();
Experiment626 0:c0f091562db4 56 wait_ms(TIME_BETWEEN_SAMPLES_ms + 1); // force a sample and display
Experiment626 0:c0f091562db4 57 } else if (btn2 == 0){
Experiment626 0:c0f091562db4 58 display = DISPLAY_CURRENT;
Experiment626 0:c0f091562db4 59 start_time_ms = clock.read_ms();
Experiment626 0:c0f091562db4 60 wait_ms(TIME_BETWEEN_SAMPLES_ms + 1);
Experiment626 0:c0f091562db4 61 }
Experiment626 0:c0f091562db4 62
Experiment626 0:c0f091562db4 63 if (clock.read_ms() - start_time_ms >= TIME_BETWEEN_SAMPLES_ms) {
Experiment626 0:c0f091562db4 64 lcd.clear();
Experiment626 0:c0f091562db4 65 switch (display){
Experiment626 0:c0f091562db4 66 case DISPLAY_VOLTAGE:
Experiment626 1:ea25641678f7 67 print_float(voltage.read()*3.3*2,"V",2);
Experiment626 0:c0f091562db4 68 break;
Experiment626 0:c0f091562db4 69 case DISPLAY_CURRENT:
Experiment626 0:c0f091562db4 70 print_float((current.read()*3.3)/1.1,"A",2);
Experiment626 0:c0f091562db4 71 break;
Experiment626 0:c0f091562db4 72 default:
Experiment626 0:c0f091562db4 73 lcd.print("1V 2A");
Experiment626 0:c0f091562db4 74 break;
Experiment626 0:c0f091562db4 75 }
Experiment626 0:c0f091562db4 76
Experiment626 0:c0f091562db4 77 start_time_ms = clock.read_ms();
Experiment626 0:c0f091562db4 78 }
Experiment626 0:c0f091562db4 79 }
Experiment626 0:c0f091562db4 80 }
Experiment626 0:c0f091562db4 81
Experiment626 0:c0f091562db4 82 DigitalOut LED[]={(P0_18),(P0_19)};
Experiment626 0:c0f091562db4 83 uint8_t whichLED=0;
Experiment626 0:c0f091562db4 84
Experiment626 0:c0f091562db4 85 void led_flash()
Experiment626 0:c0f091562db4 86 {
Experiment626 0:c0f091562db4 87 LED[whichLED]=1;
Experiment626 0:c0f091562db4 88 whichLED = whichLED ? 0 : 1 ;
Experiment626 0:c0f091562db4 89 LED[whichLED]=0;
Experiment626 0:c0f091562db4 90 }
Experiment626 0:c0f091562db4 91
Experiment626 0:c0f091562db4 92 void print_float(float value,char * suffix, int fractional_digits)
Experiment626 0:c0f091562db4 93 {
Experiment626 0:c0f091562db4 94 char buffer[16];
Experiment626 0:c0f091562db4 95 (void) sprintf(buffer,"%7.*f%1.1s", fractional_digits, value, suffix); // 8 total: 6 numeric digits, one for the decimal point, 1 for suffix
Experiment626 0:c0f091562db4 96 lcd.print(buffer);
Experiment626 0:c0f091562db4 97 }