|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
16 bit pointer write problem |
Posted: Tue Oct 10, 2006 9:47 am |
|
|
Hello,
Just trying to write a 16 bit value from a called function via a pointer. I have defined *=16 to use 16 bit pointers. It appears that only the lower 8 bits are being written when using the following code. The upper byte is left unchanged in program memory. What do i need to do to write the full 16 bits??
Thank you!
Code: |
#include <16F877>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT,NOPROTECT,NOWRT,NOCPD
#use delay(clock=20000000)
void point_back_long(a,b,c,d){
delay_cycles(1);
*a=4;
*b=65535;
*c=65535;
*d=65535;
}
void main(){
int16 a=3,b=256+3,c=512+3,d=1024+3,e=65535;
delay_ms(50);
#ifdef use_lcd
lcd_init();
#endif
//printf(lcd_putc,"\fa before: %lu",a);
delay_cycles(1);
point_back_long(&a,&b,&c,&d,&e);
delay_cycles(1);
//printf(lcd_putc,"\na after: %lu",a);
}
|
|
|
|
Guest
|
|
Posted: Tue Oct 10, 2006 10:33 am |
|
|
To simplify the question, Can i write a 16 bit value value using a pointer? If so how?
Thanks |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Oct 10, 2006 12:01 pm |
|
|
I'm surprised your code compiles without error (but the CCS compiler is known not to be very strict).
I didn't test the result, but at least you should fix the following:
Change Code: | void point_back_long(a,b,c,d){ |
to Code: | void point_back_long(int16 *a, *b, *c, *d){ |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 10, 2006 12:12 pm |
|
|
All of the parameters have to be declared as int16* (not just the 1st one).
Example:
Code: |
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void point_back_long(int16 *a, int16 *b, int16 *c, int16 *d)
{
*a = 4;
*b = 0xFFFF;
*c = 0xFFFF;
*d = 0xFFFF;
}
//=========================
void main()
{
int16 a = 3;
int16 b = 0x103;
int16 c = 0x203;
int16 d = 0x403;
printf("Before:\n\r");
printf("a = %LX\n\r", a);
printf("b = %LX\n\r", b);
printf("c = %LX\n\r", c);
printf("d = %LX\n\r", d);
printf("\n\r");
point_back_long(&a,&b,&c,&d);
printf("After:\n\r");
printf("a = %LX\n\r", a);
printf("b = %LX\n\r", b);
printf("c = %LX\n\r", c);
printf("d = %LX\n\r", d);
while(1);
} |
The output of the program is:
Quote: |
Before:
a = 0003
b = 0103
c = 0203
d = 0403
After:
a = 0004
b = FFFF
c = FFFF
d = FFFF |
|
|
|
Guest
|
|
Posted: Tue Oct 10, 2006 12:21 pm |
|
|
Thank you very much for the feedback and the time in running the program. That did the trick. As always you rock, PCM! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Oct 10, 2006 1:20 pm |
|
|
PCM programmer wrote: | All of the parameters have to be declared as int16* (not just the 1st one). | You are right PCM. I've been programming to long in Delphi and was confused by classic C where we wrote Code: | void point_back_long(a, b, c, d) int16 *a, *b, *c, *d; |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 10, 2006 11:00 pm |
|
|
Hi ckielstra,
I wasn't trying to upstage you. I had spent a few minutes
making that test program when I noticed that you had already
replied. I felt like since I put in so much time on it, I might
as well post it. |
|
|
|
|
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
|