Jump to content
Neil

Problems With Arras Abd Dereference!

Recommended Posts

Hi All

 

Let me start by saying sorry if this is the wrong place to post. Im new here, and to BoostC

 

Im having big problems which should be simple. Im using the evaluation version of the IDE ver 6.87. Ive tryed the following with both BoostC and C++

 

It seems that I can not get the element of an array of chars to come out of my serial port. It works with rom char*, but not char[]. It apears that using a variable between the lable[idx] to get acces to a specific char does not work (int or char as the index). It also seems that using * to dereference a char* to a char does not work either. All I seem to get is 0x00 not the char value I want!

 

Am I going mad? This seems like very basic C, but I cant index an array or use a pointer! Am I doing somthing very silly. This seems like a compiler bug, but if it was Im sure it would have jumped out by now.

 

Any and all help is welcome!!!

 

Neil

 

 

This code works!! prints eeee

 

void
sendSerial(const char* foo)
{
int cnt = 0;
char msg[] = "Neil";
char tsp;

tsp = msg[1];

while(cnt < 4)
{
	while(test_bit(txsta1, TRMT) == 0);
	txreg1 = tsp;
	cnt++;
}

return;
}

This code does NOT work send 4, 0x00's

void
sendSerial(const char* foo)
{
int cnt = 0;
char msg[] = "Neil";
char tsp;

tsp = msg[cnt];

while(cnt < 4)
{
	while(test_bit(txsta1, TRMT) == 0);
	txreg1 = tsp;
	cnt++;
}

return;
}

This also does NOT work sends 4 0x0's

void
sendSerial(const char* foo)
{
int cnt = 0;
char msg[] = "Neil";
char tsp;

tsp = *msg;

while(cnt < 4)
{
	while(test_bit(txsta1, TRMT) == 0);
	txreg1 = tsp;
	cnt++;
}

return;
}

Share this post


Link to post
Share on other sites

Neil,

 

Am I going mad? This seems like very basic C, but I cant index an array or use a pointer! Am I doing somthing very silly. This seems like a compiler bug, but if it was Im sure it would have jumped out by now.

 

I dont see a problem. The following code works for me, please try it:

#include <system.h>

main()
{
int badcnt = 0;
char tmp;	

char foo[] = "Neil";
tmp = *foo;	
if( tmp != 'N' ) 
	badcnt++;


char foo2[] = "Neil";
tmp = foo2[1];
if( tmp != 'e' ) 
	badcnt++;


char foo3[] = "Neil";
char idx = 2;
tmp = foo3[idx];
if( tmp != 'i' ) 
	badcnt++;

while( 1 );
}

 

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Dave

 

Here is the code I actuall used. Note, I changed badcnt to a char. I also added som simple serial code to output the value of badcnt. I got the result

 

112

 

showing that the first and third blocks failed!

 

So, your code also does not work. Have you any idea? Ive pasted in the build text out

 

Neil

 

 

 

 

#include <system.h>

//Set clock frequency
#pragma CLOCK_FREQ	40000000

#pragma DATA _CONFIG1H, _OSC_HSPLL_1H
#pragma DATA _CONFIG2L, _PWRT_ON_2L
#pragma DATA _CONFIG2H, _WDT_OFF_2H
//#pragma DATA _CONFIG3L, _WAIT_OFF_3L			 
#pragma DATA _CONFIG4L, _LVP_OFF_4L
#pragma DATA _CONFIG5L, _CP0_OFF_5L


void
cfg_serial()
{
spbrg1  = 0x40;						// 9600 baud
spbrgh1 = 0x00;						// 9600 baud
txsta1  = 0x00;						// 8 bit, no par, 1 stop
set_bit  (rcsta1, SPEN);			// Async enable
set_bit  (rcsta1, CREN);			// continuous recieve
set_bit  (txsta1, TXEN);			// TX enable
clear_bit(txsta1, BRGH);			// Lo speed

return;
}



main()
{
char badcnt = 0;
char tmp;	
char foo[] = "Neil";

cfg_serial();

tmp = *foo;	
if( tmp != 'N' ) 
	badcnt++;

while(test_bit(txsta1, TRMT) == 0);
txreg1 = badcnt + 0x30;


char foo2[] = "Neil";
tmp = foo2[1];
if( tmp != 'e' ) 
	badcnt++;

while(test_bit(txsta1, TRMT) == 0);
txreg1 = badcnt + 0x30;

char foo3[] = "Neil";
char idx = 2;
tmp = foo3[idx];
if( tmp != 'i' ) 
	badcnt++;


while(test_bit(txsta1, TRMT) == 0);
txreg1 = badcnt + 0x30;


while( 1 )
{
	delay_s(1);
	porth = 0xff;
	delay_s(1);
	porth = 0x00;
}
}

 

 

Building...

BoostC Optimizing C Compiler Version 6.87 (for PIC18 architecture)

http://www.sourceboost.com

Copyright© 2004-2008 Pavel Baranov

Copyright© 2004-2008 David Hobday

 

Single user Lite License (Unregistered) for 0 node(s)

Limitations: PIC18 max code size:8192 bytes, max RAM banks:2, Non commercial use only

 

 

test1.c

 

success

BoostLink Optimizing Linker Version 6.87

http://www.sourceboost.com

Copyright© 2004-2008 Pavel Baranov

Copyright© 2004-2008 David Hobday

 

 

Building CASM file

 

Memory Usage Report

===================

RAM available:3936 bytes, used:23 bytes (0.6%), free:3913 bytes (99.4%),

Heap size:489 bytes, Heap max single alloc:127 bytes

ROM available:131072 bytes, used:418 bytes (0.4%), free:130654 bytes (99.6%)

 

 

success

"C:\Program Files\SourceBoost\boostc.pic18.exe" test1.c -t PIC18F8722

"C:\Program Files\SourceBoost\boostlink.pic.exe" /ld "C:\Program Files\SourceBoost\lib" libc.pic18.lib test1.obj /t PIC18F8722 /d "C:\Program Files\SourceBoost\Samples\C\BoostC" /p foo

File 'C:\Program Files\SourceBoost\Samples\C\BoostC\foo.asm' has been reloaded

Done

Share this post


Link to post
Share on other sites

Hi

 

Following is the code I actually used. I changed badcnt to a char and added some simple serial IO to display the results. The result was

 

112

 

showing blocks 1 and 3 failed. I ran it on an actual PIC not in the debugger, which I think shows it work!!

 

I have attached the actual code I used and the build log

 

Hav you any ideas?

 

Neil

 

#include <system.h>

//Set clock frequency
#pragma CLOCK_FREQ	40000000

#pragma DATA _CONFIG1H, _OSC_HSPLL_1H
#pragma DATA _CONFIG2L, _PWRT_ON_2L
#pragma DATA _CONFIG2H, _WDT_OFF_2H
//#pragma DATA _CONFIG3L, _WAIT_OFF_3L			 
#pragma DATA _CONFIG4L, _LVP_OFF_4L
#pragma DATA _CONFIG5L, _CP0_OFF_5L


void
cfg_serial()
{
spbrg1  = 0x40;						// 9600 baud
spbrgh1 = 0x00;						// 9600 baud
txsta1  = 0x00;						// 8 bit, no par, 1 stop
set_bit  (rcsta1, SPEN);			// Async enable
set_bit  (rcsta1, CREN);			// continuous recieve
set_bit  (txsta1, TXEN);			// TX enable
clear_bit(txsta1, BRGH);			// Lo speed

return;
}



main()
{
char badcnt = 0;
char tmp;	
char foo[] = "Neil";

cfg_serial();

tmp = *foo;	
if( tmp != 'N' ) 
	badcnt++;

while(test_bit(txsta1, TRMT) == 0);
txreg1 = badcnt + 0x30;


char foo2[] = "Neil";
tmp = foo2[1];
if( tmp != 'e' ) 
	badcnt++;

while(test_bit(txsta1, TRMT) == 0);
txreg1 = badcnt + 0x30;

char foo3[] = "Neil";
char idx = 2;
tmp = foo3[idx];
if( tmp != 'i' ) 
	badcnt++;


while(test_bit(txsta1, TRMT) == 0);
txreg1 = badcnt + 0x30;


while( 1 )
{
	delay_s(1);
	porth = 0xff;
	delay_s(1);
	porth = 0x00;
}
}

 

Building...

BoostC Optimizing C Compiler Version 6.87 (for PIC18 architecture)

http://www.sourceboost.com

Copyright© 2004-2008 Pavel Baranov

Copyright© 2004-2008 David Hobday

 

Single user Lite License (Unregistered) for 0 node(s)

Limitations: PIC18 max code size:8192 bytes, max RAM banks:2, Non commercial use only

 

 

test1.c

 

success

BoostLink Optimizing Linker Version 6.87

http://www.sourceboost.com

Copyright© 2004-2008 Pavel Baranov

Copyright© 2004-2008 David Hobday

 

 

Building CASM file

 

Memory Usage Report

===================

RAM available:3936 bytes, used:23 bytes (0.6%), free:3913 bytes (99.4%),

Heap size:489 bytes, Heap max single alloc:127 bytes

ROM available:131072 bytes, used:418 bytes (0.4%), free:130654 bytes (99.6%)

 

 

success

"C:\Program Files\SourceBoost\boostc.pic18.exe" test1.c -t PIC18F8722

"C:\Program Files\SourceBoost\boostlink.pic.exe" /ld "C:\Program Files\SourceBoost\lib" libc.pic18.lib test1.obj /t PIC18F8722 /d "C:\Program Files\SourceBoost\Samples\C\BoostC" /p foo

File 'C:\Program Files\SourceBoost\Samples\C\BoostC\foo.asm' has been reloaded

Done

Share this post


Link to post
Share on other sites
Hi Dave

 

Here is the code I actuall used. Note, I changed badcnt to a char. I also added som simple serial code to output the value of badcnt. I got the result

 

112

 

showing that the first and third blocks failed!

 

So, your code also does not work. Have you any idea?

Please try the code I have supplied under the SouceBoost IDE debugger/simulator.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Dave

 

Sorry for posting twice.

 

I have tryed your code in the simulator / debugger and it works as expected. That good news, but does not help solve my problem. I need the code to work on a real PIC. As you can see my very very similar code does not work on a real device.

 

Neil

Share this post


Link to post
Share on other sites

Neil,

Hi Dave

 

Sorry for posting twice.

 

I have tryed your code in the simulator / debugger and it works as expected. That good news, but does not help solve my problem. I need the code to work on a real PIC. As you can see my very very similar code does not work on a real device.

So the compiler is generating the correct code for array indexing, so this problem is not a compiler bug :(

 

I would suggest you simplify your program, remove all the comms code. Flash you outputs at one rate if badcnt == 0 and at a faster rate if badcnt != 0.

 

Please create a new topic not in bug reports to continue this discussion.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Dave

 

I have done a simple LED flas test. The code follows. The LED flashes. This means badcnt is greater than 0 so one or more of the code blocks fails. Can you confirm this on a real PIC not a simulated one?

 

I dont know how you can say that just because your simulator works that the compiler is not faulty. This is not sound logic as it assumes the simulator is also fault free.

 

I do feel that if your compiler did not de-reference correctly then the screeming would be not just be from me, so Im assuming that Im at fault.

 

Im hoping some one can point me at where my mistakes may me. It seems so simple that I must be missing somthing

 

Neil

 

#include <system.h>

//Set clock frequency
#pragma CLOCK_FREQ	40000000

#pragma DATA _CONFIG1H, _OSC_HSPLL_1H
#pragma DATA _CONFIG2L, _PWRT_ON_2L
#pragma DATA _CONFIG2H, _WDT_OFF_2H
//#pragma DATA _CONFIG3L, _WAIT_OFF_3L			 
#pragma DATA _CONFIG4L, _LVP_OFF_4L
#pragma DATA _CONFIG5L, _CP0_OFF_5L

main()
{
//Configure ports all inputs
trisa = 0x00;
trisb = 0x00;
trisc = 0x00;
trisd = 0x00;
trise = 0x00;
trisf = 0x00;
trisg = 0x00;
trish = 0x00;
trisj = 0x00;

//Initialize ports all 0s
porta = 0x00;
portb = 0x00;
portc = 0x00;
portd = 0x00;
porte = 0x00;
portf = 0x00;
portg = 0x00;
porth = 0x00;
portj = 0x00;

char badcnt = 0;
char tmp;	
char foo[] = "Neil";

tmp = *foo;	
if( tmp != 'N' ) 
	badcnt++;


char foo2[] = "Neil";
tmp = foo2[1];
if( tmp != 'e' ) 
	badcnt++;


char foo3[] = "Neil";
char idx = 2;
tmp = foo3[idx];
if( tmp != 'i' ) 
	badcnt++;



while( badcnt != 0)
{
	delay_s(1);
	porth = 0xff;
	delay_s(1);
	porth = 0x00;
}

while( 1);
}

Share this post


Link to post
Share on other sites

Hi Dave

 

With fresh eyes I finally found the answer to my problem this morning. It has also been mentioned once before in your forum, but you have to know the answer to find it!!

 

The problem is specific to PIC18s. It has the option of an enhanced instruction set, which IS selected by default. This significantly effect the way indext addressing works.

 

The assembler code generated by Source Boost shows extensive use of these instructions to implement pointers and array indexes (which makes sence as thats what its for). So, if you have the extended instruction set enabled, then all your pointer code stops working!!!

 

The simple fix for this is to set

 

#pragma DATA _CONFIG4L, __XINST_OFF_4L

 

Which returns the PIC18 to standard (or legacy as described in PIC18F8722.h). Now all the code runs as expected.

 

So, my statement above holds true. The simulator expects all PICs to run the standard by default, which was a good idea, but is no longer true. This is NOT a complier bug, but a limitation of the simulator.

 

I dont want to point fingers and say all is bad. So far Im finding Source Boost to be very good. I do feel this is a significant limitation / assumsion on the part of the simulator. It would be good if a note on this was added to the manual or someware else, so others using PIC18s dont fall into this trap as I did.

 

Thanks for the help. I hope my comments add to your product.

 

Best regards

 

 

Neil Robinson

Share this post


Link to post
Share on other sites
Neil,
Hi Dave

 

Sorry for posting twice.

 

I have tryed your code in the simulator / debugger and it works as expected. That good news, but does not help solve my problem. I need the code to work on a real PIC. As you can see my very very similar code does not work on a real device.

So the compiler is generating the correct code for array indexing, so this problem is not a compiler bug :)

 

 

Regards

Dave

 

It IS a bug. The code that is generated does work in the simulator but not in real.

As Neil reported (Thank you Neil, this bug had cost me already a day) the problem is a configuration bit (#pragma DATA _CONFIG4L, __XINST_OFF_4L).

If this bit is not activated the code generated will not work on my PIC18F2321.

 

Please fix this bug or give a warning if the bit is not set.

 

 

Kind regards John S.

Share this post


Link to post
Share on other sites

There are several bits in the configuration bytes that are set to '0' for an erased device.

 

I kind of agree that the compiler should respect the device default values and only change the bits that have been declare in the pragma.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...