Bin10bitBCD Code

Дорогие друзья, Привет!
Алгоритм на предыдущей записи реализован в программный  код, MpLab v.4.05 и встроенный ассемблер MPassemb 5.76  под эту версию. Это очень важно.
Текст — кода — ниже, комментарии — на английском… Контроллер  Pic16F(L)1855. Код без изменений ляжет под 76 контроллер — 40 ног, тот же выпуск по дата_шит. Config Word — Debud mode. Закомментированные строки — отмечены впереди (;) — точкой с запятой. — они из программы Микрочип — AN526  с косяками… — Это был сложный проект, самое актуальное преобразование в измерениях аналогового мира…  Мои комментарии на английском — взбодрят продвинутых юзеров, а также их родителей ( песенка из м-ф Еролаш — Мальчишки и Девчонки, а также их родители…))). Счастья, друзья!!!

Итак, код из  Notepad++:

; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR

;RES_VECT CODE 0x0000 ; processor reset vector
; GOTO START ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

;MAIN_PROG CODE ; let linker place main program

;START

; GOTO $ ; loop forever

list p = 16f18855
#include p16f18855.inc

; CONFIG1 for p16f1509
; __config 0x1A4
; __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
; CONFIG2
; __config 0x19FF
; __CONFIG _CONFIG2, _WRT_OFF & _STVREN_OFF & _BORV_HI & _LPBOR_OFF & _LVP_OFF

; CONFIG1
; __config 0xD7EC
__CONFIG _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT1 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_OFF
; CONFIG2
; __config 0xF7FE
__CONFIG _CONFIG2, _DEBUG_ON & _MCLRE_OFF & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON
; CONFIG3
; __config 0xC89F
__CONFIG _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_OFF & _WDTCWS_WDTCWS_0 & _WDTCCS_HFINTOSC
; CONFIG4
; __config 0xDFFF
__CONFIG _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
; CONFIG5
; __config 0xFFFF
__CONFIG _CONFIG5, _CP_OFF & _CPD_OFF

UDATA 0x20;Specify RAM
Temp_N res 1; Counter loop
delcntr2 res 1
delcntr3 res 1; counter for 1,625 sec delay
d1cc res 1; RA0? digit common anode drives
d2cc res 1; RA1
d3cc res 1; RA2
dc1 res 1;binary d1cc
dc2 res 1;binary d2cc
dc3 res 1;binary d3cc
;
;BINARY TO BCD
CountB res 1;
temp res 1;equ 17
;
H_byte res 1; equ 10
L_byte res 1; equ 11
R0 res 1; equ 12 ; RAM Assignments
R1 res 1; equ 13
R2 res 1; equ 14
;Port B
;d4cc res 1; set 0
; Block termo variables
PointerB res 1; 0x26 address
DGcnt res 1;
BIN_TEMP res 1;
;HEX_TEMP res 1;
DECM_TEMP RES 1;
; Block ADC variables
PhaseL res 1;
PhaseH res 1;
Count res 1; counter delay for Tacq
;section of Hex to Decimal convertion
NUME res 1 ;EQU 0x40;15 ; Memory Cell for temp divident prev m.cell for PIC18
QU res 1 ;EQU 0x41;20 ; Memory Cell for quotient
;RMMD_L res 1;EQU 0x42 Low digit -d3cc
;RMMD_M res 1;EQU 0x43 Middle digit -d2cc
;RMMD_H res 1;EQU 0x44 High digit — d1cc
;MYNUM res 2;EQU 0xFD Turget number in hex. BIN_TEMP// FDh = 253 (decimal)
MYDEN res 1;EQU D’10’ ; 253/10 My divider is 10

; code start from here

org 0x0000 ; processor reset vector
nop
nop
nop
GOTO START ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED
nop
nop
nop
;org 0x0004
Interrupt CODE; 0x04

nop
nop
MAIN_PROG CODE ; let linker place main program
;START

;BANKSEL OPTION_REG
; movlw B’01000001′

; p.153 opt_reg//b.7 weak-up DSBL//enbl;b.6 itrp on rising
;b.5 Intern instr cycle Fosc/4; b.2-0 Prsc 1/4;
; movwf OPTION_REG;

banksel STATUS
movlw B’00011000′
movwf STATUS
; PORTA
BANKSEL PORTA
CLRF PORTA
BANKSEL LATA
CLRF LATA

BANKSEL ANSELA
CLRF ANSELA
;movlw B’00000000′; AN4 unimplements 1// 1- input
;movwf ANSELA
BANKSEL TRISA
;CLRF TRISA
movlw B’00001000′ ;set «0» port bits outp, b4 — input
movwf TRISA
;BANKSEL WPUA
;BCF WPUA,4

; LIST P = 16C54, n = 66
;
;********************************************************************
; Binary To BCD Conversion Routine
; This routine converts a 16 Bit binary Number to a 5 Digit
; BCD Number. This routine is useful since PIC16C55 & PIC16C57
; have two 8 bit ports and one 4 bit port ( total of 5 BCD digits)
;
; The 16 bit binary number is input in locations H_byte and
; L_byte with the high byte in H_byte.
; The 5 digit BCD number is returned in R0, R1 and R2 with R0
; containing the MSD in its right most nibble.
;
; Performance :
; Program Memory : 35
; Clock Cycles : 885
;
;
; Program: B16TOBCD.ASM
; Revision Date:
; 1-13-97 Compatibility with MPASMWIN 1.40
;
;*******************************************************************;
;
;count equ 16
;temp equ 17
;
;H_byte equ 10
;L_byte equ 11
;R0 equ 12 ; RAM Assignments
;R1 equ 13
;R2 equ 14
;
; include «p16c5x.inc»
;
B2_BCD bcf STATUS,0 ; clear the carry bit
movlw .10
movwf CountB
clrf R0
clrf R1
clrf R2

movlw 0xA0;
movwf FSR0; open indeirect bank 0xA0,0xA1,0xA2
movlw 0x0F
movwf INDF0 ; Data inR0 — cell N0
;CLRF INDF0
INCF FSR0
movlw 0x10
movwf INDF0 ;FSR0 Data in R1 — ctll N1
;CLRF INDF0
INCF FSR0
movlw 0x11
movwf INDF0 ; Data in R2- cell N2

Loop10 rlf H_byte,1
btfsc STATUS,C
incf R0,1;+1 MSD
;lslf L_byte; shift left B’11011100′ after B’10111000′

;STEP N 2 for R0
lslf R0,1;
rlf H_byte,1
btfsc STATUS,C
incf R0,1; +1 MSD

;STEP N3-0 for R0=B’0000 0101′ L_byte=B’1011 1000′
; Low byte
rlf R0,1;
rlf L_byte,1; shift left low byte — after B’0111 0000′
btfsc STATUS,C
incf R0,1
;Check low L_tetrada R0 for =>5

;STEP N 3-1
; rlf R0,1

movlw 3
btfsc R0,2
addwf R0,1 ; if L_T=5 of R0 then R0+3
; R0= B’1000′
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxx—check farther code — !!!!!!!!!!!!!!

;STEP N4 — after Step n3 R0=B’0000 1000′ L_byte=B’1011 1000′
;R0=B’0001 0001′; L_byte=B’0111 0000′

rlf R0,1;
rlf L_byte,1; shift left low byte — after B’0111 0000′
btfsc STATUS,C
incf R0,1

;STEP N5 After Stp R0=B’0010 0010′ L_byte B’1110 0000′

lslf R0,1;
rlf L_byte,1; shift left L_byte — after B’0111 0000′
btfsc STATUS,C
incf R0,1

; STEP N6(0) After step R0=B’0100 0101′ L_byte B’1100 0000′
rlf R0,1;
rlf L_byte,1; shift left low byte — after B’0111 0000′
btfsc STATUS,C
incf R0,1 ; code — right — o.k.

;STEP N6(1) Adding to L_byte (B’0101′) + 3 (B’0011′)
;After step R0=B’0100 1000′ L_byte B’1100 0000′

movlw 3
btfsc R0,2
addwf R0,1 ; if L_T=5 of R0 then R0+3
; R0= B’1000′

;STEP N7 After Stp R0=B’1001 0001′ L_byte B’1000 0000′
rlf R0,1;
rlf L_byte,1; shift left low byte — after B’1000 0000′
btfsc STATUS,C
incf R0,1 ; code — right — o.k.

;STEP N7-1 Add Hx30=B’0011 0000′ to R0 B’1001 0001′
;After Stp R0=B’1100 0001′ L_byte B’1000 0000′ — the same
movlw 30
btfsc R0,7
addwf R0,1 ; if L_T=5 of R0 then R0+3
; R0= B’1000′

;STEP N8 Add R1 to calculation; shiflt left L_byte
;control R0 — bit7 (btfsc R0,7) — if b7=1, then incrf R1
; farther shifting only R0 and R1

rlf R0 ; we shall lost oupper bit?
btfsc STATUS,C
incf R1,1
rlf L_byte,1
btfsc STATUS,0
incf R0,1 ; B’1100 0010′
;rlf R0,1 ;b’1000 0101′ -low tetrada uncorrect
;btfsc R0,7
;incf R1,1 ; if L_T=5 of R0 then R0+3
; R0= B’1000′ R1 B’0001′

;STEP N8 Add Hx30 to R0=B’1011 0011′ — after stepN8
movlw 30
btfsc R0,7
addwf R0,1 ; if L_T=5 of R0 then R0+3
; R0= B’1000′

;STEP N9 After step R0=B’0110 0110′ R1=B’0000 0011′
rlf R1,1;
rlf R0,1; shift left low byte — after B’1000 0000′
btfsc STATUS,C
incf R1,1 ; code — right — o.k.?

;next step — add Hx33
;STEP N9-1 After Step R0=B’1001 1001′ r1 — the same b’0011′
movlw 33
;!!!!!
btfss R0,7; check msb for Zerro, if Z then add Hx33
addwf R0,1 ; if L_T=5 of R0 then R0+3
; R0= B’1001 1001′

;last Step
;Step N10
rlf R1,1;
rlf R0,1; shift left low byte — after B’1000 0000′
btfsc STATUS,C
incf R1,1 ; code — right — o.k.?

nop; CORRECT — ALL RIGHT — then next sbrt will send data to DEC to 7si
; Result BDC = 7 B’0111′ (R1) BCD 3 = B’0011′ BCD 2 = B’0010’== D732
; Binary B’0111′ =Hx07 and B’0010 0010′ = Hx32; We shall mask upper and then low
; tetrades for separate 32 to digits 3 and 2
return ; go back to main — test BCD
;xxxxxxxxxx — STOP — STOP — STOP -xxxxxxxxxxxxxxxxxx
;xxxxxxxxxx chec code uper — code below — has mistake AN 526
;xxxxxxxxxx it`s saved for trainning of indirect addressin to save and load
;data through RGP memory — linear part See <Window> -> (Pic Memory views) ->
;-> Liniear Data. Can You make my programm 10bit B2BCD in FSR ( indirect) mode?
;Thank You for Join !!!))) //date 04.09.21 msc 15:24
lslf H_byte, 1;F
rlf R2, 1;0;1;F; where stored result? F or W ?
rlf R1, 1;0;1;F
rlf R0, 1;0;1;F

decfsz CountB, F
goto adjDEC
RETLW 0
;
adjDEC movlw 0xA0;- first bank /R2
MOVWF FSR0;movwi ++FSR0; load new address bank 0xA3 =++FSR0 i ++FSR0;INDF0;FSR0;movwf FSR
movfw R2
movwf INDF0
;movwf temp
;movwf ++FSR0L;INDF0; memory cell A2???
;movf INDF0,1
call adjBCD
;
INCF FSR0
movfw R1
movwf INDF0
;movwf —FSR0L; i FSR0++;INDF1;FSR1;movwf FSR
call adjBCD
;
INCF FSR0
movfw R0
movwf INDF0
;movwi FSR0++;INDF1+1;FSR1+ ;movwf FSR
call adjBCD
;
goto Loop10
;
adjBCD movlw 3
addwf INDF0,0;FSR0L,0;INDF0,1;R2,0;W;R2;0,W
;movf FSR0L,0;INDF1
movwf temp
btfsc temp,3 ; test if result > 7 (b’1000’=Hx08 carry from 7 to 8 = B’0111′ to B’1000′
movwf INDF0;FSR0L;INDF0;R2;0
movlw 30 ;B’0011 0000′
addwf INDF0,0;FSR0L;INDF0;R2,0;0,W
;movf FSR0L;INDF1,0
movwf temp
btfsc temp,7 ; test if result > 7 bit=H 0x40 =D64
movwf INDF0 ;FSR0L;R2;0 ; save as MSD
RETLW 0
;
;********************************************************************
; Test Program
;*********************************************************************
START
mainbcd
movlw 0x02;
movwf H_byte
SWAPF H_byte,1
rlf H_byte,1
rlf H_byte,1
movlw 0xDC;0xDC
movwf L_byte ; The 16 bit binary number = FFFF
call B2_BCD ; After conversion the Decimal Number
; ; in R0,R1,R2 = 06,55,35
;
;self goto self
;
;org 1FF
goto mainbcd
;
END
;
;********************************************************************
; Binary To BCD Conversion Routine
;
; This routine converts the 8 bit binary number in the W Register
; to a 2 digit BCD number.
; The least significant digit is returned in location LSD and
; the most significant digit is returned in location MSD.
;
; Performance :
; Program Memory : 10
; Clock Cycles : 81 (worst case when W = 63 Hex )
; ( i.e max Decimal number 99 )
;
; Program: BIN8BCD.ASM
; Revision Date:
; 1-13-97 Compatibility with MPASMWIN 1.40
;
;*******************************************************************
;
;LSD equ 10
;MSD equ 11
;
; INCLUDE «p16c5x.inc»
;
;Bin8BCD clrf MSD ; AN526
; movwf LSD
;gtenth movlw .10
; subwf LSD,W
; BTFSS STATUS,C
; goto over
; movwf LSD
; incf MSD, F
; goto gtenth
;over retlw 0
;*******************************************************************
;
;main movlw 63 ; W reg = 63 Hex
; call Bin8BCD ; after conversion, MSD = 9 & LSD = 9
;self goto self ; ( 63 Hex = 99 Decimal )
;
; org 1FF
; goto main
;
; END