David Buck / Mbed 2 deprecated voltmeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //main.cpp
00002 /****************************************************************
00003 * Include files
00004 ****************************************************************/
00005 #include "header.h"
00006 
00007 /****************************************************************
00008 * data values
00009 ****************************************************************/
00010 #define cal -200   // calibration value
00011 
00012 int A2D_in;      /* input variable */
00013 int V_in;        /* input voltage */
00014 int V_unit;      /* voltage units value */
00015 int V_unit_old;  // previous unit value
00016 int V_in_dec;    /* input voltage decimal value */
00017 int V_dec;       /* voltage decimal value */
00018 int V_dec_old;   // previous decimal value
00019 float x;
00020 
00021 /****************************************************************
00022 * Main part of the program
00023 ****************************************************************/
00024 int main()
00025 {
00026   
00027   ADC_init();
00028   LCD_init();
00029   LCD_displayV();
00030   
00031 while(1){               /*  infinite loop */
00032  
00033     A2D_in=ADC_read();
00034     V_in=A2D_in*33;       /* calculate voltage from 8-bit ADC input 0xFF = 3.3V */
00035                           // all voltage values multiplied by 10 to allow integer manipulation
00036       
00037       
00038       //****************** calculate voltage unit = 0-5V ******************************
00039       if(V_in >= 50*255)        // if greater than 5V then unit to display = 5  
00040       {
00041         V_unit = 0x05;
00042         V_in_dec  = 0;          // if unit = 5 then decimal must be 0 (5.0V=max) 
00043       } 
00044       else if(V_in > 40*255)    // else if greater than 4V then unit to display = 4 
00045       {
00046         V_unit = 0x04;
00047         V_in_dec  = V_in-40*256;  // calculate remainder for decimal processing
00048       }
00049       else if(V_in > 30*255)     // else if greater than 3V then unit to display = 3
00050       {
00051         V_unit = 0x03;
00052         V_in_dec  = V_in-30*256;
00053       }      
00054       else if(V_in > 20*255)     // else if greater than 2V then unit to display = 2
00055       { 
00056         V_unit = 0x02;
00057         V_in_dec  = V_in-20*256;
00058       } 
00059       else if(V_in > 10*255)     // else if greater than 1V then unit to display = 1
00060       {
00061         V_unit = 0x01;
00062         V_in_dec  = V_in-10*256;
00063       } 
00064       else                       // else unit to display = 0
00065       {
00066         V_unit = 0x00;
00067         V_in_dec  = V_in;
00068       }
00069       
00070       // if unit has changed, then display the new unit on the screen
00071       if (V_unit!= V_unit_old){
00072         
00073        //  start of line 2  
00074       FIO2CLR0=0x02;         /* set RS to 'instruction' */
00075       FIO0PIN2=0xC6;         /* set address to line 2 units position*/
00076       toggle_enable();
00077       FIO2SET0=0x02;         /* set RS to 'data' */
00078           
00079       FIO0PIN2=(0x30+V_unit);    // display unit value on screen               
00080       toggle_enable();
00081       V_unit_old=V_unit;
00082       }
00083       
00084       
00085       //****************** calculate decimal value = .0 - .9 ******************************
00086       if(V_in_dec <=260+cal)    
00087        V_dec = 0x00;
00088       if(V_in_dec > 260+cal)       // decimal value must be between 0 and 2550 (ie 255*10)
00089        V_dec = 0x01;
00090       if(V_in_dec > 510+cal)       // calibration value allows an amount of simple tuning
00091        V_dec = 0x02;               // any error should be linear, but maybe with an offset
00092       if(V_in_dec > 770+cal)       // a complete look-up table would be better though!
00093        V_dec = 0x03;
00094       if(V_in_dec > 1120+cal)
00095        V_dec = 0x04;
00096       if(V_in_dec > 1280+cal)
00097        V_dec = 0x05;
00098       if(V_in_dec > 1530+cal)
00099        V_dec = 0x06;
00100       if(V_in_dec > 1780+cal)
00101        V_dec = 0x07;
00102       if(V_in_dec > 2040+cal)
00103        V_dec = 0x08;
00104       if(V_in_dec > 2300+cal)
00105        V_dec = 0x09; 
00106        
00107       // if decimal value has changed then update display
00108       if (V_dec!= V_dec_old){
00109         
00110         // start of line 2  */
00111         FIO2CLR0=0x02;       /* set RS to 'instruction' */
00112         FIO0PIN2=0xC8;             /* set address to line 2 */
00113         toggle_enable();
00114         FIO2SET0=0x02;       /* set RS to 'data' */
00115         
00116         FIO0PIN2=(0x30+V_dec);             
00117         toggle_enable();
00118         V_dec_old=V_dec;
00119       }
00120   }
00121   
00122 } /* end main() */