I thought I would share a personal milestone after programming PIC's for the past 1-1/2 years...matching Scott Dattalo for a piece of optimized code that he wrote 6 years ago on PICList:
http://www.piclist.com/techref/microchi ... b-2d8b.htm
Just a little bragadosio...
Moderators: David Barker, Jerry Messina
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
For those who would like to use this routine in a function...
For those of you interested in such things...
(a) the 'access' keyword is used to guarantee pBCD is located in access RAM - so no bank switching is ever required
(b) the function returns in WREG, thus removing the need to assign and allocate 'result'
Code: Select all
function access BCDToDec(pBCD as byte) as WREG
asm
rrcf pBCD, W ; BCD/2 in WREG
rrcf WREG, W ; BCD/2/2 in WREG
andlw 0x3C ; mask upper nibble as 4 * tens
subwf pBCD, F ; BCD = (16 * tens) - (4 * tens) = 12 * tens + ones
rrncf WREG, W ; BCD/2/2/2 in WREG = (2 * tens)
subwf pBCD, W ; BCD = (12 * tens) - (2 * tens) = 10 * tens + ones
end asm
end function
(a) the 'access' keyword is used to guarantee pBCD is located in access RAM - so no bank switching is ever required
(b) the function returns in WREG, thus removing the need to assign and allocate 'result'
Thanks Dave....I was thinking about writing a function but it was getting late. I have especially learned something new from your demonstration of code. You have created a very powerful compiler.
I was trying to think of where such a routine might be useful and I believe that the DS1307 uses a packed BCD format.
I was trying to think of where such a routine might be useful and I believe that the DS1307 uses a packed BCD format.