Low byte high byte

Discussion in 'Programmer's Corner' started by camerart, Dec 23, 2015.

  1. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    Hi,

    I need to load CAP2BUFL = 0xXX CAP2BUFH = 0xXX into a program with 1800. Can someone tell me what values to enter please?

    I've guessed CAP2BUFL = 0x08 and CAP2BUFH = 0x07

    Camerart.
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,734
    4,789
    You aren't giving enough information to really answer.

    Is 1800 in decimal? In hex?

    Is CAP2BUFL the low-order byte of CAP2BUF which is, presumable, a two-byte value?

    If 1800 is in decimal, then in hex it would be 0x0708, right? That would certainly look like the high-order byte is 0x07 and the low-order byte is 0x08.
     
  3. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    Hi WB,

    From my limited information, you give the same as my guess. Yes 1800 Decimal to HEX.

    Thanks, C
     
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    You can do this, but it would make for an easier program to read, and less likely to have errors, if you declared the variable as a 16-bit quantity, and then loaded your value in a single statement.
     
  5. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    Hi John,

    I didn't know I could do that. The program was written for me, and I think it will have been done correctly, perhaps for the simulator?

    Anyway, I could see WBs suggestion was correct, once I saw the variable in the SIM.

    C.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,734
    4,789
    You may or may not be able to do that. It's worth looking into as it makes your code a lot more readable and maintainable. But many assemblers don't support stuff like that.
     
  7. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    Hi,

    Here is what it looks like in the SIM.

    I struggle with programming, so it would be difficult for me to change, especially as I can just about do something.

    C.
     
  8. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    PIC 18F Assembler allows to distinguish lower, high and upper bytes of a constant / address.
     
  9. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    Hi A,

    I can't do Assembler, I wish I was a bit younger and smarter.

    I have the answer to my question now, thanks.

    C.
     
  10. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    Come on, no despair man. Almost 69 here and still enjoying all this.

    Buena suerte in your project and Merry Christmas.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,880
    999

    You need to be more clear of exactly what you want to do before anyone can help. I have been reading this thread over and over again and I do not have a clue.

    Not sure if these are answers to your questions but"

    To get the low byte of a variable or register
    unsigned low=v & 0xff;

    to get get the high byte
    unsigned char high=(input>>8) & 0xff;


    If you just need to split 1800 you first need to determine if that number is in hex or base 10

    If it is hex then it is simple 18 is high 00 is low.

    If it is base 10 then it helps if you first convert to hex.

    1800 = 0708 in hex so 07 is high 06 is low.
     
  12. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    We've gone a bit complicated.
    I simply wanted a quick answer to my question and I understood WBahn's answer in post #2, was what I wanted,.and said, I could see it was correct in the variables in the Simulator.

    Thanks, post closed. C.
     
  13. dannyf

    Well-Known Member

    Sep 13, 2015
    1,782
    360
    The question really cannot be answered given the information you have provided.

    And I would say further that the question shouldn't have been asked - in that you really should only be concerned about what values to be stored into CAP2BUFH/L registers.

    Without knowing more about the chip in question, it is not advisable to assume CAP2BUFL is the low byte.
     
  14. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,503
    380
    hi C,
    This is a clip from the programs I posted for you, sometime ago.
    Code (Text):
    1.  
    2. CAP2BUFL = 0x0f  'POSCNT from quad signals, preload 3599
    3. CAP2BUFH = 0x0e
    4.  
    5. CAP3BUFL = 0x0f  'MAXCNT, preload with 3599 degrees
    6. CAP3BUFH = 0x0e
    7.  
    Why would you want to load 1800.?
    CAP2BUFL = 0x08
    CAP3BUFH = 0x07

    E
     
  15. camerart

    Thread Starter Active Member

    Feb 25, 2013
    517
    30
    Hi Eric,

    I'm just ironing out a timing glytch, and I didn't want pass zero. Glytch ironed out and back to normal. Plus, I've learnt something. BTW: Can it be done in 16 bit as suggested in post #4?

    Thanks HAPPY CHRISTMAS. C.
     
    Last edited: Dec 24, 2015
  16. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,503
    380
    hi,
    Oshonsoft Basic compiler will not recognise CAP2BUF as a Word address.

    You could try writing to directly to the CAP2 registers, see image, but its not really easier than using CAP2BUFL &...H.

    E
    Have a Happy Xmas and 2016.:)
     
    • A38.gif
      A38.gif
      File size:
      12.1 KB
      Views:
      3
  17. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    That was me, and I believe I did it once, but I can't find the program I did it in. The way I handled it was to set up a union placed in memory at the location of the registers I wanted to set as a 16-bit quantity, so it would look like this:

    Code (Text):
    1.  
    2.      union c2buf {
    3.      unsigned cap2_16bit;
    4.       } cap2buf @CAP2BUFL;
    5.  
    6. // You would access it like this:
    7.     cap2buf.cap2_16bit = 1500;
    8.  
    However, I just tried it using the MikroC compiler, and it wouldn't accept the <cap2buf @CAP2BUFL;> construction. I've used other compilers in the past, and perhaps it's something that some will let you get away with and others not.
     
  18. dannyf

    Well-Known Member

    Sep 13, 2015
    1,782
    360
    If the two (CAP2BUFL and CAP2BUFH) are continuous in memory and you know the endianness of your compiler, you can define CAP2BUF as a pointer to 16-bit type and reference CAP2BUF instead of CAP2BUFH/L separately.
     
  19. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    That does work, but then you're accessing the registers via a pointer, and although that's easy to do in your C code, we know that it takes extra program space and execution time. I wish there were some easy way to make a pair or registers act like a single 16-bit variable. The design of the PIC usually does make it tempting by putting them in adjacent locations.

    At least you could make it work in a way that puts the number you're working with into the code:
    Code (Text):
    1.  
    2.     CAP2BUFL = (1800 & 0xFF);
    3.     CAP2BUFH = (1800 >> 8);
    4.  
    5. // or even better, define it first, so you can change it in one place if you need to
    6.  
    7. #define CAP2LOAD 1800
    8.     CAP2BUFL = (CAP2LOAD & 0xFF);
    9.     CAP2BUFH = (CAP2LOAD >> 8);
    10.  
     
    Last edited: Dec 28, 2015
  20. dannyf

    Well-Known Member

    Sep 13, 2015
    1,782
    360
    Easy, assuming 1) adjacent memory location and 2) little endian compiler:

    Code (Text):
    1.  
    2. //approach 1
    3. //#define myCCPBUF  ((uint16_t *) &CCPBUFL)
    4. //approach 2
    5. uint16_t *myCCPBUF_ptr = (uint16_t *) &CCPBUFL;
    6. #define myCCPBUF  *myCCPBUF_ptr
    7.  
    8. //in your code
    9.   myCCPBUF = 1800;  //set CCPBUFH..L to 0x0708
    10.  
    We know that it doesn't.
     
Loading...