Mesure pos
Dependencies: C12832 SWSPI mbed
Fork of IonScan_DO_SPI by
main.cpp@0:c8c1409ea335, 2018-04-11 (annotated)
- Committer:
- Esnault
- Date:
- Wed Apr 11 14:53:34 2018 +0000
- Revision:
- 0:c8c1409ea335
Version du 11/04/2018
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Esnault | 0:c8c1409ea335 | 1 | #include "mbed.h" |
Esnault | 0:c8c1409ea335 | 2 | #include "C12832.h" |
Esnault | 0:c8c1409ea335 | 3 | #include "SWSPI.h" |
Esnault | 0:c8c1409ea335 | 4 | #include <cmath> |
Esnault | 0:c8c1409ea335 | 5 | |
Esnault | 0:c8c1409ea335 | 6 | SWSPI spi(PC_4,PC_12,PA_13); // MOSI"bidon" / MISO /CLK |
Esnault | 0:c8c1409ea335 | 7 | |
Esnault | 0:c8c1409ea335 | 8 | C12832 lcd(D11, D13, D12, D7, D10); |
Esnault | 0:c8c1409ea335 | 9 | |
Esnault | 0:c8c1409ea335 | 10 | DigitalOut CS(PA_14); |
Esnault | 0:c8c1409ea335 | 11 | |
Esnault | 0:c8c1409ea335 | 12 | DigitalIn DO(PC_12); |
Esnault | 0:c8c1409ea335 | 13 | |
Esnault | 0:c8c1409ea335 | 14 | InterruptIn index(PA_15); |
Esnault | 0:c8c1409ea335 | 15 | |
Esnault | 0:c8c1409ea335 | 16 | double cpt_fall=0; |
Esnault | 0:c8c1409ea335 | 17 | |
Esnault | 0:c8c1409ea335 | 18 | float distance=0,cpt_rise=0,compteur_index=0; |
Esnault | 0:c8c1409ea335 | 19 | |
Esnault | 0:c8c1409ea335 | 20 | short cpt=0; |
Esnault | 0:c8c1409ea335 | 21 | |
Esnault | 0:c8c1409ea335 | 22 | long x=0,x1=0,pas,RAZ = 0,pos_finale=0, pos_initiale=0; |
Esnault | 0:c8c1409ea335 | 23 | |
Esnault | 0:c8c1409ea335 | 24 | |
Esnault | 0:c8c1409ea335 | 25 | |
Esnault | 0:c8c1409ea335 | 26 | void pressed() |
Esnault | 0:c8c1409ea335 | 27 | { |
Esnault | 0:c8c1409ea335 | 28 | if (x >= 3000) { |
Esnault | 0:c8c1409ea335 | 29 | cpt_rise++; |
Esnault | 0:c8c1409ea335 | 30 | } |
Esnault | 0:c8c1409ea335 | 31 | if (x <= 1000) { |
Esnault | 0:c8c1409ea335 | 32 | cpt_rise--; |
Esnault | 0:c8c1409ea335 | 33 | } |
Esnault | 0:c8c1409ea335 | 34 | |
Esnault | 0:c8c1409ea335 | 35 | } |
Esnault | 0:c8c1409ea335 | 36 | |
Esnault | 0:c8c1409ea335 | 37 | void released() |
Esnault | 0:c8c1409ea335 | 38 | { |
Esnault | 0:c8c1409ea335 | 39 | cpt_fall++; |
Esnault | 0:c8c1409ea335 | 40 | } |
Esnault | 0:c8c1409ea335 | 41 | |
Esnault | 0:c8c1409ea335 | 42 | int main () |
Esnault | 0:c8c1409ea335 | 43 | { |
Esnault | 0:c8c1409ea335 | 44 | spi.format(18,3); ////////////////////////////////////// |
Esnault | 0:c8c1409ea335 | 45 | spi.frequency(100000); // Initialisation SPI software |
Esnault | 0:c8c1409ea335 | 46 | CS=1; |
Esnault | 0:c8c1409ea335 | 47 | lcd.cls();///////////////////////////////////////////// |
Esnault | 0:c8c1409ea335 | 48 | |
Esnault | 0:c8c1409ea335 | 49 | wait_ms(100);/////////////////////////////////////////// |
Esnault | 0:c8c1409ea335 | 50 | CS=0; |
Esnault | 0:c8c1409ea335 | 51 | wait_us(50); |
Esnault | 0:c8c1409ea335 | 52 | |
Esnault | 0:c8c1409ea335 | 53 | pos_initiale=spi.write(0); //Determination pos_initialetion initiale |
Esnault | 0:c8c1409ea335 | 54 | |
Esnault | 0:c8c1409ea335 | 55 | wait_us(50); |
Esnault | 0:c8c1409ea335 | 56 | CS=1; |
Esnault | 0:c8c1409ea335 | 57 | pos_initiale = pos_initiale>>5;///////////////////////////////////////////// |
Esnault | 0:c8c1409ea335 | 58 | |
Esnault | 0:c8c1409ea335 | 59 | pos_finale=x-pos_initiale; |
Esnault | 0:c8c1409ea335 | 60 | |
Esnault | 0:c8c1409ea335 | 61 | //pas = (abs(x-x1))%4095; |
Esnault | 0:c8c1409ea335 | 62 | distance=((pos_finale*(0.000488)+(cpt_rise*2))); |
Esnault | 0:c8c1409ea335 | 63 | |
Esnault | 0:c8c1409ea335 | 64 | while(1) { |
Esnault | 0:c8c1409ea335 | 65 | pos_finale=x-pos_initiale; |
Esnault | 0:c8c1409ea335 | 66 | |
Esnault | 0:c8c1409ea335 | 67 | index.fall(&pressed); |
Esnault | 0:c8c1409ea335 | 68 | index.rise(&released); |
Esnault | 0:c8c1409ea335 | 69 | |
Esnault | 0:c8c1409ea335 | 70 | wait_ms(100); |
Esnault | 0:c8c1409ea335 | 71 | CS=0; |
Esnault | 0:c8c1409ea335 | 72 | wait_us(50); |
Esnault | 0:c8c1409ea335 | 73 | |
Esnault | 0:c8c1409ea335 | 74 | x1=x; |
Esnault | 0:c8c1409ea335 | 75 | x=spi.write(0); |
Esnault | 0:c8c1409ea335 | 76 | |
Esnault | 0:c8c1409ea335 | 77 | //pas = (abs(x-x1))%4095; |
Esnault | 0:c8c1409ea335 | 78 | distance=((pos_finale*(0.000488)+(cpt_rise*2))); // Calcul de la distance parcouru depuis le 0 en mm |
Esnault | 0:c8c1409ea335 | 79 | |
Esnault | 0:c8c1409ea335 | 80 | wait_us(50); |
Esnault | 0:c8c1409ea335 | 81 | CS=1; |
Esnault | 0:c8c1409ea335 | 82 | x = x>>5; |
Esnault | 0:c8c1409ea335 | 83 | |
Esnault | 0:c8c1409ea335 | 84 | lcd.locate(0,3); |
Esnault | 0:c8c1409ea335 | 85 | lcd.printf("DIST= %.5f",distance); |
Esnault | 0:c8c1409ea335 | 86 | lcd.locate(0,14); |
Esnault | 0:c8c1409ea335 | 87 | lcd.printf("POS= %5ld", x); |
Esnault | 0:c8c1409ea335 | 88 | |
Esnault | 0:c8c1409ea335 | 89 | /* |
Esnault | 0:c8c1409ea335 | 90 | lcd.locate(0,3); |
Esnault | 0:c8c1409ea335 | 91 | lcd.printf("pos_initiale= %ld",pos_initiale); |
Esnault | 0:c8c1409ea335 | 92 | lcd.locate(0,14); |
Esnault | 0:c8c1409ea335 | 93 | lcd.printf("POS= %5ld", x); |
Esnault | 0:c8c1409ea335 | 94 | */ |
Esnault | 0:c8c1409ea335 | 95 | } |
Esnault | 0:c8c1409ea335 | 96 | } |