Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



All about PIC microcontrollers

Within these pages, you can find many useful pieces of code mostly in assembly for the Microchip PIC micro controller family. A lot of people have spend many hours trying to put the bits and bytes together. If the code is NOT written by a member of the PCB Heaven community, then a link will be added above the code with the original website that this code was found.
Because the code is copied to our servers, you should know that:

  • The responsible web master of the website that the code is taken, has been informed and he has agreed to copy the code
  • All emails from the above contact have been kept as records but due to personal privacy cannot be shown in public.
  • The author of the code is always clearly indicated above the code. In some cases the author is unknown. If you happen to be the author of the code or you know the person who wrote it, please inform us by email and it will be added ASAP.

We would personally like to send the credits to all the people that managed to write some very interesting code and publish it, and special thanx to the people that originally hosted those code snippets and gave us the permission to copy them.


View code
Binary to BCD unpacked 16 bit to 5 digit
Author: Juan Mayoral
This code was originally located @ http://www.piclist.com


Follow this link to go back

I wrote this linear code to convert binary to bcd unpacked.
The code isn't too short (131 words), but it always takes the
same time.

aar0 = binary number low byte
aar1 = binary number high byte
aac0 = bcd number ones
aac1 = bcd number tens
aac2 = bcd number hundreds
aac3 = bcd number thousands
aac4 = bcd number ten-thousands

b16_d5
swapf aar0,w ; partial ones sum in low byte
addwf aar0,w ;
andlw 0x0f ;
skpndc ;
addlw 0x16 ;
skpndc ;
addlw 0x06 ;
addlw 0x06 ;
skpdc ;
addlw -0x06 ; wmax=3:0
;
btfsc aar0,4 ; complete ones sum in low byte
addlw 0x15+0x06
skpdc
addlw -0x06 ; wmax=4:5
movwf aac0 ; save sum in aac0
;
; 8+ 4+ 2+ 1+ 8+ 4+ 2+ 1+
; 20
; 100 60 30 15+
; ----------------------------------------------------
; 128 64 32 16 8 4 2 1
;
swapf aar1,w ; partial ones sum in high byte
addwf aar1,w ;
andlw 0x0f ;
skpndc ;
addlw 0x16 ;
skpndc ;
addlw 0x06 ;
addlw 0x06 ;
skpdc ;
addlw -0x06 ; wmax=3:0
;
btfsc aar1,0 ; complete ones sum in high byte
addlw 0x05+0x06
skpdc
addlw -0x06 ; wmax=3:5
;
btfsc aar1,4
addlw 0x15+0x06
skpdc
addlw -0x06 ; wmax=5:0
;
addlw 0x06 ; include previous sum
addwf aac0,w
skpdc
addlw -0x06 ; wmax=9:5, ones sum ended
;
movwf aac0
movwf aac1
swapf aac1,f
movlw 0x0f
andwf aac0,f ; save total ones sum in aac0
andwf aac1,f ; save partial tens sum in aac1
;
; 8+ 4+ 2+ 1+ 8+ 4+ 2+ 1+
; 5+
; 60 80 90 10+ 5+
; 700 300 100 80 40 20 10 50
; 32000 16000 8000 4000 2000 1000 500 200
; ------------------------------------------------------
; 32768 16384 8192 4096 2048 1024 512 256
;
; complete tens sum in low and high byte
rrf aar1,w ; rotate right high byte once
andlw 0x0f ; clear high nibble
addlw 0x06 ; adjust bcd
skpdc
addlw -0x06 ; wmax=1:5
;
addlw 0x06 ; include previous sum
addwf aac1,w
skpdc
addlw -0x06 ; wmax=2:4
;
btfsc aar0,5
addlw 0x03+0x06
skpdc
addlw -0x06 ; wmax=2:7
;
btfsc aar0,6
addlw 0x06+0x06
skpdc
addlw -0x06 ; wmax=3:3
;
btfsc aar0,7
addlw 0x12+0x06
skpdc
addlw -0x06 ; wmax=4:5
;
btfsc aar1,0
addlw 0x25+0x06
skpdc
addlw -0x06 ; wmax=7:0
;
btfsc aar1,5
addlw 0x09+0x06
skpdc
addlw -0x06 ; wmax=7:9
;
btfsc aar1,6
addlw 0x08+0x06
skpdc
addlw -0x06 ; wmax=8:7
;
btfsc aar1,7
addlw 0x06+0x06
skpdc
addlw -0x06 ; wmax=9:3, tens sum ended
;
movwf aac1 ; save total tens sum in aac1
swapf aac1,w
andlw 0x0f ; load partial hundreds sum in w
;
; 8+ 4+ 2+ 1+ 8+ 4+ 2+ 1+
; 20+ 5+
; 100+ 60+ 30+ 10+
; ----------------------------------------------------
; 128 64 32 16 8 4 2 1
;
; 8+ 4+ 2+ 1+ 8+ 4+ 2+ 1+
; 5+
; 60+ 80+ 90+ 10+ 5+
; 700 300 100 80+ 40+ 20+ 10+ 50+
; 32000 16000 8000 4000 2000 1000 500 200+
; ------------------------------------------------------
; 32768 16384 8192 4096 2048 1024 512 256
;
; complete hundreds sum in high byte
btfsc aar1,1
addlw 0x05+0x06
skpdc
addlw -0x06 ; wmax=1:4
;
btfsc aar1,5
addlw 0x01+0x06
skpdc
addlw -0x06 ; wmax=1:5
;
btfsc aar1,6
addlw 0x03+0x06
skpdc
addlw -0x06 ; wmax=1:8
;
btfsc aar1,7
addlw 0x07+0x06
skpdc
addlw -0x06 ; wmax=2:5, hundreds sum ended
;
movwf aac2 ; save total hundreds sum in aac2
swapf aac2,w
movwf aac3 ; save partial thousands sum in aac3
movlw 0x0f ; clear high nibble
andwf aac1,f ;
andwf aac2,f ;
andwf aac3,f ;
;
; 8+ 4+ 2+ 1+ 8+ 4+ 2+ 1+
; 5+
; 60+ 80+ 90+ 10+ 5+
; 700+ 300+ 100+ 80+ 40+ 20+ 10+ 50+
; 32000 16000 8000 4000 2000 1000 500+ 200+
; ------------------------------------------------------
; 32768 16384 8192 4096 2048 1024 512 256
;
; complete thousands sum in low and high byte
rrf aar1,w ; rotate right high byte twice
movwf aac4 ;
rrf aac4,w ;
andlw 0x0f ; clear high nibble
addlw 0x06 ; adjust bcd
skpdc ;
addlw -0x06 ; wmax=1:5
;
addlw 0x06 ; include previous sum
addwf aac3,w
skpdc
addlw -0x06 ; wmax=1:7
;
btfsc aar1,6
addlw 0x16+0x06
skpdc
addlw -0x06 ; wmax=3:3
;
btfsc aar1,7
addlw 0x32+0x06
skpdc
addlw -0x06 ; wmax=6:5, thousands sum ended
;
movwf aac3 ; save total thousands sum in aac3
movwf aac4 ;
swapf aac4,f ; save ten-thousands sum in aac4
movlw 0x0f ; clear high nibble
andwf aac3,f ;
andwf aac4,f ;

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!