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
10bit to BCD unpacked 4 digit in 38 instructions
Author: E. A. Grens
This code was originally located @ http://www.piclist.com


Follow this link to go back

;======= Bin2Dec10G.asm ============= Sep 04 =========
; Test of 10b binary to 4 digit conversion as
; based on division by powers of 10.
;
; E. A., Grens
;
; For most PICs - here for 16F628 with 4 MHz
; internal oscillator.
;
; Average processor cycles for execution of
; subroutine over the input range 0x000 to 0x3FF
; is 88. No variables required except the
; input blh, blo and the out BCD digits.
; Subroutine length 38.
;======================================================

list p=16f628
radix dec
#include
__config 0x3D30


;-------Set Up RAM Variables---------------------------
blo equ 0x20 ;lsbyte of 10b binary
bhi equ 0x21 ;msb
d4 equ 0x23 ;msdigit
d3 equ 0x24
d2 equ 0x25
d1 equ 0x26 ;lsd
;-------Program Code--------------------------------

org 0x00 ;Effective Reset Vector
;
nop
goto Start ;Jump by interrupt
;
Start
movlw 0x07
movwf CMCON ;Digital I/O
movlw 0x03 ;Test input
movwf bhi
movlw 0xFF
movwf blo
movlw 0x04
call Bin2decg
Hold
goto Hold ;Finished

Bin2decg ;10b binaey to BCD
clrf d1
clrf d2
clrf d3
clrf d4
clrc
rrf bhi,f ;N = 4*Q + R
rrf blo,f ;blo = Q
rrf d1,f ;d1 = R as temp, R25
goto B2d2 ;repeat
B2d3
addwf blo,f ;get remainder
; after /100, C = 0
rlf d1,f ;*4 and add R back in
rlf blo,f
rlf d1,f
rlf blo,f ;4*blo + R = N -
; 1000*d4 - 100*d3
movlw 10
B2d4
subwf blo,f ;blo = N - 1000*d4 -
; 100*d3 - 10*d2
skpc
goto B2d5 ;blo10
goto B2d4
B2d5
addwf blo,f ;put last 10 back, C=0
movf blo,w
movwf d1
return

end


**********************

12-bit code

Message ; Bonus 12-Bit version

; Leaner and Meaner, 36 instructions
; Ideal for displaying A/D values
Bin2DecFast
movf NUMHI,w
iorlw 0xF0 ;w=H2-16
movwf D1 ;D1=H2-16
addwf D1,f ;D1=H2*2-32
addwf D1,f ;D1=H2*3-48
movwf D2 ;D2=H2-16
addlw -D'5' ;w=H2-21
addwf D2,f ;D2=H2*2-37 Done!
addlw D'41' ;w=H2+20
movwf D0 ;D0=H2+20

swapf NUMLO,w
iorlw 0xF0 ;w=H1-16
addwf D1,f ;D1=H2*3+H1-64
addwf D0,f ;D0=H2+H1+4, C=1
rlf D0,f ;D0=(H2+H1)*2+9, C=0
comf D0,f ;D0=-(H2+H1)*2-10
rlf D0,f ;D0=-(H2+H1)*4-20

movf NUMLO,w
andlw 0x0F ;w=H0
addwf D0,f ;D0=H0-(H2+H1)*4-20 Done!
rlf D1,f ;C=0, D1=H2*6+H1*2-128 Done!

movlw D'5'
movwf D3

movlw D'10'
mod0
addwf D0,f ;D(X)=D(X)mod10
decf D1,f ;D(X+1)=D(X+1)+D(X)div10
skpc
goto mod0
mod1
addwf D1,f
decf D2,f
skpc
goto mod1
mod2
addwf D2,f
decf D3,f
skpc
goto mod2

return

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!