Just a quick note to say your suggestions worked. Thanks again. -----Original Message----- From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf Of Alex Parkinson Sent: Tuesday, March 29, 2005 9:25 AM To: Microcontroller discussion list - Public. Subject: Re: [PIC]10-bit ADC integer formatting question This is just speculation, but it sounds to me like the left shift operation (ADRESH << 8) is being performed on a single byte before being ORed and assigned to VBattNew. Shifting a byte-sized variable by eight bits will shift all of the data out of the variable, clearing it. Try the following code: VBattNew = ADRESH; VBattNew = (VBattNew << 8) | ADRESL; This should have the same result as your fix, without requiring a temp variable. It may be useful to examine the assembly code generated to see exactly what is going on. Regards, Alex Parkinson Leslie, Shawn J wrote: > I am using the PIC18F4320. I am writing in C-language and using MPLAB > C18 tool suite(compiler V2.2). I have a question about the ADC result > registers (ADRESL and ADRESH). I must use all 10 bits; therefore, I > have to do the simple task of taking the two 8-bit result registers > and combining them into a 16-bit integer. The following code is how I > did > it: > > (obviously before this I set the ADC result to be right justified and > VBattNew is defined as an unsigned int) > > (I also send the raw values out the USART before the statements below > - getting exactly what one would expect in each of the bytes) > > //Sent data out USART here > VBattNew = ADRESL; // Get low byte > //Sent data out USART here > VBattNew |= (ADRESH << 8); // Combine with the high byte into 16 > int > //Sent data out USART here > > > The problem is the high byte continually gets cleared to 0x00 - I > cannot get past 255 decimal. > > I even sent the raw data out the USART before, between and after for > inspection, and it is clear the left shift and integer assignment is > clearing the high byte? > > I tried the code below to make sure I was not trying to do much at > once with the above syntax - same problem: > > // VBattNew = ADRESL; // Get low byte > // temp = ADRESH; > // temp << 8; > // VBattNew = temp | VBattNew; > > The final fix which works is using the ADRES definition in the > p18f4320.h file - which apparently is the entire 16-bit concatenation > of the two bytes; as follows: > > VBattNew = ADRES; // This works > > But, I want to understand why my code did not work - I bet it is > surprisingly simple (as always - its one bit screwing things up). By > the way I did search the archives and found someone posted code the > same way I did it - should be a slam dunk. > > Thanks for your time guys, > Shawn > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist