mael diy
/
cyclage_pont_H_compteur
H bridge with 4 relays for cycling motor, with a temporisatoin or position reading
main.cpp@0:7aa5e1ea57aa, 2018-02-20 (annotated)
- Committer:
- maeldiy
- Date:
- Tue Feb 20 09:48:09 2018 +0000
- Revision:
- 0:7aa5e1ea57aa
provide H bridge through 4 relays for cycling motor with a temporisation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maeldiy | 0:7aa5e1ea57aa | 1 | /* cycling using relay command through discrete |
maeldiy | 0:7aa5e1ea57aa | 2 | 2017 19 10 by mael reymond |
maeldiy | 0:7aa5e1ea57aa | 3 | function : |
maeldiy | 0:7aa5e1ea57aa | 4 | display state on led onboard |
maeldiy | 0:7aa5e1ea57aa | 5 | command 2 relays (1 per direction) |
maeldiy | 0:7aa5e1ea57aa | 6 | send on serial monitor number of cycles since power on*/ |
maeldiy | 0:7aa5e1ea57aa | 7 | |
maeldiy | 0:7aa5e1ea57aa | 8 | #include "mbed.h" |
maeldiy | 0:7aa5e1ea57aa | 9 | Serial pc(SERIAL_TX,SERIAL_RX); |
maeldiy | 0:7aa5e1ea57aa | 10 | DigitalOut myled(LED1); |
maeldiy | 0:7aa5e1ea57aa | 11 | DigitalInOut relais_1(PB_5); // voir montage pont en H http://eskimon.fr/285-arduino-601-le-moteur-courant-continu |
maeldiy | 0:7aa5e1ea57aa | 12 | DigitalInOut relais_2(PB_8); |
maeldiy | 0:7aa5e1ea57aa | 13 | DigitalInOut relais_3(PC_7); |
maeldiy | 0:7aa5e1ea57aa | 14 | DigitalInOut relais_4(PA_9); |
maeldiy | 0:7aa5e1ea57aa | 15 | AnalogIn status(PC_5); |
maeldiy | 0:7aa5e1ea57aa | 16 | |
maeldiy | 0:7aa5e1ea57aa | 17 | SPI spimax(D11, D12, D13); DigitalOut CS(D9); // for MAX7219 7 segment display |
maeldiy | 0:7aa5e1ea57aa | 18 | // the MAX7219 address map (datasheet table 2) |
maeldiy | 0:7aa5e1ea57aa | 19 | #define MAX7219_DECODE_REG (0x09) |
maeldiy | 0:7aa5e1ea57aa | 20 | #define MAX7219_INTENSITY_REG (0x0A) |
maeldiy | 0:7aa5e1ea57aa | 21 | #define MAX7219_SCANLIMIT_REG (0x0B) |
maeldiy | 0:7aa5e1ea57aa | 22 | #define MAX7219_SHUTDOWN_REG (0X0C) |
maeldiy | 0:7aa5e1ea57aa | 23 | #define MAX7219_DISPLAYTEST_REG (0x0F) |
maeldiy | 0:7aa5e1ea57aa | 24 | #define MAX7219_DIGIT_REG(pos) ((pos) + 1) |
maeldiy | 0:7aa5e1ea57aa | 25 | |
maeldiy | 0:7aa5e1ea57aa | 26 | // shutdown mode (datasheet table 3) |
maeldiy | 0:7aa5e1ea57aa | 27 | #define MAX7219_OFF (0x0) |
maeldiy | 0:7aa5e1ea57aa | 28 | #define MAX7219_ON (0x1) |
maeldiy | 0:7aa5e1ea57aa | 29 | |
maeldiy | 0:7aa5e1ea57aa | 30 | // digit pattern for a 7-segment display. datasheet table 5 |
maeldiy | 0:7aa5e1ea57aa | 31 | const char digit_pattern[16] = |
maeldiy | 0:7aa5e1ea57aa | 32 | { |
maeldiy | 0:7aa5e1ea57aa | 33 | 0x7E, // 0 |
maeldiy | 0:7aa5e1ea57aa | 34 | 0x30, // 1 |
maeldiy | 0:7aa5e1ea57aa | 35 | 0x6D, // 2 |
maeldiy | 0:7aa5e1ea57aa | 36 | 0x79, // 3 |
maeldiy | 0:7aa5e1ea57aa | 37 | 0x33, // 4 |
maeldiy | 0:7aa5e1ea57aa | 38 | 0x5B, // 5 |
maeldiy | 0:7aa5e1ea57aa | 39 | 0x5F, // 6 |
maeldiy | 0:7aa5e1ea57aa | 40 | 0x70, // 7 |
maeldiy | 0:7aa5e1ea57aa | 41 | 0x7F, // 8 |
maeldiy | 0:7aa5e1ea57aa | 42 | 0x7B, // 9 |
maeldiy | 0:7aa5e1ea57aa | 43 | 0x77, // A |
maeldiy | 0:7aa5e1ea57aa | 44 | 0x1F, // b |
maeldiy | 0:7aa5e1ea57aa | 45 | 0x4E, // C |
maeldiy | 0:7aa5e1ea57aa | 46 | 0x3D, // d |
maeldiy | 0:7aa5e1ea57aa | 47 | 0x4F, // E |
maeldiy | 0:7aa5e1ea57aa | 48 | 0x47 // F |
maeldiy | 0:7aa5e1ea57aa | 49 | }; |
maeldiy | 0:7aa5e1ea57aa | 50 | |
maeldiy | 0:7aa5e1ea57aa | 51 | |
maeldiy | 0:7aa5e1ea57aa | 52 | int counter = 0; |
maeldiy | 0:7aa5e1ea57aa | 53 | int duree_fermeture = 3; // par defaut 4 secondes |
maeldiy | 0:7aa5e1ea57aa | 54 | int duree_ouverture = 3; // par defaut 4 secondes |
maeldiy | 0:7aa5e1ea57aa | 55 | int duree_neutre = 0.2; // par defaut 200 ms |
maeldiy | 0:7aa5e1ea57aa | 56 | long n,u,d,c,m, dm,ddm,cm,mm; // pour l'afficheur |
maeldiy | 0:7aa5e1ea57aa | 57 | void fermeture(); // relais 1 & 3 fermés |
maeldiy | 0:7aa5e1ea57aa | 58 | void ouverture(); // relais 2 & 4 fermés |
maeldiy | 0:7aa5e1ea57aa | 59 | void neutre(); // tout relais ouverts |
maeldiy | 0:7aa5e1ea57aa | 60 | void max7219(char reg,char dta) { CS = 0; spimax.write(reg); spimax.write(dta); CS = 1; } |
maeldiy | 0:7aa5e1ea57aa | 61 | |
maeldiy | 0:7aa5e1ea57aa | 62 | |
maeldiy | 0:7aa5e1ea57aa | 63 | void init_max7219() |
maeldiy | 0:7aa5e1ea57aa | 64 | { |
maeldiy | 0:7aa5e1ea57aa | 65 | // Turn on the display |
maeldiy | 0:7aa5e1ea57aa | 66 | max7219(MAX7219_SHUTDOWN_REG, MAX7219_ON); |
maeldiy | 0:7aa5e1ea57aa | 67 | // enable test mode. datasheet table 10 |
maeldiy | 0:7aa5e1ea57aa | 68 | max7219(MAX7219_DISPLAYTEST_REG, MAX7219_ON); |
maeldiy | 0:7aa5e1ea57aa | 69 | wait(6); |
maeldiy | 0:7aa5e1ea57aa | 70 | // disable test mode. datasheet table 10 |
maeldiy | 0:7aa5e1ea57aa | 71 | max7219(MAX7219_DISPLAYTEST_REG, MAX7219_OFF); |
maeldiy | 0:7aa5e1ea57aa | 72 | // set medium intensity. datasheet table 7 |
maeldiy | 0:7aa5e1ea57aa | 73 | max7219(MAX7219_INTENSITY_REG, 0x8); |
maeldiy | 0:7aa5e1ea57aa | 74 | // turn off display. datasheet table 3 |
maeldiy | 0:7aa5e1ea57aa | 75 | // max7219(MAX7219_SHUTDOWN_REG, MAX7219_OFF); |
maeldiy | 0:7aa5e1ea57aa | 76 | // drive 8 digits. datasheet table 8 |
maeldiy | 0:7aa5e1ea57aa | 77 | max7219(MAX7219_SCANLIMIT_REG, 7); |
maeldiy | 0:7aa5e1ea57aa | 78 | // no decode mode for all positions. datasheet table 4 |
maeldiy | 0:7aa5e1ea57aa | 79 | max7219(MAX7219_DECODE_REG, 0x00); |
maeldiy | 0:7aa5e1ea57aa | 80 | } |
maeldiy | 0:7aa5e1ea57aa | 81 | int main() { |
maeldiy | 0:7aa5e1ea57aa | 82 | |
maeldiy | 0:7aa5e1ea57aa | 83 | init_max7219(); |
maeldiy | 0:7aa5e1ea57aa | 84 | /*max7219(1,0x02);wait(2); |
maeldiy | 0:7aa5e1ea57aa | 85 | |
maeldiy | 0:7aa5e1ea57aa | 86 | max7219(3,0x06);wait(2); |
maeldiy | 0:7aa5e1ea57aa | 87 | |
maeldiy | 0:7aa5e1ea57aa | 88 | max7219(5,0x70);wait(2); |
maeldiy | 0:7aa5e1ea57aa | 89 | max7219(7,digit_pattern[2]);wait(2); |
maeldiy | 0:7aa5e1ea57aa | 90 | max7219(2,digit_pattern[6]);wait(2); |
maeldiy | 0:7aa5e1ea57aa | 91 | |
maeldiy | 0:7aa5e1ea57aa | 92 | */ |
maeldiy | 0:7aa5e1ea57aa | 93 | /* relais_1.mode(PullDown); |
maeldiy | 0:7aa5e1ea57aa | 94 | relais_2.mode(PullDown); |
maeldiy | 0:7aa5e1ea57aa | 95 | relais_3.mode(PullDown); |
maeldiy | 0:7aa5e1ea57aa | 96 | relais_4.mode(PullDown); */ |
maeldiy | 0:7aa5e1ea57aa | 97 | |
maeldiy | 0:7aa5e1ea57aa | 98 | max7219(1,0x00);wait(2); |
maeldiy | 0:7aa5e1ea57aa | 99 | while(1) { |
maeldiy | 0:7aa5e1ea57aa | 100 | |
maeldiy | 0:7aa5e1ea57aa | 101 | /* pc.printf("status : %3.3f%%\n", status.read()); // counter display on serial monitor |
maeldiy | 0:7aa5e1ea57aa | 102 | pc.printf("status normalised: 0x%04X \n", status.read_u16()); // counter display on serial monitor |
maeldiy | 0:7aa5e1ea57aa | 103 | bool etat =0; |
maeldiy | 0:7aa5e1ea57aa | 104 | if (status.read()>0.8) { etat =1 ;}else etat = 0; |
maeldiy | 0:7aa5e1ea57aa | 105 | pc.printf("etat : %d\n", etat ); // counter display on serial monitor |
maeldiy | 0:7aa5e1ea57aa | 106 | myled = 1; // LED is ON |
maeldiy | 0:7aa5e1ea57aa | 107 | /* |
maeldiy | 0:7aa5e1ea57aa | 108 | neutre(); |
maeldiy | 0:7aa5e1ea57aa | 109 | ouverture(); |
maeldiy | 0:7aa5e1ea57aa | 110 | neutre(); |
maeldiy | 0:7aa5e1ea57aa | 111 | fermeture(); |
maeldiy | 0:7aa5e1ea57aa | 112 | counter++; //counter increment |
maeldiy | 0:7aa5e1ea57aa | 113 | // pc.printf("counter : %d\n", counter ); // counter display on serial monitor |
maeldiy | 0:7aa5e1ea57aa | 114 | |
maeldiy | 0:7aa5e1ea57aa | 115 | // display cycle number |
maeldiy | 0:7aa5e1ea57aa | 116 | dm = counter/10000000; |
maeldiy | 0:7aa5e1ea57aa | 117 | mm = counter/1000000 -10*dm; |
maeldiy | 0:7aa5e1ea57aa | 118 | cm = counter/100000 -100*dm -10*mm; |
maeldiy | 0:7aa5e1ea57aa | 119 | ddm = counter/10000 -1000*dm -100*mm - 10 *cm; |
maeldiy | 0:7aa5e1ea57aa | 120 | m = counter/1000 -10000*dm -1000*mm - 100 *cm -10*ddm; |
maeldiy | 0:7aa5e1ea57aa | 121 | c = counter/100 -100000*dm -10000*mm - 1000 *cm -100*ddm - 10*m; |
maeldiy | 0:7aa5e1ea57aa | 122 | d = counter/10 -1000000*dm -100000*mm - 10000 *cm -1000*ddm - 100*m - 10*c; |
maeldiy | 0:7aa5e1ea57aa | 123 | u = counter - 10000000*dm -1000000*mm - 100000 *cm -10000*ddm -1000*m - 100*c - 10*d; |
maeldiy | 0:7aa5e1ea57aa | 124 | |
maeldiy | 0:7aa5e1ea57aa | 125 | // dm = counter/10000000; |
maeldiy | 0:7aa5e1ea57aa | 126 | // mm = counter/1000000 -10*dm; |
maeldiy | 0:7aa5e1ea57aa | 127 | // cm = counter/100000 -100*dm -10*mm; |
maeldiy | 0:7aa5e1ea57aa | 128 | // ddm = counter/10000 -1000*dm -100*mm - 10 *cm; |
maeldiy | 0:7aa5e1ea57aa | 129 | /* m = counter/1000; |
maeldiy | 0:7aa5e1ea57aa | 130 | c = counter/100 - 10*m; |
maeldiy | 0:7aa5e1ea57aa | 131 | d = counter/10 - 100*m - 10*c; |
maeldiy | 0:7aa5e1ea57aa | 132 | u = counter -1000*m - 100*c - 10*d; |
maeldiy | 0:7aa5e1ea57aa | 133 | */ |
maeldiy | 0:7aa5e1ea57aa | 134 | if (u == 9) { |
maeldiy | 0:7aa5e1ea57aa | 135 | pc.printf("counter : %d", counter); // counter display on serial monitor |
maeldiy | 0:7aa5e1ea57aa | 136 | pc.printf(" m : %d", m); |
maeldiy | 0:7aa5e1ea57aa | 137 | pc.printf("c : %d", c); |
maeldiy | 0:7aa5e1ea57aa | 138 | pc.printf("d : %d", d); |
maeldiy | 0:7aa5e1ea57aa | 139 | pc.printf("u : %d", u); |
maeldiy | 0:7aa5e1ea57aa | 140 | } |
maeldiy | 0:7aa5e1ea57aa | 141 | // wait_ms(10); |
maeldiy | 0:7aa5e1ea57aa | 142 | |
maeldiy | 0:7aa5e1ea57aa | 143 | max7219(1,digit_pattern[u]); |
maeldiy | 0:7aa5e1ea57aa | 144 | max7219(2,digit_pattern[d]); |
maeldiy | 0:7aa5e1ea57aa | 145 | max7219(3,digit_pattern[c]); |
maeldiy | 0:7aa5e1ea57aa | 146 | max7219(4,digit_pattern[m]); |
maeldiy | 0:7aa5e1ea57aa | 147 | max7219(5,digit_pattern[ddm]); |
maeldiy | 0:7aa5e1ea57aa | 148 | max7219(6,digit_pattern[cm]); |
maeldiy | 0:7aa5e1ea57aa | 149 | max7219(7,digit_pattern[mm]); |
maeldiy | 0:7aa5e1ea57aa | 150 | max7219(8,digit_pattern[dm]); |
maeldiy | 0:7aa5e1ea57aa | 151 | } |
maeldiy | 0:7aa5e1ea57aa | 152 | } |
maeldiy | 0:7aa5e1ea57aa | 153 | |
maeldiy | 0:7aa5e1ea57aa | 154 | |
maeldiy | 0:7aa5e1ea57aa | 155 | void ouverture() |
maeldiy | 0:7aa5e1ea57aa | 156 | { |
maeldiy | 0:7aa5e1ea57aa | 157 | |
maeldiy | 0:7aa5e1ea57aa | 158 | // relais 2 & 3 fermés |
maeldiy | 0:7aa5e1ea57aa | 159 | |
maeldiy | 0:7aa5e1ea57aa | 160 | relais_2.input(); |
maeldiy | 0:7aa5e1ea57aa | 161 | relais_3.input(); |
maeldiy | 0:7aa5e1ea57aa | 162 | |
maeldiy | 0:7aa5e1ea57aa | 163 | relais_2.output(); |
maeldiy | 0:7aa5e1ea57aa | 164 | relais_2= !relais_2; |
maeldiy | 0:7aa5e1ea57aa | 165 | relais_3.output(); |
maeldiy | 0:7aa5e1ea57aa | 166 | relais_3= !relais_3; |
maeldiy | 0:7aa5e1ea57aa | 167 | |
maeldiy | 0:7aa5e1ea57aa | 168 | myled = 0; // LED is OFF |
maeldiy | 0:7aa5e1ea57aa | 169 | wait(duree_ouverture); |
maeldiy | 0:7aa5e1ea57aa | 170 | |
maeldiy | 0:7aa5e1ea57aa | 171 | |
maeldiy | 0:7aa5e1ea57aa | 172 | } |
maeldiy | 0:7aa5e1ea57aa | 173 | |
maeldiy | 0:7aa5e1ea57aa | 174 | void fermeture() |
maeldiy | 0:7aa5e1ea57aa | 175 | { |
maeldiy | 0:7aa5e1ea57aa | 176 | |
maeldiy | 0:7aa5e1ea57aa | 177 | // relais 1 & 3 fermés |
maeldiy | 0:7aa5e1ea57aa | 178 | myled = 1; // LED is ON |
maeldiy | 0:7aa5e1ea57aa | 179 | relais_1.output(); |
maeldiy | 0:7aa5e1ea57aa | 180 | relais_1= !relais_1; |
maeldiy | 0:7aa5e1ea57aa | 181 | relais_4.output(); |
maeldiy | 0:7aa5e1ea57aa | 182 | relais_4= !relais_4; |
maeldiy | 0:7aa5e1ea57aa | 183 | wait(duree_fermeture); |
maeldiy | 0:7aa5e1ea57aa | 184 | |
maeldiy | 0:7aa5e1ea57aa | 185 | } |
maeldiy | 0:7aa5e1ea57aa | 186 | |
maeldiy | 0:7aa5e1ea57aa | 187 | |
maeldiy | 0:7aa5e1ea57aa | 188 | void neutre() |
maeldiy | 0:7aa5e1ea57aa | 189 | { |
maeldiy | 0:7aa5e1ea57aa | 190 | |
maeldiy | 0:7aa5e1ea57aa | 191 | // setting every relay pins as output HIGH, since relays command is a ground |
maeldiy | 0:7aa5e1ea57aa | 192 | relais_1.output(); |
maeldiy | 0:7aa5e1ea57aa | 193 | relais_2.output(); |
maeldiy | 0:7aa5e1ea57aa | 194 | relais_3.output(); |
maeldiy | 0:7aa5e1ea57aa | 195 | relais_4.output(); |
maeldiy | 0:7aa5e1ea57aa | 196 | relais_1= 1; |
maeldiy | 0:7aa5e1ea57aa | 197 | relais_2= 1; |
maeldiy | 0:7aa5e1ea57aa | 198 | relais_3= 1; |
maeldiy | 0:7aa5e1ea57aa | 199 | relais_4= 1; |
maeldiy | 0:7aa5e1ea57aa | 200 | |
maeldiy | 0:7aa5e1ea57aa | 201 | myled = 1; // LED is ON |
maeldiy | 0:7aa5e1ea57aa | 202 | wait(duree_neutre); |
maeldiy | 0:7aa5e1ea57aa | 203 | |
maeldiy | 0:7aa5e1ea57aa | 204 | } |
maeldiy | 0:7aa5e1ea57aa | 205 | |
maeldiy | 0:7aa5e1ea57aa | 206 |