; Subroutines to communicate with TITAN TM1637 display driver by A.C.M. Daas ; Tested at speeds of 4 and 8 MHz with pull-up resistors of 12K on CLK and DIO. ;_______________________________________________________________________________ ; Subroutine to write command or data to TM1637 ; Entrypoint for command is: TM_cmd and for data write is: TM_wrt. ; On entry TM_dat has command or byte to send. ; Assume CLK and DIO are both set as input and TM_CLK and TM_DIO are both high. ; Command byte: 0100tfr0 Data t: 0=normal, 1=test mode, ; f: 0=auto increment, 1=no increment, ; r: 0=write, 1=read ; 1100aaaa Address aaaa=Character address (0= GRID1 > 5= GRID6) ; 6 or 7 are ignored ; 1000dwww Display and control d: 0=off, 1=on ; w: duty cycle 000=1/16, 001=1/8, 010=1/4 ; 011=5/8, 100=11/16, 101=3/4, 110=13/16, 111=7/8 ; TM_cmd +4 words, duration 4+98+7= 102 steps TM_cmd movlw ~(1<- allow DIO high (by pull-up) skpc ;| | | if C=0, then: bcf TRISC,TM_DIO ;| -<- force DIO output low nop ;| | | delay bsf TRISC,TM_CLK ;->- | | allow CLK high (bit n) decfsz TM_cnt,F ; | | | if not yet bit 7, then: goto TM_wlp ; | | | repeat ;TM_ack 6 words, duration 7 steps TM_ack bcf TRISC,TM_CLK ;-<- | | lower CLK last bit bsf TRISC,TM_DIO ;| ->- allow DIO high (by pull-up) nop ;| | delay bsf TRISC,TM_CLK ;->- | end byte transfer bcf STATUS,RP0 ; | | reselect bank 0 return ;done ;_______________________________________________________________________________ ; Subroutine to read byte from TM1637. Returns key scan code in TM_dat. ; Entrypoint is: TM_rdk ; Assume CLK is initialized as output and high, and DIO as input. ; TM_rdk 14 words, duration 4+8*10+9= 93 steps TM_rdk bsf STATUS,RP0 ; | select bank 1 clrf TM_dat ; | initialize all bits 0 in result setc ; | set terminal marker rrf TM_dat,F ; | shift marker in collect byte, clear carry TM_rlp bcf TRISC,TM_CLK ;-<- lower CLK, keep DIO latch low bcf STATUS,RP0 ;| reselect bank 0 btfsc PORTC,TM_DIO ;| check data input; if high, then: setc ;| set carry bsf STATUS,RP0 ;| select bank 1 rrf TM_dat,F ;| shift marker and rotate data bit in bsf TRISC,TM_CLK ;->- allow CLK to raise skpc ; | if not last bit received, then: goto TM_rlp ; | repeat bit read goto TM_ack ; | append with acknowledge sequence ;_______________________________________________________________________________ ; Subroutine to terminate transmission with TM1637. ; Entrypoint is: TM_stp ; TM_stp 9 words, duration 10 steps TM_stp bsf STATUS,RP0 ; | | select bank 1 bcf TRISC,TM_CLK ;-<- | force CLK low bcf TRISC,TM_DIO ;| | force DIO low nop ;| | delay bsf TRISC,TM_CLK ;->- | allow CLK high nop ; | | delay bsf TRISC,TM_DIO ; | ->- allow DIO go high (by pull-up) bcf STATUS,RP0 ; | | reselect bank 0 return ;done