The preloaded firmware shipped on the PowerMate

Dependencies:   USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
Experiment626
Date:
Tue Dec 02 19:58:25 2014 +0000
Child:
1:ea25641678f7
Commit message:
Initial PowerMate Official Firmware

Changed in this revision

USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
character_display/character_display.cpp Show annotated file Show diff for this revision Revisions of this file
character_display/character_display.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Dec 02 19:58:25 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#dfe51ad5cacf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/character_display/character_display.cpp	Tue Dec 02 19:58:25 2014 +0000
@@ -0,0 +1,115 @@
+/*
+Copyright 2013 GHI Electronics LLC
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#include "mbed.h"
+#include "character_display.h"
+
+  
+character_display::character_display(     
+                 PinName  a_lcd_rs
+                ,PinName  a_lcd_e
+                ,PinName  a_lcd_d4
+                ,PinName  a_lcd_d5
+                ,PinName  a_lcd_d6
+                ,PinName  a_lcd_d7
+        ) :
+                  lcd_rs( a_lcd_rs,false)
+                , lcd_e( a_lcd_e,false)
+                , lcd_d4( a_lcd_d4,false)
+                , lcd_d5( a_lcd_d5,false)
+                , lcd_d6( a_lcd_d6,false)
+                , lcd_d7( a_lcd_d7,false)
+{
+        lcd_rs=0;   // command mode
+        wait_ms(2); // set-up time
+        for (int i=0; i<3; i++) {
+            send_nibble(0x3);
+            wait_ms(15);
+        }
+        send_nibble(0x02);   // 4 bit mode
+        wait_us(40);
+           
+        send_command(character_display::FUNCTION_SET); // characteristics of display
+        clear();
+        cursor_home();
+        send_command(character_display::MODE_SET);
+        send_command(character_display::CUR_V_SHIFT);       
+        send_command(character_display::DISP_ON);
+       
+
+        wait_ms(4);
+}
+
+
+
+
+void character_display::send_nibble(unsigned char b)
+{
+    lcd_e.write(true);
+    lcd_d7.write((b & 0x8) != 0);
+    lcd_d6.write((b & 0x4) != 0);
+    lcd_d5.write((b & 0x2) != 0);
+    lcd_d4.write((b & 0x1) != 0);
+    wait_us(1); 
+    lcd_e.write(false); // enable data sampling
+    wait_us(1);  
+}
+
+void character_display::send_byte(unsigned char b)
+{
+    
+    send_nibble(b >> 4);
+    send_nibble(b);
+}
+
+void character_display::send_command(unsigned char c)
+{
+    lcd_rs.write(false); //set LCD to command mode
+    wait_us(1);  //for rs samplin
+    send_byte(c);
+    wait_us(40);
+    lcd_rs.write(true); //set LCD to data mode
+    wait_us(1);
+}
+
+void character_display::print(const char* string)
+{
+    while (*string != '\0')
+        print(*(string++));
+}
+
+void character_display::print(char character)
+{
+    send_byte(character);
+    wait_us(40);
+}
+
+void character_display::clear()
+{
+    send_command(character_display::CLR_DISP);
+    wait_ms(10);
+}
+
+void character_display::cursor_home()
+{
+    send_command(character_display::CUR_HOME);
+}
+
+void character_display::set_cursor(unsigned char row, unsigned char col)
+{
+    char offsets[] = { 0x00, 0x40, 0x14, 0x54 };
+    send_command(character_display::SET_CURSOR | offsets[row] | col);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/character_display/character_display.h	Tue Dec 02 19:58:25 2014 +0000
@@ -0,0 +1,60 @@
+/*
+Copyright 2013 GHI Electronics LLC
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#pragma once
+#include "mbed.h"
+
+class character_display{
+
+    public:
+
+        character_display(
+             PinName  lcd_rs
+            ,PinName   lcd_e
+            ,PinName   lcd_d4
+            ,PinName   lcd_d5
+            ,PinName   lcd_d6
+            ,PinName   lcd_d7
+        );
+
+        void send_nibble(unsigned char b);
+        void send_byte(unsigned char b);
+        void send_command(unsigned char c);
+        void print(const char* string);
+        void print(char character);
+        void clear();
+        void cursor_home();
+        void set_cursor(unsigned char row, unsigned char col);
+
+    private:
+
+        static const uint8_t DISP_ON = 0xC;    //Turn visible LCD on
+        static const uint8_t CLR_DISP = 1;      //Clear display
+        static const uint8_t CUR_HOME = 2;      //Move cursor home and clear screen memory
+        static const uint8_t SET_CURSOR = 0x80;   //SET_CURSOR + X : Sets cursor position to X
+        static const uint8_t FUNCTION_SET = 0x20; // 001 4 bit bus, 1 line, font 5x7
+        static const uint8_t MODE_SET = 0x06; // mode, direction,...
+        static const uint8_t CUR_V_SHIFT = 0x14; // cursor right 
+        
+        
+        DigitalOut  lcd_rs;
+        DigitalOut  lcd_e;
+        DigitalOut  lcd_d4;
+        DigitalOut  lcd_d5;
+        DigitalOut  lcd_d6;
+        DigitalOut  lcd_d7;
+
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Dec 02 19:58:25 2014 +0000
@@ -0,0 +1,97 @@
+/*
+** This software can be freely used, even comercially, as highlighted in the license.
+** 
+** Copyright 2014 GHI Electronics, LLC
+** 
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+** 
+**     http://www.apache.org/licenses/LICENSE-2.0
+** 
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+**
+**/
+
+#include "mbed.h"
+#include "character_display.h"
+
+// Outrageous Circuits PowerMate
+
+#define TIME_BETWEEN_SAMPLES_ms 300
+
+void led_flash();
+void print_float(float value, char* units, int fraction_digits);
+
+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
+
+main()
+{
+    Ticker blink;
+    
+    AnalogIn voltage(P0_23);
+    AnalogIn current(P0_15);
+   
+    DigitalIn btn1(P0_1,PullUp);
+    DigitalIn btn2(P1_19,PullUp);
+    
+    const int DISPLAY_VOLTAGE = 1;
+    const int DISPLAY_CURRENT = 2;
+    int display = 0;
+    
+    Timer clock;
+    uint32_t start_time_ms = 0;
+
+    blink.attach(&led_flash, 0.5);                   
+    clock.start();
+    
+    while(1) { 
+        if (btn1 == 0) {
+            display = DISPLAY_VOLTAGE;
+            start_time_ms = clock.read_ms(); 
+            wait_ms(TIME_BETWEEN_SAMPLES_ms + 1);  // force a sample and display
+        } else if (btn2 == 0){
+            display = DISPLAY_CURRENT;
+            start_time_ms = clock.read_ms();
+            wait_ms(TIME_BETWEEN_SAMPLES_ms + 1);
+        }
+
+        if (clock.read_ms() - start_time_ms >= TIME_BETWEEN_SAMPLES_ms) {
+            lcd.clear();
+            switch (display){
+                case DISPLAY_VOLTAGE:
+                    print_float(voltage.read()*3.3*2,"V",3);
+                    break;
+                case DISPLAY_CURRENT:
+                    print_float((current.read()*3.3)/1.1,"A",2);
+                    break;
+                default:
+                    lcd.print("1V 2A");
+                    break;
+            }
+            
+            start_time_ms = clock.read_ms();
+        }
+    }
+}
+
+DigitalOut LED[]={(P0_18),(P0_19)};
+uint8_t whichLED=0;
+
+void led_flash()
+{
+    LED[whichLED]=1;
+    whichLED = whichLED ? 0 : 1 ;
+    LED[whichLED]=0;
+}
+ 
+void print_float(float value,char * suffix, int fractional_digits)
+{
+    char buffer[16];
+    (void) sprintf(buffer,"%7.*f%1.1s", fractional_digits, value, suffix); // 8 total: 6 numeric digits, one for the decimal point, 1 for suffix
+    lcd.print(buffer);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Dec 02 19:58:25 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file