Elmir Hodžić Sumejja Porča

Dependencies:   mbed

Committer:
tim008
Date:
Mon Mar 24 19:23:42 2014 +0000
Revision:
0:d995086ea45d
LV4_Z02_tim008;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tim008 0:d995086ea45d 1 #include "mbed.h"
tim008 0:d995086ea45d 2 // 0 1 2 3 4 5 6 7 8 9 a b c d e * #
tim008 0:d995086ea45d 3 char lookup[17] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10, 0x20, 0x03, 0x46, 0x21, 0x04, 0x09, 0x01};
tim008 0:d995086ea45d 4 DigitalOut displays[3] = { dp25, dp24, dp23 };
tim008 0:d995086ea45d 5 DigitalOut dec_p = dp4;
tim008 0:d995086ea45d 6 AnalogIn ain(dp9);
tim008 0:d995086ea45d 7
tim008 0:d995086ea45d 8 // Mapiranja su:
tim008 0:d995086ea45d 9 // A - dp26 (bit 0)
tim008 0:d995086ea45d 10 // B - dp27 (bit 1)
tim008 0:d995086ea45d 11 // C - dp5 (bit 2)
tim008 0:d995086ea45d 12 // D - dp6 (bit 3)
tim008 0:d995086ea45d 13 // E - dp28 (bit 4)
tim008 0:d995086ea45d 14 // F - dp1 (bit 5)
tim008 0:d995086ea45d 15 // G - dp2 (bit 6)
tim008 0:d995086ea45d 16
tim008 0:d995086ea45d 17 BusOut segments (dp26, dp27, dp5, dp6, dp28, dp1, dp2);
tim008 0:d995086ea45d 18 /*
tim008 0:d995086ea45d 19 * Funkcija treba pisati na segment displaya, argumenti su:
tim008 0:d995086ea45d 20 * index - kaze na koji display treba pisati (moguce vrijednosti su 2 do 4)
tim008 0:d995086ea45d 21 * value - vrijednost koja se treba zapisati (moguce vrijednosti su cifre od 0 do 9)
tim008 0:d995086ea45d 22 * decimal_point - vrijednost koja kaze da li treba decimalnu tacku osvijetliti (moguce vrijednosti su
tim008 0:d995086ea45d 23 0 ili 1; 0 - iskljuceno, 1 - ukljuceno)
tim008 0:d995086ea45d 24 */
tim008 0:d995086ea45d 25 void write_segment(int index, int value, int decimal_point)
tim008 0:d995086ea45d 26 {
tim008 0:d995086ea45d 27 if(value >= 0 && value <= 9)
tim008 0:d995086ea45d 28 // Preslikavanje vrijednosti broja u kod
tim008 0:d995086ea45d 29 segments = lookup[value];
tim008 0:d995086ea45d 30
tim008 0:d995086ea45d 31 else
tim008 0:d995086ea45d 32 segments = 0xff;
tim008 0:d995086ea45d 33
tim008 0:d995086ea45d 34 // DP je spojen na dp4
tim008 0:d995086ea45d 35 dec_p = !(decimal_point & (1 << 0));
tim008 0:d995086ea45d 36
tim008 0:d995086ea45d 37 // Aktiviramo odgovarajuci display
tim008 0:d995086ea45d 38 // D2 je spojen na dp23
tim008 0:d995086ea45d 39 // D3 je spojen na dp24
tim008 0:d995086ea45d 40 // D4 je spojen na dp25
tim008 0:d995086ea45d 41 switch (index)
tim008 0:d995086ea45d 42 {
tim008 0:d995086ea45d 43 case 2:
tim008 0:d995086ea45d 44 displays[0] = 0;
tim008 0:d995086ea45d 45 displays[1] = 1;
tim008 0:d995086ea45d 46 displays[2] = 1;
tim008 0:d995086ea45d 47 break;
tim008 0:d995086ea45d 48 case 3:
tim008 0:d995086ea45d 49 displays[0]= 1;
tim008 0:d995086ea45d 50 displays[1] = 0;
tim008 0:d995086ea45d 51 displays[2] = 1;
tim008 0:d995086ea45d 52 break;
tim008 0:d995086ea45d 53 case 4:
tim008 0:d995086ea45d 54 displays[0] = 1;
tim008 0:d995086ea45d 55 displays[1] = 1;
tim008 0:d995086ea45d 56 displays[2] = 0;
tim008 0:d995086ea45d 57 break;
tim008 0:d995086ea45d 58 }
tim008 0:d995086ea45d 59 }
tim008 0:d995086ea45d 60
tim008 0:d995086ea45d 61 int main()
tim008 0:d995086ea45d 62 {
tim008 0:d995086ea45d 63 int i = 0;
tim008 0:d995086ea45d 64 // Varijabla koja ce reci koji display je trenutno aktivan (ima vrijednosti od 2 do 4)
tim008 0:d995086ea45d 65 int index = 2;
tim008 0:d995086ea45d 66
tim008 0:d995086ea45d 67 float Vmin(0.7), Vmax(2.1);
tim008 0:d995086ea45d 68
tim008 0:d995086ea45d 69 float value, old_value, value_proc, old_value_proc;
tim008 0:d995086ea45d 70
tim008 0:d995086ea45d 71 // Vrijednosti na pojedinim displayjima:
tim008 0:d995086ea45d 72 int d2 = 'N', d3 = 'N', d4 = 'N';
tim008 0:d995086ea45d 73
tim008 0:d995086ea45d 74 while (true)
tim008 0:d995086ea45d 75 {
tim008 0:d995086ea45d 76 i++;
tim008 0:d995086ea45d 77 // Pisanje cifara na display
tim008 0:d995086ea45d 78 switch (index)
tim008 0:d995086ea45d 79 {
tim008 0:d995086ea45d 80 case 2:
tim008 0:d995086ea45d 81 write_segment(index, d2, 0);
tim008 0:d995086ea45d 82 break;
tim008 0:d995086ea45d 83
tim008 0:d995086ea45d 84 case 3:
tim008 0:d995086ea45d 85 write_segment(index, d3, 0);
tim008 0:d995086ea45d 86 break;
tim008 0:d995086ea45d 87 case 4:
tim008 0:d995086ea45d 88 write_segment(index, d4, 0);
tim008 0:d995086ea45d 89 break;
tim008 0:d995086ea45d 90 }
tim008 0:d995086ea45d 91 // 1 ms cekanje
tim008 0:d995086ea45d 92 // Ako je pauza izmedju pojedinih pisanja 1ms to znaci da je za 3 displaya potrebno 3ms
tim008 0:d995086ea45d 93 // Sto ispadne 1s / 3ms = 333.33 osvjezavanja u sekundi
tim008 0:d995086ea45d 94 wait(0.001);
tim008 0:d995086ea45d 95
tim008 0:d995086ea45d 96 value = 3.3 * ain;
tim008 0:d995086ea45d 97
tim008 0:d995086ea45d 98 if (i == 200 )
tim008 0:d995086ea45d 99 {
tim008 0:d995086ea45d 100 //value_proc = (Vmax - value)/(Vmax - Vmin) * 100.00;
tim008 0:d995086ea45d 101 //if(old_value_proc >=98 && value > old_value) value_proc == 100;
tim008 0:d995086ea45d 102 value_proc = -100/(Vmax-Vmin)*(value - Vmin) + 100;
tim008 0:d995086ea45d 103 if(value_proc >= 99.0001 ) value_proc = 100;
tim008 0:d995086ea45d 104 else if(value_proc <= 0.0001 ) value_proc = 0;
tim008 0:d995086ea45d 105
tim008 0:d995086ea45d 106 d4 = value_proc / 100;
tim008 0:d995086ea45d 107
tim008 0:d995086ea45d 108 if(d4 == 0)
tim008 0:d995086ea45d 109 d4 = 'N';
tim008 0:d995086ea45d 110
tim008 0:d995086ea45d 111 d3 =(int) (value_proc / 10) % 10;
tim008 0:d995086ea45d 112 if( d3 == 0 )
tim008 0:d995086ea45d 113 d3 = 'N';
tim008 0:d995086ea45d 114
tim008 0:d995086ea45d 115 d2 = (int) value_proc % 10;
tim008 0:d995086ea45d 116 i=0;
tim008 0:d995086ea45d 117 }
tim008 0:d995086ea45d 118
tim008 0:d995086ea45d 119 // Prelazimo na iduci display
tim008 0:d995086ea45d 120 index++;
tim008 0:d995086ea45d 121 if (index == 5)
tim008 0:d995086ea45d 122 index = 2;
tim008 0:d995086ea45d 123 }
tim008 0:d995086ea45d 124
tim008 0:d995086ea45d 125 return 0;
tim008 0:d995086ea45d 126 }
tim008 0:d995086ea45d 127