Help with ADC on PIC16F88 in C with ADXL335's

Discussion in 'Embedded Systems and Microcontrollers' started by g3nghis, Apr 7, 2010.

  1. g3nghis

    Thread Starter New Member

    Apr 7, 2010
    1
    0
    Hello everyone,

    I am trying to get an auto-actuation circuit built with the use of a PIC, and two ADXL335 used for tilt information. The ADXL is 3 axis chip but I am using only one axis on each chip. The accelerometer reads 1.5V at 0degrees and goes to 1.2V at 90 degrees down and 1.8V at 90 degrees up. What i want to do is, have the chair to this base to auto actuate back to level when the base goes below 0 degrees. There's also a threshold angle that will cause a buzzer to go off indicating the base is tilted too much.
    The actuator has its own controller and I am splicing into the actuator joystick which is a single pull with two throws. I was thinking of using a relay on each pull-throw connection and having the PIC output pin connected to the relay.
    I have been reading a bit on ADC and the ADXL but want to double check if my code is correct before I connect anything. I know C but have never used PIC's so any help is appreciated. Heres what i have. Hopefully its good....I am mainly not sure if the whole top and initialization is correct (kinda just c&p from other example codes). I am using a 20Mhz external crystal. The ADXL uses 3V and so will the PIC if allowed which i believe it is.

    Code ( (Unknown Language)):
    1. #include <stdlib.h>  //standard include files
    2. #include <pic.h>
    3. #include <htc.h>        
    4. __CONFIG(DUNPROT & PWRTDIS & XT & WDTDIS & BORDIS & LVPDIS);
    5.  
    6. #ifndef _XTAL_FREQ
    7.  // Unless already defined assume 4MHz system frequency
    8.  // This definition is required to calibrate __delay_us() and __delay_ms()
    9. #define _XTAL_FREQ 20000000
    10. #endif
    11.  
    12. #define seatinput AN0             // variable seat will be used to reference AN0
    13. #define chairinput AN1        // variable chair will be used to reference AN1
    14.  
    15. void InitLCD(void);
    16. void DisplayC(unsigned char position, const char *str);
    17. void DisplayCharacter(unsigned char pos, unsigned char c);
    18.  
    19. void Initial(void)
    20. {
    21.         ADCON1 = 0x44;        // Select PORTA pins for ADC or digital I/O
    22.         ADCS0 = 1;                // Use A/D FOSC/8
    23.         TRISA = 0x0B;        // Set I/O for PORTA
    24.         TRISB = 0x00;        // Set I/O for PORTB                
    25. }
    26.  
    27. //chair angle
    28. double ADConvert1(void)
    29. {
    30.         double chairvolt=0;
    31.         CHS0 = 1;              // Use channel AN1
    32.         CHS1 = 0;
    33.         ADON = 1;              // Turn A/D on
    34.         __delay_us(20);        // delay 30 usec to settle A/D acquisition
    35.         ADGO = 1;              // Start conversion
    36.         while ( ADGO );        // wait for ADGO to go off signalling end of conversion
    37.         chairvolt = (ADRESH*4)+(ADRESL/64);    //Caulating value of N
    38.         return chairvolt;        // Display result
    39. }
    40.  
    41. // seat angle
    42. double ADConvert2(void)
    43. {
    44.         double seatvolt=0;
    45.         CHS0 = 1;              // Use channel AN2
    46.         CHS1 = 1;
    47.         ADON = 1;              // Turn A/D on
    48.         __delay_us(20);        // delay 30 usec to settle A/D acquisition
    49.         ADGO = 1;              // Start conversion
    50.         while ( ADGO );        // wait for ADGO to go off signalling end of conversion
    51.         seatvolt = (ADRESH*4)+(ADRESL/64);    //Caulating value of N
    52.         return seatvolt;        // Display result
    53.  
    54.  
    55. }
    56.  
    57. int main(void)
    58. {
    59.         Initial();
    60.         double chair = 0;
    61.         double seat = 0;
    62.  
    63.         for (;;)
    64.         {
    65.         chair = ADConvert1();
    66.         seat = ADConvert2();
    67.  
    68.                 if(chair>1.5)
    69.                 {
    70.                         if(seat!=chair)
    71.                         {
    72.                                 RB7=0; //no up
    73.                                 __delay_us(100);
    74.                                 RB6=1; //down to make even
    75.                         }                                
    76.                 }
    77.  
    78.                 if(chair<1.5)
    79.                 {
    80.                         if(seat<1.5)
    81.                         {
    82.                                 RB6 = 0; //down = off
    83.                                 __delay_us(100);
    84.                                 RB7 = 1; //up = on
    85.                         }
    86.                         if(seat>1.5)
    87.                         {
    88.                                 RB7 = 0; //up = off
    89.                                 __delay_us(100);
    90.                                 RB6 = 1; //down = on
    91.                         }
    92.                 }
    93.  
    94.                 if(chair<1.35)
    95.                         RB5 = 1; // buzzer at threshold
    96.                 if(chair>1.35)
    97.                         RB5 = 0; //no buzzer
    98.  
    99.         }
    100. }
    Thanks
     
Loading...