Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4747

PIC16F88 I2C Accelerometer Help

$
0
0
Im using a pic16f88 in conjuction with a MMA7455 accelerometer to make an auto leveling jack system. I have run into the problem of not being able to get the I2C functions from the inclinometer to register within the pic. Any help would be much appreciated. Below is the current code i have. I am not sure how to navigate the read/write functions correctly in order to get the value from the Y axis only. I know that the register is $07 but im not sure where to go from here.

Thanks!

#CONFIG
__cONFIG _CONFIG1, _INTRC_IO & _PWRTE_ON & _MCLR_OFF & _LVP_OFF
#endconfig
'
'Set the internal oscillator frequency (8 MHz)
Include "modedefs.bas"
DEFINE OSC 8
OSCCON.4 = 1
OSCCON.5 = 1
OSCCON.6 = 1


'Setup A/D Converter
ANSEL = 0 'turn off all A/D converters


TRISA= %00000010
TRISB= %00110000




up1 var PORTA.0 'motor1 up
up2 var PORTA.7 'motor2 up
button_level var PORTA.1
DPIN1 var PORTB.4 'data pin
DPIN2 var PORTB.5
CPIN1 var PORTA.2 'clock
CPIN2 var PORTA.3
y1 var byte[8]
y2 var byte[8]
led var PORTB.6


start:
clear
low button_level
low led
low up1
low up2


pause 100

if (button_level==1) then
goto level
else
goto start
endif

goto start


end


level:
clear
For y1 = 0 To 7
i2cwrite DPIN1, CPIN1, $07, y1, [y1]
pause 1
next y1

For y2 = 0 To 7
i2cwrite DPIN2, CPIN2, $07, y2, [y2]
pause 1
next y2


For y1 = 0 To 7
i2cread DPIN1, CPIN1, $07, y1, [y1]
PAUSE 1
next y1

For y2 = 0 To 7
I2CREAD DPIN2, CPIN2, $07, y2, [y2]
PAUSE 1
next y2

if (y1>y2) then
gosub blink
goto jack2up
endif

if (y1<y2) then
gosub blink
pause 500
gosub blink
goto jack1up
endif
goto start


jack2up:
high up2

For y2 = 0 To 7
I2CREAD DPIN2, CPIN2, $07, y2, [y2]
PAUSE 1
next y2
if (y1=<y2) then
low up2
else
goto jack2up
endif
goto start


jack1up:
high up1
For y1 = 0 To 7
i2cread DPIN1, CPIN1, $07, y1, [y1]
PAUSE 10
next y1

if (y1>=y2) then
low up1
else
goto jack1up
endif
goto start


blink:
high led
pause 500
low led
return

Viewing all articles
Browse latest Browse all 4747

Trending Articles