Low byte high byte

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
 

WBahn

Joined Mar 31, 2012
29,979
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.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
Hi WB,

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

Thanks, C
 

John P

Joined Oct 14, 2008
2,025
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.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
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.
 

WBahn

Joined Mar 31, 2012
29,979
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.
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.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.

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.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
 

Attachments

dannyf

Joined Sep 13, 2015
2,197
I need to load CAP2BUFL = 0xXX CAP2BUFH = 0xXX into a program with 1800. Can someone tell me what values to enter please?
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.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi C,
This is a clip from the programs I posted for you, sometime ago.
Code:
CAP2BUFL = 0x0f  'POSCNT from quad signals, preload 3599
CAP2BUFH = 0x0e

CAP3BUFL = 0x0f  'MAXCNT, preload with 3599 degrees
CAP3BUFH = 0x0e
Why would you want to load 1800.?
CAP2BUFL = 0x08
CAP3BUFH = 0x07

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi C,
This is a clip from the programs I posted for you, sometime ago.
Code:
CAP2BUFL = 0x0f  'POSCNT from quad signals, preload 3599
CAP2BUFH = 0x0e

CAP3BUFL = 0x0f  'MAXCNT, preload with 3599 degrees
CAP3BUFH = 0x0e
Why would you want to load 1800.?
CAP2BUFL = 0x08
CAP3BUFH = 0x07

E
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:

ericgibbs

Joined Jan 29, 2010
18,766
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.:)
 

Attachments

John P

Joined Oct 14, 2008
2,025
BTW: Can it be done in 16 bit as suggested in post #4?
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:
     union c2buf {
     unsigned cap2_16bit;
      } cap2buf @CAP2BUFL;

// You would access it like this:
    cap2buf.cap2_16bit = 1500;
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.
 

dannyf

Joined Sep 13, 2015
2,197
it wouldn't accept the <cap2buf @CAP2BUFL;> construction.
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.
 

John P

Joined Oct 14, 2008
2,025
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:
    CAP2BUFL = (1800 & 0xFF);
    CAP2BUFH = (1800 >> 8);

// or even better, define it first, so you can change it in one place if you need to

#define CAP2LOAD 1800
    CAP2BUFL = (CAP2LOAD & 0xFF);
    CAP2BUFH = (CAP2LOAD >> 8);
 
Last edited:

dannyf

Joined Sep 13, 2015
2,197
I wish there were some easy way to make a pair or registers act like a single 16-bit variable.
Easy, assuming 1) adjacent memory location and 2) little endian compiler:

Code:
//approach 1
//#define myCCPBUF  ((uint16_t *) &CCPBUFL)
//approach 2
uint16_t *myCCPBUF_ptr = (uint16_t *) &CCPBUFL;
#define myCCPBUF  *myCCPBUF_ptr

//in your code
  myCCPBUF = 1800;  //set CCPBUFH..L to 0x0708
we know that it takes extra program space and execution time.
We know that it doesn't.
 
Top