View previous topic :: View next topic |
Author |
Message |
nalixis
Joined: 08 Jan 2005 Posts: 6 Location: Belfort, France
|
8 bit shift error |
Posted: Fri Dec 08, 2006 5:34 am |
|
|
hey all,
i'm having some trouble with a piece of code that seemed to me quite simple at first...but i just get the wrong result
I'm probably doing something wrong but i can't figure what, if you have any suggestion..
the goal is just to "concatenate" 2 bytes into one 16bit variable:
Code: | value = (sample[1]<<8) | sample[0]; |
RESULT: the lowest byte is OK; the highest is ALWAYS zero.
here's what i've tried so far:
Code: |
signed int16 s16CodeCH0;
unsigned int8 u8Sample[4];
s16CodeCH1 = ((int16)(u8Sample[1]<<8) | u8Sample[0]);
|
Code: |
signed int16 s16CodeCH0;
unsigned int8 u8Sample[4];
int8 *temp1, *temp2;
temp1 = &s16CodeCH1;
temp2 = temp1 + 1;
*temp1 = u8Sample[0];
*temp2 = u8Sample[1];
|
Thanks for your help !
Last edited by nalixis on Fri Dec 08, 2006 9:21 am; edited 2 times in total |
|
![](templates/subSilver/images/spacer.gif) |
davekelly
Joined: 04 Oct 2006 Posts: 53 Location: Berkshire, England
|
|
Posted: Fri Dec 08, 2006 5:46 am |
|
|
Just use the make16 macro:
int16 = make16 (high, low); |
|
![](templates/subSilver/images/spacer.gif) |
nalixis
Joined: 08 Jan 2005 Posts: 6 Location: Belfort, France
|
|
Posted: Fri Dec 08, 2006 5:58 am |
|
|
thanks davekelly
unfortunately it doesn't work better...
i'm checking the generated code by sending it through rs232:
Code: |
s16CodeCH1 = make16(u8Sample[1],u8Sample[0]);
fprintf(COM_1,"%02X%02X = %04X\t",u8Sample[1],u8Sample[0],s16CodeCH1);
|
RESULT: FBFF = 00FF
any idea ?? |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Fri Dec 08, 2006 8:40 am |
|
|
Make16 works
This code works
Is your result((s16CodeCH1 )) an int16?
Code: | #define VER_MINOR 19
#define VER_MAJOR 'E'
#include <16F877A.h>
#device *=16
#fuses hs,nowdt,noprotect,nolvp,put
#use delay(clock=11059200)
#use rs232(baud=19200,xmit=PIN_E2,invert,stream=debug,disable_ints)
#case
#zero_ram
int8 t_buffer[4];
int16 result;
//=== MAIN ===//
void main(void)
{
fprintf(DEBUG,"Ver. %C_%03u.c\n\r",VER_MAJOR,VER_MINOR);
t_buffer[0]=0xCD;
t_buffer[1]=0xAB;
printf("t_buffer[0]=0x%2X\n\r",t_buffer[0]);
printf("t_buffer[1]=0x%2X\n\r",t_buffer[1]);
result=make16(t_buffer[1],t_buffer[0]);
printf("result=0x%4lX\n\r",result);
while(1);
}
|
Quote: | Ver. E_019.c
t_buffer[0]=0xCD
t_buffer[1]=0xAB
result=0xABCD
|
|
|
![](templates/subSilver/images/spacer.gif) |
nalixis
Joined: 08 Jan 2005 Posts: 6 Location: Belfort, France
|
|
Posted: Fri Dec 08, 2006 9:11 am |
|
|
thanks treitmey
i think i found the error:
#device *=16 was missing in my code...
i haven't tried yet but i'm pretty sure that was the problem !
thanks for posting your code
ciao |
|
![](templates/subSilver/images/spacer.gif) |
nalixis
Joined: 08 Jan 2005 Posts: 6 Location: Belfort, France
|
|
Posted: Fri Dec 08, 2006 9:25 am |
|
|
sorry folks,
i think i went a little too fast here...
actually, the *=16 doesn't change anything wich is strange because it should...i guess..
i'm kind of lost. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Dec 08, 2006 10:49 am |
|
|
Have you tried Treitmey's program as posted?
Change %04X to %04LX. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 08, 2006 10:52 am |
|
|
*=16 is irrelevant to your problem. It only enables the use of
RAM at addresses of 0x100 and above in 16-series PICs (16F877, etc.).
Download the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look in the section on printf() on page 186 (page 196 in the Acrobat
reader). There is a list of format specifiers. You want to display
a 16-bit value. Which format specifier should you use to do that ?
The information on that page will tell you, and it will solve your problem. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Dec 08, 2006 11:04 am |
|
|
You need to cast the sample byte, into an int16 _before_ you shift it. The problem is that you are shifting an 8bit value, in an 8bit register, so the data just disappears of the end. Your cast then converts this to an int16, but the damage is already done.
s16CodeCH1 = ((((int16)u8Sample[1])<<8) | u8Sample[0]);
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|