David Buck
/
voltmeter
main.cpp@0:d322a5241da8, 2009-12-16 (annotated)
- Committer:
- Kaikestu
- Date:
- Wed Dec 16 12:04:23 2009 +0000
- Revision:
- 0:d322a5241da8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kaikestu | 0:d322a5241da8 | 1 | //main.cpp |
Kaikestu | 0:d322a5241da8 | 2 | /**************************************************************** |
Kaikestu | 0:d322a5241da8 | 3 | * Include files |
Kaikestu | 0:d322a5241da8 | 4 | ****************************************************************/ |
Kaikestu | 0:d322a5241da8 | 5 | #include "header.h" |
Kaikestu | 0:d322a5241da8 | 6 | |
Kaikestu | 0:d322a5241da8 | 7 | /**************************************************************** |
Kaikestu | 0:d322a5241da8 | 8 | * data values |
Kaikestu | 0:d322a5241da8 | 9 | ****************************************************************/ |
Kaikestu | 0:d322a5241da8 | 10 | #define cal -200 // calibration value |
Kaikestu | 0:d322a5241da8 | 11 | |
Kaikestu | 0:d322a5241da8 | 12 | int A2D_in; /* input variable */ |
Kaikestu | 0:d322a5241da8 | 13 | int V_in; /* input voltage */ |
Kaikestu | 0:d322a5241da8 | 14 | int V_unit; /* voltage units value */ |
Kaikestu | 0:d322a5241da8 | 15 | int V_unit_old; // previous unit value |
Kaikestu | 0:d322a5241da8 | 16 | int V_in_dec; /* input voltage decimal value */ |
Kaikestu | 0:d322a5241da8 | 17 | int V_dec; /* voltage decimal value */ |
Kaikestu | 0:d322a5241da8 | 18 | int V_dec_old; // previous decimal value |
Kaikestu | 0:d322a5241da8 | 19 | float x; |
Kaikestu | 0:d322a5241da8 | 20 | |
Kaikestu | 0:d322a5241da8 | 21 | /**************************************************************** |
Kaikestu | 0:d322a5241da8 | 22 | * Main part of the program |
Kaikestu | 0:d322a5241da8 | 23 | ****************************************************************/ |
Kaikestu | 0:d322a5241da8 | 24 | int main() |
Kaikestu | 0:d322a5241da8 | 25 | { |
Kaikestu | 0:d322a5241da8 | 26 | |
Kaikestu | 0:d322a5241da8 | 27 | ADC_init(); |
Kaikestu | 0:d322a5241da8 | 28 | LCD_init(); |
Kaikestu | 0:d322a5241da8 | 29 | LCD_displayV(); |
Kaikestu | 0:d322a5241da8 | 30 | |
Kaikestu | 0:d322a5241da8 | 31 | while(1){ /* infinite loop */ |
Kaikestu | 0:d322a5241da8 | 32 | |
Kaikestu | 0:d322a5241da8 | 33 | A2D_in=ADC_read(); |
Kaikestu | 0:d322a5241da8 | 34 | V_in=A2D_in*33; /* calculate voltage from 8-bit ADC input 0xFF = 3.3V */ |
Kaikestu | 0:d322a5241da8 | 35 | // all voltage values multiplied by 10 to allow integer manipulation |
Kaikestu | 0:d322a5241da8 | 36 | |
Kaikestu | 0:d322a5241da8 | 37 | |
Kaikestu | 0:d322a5241da8 | 38 | //****************** calculate voltage unit = 0-5V ****************************** |
Kaikestu | 0:d322a5241da8 | 39 | if(V_in >= 50*255) // if greater than 5V then unit to display = 5 |
Kaikestu | 0:d322a5241da8 | 40 | { |
Kaikestu | 0:d322a5241da8 | 41 | V_unit = 0x05; |
Kaikestu | 0:d322a5241da8 | 42 | V_in_dec = 0; // if unit = 5 then decimal must be 0 (5.0V=max) |
Kaikestu | 0:d322a5241da8 | 43 | } |
Kaikestu | 0:d322a5241da8 | 44 | else if(V_in > 40*255) // else if greater than 4V then unit to display = 4 |
Kaikestu | 0:d322a5241da8 | 45 | { |
Kaikestu | 0:d322a5241da8 | 46 | V_unit = 0x04; |
Kaikestu | 0:d322a5241da8 | 47 | V_in_dec = V_in-40*256; // calculate remainder for decimal processing |
Kaikestu | 0:d322a5241da8 | 48 | } |
Kaikestu | 0:d322a5241da8 | 49 | else if(V_in > 30*255) // else if greater than 3V then unit to display = 3 |
Kaikestu | 0:d322a5241da8 | 50 | { |
Kaikestu | 0:d322a5241da8 | 51 | V_unit = 0x03; |
Kaikestu | 0:d322a5241da8 | 52 | V_in_dec = V_in-30*256; |
Kaikestu | 0:d322a5241da8 | 53 | } |
Kaikestu | 0:d322a5241da8 | 54 | else if(V_in > 20*255) // else if greater than 2V then unit to display = 2 |
Kaikestu | 0:d322a5241da8 | 55 | { |
Kaikestu | 0:d322a5241da8 | 56 | V_unit = 0x02; |
Kaikestu | 0:d322a5241da8 | 57 | V_in_dec = V_in-20*256; |
Kaikestu | 0:d322a5241da8 | 58 | } |
Kaikestu | 0:d322a5241da8 | 59 | else if(V_in > 10*255) // else if greater than 1V then unit to display = 1 |
Kaikestu | 0:d322a5241da8 | 60 | { |
Kaikestu | 0:d322a5241da8 | 61 | V_unit = 0x01; |
Kaikestu | 0:d322a5241da8 | 62 | V_in_dec = V_in-10*256; |
Kaikestu | 0:d322a5241da8 | 63 | } |
Kaikestu | 0:d322a5241da8 | 64 | else // else unit to display = 0 |
Kaikestu | 0:d322a5241da8 | 65 | { |
Kaikestu | 0:d322a5241da8 | 66 | V_unit = 0x00; |
Kaikestu | 0:d322a5241da8 | 67 | V_in_dec = V_in; |
Kaikestu | 0:d322a5241da8 | 68 | } |
Kaikestu | 0:d322a5241da8 | 69 | |
Kaikestu | 0:d322a5241da8 | 70 | // if unit has changed, then display the new unit on the screen |
Kaikestu | 0:d322a5241da8 | 71 | if (V_unit!= V_unit_old){ |
Kaikestu | 0:d322a5241da8 | 72 | |
Kaikestu | 0:d322a5241da8 | 73 | // start of line 2 |
Kaikestu | 0:d322a5241da8 | 74 | FIO2CLR0=0x02; /* set RS to 'instruction' */ |
Kaikestu | 0:d322a5241da8 | 75 | FIO0PIN2=0xC6; /* set address to line 2 units position*/ |
Kaikestu | 0:d322a5241da8 | 76 | toggle_enable(); |
Kaikestu | 0:d322a5241da8 | 77 | FIO2SET0=0x02; /* set RS to 'data' */ |
Kaikestu | 0:d322a5241da8 | 78 | |
Kaikestu | 0:d322a5241da8 | 79 | FIO0PIN2=(0x30+V_unit); // display unit value on screen |
Kaikestu | 0:d322a5241da8 | 80 | toggle_enable(); |
Kaikestu | 0:d322a5241da8 | 81 | V_unit_old=V_unit; |
Kaikestu | 0:d322a5241da8 | 82 | } |
Kaikestu | 0:d322a5241da8 | 83 | |
Kaikestu | 0:d322a5241da8 | 84 | |
Kaikestu | 0:d322a5241da8 | 85 | //****************** calculate decimal value = .0 - .9 ****************************** |
Kaikestu | 0:d322a5241da8 | 86 | if(V_in_dec <=260+cal) |
Kaikestu | 0:d322a5241da8 | 87 | V_dec = 0x00; |
Kaikestu | 0:d322a5241da8 | 88 | if(V_in_dec > 260+cal) // decimal value must be between 0 and 2550 (ie 255*10) |
Kaikestu | 0:d322a5241da8 | 89 | V_dec = 0x01; |
Kaikestu | 0:d322a5241da8 | 90 | if(V_in_dec > 510+cal) // calibration value allows an amount of simple tuning |
Kaikestu | 0:d322a5241da8 | 91 | V_dec = 0x02; // any error should be linear, but maybe with an offset |
Kaikestu | 0:d322a5241da8 | 92 | if(V_in_dec > 770+cal) // a complete look-up table would be better though! |
Kaikestu | 0:d322a5241da8 | 93 | V_dec = 0x03; |
Kaikestu | 0:d322a5241da8 | 94 | if(V_in_dec > 1120+cal) |
Kaikestu | 0:d322a5241da8 | 95 | V_dec = 0x04; |
Kaikestu | 0:d322a5241da8 | 96 | if(V_in_dec > 1280+cal) |
Kaikestu | 0:d322a5241da8 | 97 | V_dec = 0x05; |
Kaikestu | 0:d322a5241da8 | 98 | if(V_in_dec > 1530+cal) |
Kaikestu | 0:d322a5241da8 | 99 | V_dec = 0x06; |
Kaikestu | 0:d322a5241da8 | 100 | if(V_in_dec > 1780+cal) |
Kaikestu | 0:d322a5241da8 | 101 | V_dec = 0x07; |
Kaikestu | 0:d322a5241da8 | 102 | if(V_in_dec > 2040+cal) |
Kaikestu | 0:d322a5241da8 | 103 | V_dec = 0x08; |
Kaikestu | 0:d322a5241da8 | 104 | if(V_in_dec > 2300+cal) |
Kaikestu | 0:d322a5241da8 | 105 | V_dec = 0x09; |
Kaikestu | 0:d322a5241da8 | 106 | |
Kaikestu | 0:d322a5241da8 | 107 | // if decimal value has changed then update display |
Kaikestu | 0:d322a5241da8 | 108 | if (V_dec!= V_dec_old){ |
Kaikestu | 0:d322a5241da8 | 109 | |
Kaikestu | 0:d322a5241da8 | 110 | // start of line 2 */ |
Kaikestu | 0:d322a5241da8 | 111 | FIO2CLR0=0x02; /* set RS to 'instruction' */ |
Kaikestu | 0:d322a5241da8 | 112 | FIO0PIN2=0xC8; /* set address to line 2 */ |
Kaikestu | 0:d322a5241da8 | 113 | toggle_enable(); |
Kaikestu | 0:d322a5241da8 | 114 | FIO2SET0=0x02; /* set RS to 'data' */ |
Kaikestu | 0:d322a5241da8 | 115 | |
Kaikestu | 0:d322a5241da8 | 116 | FIO0PIN2=(0x30+V_dec); |
Kaikestu | 0:d322a5241da8 | 117 | toggle_enable(); |
Kaikestu | 0:d322a5241da8 | 118 | V_dec_old=V_dec; |
Kaikestu | 0:d322a5241da8 | 119 | } |
Kaikestu | 0:d322a5241da8 | 120 | } |
Kaikestu | 0:d322a5241da8 | 121 | |
Kaikestu | 0:d322a5241da8 | 122 | } /* end main() */ |