T&t - PIC 

 

Un driver per TM1637


E' stato elaborato un driver in Assembly per comandare il display controller TM1637.

La versione proposta è scritta per Enhanced Midrange, ma è facilmente adeguabile a PIC18F.

Sono utilizzati due pin di un port qualsiasi.

Il driver fornisce le seguenti funzioni:

; TMInit    - inizializza l'I/O necessario al driver (2 pin)
; TMStart   - invia uno START al TM1637
; TMStop    - invia uno STOP al TM1637
; TMAsk     - attende la risposta ASK dal TM1637
; TMWrByte  - scrive un byte dal WREG verso il TM1637
; TMWr4Disp - scrive i valori relativi ai 4 display più la luminosità
; TMRdKey   - legge il set dei pulsanti, risultato reso in WREG

I ritardi necessari sono sono realizzabili con poche istruzioni, a seconda del clock del processore oppure con una macro del genere Delayus.

Assieme al file principale delle subroutines esiste un file di intestazione che va modificato per assegnare i pin desiderati.


I sorgenti.

L'header da modificare a seconda delle assegnazioni hardware:

;************************************************************************
;**                                                                    **
;** Title     : Pseudo I2C driver for TM1637                           **
;**             User definition header                                 **
;** Hardware  :                                                        **
;** Processor : PIC Enhanced Midrange                                  **
;** Date      : 8-01-2009                                              **
;** Version   : V.01                                                   **
;** Autore    : afg                                                    **
;**                                                                    **
;************************************************************************
;** This file defines the microcontroller pin used for TM1637          
;** communication and the RAM required for the driver.              
;** This file needs to be modified depending on the hardware connection. 
;************************************************************************
NOLIST

; 1. - Hardware interface - 2 digital I/O pin on the same port
;========================================================================
;---->>>> Change depending on the hardware connection.

; Control lines of the interface
#define TMCLK LATB,6
#define TMCLKTRIS TRISB,6

#define TMDIO LATB,7
#define TMDIOTRIS TRISB,7

#define TMPORT PORTB
#define TMLAT LATB
#define TMTRIS TRISB

; 2. - Driver required memory: 8 bytes on banked RAM
;========================================================================
TMRAM  UDATA

TMTMP  res 1 ; variabli
TMTMP1 res 1
TMBUF  res 1 ; I2C Communication Register
TMCHR  res 4 ; display buffer
TMBRI  res 1 ; brightness buffer

;************************************************************************
 LIST

Il set di subroutines del driver:

;************************************************************************
;**                                                                    **
;**  Title     : Pseudo I2C driver for TM1637                          **
;**  Hardware  :                                                       **
;**  Processor : PIC Enhanced Midrange                                 **
;**  Date      : 8-01-2009                                             **
;**  Version   : V.01                                                  **
;**  Autore    : afg                                                   **
;**                                                                    **
;************************************************************************
;************************************************************************
; Functions (subroutines):
; TMInit    - initialize I/O for TM1637 driver
; TMStart   - Send START to TM1637
; TMStop    - Send STOP to TM1637
; TMAsk     - Wait for ASK from TM1637
; TMWrByte  - Write a byte to TM1637
; TMWr4Disp - Write 4 display on TM1637 and add brightness
; TMRdKey   - Read key set, result in WREG
;
;************************************************************************
; Note:
;************************************************************************

;************************************************************************
; TMInit - sub - initialize I/O for TMdriver
; clk = 1 , dio = 1
;************************************************************************
TMInit:
   GLOBAL TMInit
       banksel TMLAT
       bsf     TMCLK        ; preset clk=1
       bsf     TMDIO        ; preset dio=1
       banksel TMTRIS
       bcf     TMCLKTRIS    ; clk=out=1
       bcf     TMDIOTRIS    ; dio=out=1
       return

;************************************************************************
; TMStart - sub - Send START to TM1637
;************************************************************************
TMStart:
   GLOBAL TMStart
       banksel TMLAT
       bsf     TMCLK        ; clk=1
       bsf     TMDIO        ; dio=1
       Delayus 5            ; Delayus 2
       bcf     TMDIO        ; dio=0
       return

;************************************************************************
; TMStop - sub - Send STOP to TM1637
;************************************************************************
TMStop:
   GLOBAL TMStop
       banksel TMLAT
       bcf     TMCLK        ; clk=0
       Delayus 5            ; Delayus 2
       banksel TMLAT
       bcf     TMDIO        ; dio=0
       Delayus 5            ; Delayus 2
       banksel TMLAT
       bsf     TMCLK        ; clk=1
       Delayus 5            ; Delayus 2
       banksel TMLAT
       bsf     TMDIO        ; dio=1
       return

;************************************************************************
; TMAsk - sub - Wait for ASK from TM1637
;************************************************************************
TMAsk: 
   GLOBAL TMAsk
       banksel TMLAT
       bcf     TMCLK        ; clk=0
       banksel TMTRIS
       bsf     TMDIOTRIS    ; dio=in 
       Delayus 5            ; Delayus 2
       banksel TMPORT
tmask0 btfsc   TMDIO        ; while (dio)
        bra    tmask0
       banksel TMLAT
       bcf     TMDIO        ; preset latch=0
       banksel TMTRIS
       bcf     TMDIOTRIS    ; dio=out=0
       banksel TMLAT
       bsf     TMCLK        ; clk=1
       Delayus 5            ; Delayus 2
       banksel TMLAT
       bcf     TMCLK        ; clk=0
       return

;************************************************************************
; TMWrByte - sub - Write a byte to TM1637
; Data/command come on WREG
;************************************************************************
TMWrByte:
  GLOBAL TMWrByte
       banksel TMBUF
       movwf   TMBUF        ; save data
       banksel TMTMP
       movlw   8            ; cntr = 8
       movwf   TMTMP
tmwrlp:
       banksel TMLAT
       bcf     TMCLK        ; clk=0
       Delayus 5            ; Delayus 2
       banksel TMBUF
       rrf     TMBUF,f      ; TMBUF = TMBUF >> 1
       banksel TMLAT
       bsf     TMDIO        ; dio=1
       skpc                 ; if (TMBUF & 0x01), dio=1
        bcf    TMDIO        ; else (dio=0)
       Delayus 5            ; Delayus 3
       banksel TMLAT
       bsf     TMCLK        ; clk=1
       Delayus 5            ; Delayus 3
       banksel TMTMP
       decfsz  TMTMP,f      ; if cntr >0 cntr --
        bra    tmwrlp
       return

;************************************************************************
; TMWr4Disp - sub - Write 4 display on TM1637 and add brightness
; Data/command stored on TMCHAR buffer
; Brightness stored on TMBRI buffer
;************************************************************************
TMWr4Disp:
GLOBAL TMWr4Disp
       call    TMStart
       movlw   0x40         ; auto increment
       call    TMWrByte
       call    TMAsk
       call    TMStop
       banksel TMTMP1
       movlw   4            ; counter=4
       movwf   TMTMP1
       Delayus 5            ; Delayus 2
       call    TMStart
       movlw   0xC0         ; first addr.
       call    TMWrByte
       call    TMAsk
       movlw   LOW (TMCHR)  ; set indirect pointer
       movwf   FSR1L
       movlw   HIGH (TMCHR)
       movwf   FSR1H
tmwr4lp:
       movf    INDF1,w
       call    TMWrByte
       call    TMAsk
       incf    FSR1L,f
       skpnz
        incf   FSR1H,f
       banksel TMTMP1
       decfsz  TMTMP1,f
        bra    tmwr4lp
       call    TMStop
       call    TMStart
       Delayus 5            ; Delay 5us
       banksel TMBRI
       movf    TMBRI,w      ; brightness
       call    TMWrByte
       call    TMAsk
       call    TMStop
       return

;************************************************************************
; TMRdKey - sub - Read key set, result in WREG
;************************************************************************
TMRdKey:
  GLOBAL TMRdKey
       call    TMStart
       movlw   0x42         ; read button command
       call    TMWrByte
       call    TMAsk
       banksel TMTRIS
       bsf     TMDIOTRIS    ; dio=input=1
       movlw   8            ; cntr = 8
       banksel TMTMP
       movwf   TMTMP
       clrf    TMBUF        ; clear result buffer
tmsklp:
       banksel TMLAT
       bcf     TMCLK        ; clk=0
       banksel TMBUF
       clrc
       rrf     TMBUF,f      ; TMBUF = TMBUF >> 1
       Delayus 30           ; delay 30us
       banksel TMLAT
       bsf     TMCLK        ; clk=1
       banksel TMPORT
       btfss   TMDIO        ; if(dio)
       bra     tmsk0
       banksel TMBUF        ; TMBUF = TMBUF|0x80
       bsf     TMBUF,7
tmsk0  Delayus 30           ; delay 30us
       banksel TMTMP        ; while cntr >0 cntr --
       decfsz  TMTMP,f
       bra     tmsklp
       call    TMAsk
       call    TMStop
       banksel TMBUF
       movf    TMBUF,w
       return

;************************************************************************

 
I ritardi inseriti sono tutti da 5us (esclusi quelli da 30us per la lettura dei contatti).
Questo valore è di ampia sicurezza ed ha permesso il funzionamento i tutte le condizioni provate. Se si vuole ottenere una maggiore velocità di trasmissione si possono portare i ritardi a 2 e 3us come indicato nei commenti.

E' stato elaborato anche un semplice programma di test.

 


 


 

 

Copyright © afg. Tutti i diritti riservati.
Aggiornato il 31/08/17.