PDA

View Full Version : Strange I2C and serial behaviour.......



jimbostlawrence
- 28th October 2012, 22:43
Hi all,

I am using an 18F2820 clocked at 20 MHz, connected to an ADXL345 accelerometer and reading it as fast as I can and pushing the data out on the USART and looping back for more data. Code is as below.

My question is this... Why does the output go to fixed values when I try to pass out on the serial bus all of the axes (x, Y, and Z), but if I only try to output maybe one or two axes worth of data it becomes real numbers reflecting the orientation of the sensor???



define OSC 20
DEFINE HSER_BAUD 38400
DEFINE HSER_CLROERR 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h

Include "modedefs.bas"
Serial_out var portc.6
i2cSDA VAR PORTC.4
i2cSCL VAR PORTC.3
accx VAR byte
accx2 var byte
accy VAR byte
accy2 var byte
accz VAR byte
accz2 var byte

I2CWRITE i2cSDA,i2cSCL, $a6, $2d, [0] 'accelerometer
pauseus 5
i2CWRITE i2cSDA,i2cSCL, $a6, $2d, [16] 'accelerometer
pauseus 5
I2CWRITE i2cSDA,i2cSCL, $a6, $2d, [8] 'accelerometer
pauseus 5
I2CWRITE i2cSDA,i2cSCL, $a6, $31, [11] 'accelerometer
pauseus 5
I2CWRITE i2cSDA,i2cSCL, $a6, $2c, [$f] 'set accelerometer sampling frequency
porta = %00000000
portc = %00000000
adcon1 = %00000111
CMCON = %00000111
trisa = %00000000
trisc = %00000000
led var porta.0

pauseus 10

main:
i2cread i2cSDA,i2cSCL,$a7,50,[accx,accx2,accy,accy2,accz,accz2];,error;
pauseus 10
hserout [13,10,hex accx," ",hex accx2," ",hex accy," ",hex accy2," "]
hserout [13,10,hex accx]
goto main

error:
high led
hserout[13,10,"Error"]
goto error


Output is as follows:

10
10 10 10 10
10
10 10 10 10
10
10 10 10 10
10
10 10 10 10
10
10 10 10 10
10
10 10 10 10
10
10 10 10 10

But if I drop the HSEROUT[13,10,HEX accx," ",HEX accx2," ",HEX accy," ",HEX accy2," "] line, the output reverts to variable output as follows...

5E
84
6A
50
90
6A
20
C0
6A
A4
D0
DA
C0
68
DE


Something odd going on inside the chip or am I missing something daft (again!)? :)

Also, not sure if this is relevant, but if I enable the redirect on fail 'error' at the end of the I2CREAD command, it jumps out of the loop and switches on the LED and goes into the infinite loop 'Error'... Not sure why it's giving this error when it is acutally receiving data from the sensor as demonstrated above.

Cheers all

- Jimbo -

jimbostlawrence
- 28th October 2012, 23:48
Edit: PIC18F2620 not 2820!

mackrackit
- 29th October 2012, 02:01
Try

hserout [13,10,hex accx," ",hex accx2," ",hex accy," ",hex accy2," "]
PAUSE 10
hserout [13,10,hex accx]

jimbostlawrence
- 30th October 2012, 00:23
Hi Dave,

It's the line with the different variables that's the issue. I essentially just want to use one HSEROUT and output all the variables in one shot and then call the I2C bus and get more, ad infinitum. But the serial output goes crap if I ask it to output more than one or two variables.

I had tried what you suggested to no avail. It would also go screwy if I changed the pause value directly after the I2CREAD (just before the HSEROUT) from 10us to something like 1ms... ?!

Made no sense to me whatsoever!

Cheers

Jimbo

mackrackit
- 30th October 2012, 01:02
Try SEROUT2 . Might be something about a buffer I am not able to remember...
Or DEBUG ???

Sorry, but I can not think what it is and I am not able to test on hardware at the moment.