Guest User

Untitled

a guest
Jan 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <at89c51cc03er.h>
  3.  
  4. __sbit __at 0x91 BEEP;
  5. __sbit __at 0x92 LED1;
  6. __sbit __at 0x93 LED2;
  7. __sbit __at 0x94 LED3;
  8. __sbit __at 0x95 LED4;
  9.  
  10. // Serielle Schnittstelle Initialisieren
  11. void initSerial(void)
  12. {
  13.         SCON = 0x52;    //  9600 Baud, 8 Datenbit, 1 Stopp-Bit, asynchroner Betrieb,
  14.         TMOD |=0x20;    //  keine Paritätsprüfung.
  15.         TH1  = 0xfd;    //
  16.         TR1  = 1;       //  Datenübertragungs-Parameter übernehmen
  17.         TI   = 1;       //  nichts übertragen
  18. }
  19.  
  20. void clearscreen(void)
  21. {
  22.         printf("\x1B[H"); //VT100: goto 0,0
  23.         printf("\x1B[J"); //VT100: clear screen
  24.  
  25.         printf("rough values:\n");
  26.         printf("----------\n");
  27.         printf("poti outer:\n");
  28.         printf("poti inner:\n");
  29.         printf("\n");
  30.         printf("precise values:\n");
  31.         printf("----------\n");
  32.         printf("poti outer:\n");
  33.         printf("poti inner:");
  34. }
  35.  
  36. void showValueWithLEDs(float value)
  37. {
  38.         LED1=1;
  39.         LED2=1;
  40.         LED3=1;
  41.         LED4=1;
  42.         BEEP=1;
  43.  
  44.         if (value >= 1)
  45.         {
  46.                 LED1 = 0;
  47.         }
  48.         if (value >= 2)
  49.         {
  50.                 LED2 = 0;
  51.         }
  52.         if (value >= 3)
  53.         {
  54.                 LED3 = 0;
  55.         }
  56.         if (value >= 4)
  57.         {
  58.                 LED4 = 0;
  59.         }
  60.         if (value > 4.2 && value < 4.3)
  61.         {
  62.                 BEEP = 0; //42 yeah
  63.         }
  64. }
  65.  
  66. void main(void)
  67. {
  68.         int convResult = 0;
  69.         int i = 0;
  70.        
  71.         initSerial();
  72.  
  73.         ADCF = 128+64; //P1.7 und P1.6 analog
  74.         ADCON |= 32; //enable ADC
  75.  
  76.         clearscreen();
  77.  
  78.         while (1) {
  79.                 /*
  80.                  * rough values
  81.                  */
  82.                 ADCON &= 255-7; //clear channel selection
  83.                 ADCON |= 6; // set channel 6
  84.                 ADCON |= 8; // start conversion
  85.                 while ((ADCON & 16) == 0){} //wait for conversion to complete
  86.                 ADCON &= 255-16; //clear ADEOC
  87.                 convResult = ADDH;
  88.                
  89.                 printf("\x1B[3;15H"); //place cursor
  90.                 printf("\x1B[K"); //clear to eofl
  91.                 printf_fast_f("%d\t= %fV\n", convResult, convResult*5/256.0);
  92.                 showValueWithLEDs(convResult*5/256.0);
  93.  
  94.                 ADCON &= 255-7; //channel reset
  95.                 ADCON |= 7; //channel select
  96.                 ADCON |= 8; //start conversion
  97.                 while ((ADCON & 16) == 0){} //wait for completion
  98.                 ADCON &= 255-16; //clear ADCON
  99.                 convResult = ADDH;
  100.                
  101.                 printf("\x1B[4;15H"); //place cursor
  102.                 printf("\x1B[K"); //clear to eofl
  103.                 printf_fast_f("%d\t= %fV\n", convResult, convResult*5/256.0);
  104.  
  105.                 /*
  106.                  * precise values
  107.                  */
  108.                 ADCON &= 255-7; //clear channel selection
  109.                 ADCON |= 6; // set channel 6
  110.                 ADCON |= 64+8; // start conversion with high precision
  111.                 while ((ADCON & 16) == 0){} //wait for conversion to complete
  112.                 ADCON &= 255-16; //clear ADEOC
  113.                 convResult = (ADDH<<2)+ADDL;
  114.                
  115.                 printf("\x1B[8;15H"); //place cursor
  116.                 printf("\x1B[K"); //clear to eofl
  117.                 printf_fast_f("%d\t= %fV\n", convResult, convResult*5/1024.0);
  118.  
  119.                 ADCON &= 255-7; //channel reset
  120.                 ADCON |= 7; //channel select
  121.                 ADCON |= 64+8; //start conversion
  122.                 while ((ADCON & 16) == 0){} //wait for completion
  123.                 ADCON &= 255-16; //clear ADCON
  124.                 convResult = (ADDH<<2)+ADDL;
  125.  
  126.                 printf("\x1B[9;15H"); //place cursor
  127.                 printf("\x1B[K"); //clear to eofl
  128.                 printf_fast_f("%d\t= %fV\n", convResult, convResult*5/1024.0);
  129.                
  130.                 _wait_ms(250);
  131.  
  132.                 if (i == 10)
  133.                 {
  134.                         clearscreen();
  135.                         i=-1;
  136.                 }
  137.                 i++;
  138.         }
  139. }
Add Comment
Please, Sign In to add comment