By Tom Igoe

This PicBasic Pro program reads a 3-axis accelerometer and returns the values serially. Originally tested on a PIC 18F252.

Back to 3-Axis Accelerometer Report

'    example for the MMA7260Q 3-axiz accelerometer. 
'    This program waits for a single byte input on RB7 at 9600 bpd.
'    Then it reads all three axes of the accelerometer,
'    converts the values to a one-byte range, and
'    sends the results out RB6 at 9600 bps.
'    written for PIC18F452
'    PORTD.0: SLP
'    PORTA.0: X
'    PORTA.1: Y
'    PORTA.2: Z
'    PORTA.3: 3.3V reference voltage

' Define ADCIN parameters
DEFINE  ADC_BITS        10     	' Set number of bits in result
DEFINE  ADC_CLOCK       3     	' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    15  	' Set sampling time in uS

tx var portc.6
rx var portc.7
n9600 con 16468

adcVar  VAR word[3] ' Create variable to store results
channel var byte
byteVar var byte[3]
inByte var byte

'   set Sleep pin high:

' Set PORTA to all input
TRISA = %11111111 

' Set up ADCON1:
' AN3 = VREF+, all other analog pins are set to analog.
' See PIC 18F452 datasheet for details:
ADCON1 = %10000001

    ' wait for a byte:
    serin2 rx, n9600, [inByte]

    ' read all channels of the accelerometer:
    for channel = 0 to 2
        ADCIN channel, adcVar[channel]
       ' convert to a byte:
        byteVar[channel] = adcVar[channel] /4 
        pause 1

    ' send results out the serial port:
    serout2 tx, n9600, [byteVar[0],byteVar[1],byteVar[2]]
GoTo main