First Upload
Dependencies: mbed
main.cpp@0:41d540d48633, 2016-06-08 (annotated)
- Committer:
- azer136
- Date:
- Wed Jun 08 11:55:23 2016 +0000
- Revision:
- 0:41d540d48633
first upload
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
azer136 | 0:41d540d48633 | 1 | #include "mbed.h" |
azer136 | 0:41d540d48633 | 2 | |
azer136 | 0:41d540d48633 | 3 | AnalogIn adc(A0); // Objet (ADC) utilisé pour effectuer la démodulation |
azer136 | 0:41d540d48633 | 4 | Timer timer; |
azer136 | 0:41d540d48633 | 5 | |
azer136 | 0:41d540d48633 | 6 | bool wait_zero(void); //Cette fonction est nécessaire pour commencer la démodulation, elle permet de différencier 1 zéro d'un niveau logique bas lors de la modulation d'un 1 |
azer136 | 0:41d540d48633 | 7 | char decale_mot(char mot); //Cette fonction effectue une rotation des bits d'un caractère: utile pour la vérification du mot reçu |
azer136 | 0:41d540d48633 | 8 | char get_word(void); // Permet d'obtenir le mot démodulé; utilise la fonction: get_bit() |
azer136 | 0:41d540d48633 | 9 | char get_bit(void); //Permet de démoduler un bit du mot émis |
azer136 | 0:41d540d48633 | 10 | |
azer136 | 0:41d540d48633 | 11 | #define delay_bit 119 |
azer136 | 0:41d540d48633 | 12 | #define DELAY_ZERO 10 |
azer136 | 0:41d540d48633 | 13 | #define SEUIL_MOYEN 0.56 |
azer136 | 0:41d540d48633 | 14 | |
azer136 | 0:41d540d48633 | 15 | int main() { |
azer136 | 0:41d540d48633 | 16 | printf("Debut\n"); |
azer136 | 0:41d540d48633 | 17 | char mot; |
azer136 | 0:41d540d48633 | 18 | int i; |
azer136 | 0:41d540d48633 | 19 | printf("Attente presence tension.\n"); |
azer136 | 0:41d540d48633 | 20 | while(adc.read() > SEUIL_MOYEN); //On attend un zéro |
azer136 | 0:41d540d48633 | 21 | while (adc.read() < SEUIL_MOYEN); //On attend la fin du zéro |
azer136 | 0:41d540d48633 | 22 | // Démodulation |
azer136 | 0:41d540d48633 | 23 | mot=get_word(); // Récupération du bit démodulé |
azer136 | 0:41d540d48633 | 24 | /* Vérification du mot reçu en utilisant le décalage binaire */ |
azer136 | 0:41d540d48633 | 25 | for (i=0; i<7; i++){ |
azer136 | 0:41d540d48633 | 26 | printf("%x\n", mot); |
azer136 | 0:41d540d48633 | 27 | mot=decale_mot(mot); |
azer136 | 0:41d540d48633 | 28 | } |
azer136 | 0:41d540d48633 | 29 | printf("%x\n", mot); |
azer136 | 0:41d540d48633 | 30 | printf("Fin\n"); |
azer136 | 0:41d540d48633 | 31 | while(1); |
azer136 | 0:41d540d48633 | 32 | |
azer136 | 0:41d540d48633 | 33 | } |
azer136 | 0:41d540d48633 | 34 | |
azer136 | 0:41d540d48633 | 35 | |
azer136 | 0:41d540d48633 | 36 | bool wait_zero(void){ |
azer136 | 0:41d540d48633 | 37 | int valeur; |
azer136 | 0:41d540d48633 | 38 | valeur=0; |
azer136 | 0:41d540d48633 | 39 | while (adc.read() < DELAY_ZERO){ |
azer136 | 0:41d540d48633 | 40 | valeur++; |
azer136 | 0:41d540d48633 | 41 | wait_us(5); |
azer136 | 0:41d540d48633 | 42 | } |
azer136 | 0:41d540d48633 | 43 | return (valeur < SEUIL_MOYEN)?true:false; |
azer136 | 0:41d540d48633 | 44 | } |
azer136 | 0:41d540d48633 | 45 | |
azer136 | 0:41d540d48633 | 46 | char decale_mot(char mot){ //Cette fonction effectue un décalage du mot de 1 cran vers la gauche |
azer136 | 0:41d540d48633 | 47 | mot=((mot&0x7F)<<1)|((mot&0x80)>>7); |
azer136 | 0:41d540d48633 | 48 | return mot; |
azer136 | 0:41d540d48633 | 49 | } |
azer136 | 0:41d540d48633 | 50 | |
azer136 | 0:41d540d48633 | 51 | char get_word(void){ |
azer136 | 0:41d540d48633 | 52 | int i,time[2]; |
azer136 | 0:41d540d48633 | 53 | char word; //Mots acquis |
azer136 | 0:41d540d48633 | 54 | word=0x00; |
azer136 | 0:41d540d48633 | 55 | timer.reset(); |
azer136 | 0:41d540d48633 | 56 | timer.start(); |
azer136 | 0:41d540d48633 | 57 | time[0]=timer.read_us(); |
azer136 | 0:41d540d48633 | 58 | for(i=7;i>=0;i--){ |
azer136 | 0:41d540d48633 | 59 | word |= get_bit()<<i; |
azer136 | 0:41d540d48633 | 60 | } |
azer136 | 0:41d540d48633 | 61 | time[1]=timer.read_us(); |
azer136 | 0:41d540d48633 | 62 | printf("La difference des deux est: %d %d\n", time[1],time[0]); |
azer136 | 0:41d540d48633 | 63 | return word; |
azer136 | 0:41d540d48633 | 64 | } |
azer136 | 0:41d540d48633 | 65 | |
azer136 | 0:41d540d48633 | 66 | char get_bit(void){ |
azer136 | 0:41d540d48633 | 67 | float moyenne, delai; |
azer136 | 0:41d540d48633 | 68 | char retour; //Valeur retournée |
azer136 | 0:41d540d48633 | 69 | moyenne = 0; //Variable pour contenir la valeur moyenne d'un bit de la trame |
azer136 | 0:41d540d48633 | 70 | delai=0; //Variable permettant de vérifier une variable; |
azer136 | 0:41d540d48633 | 71 | while(delai < delay_bit){ |
azer136 | 0:41d540d48633 | 72 | moyenne += adc.read(); |
azer136 | 0:41d540d48633 | 73 | wait_us(5); |
azer136 | 0:41d540d48633 | 74 | delai += 5; |
azer136 | 0:41d540d48633 | 75 | } |
azer136 | 0:41d540d48633 | 76 | moyenne /= 24; |
azer136 | 0:41d540d48633 | 77 | retour=(moyenne > SEUIL_MOYEN)?0x01:0x00; |
azer136 | 0:41d540d48633 | 78 | return retour; |
azer136 | 0:41d540d48633 | 79 | } |
azer136 | 0:41d540d48633 | 80 | |
azer136 | 0:41d540d48633 | 81 |