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