![](/media/cache/group/Display_Nokia_Insper_v01.jpg.50x50_q85.jpg)
12/mar ignicao funcionando
Dependencies: mbed reScale TextLCD
main.cpp@7:5cd0b64dea2a, 2019-03-12 (annotated)
- Committer:
- Marcelocostanzo
- Date:
- Tue Mar 12 19:01:55 2019 +0000
- Revision:
- 7:5cd0b64dea2a
- Parent:
- 6:111a7f1b978f
Ignicao funcionando 12/mar
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:b5a9e0614efd | 1 | #include "mbed.h" |
Marcelocostanzo | 5:e04419b1e369 | 2 | #include "reScale.h" |
Marcelocostanzo | 5:e04419b1e369 | 3 | |
Marcelocostanzo | 7:5cd0b64dea2a | 4 | Ticker flipper; |
Marcelocostanzo | 7:5cd0b64dea2a | 5 | |
Marcelocostanzo | 7:5cd0b64dea2a | 6 | DigitalOut controlLED(D13); |
Marcelocostanzo | 7:5cd0b64dea2a | 7 | DigitalOut Splug_1(D2); |
Marcelocostanzo | 7:5cd0b64dea2a | 8 | DigitalOut Splug_2(D3); |
Marcelocostanzo | 7:5cd0b64dea2a | 9 | DigitalOut Splug_3(D4); |
Marcelocostanzo | 7:5cd0b64dea2a | 10 | DigitalOut Splug_4(D5); |
Marcelocostanzo | 7:5cd0b64dea2a | 11 | |
Marcelocostanzo | 7:5cd0b64dea2a | 12 | //Timer t1; |
Marcelocostanzo | 7:5cd0b64dea2a | 13 | Timer t2; |
Marcelocostanzo | 7:5cd0b64dea2a | 14 | |
Marcelocostanzo | 7:5cd0b64dea2a | 15 | Serial device(USBTX, USBRX); // tx, rx |
Marcelocostanzo | 7:5cd0b64dea2a | 16 | |
Marcelocostanzo | 7:5cd0b64dea2a | 17 | bool flag; |
Marcelocostanzo | 7:5cd0b64dea2a | 18 | float time_rpm = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 19 | int dwell = 2500; |
Marcelocostanzo | 5:e04419b1e369 | 20 | |
Marcelocostanzo | 5:e04419b1e369 | 21 | |
Marcelocostanzo | 6:111a7f1b978f | 22 | |
Marcelocostanzo | 6:111a7f1b978f | 23 | |
Marcelocostanzo | 6:111a7f1b978f | 24 | void flip() |
Marcelocostanzo | 2:3c69c0772709 | 25 | { |
Marcelocostanzo | 6:111a7f1b978f | 26 | flag=!flag; |
Marcelocostanzo | 6:111a7f1b978f | 27 | controlLED=!controlLED; |
Marcelocostanzo | 5:e04419b1e369 | 28 | } |
Marcelocostanzo | 5:e04419b1e369 | 29 | |
Marcelocostanzo | 7:5cd0b64dea2a | 30 | void flushSerialBuffer(void) //rotina para limpar o buffer da serial |
Marcelocostanzo | 7:5cd0b64dea2a | 31 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 32 | char char1 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 33 | while (device.readable()) |
Marcelocostanzo | 7:5cd0b64dea2a | 34 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 35 | char1 = device.getc(); |
Marcelocostanzo | 7:5cd0b64dea2a | 36 | } return; |
Marcelocostanzo | 5:e04419b1e369 | 37 | } |
Marcelocostanzo | 6:111a7f1b978f | 38 | |
Marcelocostanzo | 7:5cd0b64dea2a | 39 | void set_parameters() |
Marcelocostanzo | 2:3c69c0772709 | 40 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 41 | int uni, dez, cen, mil; |
Marcelocostanzo | 7:5cd0b64dea2a | 42 | char c[5]; |
Marcelocostanzo | 7:5cd0b64dea2a | 43 | int i = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 44 | |
Marcelocostanzo | 7:5cd0b64dea2a | 45 | c[i] = device.getc(); |
Marcelocostanzo | 7:5cd0b64dea2a | 46 | i++; |
Marcelocostanzo | 7:5cd0b64dea2a | 47 | |
Marcelocostanzo | 7:5cd0b64dea2a | 48 | if(c[0] == 'T' || 'R') |
Marcelocostanzo | 7:5cd0b64dea2a | 49 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 50 | while(i<5) |
Marcelocostanzo | 7:5cd0b64dea2a | 51 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 52 | c[i] = device.getc(); |
Marcelocostanzo | 7:5cd0b64dea2a | 53 | i++; |
Marcelocostanzo | 7:5cd0b64dea2a | 54 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 55 | |
Marcelocostanzo | 7:5cd0b64dea2a | 56 | device.printf("%c %c %c %c %c \n\r",c[0], c[1], c[2], c[3], c[4]); |
Marcelocostanzo | 7:5cd0b64dea2a | 57 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 58 | |
Marcelocostanzo | 7:5cd0b64dea2a | 59 | else |
Marcelocostanzo | 7:5cd0b64dea2a | 60 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 61 | device.printf("Invalid\n\r"); |
Marcelocostanzo | 7:5cd0b64dea2a | 62 | flushSerialBuffer(); |
Marcelocostanzo | 7:5cd0b64dea2a | 63 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 64 | |
Marcelocostanzo | 7:5cd0b64dea2a | 65 | if(c[0] == 'T') |
Marcelocostanzo | 7:5cd0b64dea2a | 66 | { |
Marcelocostanzo | 2:3c69c0772709 | 67 | |
Marcelocostanzo | 7:5cd0b64dea2a | 68 | mil = atoi(&c[1]); |
Marcelocostanzo | 7:5cd0b64dea2a | 69 | //mil = mil * 1000; |
Marcelocostanzo | 7:5cd0b64dea2a | 70 | dwell = mil; |
Marcelocostanzo | 7:5cd0b64dea2a | 71 | device.printf("%i \n\r",mil); |
Marcelocostanzo | 7:5cd0b64dea2a | 72 | |
Marcelocostanzo | 7:5cd0b64dea2a | 73 | cen = atoi(&c[2]); |
Marcelocostanzo | 7:5cd0b64dea2a | 74 | //cen = cen * 100; |
Marcelocostanzo | 7:5cd0b64dea2a | 75 | device.printf("%i \n\r",cen); |
Marcelocostanzo | 7:5cd0b64dea2a | 76 | |
Marcelocostanzo | 7:5cd0b64dea2a | 77 | dez = atoi(&c[3]); |
Marcelocostanzo | 7:5cd0b64dea2a | 78 | //dez = dez * 10; |
Marcelocostanzo | 7:5cd0b64dea2a | 79 | device.printf("%i \n\r",dez); |
Marcelocostanzo | 7:5cd0b64dea2a | 80 | |
Marcelocostanzo | 7:5cd0b64dea2a | 81 | uni = atoi(&c[4]); |
Marcelocostanzo | 7:5cd0b64dea2a | 82 | device.printf("%i \n\r",uni); |
Marcelocostanzo | 7:5cd0b64dea2a | 83 | |
Marcelocostanzo | 7:5cd0b64dea2a | 84 | //dwell = mil + cen + dez + uni; |
Marcelocostanzo | 7:5cd0b64dea2a | 85 | device.printf("Dwell %i uS\n\r",dwell); |
Marcelocostanzo | 7:5cd0b64dea2a | 86 | flushSerialBuffer(); |
Marcelocostanzo | 7:5cd0b64dea2a | 87 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 88 | |
Marcelocostanzo | 7:5cd0b64dea2a | 89 | if(c[0] == 'R') |
Marcelocostanzo | 7:5cd0b64dea2a | 90 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 91 | mil = atoi(&c[1]); |
Marcelocostanzo | 7:5cd0b64dea2a | 92 | //mil = mil * 1000; |
Marcelocostanzo | 7:5cd0b64dea2a | 93 | time_rpm = mil; |
Marcelocostanzo | 7:5cd0b64dea2a | 94 | //device.printf("%i \n\r",mil); |
Marcelocostanzo | 7:5cd0b64dea2a | 95 | |
Marcelocostanzo | 7:5cd0b64dea2a | 96 | cen = atoi(&c[2]); |
Marcelocostanzo | 7:5cd0b64dea2a | 97 | //cen = cen * 100; |
Marcelocostanzo | 7:5cd0b64dea2a | 98 | //device.printf("%i \n\r",cen); |
Marcelocostanzo | 7:5cd0b64dea2a | 99 | |
Marcelocostanzo | 7:5cd0b64dea2a | 100 | dez = atoi(&c[3]); |
Marcelocostanzo | 7:5cd0b64dea2a | 101 | //dez = dez * 10; |
Marcelocostanzo | 7:5cd0b64dea2a | 102 | //device.printf("%i \n\r",dez); |
Marcelocostanzo | 7:5cd0b64dea2a | 103 | |
Marcelocostanzo | 7:5cd0b64dea2a | 104 | uni = atoi(&c[4]); |
Marcelocostanzo | 7:5cd0b64dea2a | 105 | //device.printf("%i \n\r",uni); |
Marcelocostanzo | 7:5cd0b64dea2a | 106 | |
Marcelocostanzo | 7:5cd0b64dea2a | 107 | //time_rpm = mil + cen + dez + uni; |
Marcelocostanzo | 7:5cd0b64dea2a | 108 | |
Marcelocostanzo | 7:5cd0b64dea2a | 109 | time_rpm = 1/time_rpm; |
Marcelocostanzo | 7:5cd0b64dea2a | 110 | |
Marcelocostanzo | 7:5cd0b64dea2a | 111 | device.printf("%f S\n\r",time_rpm); |
Marcelocostanzo | 7:5cd0b64dea2a | 112 | |
Marcelocostanzo | 7:5cd0b64dea2a | 113 | flipper.attach(&flip, time_rpm); |
Marcelocostanzo | 7:5cd0b64dea2a | 114 | flushSerialBuffer(); |
Marcelocostanzo | 7:5cd0b64dea2a | 115 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 116 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 117 | |
Marcelocostanzo | 6:111a7f1b978f | 118 | int main() |
Marcelocostanzo | 6:111a7f1b978f | 119 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 120 | bool valid_pulse; |
Marcelocostanzo | 7:5cd0b64dea2a | 121 | int Splug_counter = 1; |
Marcelocostanzo | 5:e04419b1e369 | 122 | |
Marcelocostanzo | 7:5cd0b64dea2a | 123 | time_rpm = 0.02; |
Marcelocostanzo | 7:5cd0b64dea2a | 124 | flipper.attach(&flip, time_rpm); |
Marcelocostanzo | 2:3c69c0772709 | 125 | |
Marcelocostanzo | 7:5cd0b64dea2a | 126 | device.printf("Start\n\r"); |
Marcelocostanzo | 7:5cd0b64dea2a | 127 | |
Marcelocostanzo | 7:5cd0b64dea2a | 128 | while(1) |
Marcelocostanzo | 6:111a7f1b978f | 129 | { |
Marcelocostanzo | 6:111a7f1b978f | 130 | |
Marcelocostanzo | 6:111a7f1b978f | 131 | //t1.reset(); |
Marcelocostanzo | 7:5cd0b64dea2a | 132 | //t1.start(); |
Marcelocostanzo | 7:5cd0b64dea2a | 133 | |
Marcelocostanzo | 7:5cd0b64dea2a | 134 | if(device.readable()) |
Marcelocostanzo | 7:5cd0b64dea2a | 135 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 136 | set_parameters(); |
Marcelocostanzo | 7:5cd0b64dea2a | 137 | } |
Marcelocostanzo | 6:111a7f1b978f | 138 | |
Marcelocostanzo | 6:111a7f1b978f | 139 | |
Marcelocostanzo | 7:5cd0b64dea2a | 140 | if(Splug_counter > 4) |
Marcelocostanzo | 5:e04419b1e369 | 141 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 142 | Splug_counter = 1; |
Marcelocostanzo | 2:3c69c0772709 | 143 | } |
Marcelocostanzo | 2:3c69c0772709 | 144 | |
Marcelocostanzo | 7:5cd0b64dea2a | 145 | if(t2.read_us() > dwell) |
Marcelocostanzo | 7:5cd0b64dea2a | 146 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 147 | t2.stop(); |
Marcelocostanzo | 7:5cd0b64dea2a | 148 | Splug_1 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 149 | Splug_2 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 150 | Splug_3 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 151 | Splug_4 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 152 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 153 | if((Splug_counter == 1) && (valid_pulse == 1)) |
Marcelocostanzo | 2:3c69c0772709 | 154 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 155 | valid_pulse = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 156 | Splug_1 = 1; |
Marcelocostanzo | 7:5cd0b64dea2a | 157 | Splug_2 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 158 | Splug_3 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 159 | Splug_4 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 160 | t2.reset(); |
Marcelocostanzo | 7:5cd0b64dea2a | 161 | t2.start(); |
Marcelocostanzo | 6:111a7f1b978f | 162 | } |
Marcelocostanzo | 7:5cd0b64dea2a | 163 | |
Marcelocostanzo | 7:5cd0b64dea2a | 164 | if((Splug_counter == 2) && (valid_pulse == 1)) |
Marcelocostanzo | 6:111a7f1b978f | 165 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 166 | valid_pulse = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 167 | Splug_1 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 168 | Splug_2 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 169 | Splug_3 = 1; |
Marcelocostanzo | 7:5cd0b64dea2a | 170 | Splug_4 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 171 | t2.reset(); |
Marcelocostanzo | 7:5cd0b64dea2a | 172 | t2.start(); |
Marcelocostanzo | 2:3c69c0772709 | 173 | } |
Marcelocostanzo | 5:e04419b1e369 | 174 | |
Marcelocostanzo | 7:5cd0b64dea2a | 175 | if((Splug_counter == 3) && (valid_pulse == 1)) |
Marcelocostanzo | 6:111a7f1b978f | 176 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 177 | valid_pulse = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 178 | Splug_1 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 179 | Splug_2 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 180 | Splug_3 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 181 | Splug_4 = 1; |
Marcelocostanzo | 7:5cd0b64dea2a | 182 | t2.reset(); |
Marcelocostanzo | 7:5cd0b64dea2a | 183 | t2.start(); |
Marcelocostanzo | 6:111a7f1b978f | 184 | } |
Marcelocostanzo | 6:111a7f1b978f | 185 | |
Marcelocostanzo | 7:5cd0b64dea2a | 186 | if((Splug_counter == 4) && (valid_pulse == 1)) |
Marcelocostanzo | 7:5cd0b64dea2a | 187 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 188 | valid_pulse = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 189 | Splug_1 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 190 | Splug_2 = 1; |
Marcelocostanzo | 7:5cd0b64dea2a | 191 | Splug_3 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 192 | Splug_4 = 0; |
Marcelocostanzo | 7:5cd0b64dea2a | 193 | t2.reset(); |
Marcelocostanzo | 7:5cd0b64dea2a | 194 | t2.start(); |
Marcelocostanzo | 7:5cd0b64dea2a | 195 | } |
Marcelocostanzo | 6:111a7f1b978f | 196 | |
Marcelocostanzo | 6:111a7f1b978f | 197 | |
Marcelocostanzo | 6:111a7f1b978f | 198 | if(flag==1) |
Marcelocostanzo | 6:111a7f1b978f | 199 | { |
Marcelocostanzo | 7:5cd0b64dea2a | 200 | Splug_counter++; |
Marcelocostanzo | 6:111a7f1b978f | 201 | flag=0; |
Marcelocostanzo | 7:5cd0b64dea2a | 202 | valid_pulse = 1; |
Marcelocostanzo | 6:111a7f1b978f | 203 | } |
Marcelocostanzo | 6:111a7f1b978f | 204 | |
Marcelocostanzo | 6:111a7f1b978f | 205 | //t1.stop(); |
Marcelocostanzo | 6:111a7f1b978f | 206 | //printf("O ciclo levou: %i uS \n\r", t1.read_us()); |
mbed_official | 0:b5a9e0614efd | 207 | } |
Marcelocostanzo | 2:3c69c0772709 | 208 | } |