Word array behaving like byte array.... wierd


+ Reply to Thread
Results 1 to 15 of 15
  1. #1
    forgie's Avatar
    forgie Guest

    Question Word array behaving like byte array.... wierd

    Hi there,
    I have a piece of code which looks like thus:
    <code>
    Sample_Buffer[Sample_Index] = ADC_Buffer
    </code>


    All of these variables are declared as variables. When I check the data, Sample_Buffer[Sample_Index] is always less then 256, although I know that the data in ADC_Buffer is often higher then this.

    I have tested it by changing the code to this:

    <code>
    if ADC_Buffer > 255 then
    lcdout cmd, cls, "BIG NUM"
    endif
    sample_buffer[sample_index] = adc_buffer
    </code>

    And sure, enough, "BIG NUM" is spat out on the LCD. If I use the same check for the Array Variable after copying the data into the array, it always comes back as being < 256.

    Where is the other half of my word going??? Has anyone else seen similar behaviour? Am I doing something really stupid?

    EDIT: This is on an 18f452, using PBP 2.43
    Last edited by forgie; - 31st July 2005 at 15:33.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    We assume that...

    ADC_Buffer has been defined as a WORD variable

    and that...

    Sample_Buffer has been defined correctly as a WORD ARRAY

  3. #3
    forgie's Avatar
    forgie Guest

    Default

    <code>
    Sample_Buffer var word[n_samples] 'Array to store samples in
    Sample_Index var word
    ADC_Buffer var word 'Return value for ADC_Reading
    </code>

    This is copied straight from my sourcecode....

  4. #4
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762

    Default

    Quote Originally Posted by forgie
    This is copied straight from my sourcecode....
    Code:
    	Sample_Buffer	var word[n_samples]
    Are you really using a variable (n_samples) to specify the size of the array?
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  5. #5
    forgie's Avatar
    forgie Guest

    Default Nope, thats a constant....

    Lol I don't think PBP would compile if it were a VAR... nope, it's a constant and it's equal to 300.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    See if this works. If not, it might be due to your older version of PBP. I know
    they've made a few changes/fixes for the 18F series since v2.43.

    Tested with PIC18F452 PBP v2.46:
    Code:
    ' Lab-X1 LCD definitions
    Define  LCD_DREG        PORTD
    Define  LCD_DBIT        4
    Define  LCD_RSREG       PORTE
    Define  LCD_RSBIT       0
    Define  LCD_EREG        PORTE
    Define  LCD_EBIT        1
    Define  LCD_COMMANDUS   2000
    Define  LCD_DATAUS      50
    
    n_samples CON 300
    Sample_Buffer var word[n_samples] 'Array to store samples in
    Sample_Index var word
    ADC_Buffer var word 'Return value for ADC_Reading
    
    CLEAR
    ADCON1 = 7      ' Set PORTA and PORTE to digital
    ADC_Buffer = 1200
    PAUSE 1000
    lcdout $fe,1     ' Cls  Clear LCD screen
    
    Main:
         FOR Sample_Index = 0 to 299
           Sample_Buffer[Sample_Index] = ADC_Buffer
           ADC_Buffer = ADC_Buffer + 100
           LCDOUT "Sample: ",DEC Sample_Index," = ",DEC Sample_Buffer[Sample_Index]
           PAUSE 2000
           LCDOUT $fe,1     ' Cls  Clear LCD screen
        NEXT Sample_Index
        GOTO Main
        
        end
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    forgie's Avatar
    forgie Guest

    Default No luck

    Nope, still comes back as a byte..... oh well I may have to fork out the cash for another upgrade (it feels really wrong to pay for nothing other then a bug fix) but it's not a large amount of money. It's just annoying.

    Thanks for your time,
    Nick

  8. #8
    forgie's Avatar
    forgie Guest

    Default Upgrading not so easy

    Hmmm my day gets even better - the people who I bought PBP from a few years ago no longer sell it (Microzed in Australia) - so I'll have to somehow get an invoice from them to Mecanique to send me a copy from the other side of the world.... that will be easier said then done, since Microzed say on their website "PLEASE NO EMAILS AT THE MOMENT, WE ARE STILL WAITING FOR OUR EMAIL SERVICE TO BE CONNECTED AND EMAILS ARE NOT BEING RECEIVED." Ahhhh! I'll have to wait til tomorrow and call them, but I really need this update now, not in a week when all this will be sorted out.

    Sorry, I don't mean to bitch about PBPs update process, but its put me in a bit of a tight spot. Oh well, I guess it still beats doing it all in ASM :-)

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    Could Microzed contact Dontronics for you, verify the original order,
    and Dontronics issue your upgrade disc?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762

    Default

    Quote Originally Posted by forgie
    Lol I don't think PBP would compile if it were a VAR... nope, it's a constant and it's equal to 300.
    you said:
    This is copied straight from my sourcecode....
    and there was no
    Code:
    n_samples CON 300
    ROFL
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  11. #11
    forgie's Avatar
    forgie Guest

    Default

    Sorry, I meant that each line was copied straight from my sourcecode, to prove I hadn't made a stupid mistake, I just didn't copy the line
    <code>
    N_SAMPLES con 300 'Number of samples to be taken
    </code>
    Into my post as well: my bad. (again I don't think PBP would compile if I used an undefined word as an argument though.... would it?)

    I can't get any write/read cycle to work correctly on a word-array with PBP 2.43 - the high byte always comes back as zero.
    <code>
    sample_buffer[1] = 1000
    lcdout cmd, cls, dec sample_buffer[1]
    </code>
    This code here e.g. displays 232, which is of course the lowbyte of 1000.

    Looks like upgrade time again.... unless someone else still has PBP2.43 and CAN get a word array to work properly on an 18F core - in which case I'll have to go back and start banging my head on the desk again
    Last edited by forgie; - 1st August 2005 at 06:09.

  12. #12
    forgie's Avatar
    forgie Guest

    Default Hmmmm

    Ok, now things are getting strange: I updated to PBP 2.46 thanks to the helpful folks at Dontronics...... but was getting the same problem......

    Well, I have found out what was causing the problem: PBP doesn't like having the size of a word array defined by a constant, it seems to become a byte array if it is declared that way. It works fine if I declare it using a number and not a constant reference.....

    Thanks everyone for your time and advice, it is good to know that I hadn't done something blindingly stupid to cause this problem. I would assume that this bug wouldn't be very hard to fix.... I hope it will be fixed in 2.47 (whenever it comes out).

  13. #13
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    Strange. The example I posted above, that you said didn't work for you, uses a constant for n_samples, and it works. Tested it with V2.46 on a LAB-X1 board with the 18F452.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  14. #14
    forgie's Avatar
    forgie Guest

    Default

    That was puzzling me too, Bruce. After adjusting it to work with my current board, I realised that the difference between my code and yours was that in my program I was defining the constant AFTER defining the array (I cut and paste your code in segments, since to use my board there's a bit of initialisation code before the LCD will powerup). It is quite strange that when I defined them the way I did, the array worked, but as a byte array - Is what I was doing technically incorrect according to PBP specs?

    At least I know now that I have to define my constants before I define an array that uses a constant for it's size.

  15. #15
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    It's perfeclty legit using a constant like this. You'll see this in some of the code examples MeLabs provides.

    Here's one example from a hardware serial interrupt routine from MeLabs;

    buffer_size CON 32 ' Sets the size of the ring buffer
    buffer VAR BYTE[buffer_size]

    Just be sure to declare the constant before you reference it....;o]
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th July 2008, 23:19
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 18:04
  4. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23
  5. 16F877 RAM Question
    By Art in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th August 2005, 11:47

Members who have read this thread : 1

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

Posting Permissions

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