Code:
'****************************************************************
'* Name : TM1637_Include_File.PBP *
'* Author : Roberts *
'* Notice : Hack it as you wish *
'* : *
'* Date : 11/12/2019 *
'* Version : 1.0 *
'* Notes : Include File Library for TM1637 Write Subroutine *
'* : *
'****************************************************************
'
' The subroutine LEDOUT implements the write sequence consisting of a write command (CMD1=$40) followed by the address
' command (CMD2=$C0), the 4 digits CHR1 - CHR4, and terminating with CMD3=$8A which turns the display ON and sets its
' brightness level. The PBP command SHIFTOUT is used to send these commands and characters, thus avoiding bit-banging
' them.
'
' The 3 subroutines StartSeq, AckSeq, and StopSeq perform the requisite bit banging on the SDA and SCK outputs before
' and after the PBP SHIFTOUT command.
'
' This demo code writes to all 4 digits in the TM1637's "auto-increment" mode. The digits could be individually
' addressed using the TM1637's "fixed address" mode (see TM1637 spec sheet).
' Decimal points are included in this demo -- the $FF hex code activates all 7 segments and the digit's decimal
' point ("8."). For any digit, add 0x80 as shown in the code table below.
'
' Some TM1637 displays are designed for clock applications (hour & minutes) and have a center colon instead of decimal
' points. The colon is activated by adding 0x80 to CHR2 (activating the decimal point for CHR2).
'
' This INCLUDE file can be used by itself to test activate the display to "8.8.8.8." or any other set of digits by
' setting the values for CHR1 - CHR4, and un-commenting the line of code designated below.
'
' A THORUGH READING AND COMPREHENSION OF THE TM1637 DATA SHEET IS HELPFUL!
'
' Below are the hexadecimal segment codes corresponding to decimal values
'
' 0 1 2 3 4 5 6 7 8 9 (digits only)
' 0x3F 0x06 0x5B 0x4F 0x66 0x6D 0x7D 0x07 0x7F 0x67
'
' 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. (digit and decimal point)
' 0xBF 0x86 0xDB 0xCF 0xE6 0xED 0xFD 0x87 0xFF 0xE7 (digit and colon for CHR2 in clock displays)
'
'********************************Declare Variables and Constants**********************************************
'
' Place the PIC initialization INCLUDE file here to use this INCLUDE file by itself to test the LED display
' For Example:
' INCLUDE "TM1637_MELABS_Initializer.pbp"
'
SDA Var PORTB.0 'Serial data pin
SCK Var PORTB.1 'Serial clock pin
'These are the 3 commands used by the TM1637 controller:
CMD1 CON $40 'CMD1=$40 is the write command
CMD2 CON $C0 'CMD2=$C0 is the start digit position(left most)
CMD3 CON $8A 'CMD3=$8A is the end command that turns the display on and sets brightness
'(CMD3=$8F for max brightness - see TM1637 spec sheet)
'*******Un-comment the following line of code to use this INCLUDE file by itself to test the LED display*******
'CHR1 CON $FF : CHR2 CON $FF : CHR3 CON $FF : CHR4 CON $FF 'Activate all segments and decimal points "8.8.8.8"
'************************************Subrouting LEDOUT*********************************************************
LEDOUT: 'This subrountine outputs all 4 characters to the LED
GOSUB StartSeq 'SHIFTOUT Mode 0 works - LSB first, clock normally low
SHIFTOUT SDA, SCK, 0, [CMD1] 'This shifts out the 8-bit write command character $40
GOSUB AckSeq
GOSUB StopSeq
GOSUB StartSeq
SHIFTOUT SDA, SCK, 0, [CMD2] 'This shifts out the 8-bit command character $C0 that places
GOSUB AckSeq 'the first character CHR1 in the leftmost digit location
'and then writes all 4 digits.
SHIFTOUT SDA, SCK, 0, [CHR1] 'SHIFTOUT CHR1-CHR4 to fill the display left to right
GOSUB AckSeq 'TM1637 auto-increment mode
SHIFTOUT SDA, SCK, 0, [CHR2]
GOSUB AckSeq
SHIFTOUT SDA, SCK, 0, [CHR3]
GOSUB AckSeq
SHIFTOUT SDA, SCK, 0, [CHR4]
GOSUB AckSeq
GOSUB StopSeq
GOSUB StartSeq
SHIFTOUT SDA, SCK, 0, [CMD3] 'The output sequence ends with CMD3 which turns on the display
GOSUB AckSeq 'and set the brightness duty cyle
GOSUB StopSeq
Return
'****************************************Subroutine StartSeq*******************************************************
StartSeq:
HIGH SCK
HIGH SDA 'The Start sequence takes SDA low and then SCK low 5 uSec later
PAUSEUS 5
LOW SDA
PAUSEUS 5
Return
'**************************************Subroutine AckSeq*********************************************************
AckSeq:
LOW SCK 'The ACK waits for the TM1637 to assert low on SDA
PAUSEUS 5 'and then replies with a 5 uSec ACK pulse on SCK
IF SDA = 0 THEN
HIGH SCK
PAUSEUS 5
LOW SCK
ENDIF
Return
'****************************************Subroutine StopSeq******************************************************
StopSeq:
LOW SCK 'The stop sequence takes SCK high and then SDA high 5 uSec later
LOW SDA
PAUSEUS 5
HIGH SCK
PAUSEUS 5
HIGH SDA
PAUSEUS 5
LOW SDA
Return
'*****************************************End of Subroutine Programs**********************************************
Bookmarks