Code:
define OSC 32 ' (8 x 4 = 32)
; --- *** Oscillator *** ---------------------------------------------------
OSCCON = %01110000 'Int Osc: 8MHz
'(Bits 4, 5 & 6 determine the internal clock speed).
' 111 = 8MHz 110 = 4MHz
'OSCTUNE = %10000000 'PLL is disabled.
OSCTUNE = %11000000 'PLL is enabled.
TRISA = 0
TRISB = 0
TRISC = 0
CMCON = 7 'Disable the comparators of PortA
ADCON0 = 0 'Disable ADC
ADCON1 = 15 'All pins of Port A are digital I/O
' ANSELb = 0
' ANSELA = 0
' ANSELC = 0
LED VAR PORTB.0 ' Assign name "LED" to
USERCOMMAND "DISPLED" ; BUFFER{,COLON }
USERCOMMAND "STRLEN" ; BUFFER
USERCOMMAND "RJUSTBUFF" ; BUFFER
TM_BIT VAR BYTE BANK0
TM_DAT VAR BYTE BANK0
TM_NAK VAR BYTE BANK0 ;set to $80 if tx error occured
TM_DIG VAR BYTE BANK0
TM_BRIGHT VAR BYTE BANK0 ;display pwm level 0 to 7
TM_COLON VAR BYTE BANK0 ;display colon 0/1
TM_TMP VAR BYTE[2] BANK0
str_len VAR BYTE BANK0
CMD_AUTO_ADDR CON $40
START_ADDR CON $c0
NUM_DIGITS CON 4 ;number of 7seg leds
COLON_FLAG CON $80
DISPLAY_ON CON $88
;tm1637 DIO AND CLK NEED TO BE ON SAME PORT
@TM_OUT_PORT = LATB ;TM_DIO OUT
@TM_IN_PORT = PORTB ;TM_DIO IN
@TM_CLK = 6 ;PIN
@TM_DIO = 7 ;PIN
@TM_TRIS = TRISB
BLINK VAR BYTE
LEDBUFF VAR BYTE[NUM_DIGITS +1]
COUNTER VAR byte
GOTO OVERASM
ASM
DISPLED?B macro BUFFER
movlw low BUFFER
movwf FSR0L
movlw high BUFFER
movwf FSR0H
L?CALL _TM_DISP4
endm
DISPLED?BC macro BUFFER ,COLON
movlw low BUFFER
movwf FSR0L
movlw high BUFFER
movwf FSR0H
MOVE?CB COLON ,_TM_COLON
L?CALL _TM_DISP4
endm
DISPLED?BB macro BUFFER ,COLON
movlw low BUFFER
movwf FSR0L
movlw high BUFFER
movwf FSR0H
MOVE?BB COLON ,_TM_COLON
L?CALL _TM_DISP4
endm
STRLEN?B macro BUF
MOVE?CB high BUF, FSR0H ;load highbyte
MOVE?CB low BUF, FSR0L ;load low byte
L?CALL STRlen
endm
RJUSTBUFF?B macro BUF
MOVE?CB high BUF, FSR0H ;load highbyte
MOVE?CB low BUF, FSR0L ;load low byte
MOVE?CB low BUF, _TM_TMP ;load low byte
MOVE?CB HIGH BUF, _TM_TMP+1 ;load low byte
L?CALL STRlen
L?CALL _strpad
endm
STRlen ;get buffer usage size
CLRF _str_len
str_tok_chr
MOVIW FSR0 ++ ; Get character
btfsC STATUS,Z
goto exit_str_null ; EXIT ON Null char
INCF _str_len,F ; not null so increment index
goto str_tok_chr
exit_str_null
return
_strpad ;right justify by padding with spaces " "
BANKSEL _str_len
movlw _NUM_DIGITS+1 ;buffer size
subwf _str_len,f ;tx string size
comf _str_len,f ;the difference is number of spaces to SHIFT in
btfsc STATUS, Z
goto expd ;if zero difference then exit
nxpd
movf _TM_TMP,w ;addr of buffer l
MOVWF FSR0
movf _TM_TMP+1,w ;addr of buffer h
MOVWF FSR0+1
movlw _NUM_DIGITS
addwf FSR0,f
movf FSR0,w
movwf FSR1
movf FSR0+1,w
movwf FSR1+1
DECF FSR0,F ;offset pointer 0
BANKSEL _TM_BIT
movlw _NUM_DIGITS
movwf _TM_BIT
nxby ;buffer shift right x 1
MOVIW FSR0 --
MOVWI FSR1 --
DECFSZ _TM_BIT,F
GOTO nxby
movlw 0x20
MOVWI FSR1 -- ;poke a space in buff[0]
BANKSEL _str_len
DECFSZ _str_len,F ;need another one ?
goto nxpd
expd ;nah!
return
SEG_val
CHK?RP _TM_TMP
MOVWF _TM_TMP
SUBLW 0x21
btfsc STATUS, C
retlw 0 ;" "
MOVF _TM_TMP,W
SUBLW 0x2f
btfsc STATUS, C
retlw 64 ;"-"
MOVF _TM_TMP,W
MOVLW 0X40
SUBWF _TM_TMP,W
btfsC STATUS, C
GOTO TM_ALPHA
MOVF _TM_TMP,W
ANDLW 0X0F
GOTO TM_LU
TM_ALPHA
ANDLW 0xdf ;ucase
SUBLW 6
btfsS STATUS, C
retlw 0 ;ERROR
MOVLW 0X37
SUBWF _TM_TMP,W
ANDLW 0xdf ;ucase
TM_LU
BRW
retlw 0X3F ;0
retlw 6
retlw 0X5B
retlw 0X4F
retlw 0X66
retlw 0X6D
retlw 0X7D
retlw 7
retlw 0X7F
retlw 0X67 ;9
retlw 0X77 ;A
retlw 0X7C ;b
retlw 0X39 ;C
retlw 0X5E ;d
retlw 0X79 ;E
retlw 0X71 ;F
TM_START
CHK?RP TM_OUT_PORT
BSF TM_OUT_PORT,TM_CLK
BSF TM_OUT_PORT,TM_DIO
NOP
BCF TM_OUT_PORT,TM_DIO
NOP
BCF TM_OUT_PORT,TM_CLK
NOP
RETURN
TM_STOP
CHK?RP TM_OUT_PORT
BCF TM_OUT_PORT,TM_CLK
BCF TM_OUT_PORT,TM_DIO
NOP
BSF TM_OUT_PORT,TM_CLK
NOP
BSF TM_OUT_PORT,TM_DIO
NOP
RETURN
TM_WRITE
MOVLW 8
CHK?RP _TM_BIT
MOVWF _TM_BIT
NXBT
CHK?RP TM_OUT_PORT
BCF TM_OUT_PORT,TM_CLK
CHK?RP _TM_DAT
BTFSS _TM_DAT,0
GOTO TM_0
CHK?RP TM_OUT_PORT
BSF TM_OUT_PORT,TM_DIO
GOTO TM_NB
TM_0
CHK?RP TM_OUT_PORT
BCF TM_OUT_PORT,TM_DIO
TM_NB
CHK?RP _TM_DAT
RRF _TM_DAT,F
CHK?RP TM_OUT_PORT
BSF TM_OUT_PORT,TM_CLK
CHK?RP _TM_BIT
DECFSZ _TM_BIT,F
GOTO NXBT
CHK?RP TM_OUT_PORT
BCF TM_OUT_PORT,TM_CLK
CHK?RP TM_TRIS
BSF TM_TRIS,TM_DIO
CHK?RP TM_IN_PORT
BTFSC TM_IN_PORT,TM_DIO
BSF _TM_NAK,7
CHK?RP TM_OUT_PORT
BSF TM_OUT_PORT,TM_CLK
NOP
NOP
NOP
NOP
BCF TM_OUT_PORT,TM_CLK
CHK?RP TM_TRIS
BCF TM_TRIS ,TM_DIO
RST?RP
RETURN
_TM_INIT
CHK?RP _TM_COLON
CLRF _TM_COLON
CLRF _TM_BRIGHT
CHK?RP TM_TRIS
BCF TM_TRIS ,TM_DIO
BCF TM_TRIS ,TM_CLK
RST?RP
RETURN
_TM_DISP4
MOVLW _CMD_AUTO_ADDR
CHK?RP _TM_DAT
MOVWF _TM_DAT
CLRF _TM_NAK
CALL TM_START
CALL TM_WRITE
CALL TM_STOP
MOVLW _NUM_DIGITS
CHK?RP _TM_DIG
MOVWF _TM_DIG
MOVLW _START_ADDR
CHK?RP _TM_DAT
MOVWF _TM_DAT
CALL TM_START
CALL TM_WRITE
NXDIG
MOVIW FSR0 ++
CALL SEG_val
CHK?RP _TM_DAT
IORWF _TM_COLON ,W
movwf _TM_DAT
CALL TM_WRITE
CHK?RP _TM_DIG
DECFSZ _TM_DIG,F
GOTO NXDIG
CALL TM_STOP
CHK?RP _TM_BRIGHT
MOVF _TM_BRIGHT ,W
ANDLW 7
IORLW _DISPLAY_ON
CHK?RP _TM_DAT
MOVWF _TM_DAT
CALL TM_START
CALL TM_WRITE
CALL TM_STOP
RST?RP
RETURN
ENDASM
OVERASM:
CALL TM_INIT
ALOOP:
TOGGLE LED
TM_BRIGHT=TM_BRIGHT-1
if TM_BRIGHT>7 then TM_BRIGHT=7
ARRAYWRITE LEDBUFF ,["b- ",#TM_BRIGHT,0]
DISPLED LEDBUFF,0
PAUSE 500
FOR COUNTER=0 TO 255
ARRAYWRITE LEDBUFF ,[hex COUNTER,0]
RJUSTBUFF LEDBUFF
DISPLED LEDBUFF,BLINK
PAUSE 50
NEXT
FOR COUNTER=0 TO 255
IF !(COUNTER//20) THEN BLINK = TM_COLON ^ $80
ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
RJUSTBUFF LEDBUFF
DISPLED LEDBUFF,BLINK
PAUSE 50
NEXT
goto ALOOP
Screenshot showing errors:
Bookmarks