The attached demo code was developed on the MELabs Trainer Board (PIC16F1937) using PortB.0 (data) and PortB.1 (clock), and ported to a PIC12F1572 application without any problem. I've commented the code copiously to make it (hopefully) comprehensible and portable.
I've structured this code as a library file that can be inserted as an INCLUDE file in any main program. However, this library file is also executable by itself with the addition of a uC initialization INCLUDE and the un-commenting of the single line of code where so designated. "CompileProgram" and your LED display should light up all the segments "8.8.8.8.".
I suggest interested users run this file by itself as a means of testing the display, port assignments and connections before integrating it into a main program (reassign the clock and data pins as required). Once your display is functional, all that is necessary is to INCLUDE this file at the end of your main program, and insert GOSUB LEDOUT instruction(s) wherever required in your main program.
The user's main program should create a 4-digit decimal number, parse it into 4 separate digits, lookup the 7-segment hex code for each digit, and assign those hex codes to the 4 character variables CHR1 - CHR4. Then, GOSUB LEDOUT.
The subroutine LEDOUT sends the 4 digits, CHR1 - CHR4, to the display, preceded by the write command 0x40 (CMD1) and the address start command 0xC0 (CMD2), and proceeded by the end command 0x8A (CMD3). Each of the digits CHR1 - CHR4 and each of the commands CMD1 - CMD3 is sent using the SHIFTOUT command. Bracketing each SHIFTOUT command are the bit-banger start, acknowledge, and stop sequences, which are executed by the subroutines StartSeq, AckSeq, and StopSeq.
AN EXTREMELY IMPORTANT POINT: These TM1637 LED displays seem to be universally equipped with on-board capacitors that shunt the clock and data inputs, and render the displays non-functional (at least in my experience with them). I have had to remove these caps to make my displays work.
I hope this code proves useful. Comments and feedback are of course encouraged and welcome.
'**************************************************************** '* 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**********************************************
Re: K42 and Timer Interrupts
Thanks for the explanation.
Ioannis - 28th April 2025, 19:28I misinterpreted these paragraphs. My understanding was to have ASYNC cleared and use Fosc/4.
Ioannis