Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

geting first and second half of byte

Status
Not open for further replies.
hello,

i am using the basic compiler in PIC Simulator IDE to write a program but i need to split a variable in half. i do not know how to do this this is what i mean:

10110100 becomes 1011 and 0100

how is this done using the basic compiler
 
You'd use a bitmask for one half and shift the bits to get the other:

10110100 & 00001111 = 00000100
10110100 / 16 = 00001011
 
hello,

i am using the basic compiler in PIC Simulator IDE to write a program but i need to split a variable in half. i do not know how to do this this is what i mean:

10110100 becomes 1011 and 0100

how is this done using the basic compiler


I've got no idea using basic but with assembler I would :

MOVLW b'00001111'
ANDWF byte, w ; Now w has the first half of the byte, do with it what you will
Then,
RRF byte, f
RRF byte, f
RRF byte, f
RRF byte, w ; Now, w has the second half of the byte

Edit: Actually, will have to add at the end:

ANDLW b'00001111'

If that wasn't added then you will sometimes get the wrong numbers because of the RRF instruction taking Bit 7 from the Carry bit.
 
Last edited:
Did not think too much about, but could be that swapf could help along the process?
 
Is there a shift instruction you can use?

On the I x86 you can get the right part of the byte by shifting right by four bits.

E.g.

ax = EFABh
shr ax,4

Now the ax register will equal EFh

Sorry if this isn't helpful, I don't know much about PIC asm.
 
Is there a shift instruction you can use?


Yep, the RRF instruction I posted before is Rotate Right. Do it 4 times to have the last nibble replacing the first nibble.
 
Did not think too much about, but could be that swapf could help along the process?

SWAPF. Never used it but just looked it up and yes, it does it for you.


Now it becomes (in assembly, sorry OP):

MOVFW byte
ANDLW b'00001111' ; Now w has the first nibble

SWAPF byte, w
ANDLW b'00001111' ; Now w has the second nibble
 
Last edited:
You'd use a bitmask for one half and shift the bits to get the other:

10110100 & 00001111 = 00000100
10110100 / 16 = 00001011

This will work in most BASICs that I've used. The & will be replace with AND. lsb = 10110100 and 00001111 msb = 10110100 / 16
 
thanks for you help everyone. ill try that now and tell you the results. btw there is a function to use asm in the basic compiler so if it doesnt work i can try to use the code you have given me
 
thanks for you help everyone. ill try that now and tell you the results. btw there is a function to use asm in the basic compiler so if it doesnt work i can try to use the code you have given me

hi,
I use Oshonsoft, can I ask why you want to split this byte into nibbles.?
 
i am using a DS1307 Time RTC for a clock project and if you look at this datasheet : DS1307 and go to page 8 you will find that eg. seconds (00h) is split into seconds and 10 seconds and i need to split them up
 
i am using a DS1307 Time RTC for a clock project and if you look at this datasheet : DS1307 and go to page 8 you will find that eg. seconds (00h) is split into seconds and 10 seconds and i need to split them up

hi,
This will work ok for Oshonsoft.

Code:
unitsecs = 0x59''' test read byte from DS1307
tensecs = unitsecs And 0xf0
tensecs = ShiftRight(tensecs, 4)
tensecs = tensecs Or 0x30 'make ascii

unitsecs = unitsecs And 0x0f
unitsecs = unitsecs Or 0x30' make ascii
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top