Thread: Help on accelerometer with pic18f1320

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Help on accelerometer with pic18f1320

    I am hoping i can get some help from this forum as the reviews on this site has been tremendous.

    i have been given an assignment which includes the programming of a micro controller. the assignment is explained below,

    I am to measure the voltage output from an accelerometer which is a DE-ACCM3D, a 3D accelerometer. So far so good i have measured all 3 axis Xout = 1.62V, Yout = 1.62V and Zout = 1.97V, Having a source voltage of 4.83V. Using PIC18F1320 being an analog to digital converter, i need to convert those voltages from the X, Y, Z to display digital values.

    What i have done so far;

    i have created the delay h. and also written a little c program using a c18 compiler built it and it compiled successfully and after that i got stuck. I there any chance anybody could help because i was told that it should have to look like this

    "main
    while (not finished)
    {
    x_value = getADC(PA0);
    y_value = getADC(PA1);
    z_value = getADC(PA2);

    printf("Accel Data is %d, %d, %d ", x_value, y_value, z_value);

    }"

    using port 2, 6 and 7 respectively as analog inputs the code wriiten so far is described below


    /*
    * Clock frequency value.
    * This value is used to calculate other values
    * NOTE:- This value should be the crystal freq or crystal x PLL multiplier
    */
    #define CLOCK_FREQ (( unsigned long ) 8000000 ) // 8MHz
    #define INSTR_FREQ (CLOCK_FREQ/4) // one instruction take 4 clock cycles

    /*
    * Set Device config bit settings
    */
    #pragma config WDT = OFF
    #pragma config OSC = INTIO2 // Internal Osc, OSC1 as RA7, OSC2 as RA6
    #pragma config LVP = OFF
    #pragma config MCLRE = ON

    #include <p18f1320.h>
    #include <stdio.h> // Standard I/O - required for printf() function
    #include "Delay.h"

    void main(void)
    {
    OSCCON = 0x70; // set Internal Oscillator Frequency for 8MHz
    PORTB = 0x04;
    TRISA = 0x01; // RA0 as input
    TRISB = 0; // all outputs
    ADCON1 = 0x7f; // Ports as digital I/O

    } // end of main

    Can anyone please help how to define the voltage to convert them to digital outputs by completing the program.

    Thank you to whoever can save a life

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Provide details i.e the 3 outputs X, Y, and Z of the accelerometer are connected to which pins/ports of the PIC micro.
    Is the source voltage the reference voltage?? If that's the case say which pin is it connected to on the PIC micro.
    Last edited by itCbitC; 02-21-2011 at 09:05 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Smile values

    using the data sheet for the pic18f1320 and the datasheet for the DE-ACCM3D the pins connecting are:

    PIN (1) RA0 = X axis
    PIN (2) RA1/AN1 = Y axis
    PIN (6) RA2/AN2 = Z axis

    Voltage from X axis = 1.62
    Voltage from Y axis = 1.62
    Voltage from Z axis = 1.97

    operating voltage from pic18f1320 = 5V

    Vss = Pin (5)
    VDD = Pin (16)

    operating voltage from accelerometer = 4.83v

    Datasheets
    "http://ww1.microchip.com/downloads/en/devicedoc/39605c.pdf"
    "http://www.dimensionengineering.com/datasheets/DE-ACCM3D.pdf"

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    From schematic it looks like x y z outputs of accm3d are connected to pins 2 6 7 of the pic tho' your last post says pins 1 2 6, so clarify. Does the c18 compiler provide any intrinsics or APIs for interfacing to the ADC? Something like setting up the ports/channels of the ADC, and reading the ADC?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    A simple search of the microchip website pulled up this pdf manual which is all that's needed.
    All the libraries needed for the A/D conversion are documented in Chapter 2 of this manual.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You need to give it a try before we can help you. We won't write your homework for you.

    All you have there is a skeleton program. You haven't done anything.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    @itcbitc
    the pin that was definately use were
    IN (1) RA0 = X axis
    PIN (2) RA1/AN1 = Y axis
    PIN (6) RA2/AN2 = Z axis

    the schematics is what i just used as a as a sample

    here is what i done so far but i am sure i am heading down the wrong path!


    /*
    * Set Device config bit settings
    */
    #pragma config WDT = OFF
    #pragma config OSC = INTIO2 // Internal Osc, OSC1 as RA7, OSC2 as RA6
    #pragma config LVP = OFF
    #pragma config MCLRE = ON

    #include <p18f1320.h>
    #include <stdio.h> // Standard I/O - required for printf() function
    #include "Delay.h"

    void main(void)
    {
    OSCCON = 0x70; // set Internal Oscillator Frequency for 8MHz
    PORTB = 0x04;
    TRISA = 0x01; // RA0 as input
    TRISB = 0; // all outputs
    ADCON1 = 0x7f; // Ports as digital I/O

    } // end of main

    @ cyberfish i have and still trying all i was hopin was a guide not for the work to be done

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You haven't tried to use the ADC at all.

    The datasheet for your chip will have a nice detailed section on how to use the ADC.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read about the ADCONx SFRs in the online/pdf manual/data-sheet http://ww1.microchip.com/downloads/e...Doc/39605F.pdf

    Fix your code to set the right values for ADCON1 and learn how to use ADCON0 and maybe ADCON2.

    Tim S.
    Last edited by stahta01; 02-22-2011 at 09:16 PM. Reason: grammer

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    With the datasheet and c18 compiler's libs manual it'd be pretty straightforward to code the A/D process. Did you get a chance to go over them?
    Page 16 of the c18 compiler libs manual even has example code, and page 159 of the datasheet has the "cookbook recipe" for the A/D conversion.

    In pseudocode it'd be something like:
    Code:
    loop forever
        initialise and configure AN0           /* OpenADC() */
        get analog input from AN0              /* ConvertADC() */
        wait for analog-to-digital conversion  /* loop until BusyADC() returns false */
        read and print result from ADC         /* ReadADC() */
        . . .
        repeat above steps for channels AN1 and AN2
    Now all you have to do is plug in the arguments to the *ADC() compiler library functions listed in the pseudocode above and you're done.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    tht pretty much sums up what i was reading from what you gave me! thank you itcbitc u are a start! actually the lecturer mentioned exactly the same thing on the pseudo code but what i am still confused about is how i can use the voltage output from xyz and converting them to binary and from there i think i should be good?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by dapsonlee View Post
    tht pretty much sums up what i was reading from what you gave me! thank you itcbitc u are a start! actually the lecturer mentioned exactly the same thing on the pseudo code but what i am still confused about is how i can use the voltage output from xyz and converting them to binary and from there i think i should be good?
    Read the input voltages x, y, and z one at a time. Ensure that Vref+ on pic is connected to the operating voltage of the accelerometer which is your reference.

    1. Configure the ADC with OpenADC()
    2. Enter infinite loop (hopefully you know the C idiom for that).
    3. Start the analog-to-digital conversion with ConvertADC()
    4. Busy wait until AD conversion is done with BusyADC()
    5. Capture the result with ReadADC().
    6. Divide result with the scaling factor and display result.
    7. Select AN1 with SetChanADC() and repeat steps 3-6.
    8. Lastly select AN2 with SetChanADC() and repeat steps 3-6.

    This pic has 10 output pins, so output of ReadADC() is divided by a scaling factor to determine the analog voltage.
    Cast output of ReadADC() to float, and divide it by (1024/4.83) which is the scaling factor for this configuration.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Forgot to add that between taking samples of the three analog inputs: x (AN0), y (AN1), and z (AN2), insert a delay of 1 second.
    After sampling, processing, and displaying the analog input insert a 1 second delay before moving onto the next analog input.
    Insert the same 1 second delay after finishing with analog input y (z) and before moving onto analog input z (x).

    It'd be inserted between steps 6 and 7, as in
    Code:
    ...
    6a. Divide result with the scaling factor and display result.
    6b. Wait for 1 second before starting the next A/D conversion.
    7.  Select AN1 with SetChanADC() and repeat steps 3-6
    ...
    Why not post what you've coded so far because without that it'd be impossible to provide any further help.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    PHP Code:

    #define

    void main(void)
    {

        
    unsigned char arm_up;
        
    unsigned char arm_down;
        
    unsigned char state 0;
        
    unsigned char one_volt = ;
        
    unsigned char two_volt = ;

        
    initialise_io();

        
    initialise_adc();

        
    initialise_t0();

        
    initialise_t1();

        
    initialise_uart();

        while(
    1)
        {
            
            
    // Wait for 2 seconds to be up
            
    while(TMR0IF == 0)
            {

                while(
    TMR1IF == 0)
                {
                    
                    
    arm_up get_samp();

                    if( 
    arm_up two_volts)
                    {
                        
    state 1;
                    }
                    
                    
    arm_down get_samp();

                    if( 
    arm_down one_volt )
                    {
                        if(
    state 1)
                        {
                            
    step++;
                            break;
                        }
                    }
                }
            }

            if(
    step 2)
            {
                
    out_running();
            }
            else
            {
                
    out_walking();
            }
        }
    }
                        
                
    void initialise_adc(void);
    {


    }


    void initialise_uart(void);
    {



    That is what i have drafted so far i just need to know where to go next based on settin the inputs outputs as i know my program needs hquite few delays to work!

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Are you done with the ADC code for the accelerometer or is this a continuation of the same?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compensating for Accelerometer Misalignment
    By PartOfTheSoluti in forum Tech Board
    Replies: 3
    Last Post: 07-16-2010, 03:48 PM
  2. 3 axis accelerometer calculations
    By Thantos in forum Tech Board
    Replies: 1
    Last Post: 09-05-2008, 04:26 AM