Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: C++  |  size: 1.50 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <p24fj128ga010.h>
  2.  
  3. _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & COE_OFF & FWDTEN_OFF)
  4. _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRI)
  5.  
  6. #define SCALE 308L
  7.  
  8. void czekaj(unsigned long ile){
  9.     unsigned long i = 0;
  10.    
  11.     for(i=ile*SCALE; i>0; i--)
  12.         Nop();
  13. }
  14.  
  15. #define POT 5 // 10k potencjometr podlaczony do wejscia AN5
  16. #define AINPUTS 0xffdf // wejścia analogowe dla czujnika temperatury i potencjometru
  17.  
  18. void initADC (int amask)
  19. {
  20.         AD1PCFG = amask; // maska do analogowych wejść, 0-an
  21.         AD1CON1 = 0x00E0; // auto konwersja po probkowaniu na uint
  22.         AD1CSSL = 0; // bez skanowania, , bo mamy jedno wejście
  23.         AD1CON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
  24.         AD1CON3 = 0x1F02; // Tsamp = 32 x Tad; Tad=125ns, wybieramy czas probkowania
  25.         AD1CON1bits.ADON = 1; // włączmy ADC
  26. } //initADC
  27.  
  28. int readADC (int ch)
  29. {
  30.         AD1CHS = ch; // wybieramy które wejście jest analogowe
  31.         AD1CON1bits.SAMP = 1; // zaczynamy próbkowanie
  32.         while (!AD1CON1bits.DONE); // czekamy na koniec konwersji
  33.         return ADC1BUF0; // zwracamy wynik
  34. }
  35.  
  36. #define PMDATA  PMDIN1
  37.  
  38. #define TFLY 9000       // 9000 x 16us = 144ms
  39. #define DELAY() TMR1=0; while( TMR1<TFLY)
  40.  
  41.  
  42. main ()
  43. {
  44.  
  45.         int a;
  46.  
  47.         initADC( AINPUTS); // inicjalizacja przetwornika A/C
  48.         TRISA = 0xff00; // wybór PORTA jako wyjścia do wysterowania diód
  49.  
  50.         while(1)
  51.         {
  52.                 a = readADC( POT);             
  53.                 //a >>= 2; // 10-bit do 8 bit (0..7)
  54.                 PORTA = (a/4); //to to samo, przesuniecie o 1 bit to dzielenie na 2
  55.         }
  56. }