Você está na página 1de 5

; 8DGTCACC.

INC: 8 digits CA or CC 7-segments LED display


; using conditional assembly
;-------------------- keywords for conditional assembly ----------------; uncomment only one among these two directives
#define useCAdisplay
; if using Common Anode 7-segment display
;#define useCCdisplay
; if using Common Cathode 7-segment display
; uncomment only one among these five directives
;#define PORTX
5
; if using PORTA (full 8
#define PORTX
6
; if using PORTB (any 18
;#define PORTX
7
; if using PORTC (any 28
;#define PORTX
8
; if using PORTD (any 40
;#define PORTX
9
; if using PORTE (any 40

bits, example:
pins mid-range
pins mid-range
pins mid-range
pins mid-range

PIC16F628)
PICmicro)
PICmicro)
PICmicro)
PICmicro)

;-----------------------------------------------------------------------; declare in the main file:


;-----------------------------------------------------------------------;
numdgt
; .128, then .64, .32, .16, .8, .4, .2, .1
;
rlfcnt
; .8, then .7, .6, .5, .4, .3, .2, .1
;
affvis
; ratio display visible
;
dgtvis
; digit currently displayed
;
;
dgtDS7
; digit #7 (left most digit)
;
dgtDS6
; digit #6
;
dgtDS5
; digit #5
;
dgtDS4
; digit #4
;
dgtDS3
; digit #3
;
dgtDS2
; digit #2
;
dgtDS1
; digit #1
;
dgtDS0
; digit #0 (right most digit)
;
;-----------------------------------------------------------------------; macro to prepare input parameters to "Disp" routine
;-----------------------------------------------------------------------DISPDGT
macro posdgt, dgt
MOVLW posdgt
MOVWF rlfcnt
MOVF dgt, w
CALL Disp
endm
;-----------------------------------------------------------------------;config. RBx & CC as outputs, (if Common Cathode), then RBx=HIGH & CC=LOW
;Config. RBx & CA as outputs, (if Common Anode), then RBx=LOW & CA=HIGH
;-----------------------------------------------------------------------SEGx macro REGx, RBx, jump, configRB, dgtX
BTFSC dgtX, RBx
GOTO jump
#ifdef

useCCdisplay
BTFSS REGx, RBx

#endif

useCAdisplay
BTFSC REGx, RBx

; conditional assembly

#ifdef

; conditional assembly

#endif
GOTO jump
BSF STATUS, RP0
MOVLW configRB
XORWF dgtX, w
MOVWF PORTX
BCF STATUS, RP0
MOVLW configRB

; flip to bank1

; go back to bank0

#ifdef

useCCdisplay
XORLW 0FFh
; complement W (conditional assembly)

#endif
MOVWF PORTX
MOVLW
ADDLW
BTFSS
GOTO

.128
0FFh
STATUS, Z
$-2

;
;
;
;

internal delay = 0.5 ms (quartz 4 MHz)


W-bit Z=1 (i.e. W = 0 ?)
loop = 4 * 128 = 512 us

endm
;----------------------------------------------------------------------; Routine to display 8 digits
; (one digit at a time, segment by segment individually)
;----------------------------------------------------------------------Display
MOVLW .15
MOVWF affvis
dispdgts
DISPDGT
.8, dgtDS7
DISPDGT
.7, dgtDS6
DISPDGT
.6, dgtDS5
DISPDGT
.5, dgtDS4
DISPDGT
.4, dgtDS3
DISPDGT
.3, dgtDS2
DISPDGT
.2, dgtDS1
DISPDGT
.1, dgtDS0
DECFSZ
affvis, f
GOTO dispdgts
RETURN
;----------------------------------------------------------------------; Common Cathode digit, light segment by segment
; Common Anode digit, light segment by segment
;----------------------------------------------------------------------Disp
MOVWF
dgtvis
MOVLW
MOVWF
MOVF
CALL
MOVWF

high Tab7seg
PCLATH
dgtvis, w
; (dgtvis) -> W
Tab7seg
; convert
dgtvis
; (dgtvis) converted to 7 segments code

#ifdef

COMF

useCAdisplay
dgtvis, f
; conditional assembly (for Common Anode)

#endif

CLRF numdgt
BSF
dgtvis, 7
BSF
STATUS, C
rlfloop
RLF
numdgt, f
RLF
dgtvis, f
BTFSC STATUS, C
GOTO setBit0
BCF
dgtvis, 0
GOTO rlfnext
setBit0
BSF
dgtvis, 0
rlfnext
BCF
STATUS, C
DECFSZ
rlfcnt, f
GOTO rlfloop
DispsegA
DispsegB
DispsegC
DispsegD
DispsegE
DispsegF
DispsegG
DispsegX

SEGx
SEGx
SEGx
SEGx
SEGx
SEGx
SEGx
SEGx

dgtvis,0,DispsegB,0FEh,
dgtvis,1,DispsegC,0FDh,
dgtvis,2,DispsegD,0FBh,
dgtvis,3,DispsegE,0F7h,
dgtvis,4,DispsegF,0EFh,
dgtvis,5,DispsegG,0DFh,
dgtvis,6,DispsegX,0BFh,
dgtvis,7,Dispoff, 07Fh,

Dispoff
CLRF PORTX
RETURN

; clear portX

numdgt
numdgt
numdgt
numdgt
numdgt
numdgt
numdgt
numdgt

;
;
;
;
;
;
;
;

Disp.
Disp.
Disp.
Disp.
Disp.
Disp.
Disp.
Disp.

dgtvis,
dgtvis,
dgtvis,
dgtvis,
dgtvis,
dgtvis,
dgtvis,
dgtvis,

bit0
bit1
bit2
bit3
bit4
bit5
bit6
bit7

; 8DGTCNT.ASM: main file to test display file "8DGTCACC.INC"


;-----------------------------------------------------------------------------list P = 16F628A
#include <p16f628a.inc>
__CONFIG _CP_OFF &_BODEN_OFF &_PWRTE_ON &_WDT_OFF &_LVP_OFF &_MCLRE_OFF
&_INTRC_OSC_NOCLKOUT
;---------------------- DEFINE HARDWARE --------------------------------------#define
BTN
PORTA, 4
; push button connected to RA4
;--------------------- DEFINE VARIABLES --------------------------------------CBLOCK
0x70
; common RAM [16 bytes, 0x70 to 0x7F]
numdgt
; .128, puis .64, .32, .16, .8, .4, .2, .1
rlfcnt
; .8, puis .7, .6, .5, .4, .3, .2, .1
affvis
; ratio display visible
dgtvis
; digit currently displayed
dgtDS7
dgtDS6, dgtDS5, dgtDS4
dgtDS3, dgtDS2, dgtDS1
dgtDS0
ENDC

;
;
;
;

digit
digit
digit
digit

#7 (left most digit)


#6, digit #5, digit #4
#3, digit #2, digit #1
#0 (right most digit)

;-----------------------------------------------------------------------------; increment one digit from 0 to maxi-1 ; if maxi, then wrap to 0


;-----------------------------------------------------------------------------INC1 macro digitX, maxi, jump
INCF digitX, f
; increment digitX
MOVLW maxi
XORWF digitX, w
BTFSS STATUS, Z
; digitX = maxi ?
GOTO jump
; no
CLRF digitX
; yes, then clear digitX
endm
;//////////////////////////////////////////////////////////////////////////////
ORG 0x00
; reset vector
GOTO Start
;-----------------------------------------------------------------------; L.U.T. to convert a decimal value into the corresponding 7 segment code
;-----------------------------------------------------------------------Tab7seg
ADDWF PCL, f
; display 7 segments GFEDCBA
DT 3Fh, 06h, 5Bh, 4Fh, 66h, 6Dh, 7Dh, 07h, 7Fh, 6Fh
;/////////////////////////////////////////////////////////////////////////////////
#include "8DGTCACC.INC"
;/////////////////////////////////////////////////////////////////////////////////
Start
bsf
STATUS, RP0 ; flip to Bank 1
bsf
BTN
; config. BTN as input
bcf
STATUS, RP0 ; back to Bank 0
clrf

dgtDS7

clrf
clrf
clrf
clrf
clrf
clrf
clrf

tstBtn

dgtDS6
dgtDS5
dgtDS4
dgtDS3
dgtDS2
dgtDS1
dgtDS0

btfsc BTN
; BTN pushed
goto majDisp
; no
IncDgts
INC1 dgtDS0, .10, majDisp
INC1 dgtDS1, .10, majDisp
INC1 dgtDS2, .10, majDisp
INC1 dgtDS3, .10, majDisp
INC1 dgtDS4, .10, majDisp
INC1 dgtDS5, .10, majDisp
INC1 dgtDS6, .10, majDisp
INC1 dgtDS7, .10, majDisp
majDisp
CALL
Display
GOTO tstBtn
;-----------------------------------------------------------------------END

Você também pode gostar