|
|
View previous topic :: View next topic |
Author |
Message |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Better way to return 16 bits? |
Posted: Fri Jun 17, 2011 8:45 am |
|
|
Is there any better way to do this in a 18F part (PCH compiler):
Code: | unsigned int16 ReadEEdata16(int addr)
{
EEADR = addr;
EECON1.EEPGD = 0;
EECON1.CFGS = 0;
EECON1.RD = 1;
_RETURN_ = EEDATA; //..low byte..
EEADR++;
EECON1.RD = 1; //..read high byte..
#byte ReturnHighByte = _RETURN_+1
ReturnHighByte = EEDATA; //..high byte
} |
This compiles very efficiently, but I don't like having to mess with the compiler's internal _RETURN_ register, in case future implementations of the PCH compiler handle 16-bit returns with different symbols. I tried various ways of forming expressions like:
Code: | return (((unsigned int16)EEDATA)<<8)+k; |
where k was saved in an earlier step, but they all compiled with many more machine instructions. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Better way to return 16 bits? |
Posted: Mon Jun 27, 2011 5:31 am |
|
|
Well, since on one has any comments on this question, perhaps I could revise the question a little. What are the precautions, if any, of using the _RETURN_ and _RETURN_+1 compiler pseudo registers to form a 16-bit return value, as opposed to doing a normal C-language "return"? I know that the compiler gives a warning that my function is not void and does not return a value (because it does not see that I have done the equivalent thing by setting _RETURN_ and _RETURN_+1 explicitly). But beyond that, is there any risk in writing code this way? For example, is the compiler likely to change in a way that invalidates my use of this symbol? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1346
|
|
Posted: Mon Jun 27, 2011 6:59 am |
|
|
Did you try the make16() function/macro? It is usually pretty efficient in most of my programs. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 27, 2011 11:26 am |
|
|
I didn't respond because you didn't provide the PIC or the #byte and #bit
statements. If you had provided a small compilable program it would
have been easy to drop it in MPLAB and look at it. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Mon Jun 27, 2011 7:30 pm |
|
|
jeremiah wrote: | Did you try the make16() function/macro? It is usually pretty efficient in most of my programs. |
Ah, yes, that was a lot better than the various expressions I had tried. And even though it is just a tiny bit less efficient than my _RETURN_ method, I think I am going to go with your suggestion just because it avoids that "no return value" warning and is not open to compiler changes regarding the _RETURN_ symbol. For the record, here are both of them. (The part is a 18F2553, and the SFRs are the normal ones dealing with access to the EE data.)
Code: | unsigned int16 ReadEEdata16_myWay(unsigned int addr)
{
0296 C0A5 MOVFF 0xa5, 0xfa9 EEADR = addr;
029A 9EA6 BCF 0xfa6, 0x7, ACCESS EECON1.EEPGD = 0;
029C 9CA6 BCF 0xfa6, 0x6, ACCESS EECON1.CFGS = 0;
029E 80A6 BSF 0xfa6, 0, ACCESS EECON1.RD = 1;
02A0 CFA8 MOVFF 0xfa8, 0x1 _RETURN_ = EEDATA;
02A4 2AA9 INCF 0xfa9, F, ACCESS EEADR++;
02A6 80A6 BSF 0xfa6, 0, ACCESS EECON1.RD = 1;
#byte ReturnHighByte = _RETURN_+1
02A8 CFA8 MOVFF 0xfa8, 0x2 ReturnHighByte = EEDATA;
}
02AC 0012 RETURN 0 |
Code: | unsigned int16 ReadEEdata16_make16Way(unsigned int addr)
{
int k;
02AE C0A5 MOVFF 0xa5, 0xfa9 EEADR = addr;
02B2 9EA6 BCF 0xfa6, 0x7, ACCESS EECON1.EEPGD = 0;
02B4 9CA6 BCF 0xfa6, 0x6, ACCESS EECON1.CFGS = 0;
02B6 80A6 BSF 0xfa6, 0, ACCESS EECON1.RD = 1;
02B8 CFA8 MOVFF 0xfa8, 0xa6 k = EEDATA;
02BC 2AA9 INCF 0xfa9, F, ACCESS EEADR++
02BE 80A6 BSF 0xfa6, 0, ACCESS EECON1.RD = 1;
02C0 CFA8 MOVFF 0xfa8, 0x3 return make16(EEDATA, k);
02C4 C0A6 MOVFF 0xa6, 0x1
02C8 CFA8 MOVFF 0xfa8, 0x2
}
02CC 0012 RETURN 0 |
_________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
|
|
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
|