Strange I2C and serial behaviour.......


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62

    Default Strange I2C and serial behaviour.......

    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???

    Code:
    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 -

  2. #2
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62


    Did you find this post helpful? Yes | No

    Default Re: Strange I2C and serial behaviour.......

    Edit: PIC18F2620 not 2820!

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Strange I2C and serial behaviour.......

    Try
    Code:
    hserout [13,10,hex accx," ",hex accx2," ",hex accy," ",hex accy2," "]
    PAUSE 10
    hserout [13,10,hex accx]
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62


    Did you find this post helpful? Yes | No

    Default Re: Strange I2C and serial behaviour.......

    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

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Strange I2C and serial behaviour.......

    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.
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Strange IT behaviour
    By MikeBZH in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th February 2012, 09:00
  2. Strange behaviour
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th October 2009, 22:35
  3. Strange behaviour of my PBP code.
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th August 2009, 20:20
  4. Strange ADC behaviour
    By ruijc in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 12th December 2007, 20:03
  5. strange int behaviour
    By tom in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th November 2005, 15:41

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts