Professional Documents
Culture Documents
jpg
Views: 445
Size: 46.3 KB
Code:
'********************************************************************
'* Name : TM1638_8Digits_8Keys_8LEDs.PBP *
'* Author : Roberts *
'* Notice : Hack it as you wish *
'* : *
'* Date : 10/20/2020 *
'* Version : 1.0 *
'* Notes : Demo program for TM1638 "LED&KEY" PCB that has 8 *
'* : 7-segment LED digits, 8 pushbutton switchs and 8 *
'* : discrete LEDs. *
'* : THIS VERSION FOR MICROCHIP CURIOSITY BOARD PIC16F18346*
'* : *
'********************************************************************
' This is a primitive demo program that exercises the functions of the TM1638 7-Segment LED driver
IC as it is
' used in a "LED&KEY" experimenter's printed circuit board (available from many online sources). The
LED&KEY PCB
' features 8 7-segment display characters, 8 discrete LEDs, and 8 pushbutton switches, and the
TM1638 controller. This
' PCB interfaces a microcontroller with a 3-wire (data, clock, and strobe) interface, and resembles
industry standard
' SPI so that PIC Basic's SHIFTIN and SHIFTOUT commands are usable.
'
' The TM1638 data sheet provides the essential information for commands, addressing, and timing.
Experimentation with
' PIC Basic's modes resulted in finding mode 0 is optimal for SHIFTOUT and mode 1 is optimal for
SHIFTIN.
'
' The demo program is segmented in two parts:
'
' "MAIN" demonstates sending LED ON/OFF states and characters to the LED&KEY. Specifically, the
program starts by
' clearing the TM1638 memory by sending "-" characters via the Subroutine "CLEARALL". Then, each
of the 8 LED's is
' individually turned on for 1/2 second in sequence. Then, the digits 0 thru 7 are sent to the display
and cleared.
' Then, the digits 8 through F are sent to the display and cleared.
'
' "READKEY" demonstrates use of the 8 keys. The LED&KEY is cleared so that the display shows all
"-" characters.
' Pressing any key causes that key's number ("5" for example) to appear on the corresponding
display location, while
' the corresponding discrete LED flashes that number of times. Pressing the key momentarily induces
one such display
' and flash sequence, while holding the key depressed induces looping digit and flashing.
'
' The TM1638 spec sheet indicates that combination key presses are not possible with keys wired in
the manner as they
' are on the LED&KEY board (e.g., all share "K3").
'
' The LED&KEY device resembles the TM1638 spec sheet's schematic on page 13 (Section XI), but
without the 16 switches
' connected to the TM1638's K1 and K2 pins, and with 8 discrete LEDs wired in common anode per
TM1638 Figure(8).
'
' The TM1638 is designed to accommodate 7-segment LED displays that are wired as common anode
or common cathode. This
' LED&KEY device has its 7-segment displays wired in common cathode.
'
' The 8 7-segment characters thus appear at 8 address locations (each address designating a Grid#)
each with 7 data
' bits (8 including the decimal designated by SEG1-SEG8) while the 8 LEDs appear at 8 address
locations (each address
' designating a Grid#) with 1 data bit designated by the value of SEG9.
'
' There are some similar TM1638 PCBs that have bi-color LEDs that use the capability of SEG10. Thus
the LED color can
' be altered by the value of the data bits set for SEG9 and Seg10. This demo code does not
accommodate bi-color LEDs
' but can be easily modified to do so.
'
' A THORUGH READING AND COMPREHENSION OF THE TM1638 DATA SHEET IS HELPFUL!
'
' Below are the hexadecimal segment codes corresponding to decimal values
' 0 1 2 3 4 5 6 7 8 9
' 0x3F 0x06 0x5B 0x4F 0x66 0x6D 0x7D 0x07 0x7F 0x67
'
'********************************Declare Variables and
Constants**********************************************
'
' Place the PIC initialization INCLUDE file here (See PBP PIC Configuration Files)
INCLUDE "PIC16F18346_Initialize.pbp"
'
ANSELB = 000000 'Port B is used for all interface pins
TRISB = 000000
SDA Var PORTB.4 'Serial data port pin (bi-directional)
SCK Var PORTB.6 'Serial clock port pin (uC output)
STB Var LATB.5 'Chip Select/Stobe (uC output/active low)
'These are 3 of the 4 commands used by the TM1638 controller: ($40 not used)
CMD1 CON $44 'CMD1=$44 is the write command for fixed addressing
CMD2 CON $42 'CMD2=$42 is the read key matrix command
CMD3 CON $89 'CMD3=$89 is the end command that turns the display on and sets
brightness at 2/16
'(CMD3=$8F for max brightness - see TM1638 spec sheet)
B1 VAR BYTE 'B1 is the first byte value from the keypad
B2 VAR BYTE 'B2 is the second byte value from the keypad
B3 VAR BYTE 'B3 is the third byte value from the keypad
B4 VAR BYTE 'B4 is the fourth byte value from the keypad
KEYNO VAR BYTE 'KEYNO is the key number or key number hex code
'
'*************************************************** MAIN
*********************************************************
MAIN: 'This rountine flashes each LED and sends hex digits to the display
'This section activates each discrete LED sequentially for 1/2 second
For i = $C1 to $CF Step 2 'LEDs are located at $Cn where "n" are odd values 1 thru F
LOW STB 'Turn on/off each LED in sequence one time
SHIFTOUT SDA, SCK, 0, [i,$01] 'Turn on the LED for 1/2 second
PAUSE 500
HIGH STB
PAUSE 1
LOW STB
SHIFTOUT SDA, SCK, 0, [i,$00] 'Turn off the LED
HIGH STB
PAUSE 5
Next i 'Sequence thru all 8 LEDs
'The following section of code reads the keypad byte. If no key is pressed the KEYNO byte default
value is $00
'Delete this section of code if keypad reading is not required
'*********************************************************************************
********************************
'Shift each byte left incrementally so all key codes appear in a single byte
KEYNO = B1 | B2<<1 | B3<<2 | B4<<3 'KEYNO = shifted & OR'd 4 bytes for a composite
single byte
'where each bit represents one of the 8 keys
IF KEYNO = $00 THEN 'If all Bn's = $00 this means no key is pressed
GOTO READKEY 'If no key is pressed, loop to top and read key bytes again
'Display the key number at the corresponding display location and flash the LED that many times
LOW STB
SHIFTOUT SDA, SCK, 0, [$C0+(2*i),KEYNO] 'Shiftout the key number to the 7-segment digit
at the
HIGH STB 'corresponding key location
PAUSE 1
For j = 0 to i 'Flash the LED the number of times of the key
LOW STB
SHIFTOUT SDA, SCK, 0, [$C1+(2*i),$01] 'Shiftout Address byte and data byte to discrete
LED
HIGH STB 'to turn ON the LED for 1/4 second
PAUSE 250
LOW STB
SHIFTOUT SDA, SCK, 0, [$C1+(2*i),$00] 'Shiftout Address byte and data byte to discrete
LED
HIGH STB 'to turn OFF the LED for 1/4 second
PAUSE 250
Next j 'Repeat the number of times equal to the key number
LOW STB
SHIFTOUT SDA, SCK, 0, [CMD3] 'The output command sends CMD3 which turns on
the display
HIGH STB
GOSUB CLEARALL
GOTO READKEY 'Return to beginning and wait for another key press
For i = $C0 to $CF 'This loop clears the display by writing 0x40 to each digit and LED
LOW STB 'LEDs remain OFF with this input / 7-segments show "-"
SHIFTOUT SDA, SCK, 0, [i,$40] '$40 is the 7-segment "-" character (0x40)
HIGH STB
PAUSE 1
Next i
LOW STB
SHIFTOUT SDA, SCK, 0, [CMD3] 'The output command sends CMD3 which turns on the
display
HIGH STB 'and sets the brightness level
SDA Var PORTB.4 ' Broche du port de données série (bi- directionnel)
SCK Var PORTB.6 'Broche du port d'horloge série (sortie uC)
STB Var LATB.5 'Chip Select/Stobe (sortie uC/actif bas)
'Ce sont 3 des 4 commandes utilisées par le contrôleur TM1638 : (40$ non utilisé)
CMD1 CON $44 'CMD1=$44 est la commande d'écriture pour l'adressage fixe
CMD2 CON $42 'CMD2=$42 est la commande de lecture matricielle
CMD3 CON $89 ' CMD3=$89 est la commande de fin qui allume l'écran et règle la luminosité à 2/16
'(CMD3=$8F pour la luminosité maximale - voir la fiche technique TM1638)
'
'************************ ****************************** PRINCIPALE
******************* **************************************
PRINCIPAL : « Cette routine fait clignoter chaque LED et envoie des chiffres hexadécimaux à l'écran
» Cette section active chaque LED discrète de manière séquentielle pendant 1/2 seconde
Pour i = $C1 à $CF Étape 2 'Les LED sont situées à $Cn où "n" sont des valeurs impaires 1 à F
LOW STB '
Allume /éteint chaque LED en séquence une fois SHIFTOUT SDA, SCK, 0, [i, $01] 'Allumez la LED
pendant 1/2 seconde
PAUSE 500
HIGH STB
PAUSE 1
LOW STB
SHIFTOUT SDA, SCK, 0, [i,$00] 'Éteignez la LED
HIGH STB
PAUSE 5
Suivant i 'Séquence à travers les 8 LED
'La section de code suivante lit l'octet du clavier. Si aucune touche n'est enfoncée, la valeur par
défaut de l'octet KEYNO est de $00
'Supprimez cette section de code si la lecture du clavier n'est pas requise
'************************* ********************************************************
**************************************
la touche 'Décaler chaque octet vers la gauche de façon à ce que tous les codes de touche
apparaissent dans un seul octet
KEYNO = B1 | B2<<1 | B3<<2 | B4<<3 'KEYNO = décalé & OR'd 4 octets pour un seul octet
composite
'où chaque bit représente l'une des 8 touches
IF KEYNO = $00 ALORS 'Si tous les Bn = $00, cela signifie qu'aucune touche n'est enfoncée
GOTO READKEY 'Si aucune touche n'est enfoncée, boucle en haut et relis les octets de la clé
ELSE 'Lorsque KEYNO n'est pas enfoncé $00, puis attribuez un numéro de clé
LOOKDOWN KEYNO,[$01,$02,$04,$08,$10,$20,$40,$80],i 'Cela convertit la valeur de l'octet en
numéro de clé 0 à 7
ENDIF 'Ex : la valeur de l'octet $08 est la clé #4, donc i=3
LOW STB
SHIFTOUT SDA, SCK, 0, [CMD3] ' La commande de sortie envoie CMD3 qui allume l'afficheur
HIGH STB
GOSUB CLEARALL
pour i = $C0 à $CF ' Cette boucle efface l'affichage en écrivant 0x40 à chaque chiffre et les LED
LOW STB' restent éteintes avec cette entrée / 7 segments affichent "-"
SHIFTOUT SDA, SCK, 0, [i,$40] '$40 est le 7-segment " -" caractère (0x40)
HIGH STB
PAUSE 1
Suivant i
LOW STB
SHIFTOUT SDA, SCK, 0, [CMD3] « La commande de sortie envoie CMD3 qui tourne sur l'écran
HIGH STB « et définit le niveau de luminosité