|
|
View previous topic :: View next topic |
Author |
Message |
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Sanity Check: Pointers |
Posted: Wed Sep 16, 2009 4:09 pm |
|
|
Ok, this isn't working and from so many other examples, I can't seem to fish out why (other than a bug)
I have a function:
Code: | unsigned int rx_fifo_access (unsigned int *mode, char *c, unsigned int channel=0, unsigned int len=1 ) {
|
and inside, I set c with:
Code: | c = uart_rx_fifo[channel].buffer[out]; |
I've run the debugger and see this is fetching and assigning c with the right value.
upon exit of the function, the value of C is not passed back.
I'm calling it like this:
Code: | rx_fifo_access(READ, c, addr); |
(I've also tried rx_fifo_access(READ, &c, addr) )
where c is declared as unsigned int c;
Now, I've determined that it's because I'm set in the code to
#device *=16
when I debug I notice that my pointer switches from an 8bit char to a 16bit int and then just dies on the return.
If I make my passed parm a 16bit variable, then it works.
So is this a bug?
I'm using PCH 4.099 _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 16, 2009 4:22 pm |
|
|
Post a short, complete, compilable example program so we can test it. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed Sep 16, 2009 4:37 pm |
|
|
Here's what I'm running it on.. .adjust to your own as you like except to stay in the 18F family (to keep using PCH)
Code: | #include <18F97J60.h>
#device *=16 ICD=TRUE
#use delay(clock=41666700)
#fuses H4_SW
#fuses NOWDT,NOPROTECT,NODEBUG,NOIESO,WDT32768
#fuses NOFCMEN // change this eventually to FCMEN
#opt 10
#use rs232(UART1, STREAM=USER, baud=115200, ERRORS)
void pointer_test ( char * c ) {
c = 32;
}
void main (void) {
char c;
pointer_test(c);
fprintf(USER, "%c", c);
while (1);
} |
I'm feel I'm just not doing this right... _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed Sep 16, 2009 4:55 pm |
|
|
Nevermind, I think I got it.
This works:
Code: |
#include <18F97J60.h>
#device *=16
#use delay(clock=41666700)
#fuses H4_SW
#fuses NOWDT,NOPROTECT,NODEBUG,NOIESO,WDT32768
#fuses NOFCMEN // change this eventually to FCMEN
#opt 10
#use rs232(UART1, STREAM=USER, baud=115200, ERRORS)
void pointer_test ( char *c ) {
int x=0;
*c = 32;
x = 1; // this is just so I can step through MPLAB's Debug mode.
}
void main (void) {
char c=0;
pointer_test(&c);
fprintf(USER, "%c", c);
while (1);
} |
If you're not used to passing around pointers... and don't do it for a while... it gets messy.
-ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|