Help with ADC on PIC16F88 in C with ADXL335's

Thread Starter

g3nghis

Joined Apr 7, 2010
1
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.

Rich (BB code):
#include <stdlib.h>  //standard include files
#include <pic.h>
#include <htc.h>        
__CONFIG(DUNPROT & PWRTDIS & XT & WDTDIS & BORDIS & LVPDIS);

#ifndef _XTAL_FREQ
 // Unless already defined assume 4MHz system frequency
 // This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 20000000
#endif 

#define seatinput AN0             // variable seat will be used to reference AN0
#define chairinput AN1        // variable chair will be used to reference AN1

void InitLCD(void);
void DisplayC(unsigned char position, const char *str);
void DisplayCharacter(unsigned char pos, unsigned char c);

void Initial(void)
{
        ADCON1 = 0x44;        // Select PORTA pins for ADC or digital I/O
        ADCS0 = 1;                // Use A/D FOSC/8
        TRISA = 0x0B;        // Set I/O for PORTA
        TRISB = 0x00;        // Set I/O for PORTB                
}

//chair angle
double ADConvert1(void)
{
        double chairvolt=0;
        CHS0 = 1;              // Use channel AN1
        CHS1 = 0;
        ADON = 1;              // Turn A/D on
        __delay_us(20);        // delay 30 usec to settle A/D acquisition
        ADGO = 1;              // Start conversion
        while ( ADGO );        // wait for ADGO to go off signalling end of conversion
        chairvolt = (ADRESH*4)+(ADRESL/64);    //Caulating value of N
        return chairvolt;        // Display result
}

// seat angle
double ADConvert2(void)
{
        double seatvolt=0;
        CHS0 = 1;              // Use channel AN2
        CHS1 = 1;
        ADON = 1;              // Turn A/D on
        __delay_us(20);        // delay 30 usec to settle A/D acquisition
        ADGO = 1;              // Start conversion
        while ( ADGO );        // wait for ADGO to go off signalling end of conversion
        seatvolt = (ADRESH*4)+(ADRESL/64);    //Caulating value of N
        return seatvolt;        // Display result


}

int main(void)
{
        Initial();
        double chair = 0;
        double seat = 0;

        for (;;) 
        {
        chair = ADConvert1();
        seat = ADConvert2();

                if(chair>1.5)
                {
                        if(seat!=chair)
                        {
                                RB7=0; //no up
                                __delay_us(100);
                                RB6=1; //down to make even
                        }                                
                }

                if(chair<1.5)
                {
                        if(seat<1.5)
                        {
                                RB6 = 0; //down = off
                                __delay_us(100);
                                RB7 = 1; //up = on
                        }
                        if(seat>1.5)
                        {
                                RB7 = 0; //up = off
                                __delay_us(100);
                                RB6 = 1; //down = on
                        }
                }

                if(chair<1.35)
                        RB5 = 1; // buzzer at threshold
                if(chair>1.35)
                        RB5 = 0; //no buzzer

        }
}
Thanks
 
Top