PDA

View Full Version : Word array behaving like byte array.... wierd



forgie
- 31st July 2005, 15:28
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

Melanie
- 31st July 2005, 15:35
We assume that...

ADC_Buffer has been defined as a WORD variable

and that...

Sample_Buffer has been defined correctly as a WORD ARRAY

forgie
- 31st July 2005, 15:37
<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....

NavMicroSystems
- 31st July 2005, 16:43
This is copied straight from my sourcecode....



Sample_Buffer var word[n_samples]


Are you really using a variable (n_samples) to specify the size of the array?

forgie
- 31st July 2005, 17:08
Lol I don't think PBP would compile if it were a VAR... nope, it's a constant and it's equal to 300.

Bruce
- 31st July 2005, 18:16
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:

' 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

forgie
- 31st July 2005, 18:29
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

forgie
- 31st July 2005, 18:43
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 :-)

Bruce
- 31st July 2005, 19:06
Could Microzed contact Dontronics for you, verify the original order,
and Dontronics issue your upgrade disc?

NavMicroSystems
- 31st July 2005, 22:27
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


n_samples CON 300


ROFL

forgie
- 1st August 2005, 05:44
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 :)

forgie
- 2nd August 2005, 07:59
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).

Bruce
- 2nd August 2005, 14:31
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.

forgie
- 2nd August 2005, 14:59
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.

Bruce
- 2nd August 2005, 15:43
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]