Advertisement
bsddeamon

rebound_calc2.c

Mar 7th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. // rebound_calc2.c
  2. // Calcule la moyenne en boucle du temps de rebondissement d’un bouton
  3. // poussoir branche en pull-up sur un bit de port du DS89C450.
  4. // Hacker: Samuel Duclos
  5.  
  6. #include "MonDallas.h"
  7. #include <stdio.h>
  8.  
  9. // Changez a volonte...
  10. #define BUTTON P1_3
  11.  
  12. UI bounce; // Variable globale.
  13.  
  14. void reinit10msTimer0(void);
  15. void time_bounce(void);
  16.  
  17. void main(void) {
  18.   float n, total; // Statistiques (moyenne).
  19.  
  20.   SCON0 = 0x50;
  21.   TMOD |= 0x21; // Timer 0 mode 16 bits.
  22.   TH1 = 0xFF;
  23.   PCON = 0x80;
  24.   TR1 = 1;
  25.   TI_0 = 1;
  26.  
  27.   printf("Branchez BUTTON et pesez dessus…\n");
  28.  
  29.   for (n = 1.0, total = 0.0;; n++) {
  30.     while (BUTTON); while (!BUTTON);
  31.  
  32.     reinit10msTimer0();
  33.     time_bounce();
  34.     total += ((65536 - bounce) / 921.6);
  35.  
  36.     printf("Moyenne pour %d rebond(s) d’environ %f millisecondes.\n", (UI)n, total / n);
  37.   }
  38. }
  39.  
  40. void reinit10msTimer0(void) {
  41.   bounce = 0xDC00;
  42.   TH0 = 0xDC; TL0 = 0x00;
  43.   TR0 = 1;
  44. }
  45.  
  46. void time_bounce(void) {
  47.   while (!TF0) if (!BUTTON) bounce = (TH0 << 8) + TL0;
  48.   TF0 = 0;
  49.   TR0 = 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement