You are on page 1of 14

Name: LEDKEY.

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)

HIGH SDA 'Initialize Data Output


HIGH STB 'Initialize Strobe Output

i VAR BYTE 'loop variable for multiple use


j VAR BYTE 'loop variable for multiple use

'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
*********************************************************

GOSUB CLEARALL 'Clear the display with all "-" characters

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

'This loop sends characters 0 thru 7 to the 8 digit display


For i = 0 to 7
LOOKUP i,[$3F,$06,$5B,$4F,$66,$6D,$7D,$07],j 'Lookup the hex code for each display digit
Low STB
SHIFTOUT SDA, SCK, 0, [$C0 + 2*i,j] 'Send even number addresses for 7-segment digits &
output digit
High STB
PAUSE 500 'Pause 1/2 second
Next i

GOSUB CLEARALL 'Clear the display to all "-" characters

'This loop sends characters 8 thru F to the 8 digit display


For i = 0 to 7
LOOKUP i,[$7F,$67,$77,$7C,$39,$5E,$79,$71],j 'Lookup the hex code for each display digit
LOW STB
SHIFTOUT SDA, SCK, 0, [$C0 + 2*i,j] 'Send even number addresses for 7-segment digits &
output digit
HIGH STB
PAUSE 500 'Pause 1/2 second
Next i

GOSUB CLEARALL 'Clear the display to all "-" characters

'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
'*********************************************************************************
********************************

READKEY: 'READKEY processes only single key inputs


'Send the key read command and shiftin the 4 key matrix bytes
LOW STB
SHIFTOUT SDA, SCK, 0, [CMD2] 'Send the Read keypad command CMD2 = $42
PAUSEUS 10
SHIFTIN SDA, SCK, 1, [B1,B2,B3,B4] 'Shiftin all 4 keypad bytes
PAUSEUS 1
HIGH STB
PAUSE 50 'Delay to allow for key release

'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

ELSE 'When KEYNO is not $00, then assign a key number


LOOKDOWN KEYNO,[$01,$02,$04,$08,$10,$20,$40,$80],i 'This converts the byte value to the key
number 0 thru 7
ENDIF 'Ex: the byte value $08 is key #4, so i=3

'Convert the key number 0 thru 7 to a 7-segment hex code 1 thru 8


LOOKUP i,[$06,$5B,$4F,$66,$6D,$7D,$07,$7F],KEYNO 'Redefine KEYNO as the display hex code

LOW STB 'Enable chip select


SHIFTOUT SDA, SCK, 0, [CMD1] 'This shifts out the 8-bit write command character $44
HIGH STB 'for fixed mode (random access) addressing
PAUSE 1

'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

'******************************************* Subroutine CLEARALL


****************************************************
'This section clears the LEDs and sets the 7-segments digits to "-" in TM1638's display memory
CLEARALL:
LOW STB 'Enable chip select
SHIFTOUT SDA, SCK, 0, [CMD1] 'This shifts out the 8-bit write command character $44
HIGH STB 'for fixed mode (random access) addressing
PAUSE 1

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

PAUSE 500 'Wait 1/2 second after clearing the display


RETURN

' NOTES ON THE KEY MATRIX:


' On this display/key assembly, the 8 switches are wired in the K3/KS1-KS8 matrix column (see
TM1638 data sheet V1.3
' Figures(3)&(4) on page 7). This means switches S1-S4 activate the lsb "B0" in Bytes 1-4 respectively,
and switches
' S5-S8 activate bit "B4" in Bytes 1-4 respectively. The code above shifts each of the 4 Bytes left by an
incremental
' bit and ORs them to create a single byte KEYNO with all 8 switch bits present.
'
' Bit Number: B7 B6 B5 B4 B3 B2 B1 B0
' Switch #1 0 0 0 0 0 0 0 1 Byte 1 - not shifted left
' Switch #2 0 0 0 0 0 0 1 0 Byte 2 - shifted left 1 bit
' Switch #3 0 0 0 0 0 1 0 0 Byte 3 - shifted left 2 bits
' Switch #4 0 0 0 0 1 0 0 0 Byte 4 - shifted left 3 bits
' Switch #5 0 0 0 1 0 0 1 0 Byte 1 - not shifted left
' Switch #6 0 0 1 0 0 0 1 0 Byte 2 - shifted left 1 bit
' Switch #7 0 1 0 0 0 0 1 0 Byte 3 - shifted left 2 bits
' Switch #8 1 0 0 0 0 0 1 0 Byte 4 - shifted left 3 bits
'
' KEYNO OR's Value 1 1 1 1 1 1 1 1 ONLY SINGLE KEY PRESSES ARE
ALLOWED
' SO ONLY 1 OF 8 BITS WILL = 1
'
' LOOKDOWN SEARCH $80 $40 $20 $10 $08 $04 $02 $01 LOOKDOWN converts
KEYNO to key number
' LOOKDOWN VALUE 7 6 5 4 3 2 1 0
'
' Key: S8 S7 S6 S5 S4 S3 S2 S1 Switch Numbers 1 - 8
' Matrix Loc: K3/KS8 K3/KS6 K3/KS4 K3/KS2 K3/KS7 K3/KS5 K3/KS3 K3/KS1 TM1638 Matrix
Location
' Byte Number: B4 B3 B2 B1 B4 B3 B2 B1 Following Key Read 0x42
command
' Key Hex Code: 0x10 0x10 0x10 0x10 0x01 0x01 0x01 0x01 Non-shifted hex code
' Shifted Left: 0x80 0x40 0x20 0x10 0x08 0x04 0x20 0x01 Shifted hex code
' Key# Code: 0x7F 0x07 0x7D 0x6D 0x66 0x4F 0x5B 0x06 Digit value and LED 7 Seg
Code
' Displayed Digit: 8 7 6 5 4 3 2 1
'
'************************************** End of Keypad Reading
****************************************************
Code:
'****************************************************** *******************
'* Nom : TM1638_8Digits_8Keys_8LEDs.PBP *
'* Auteur : Roberts *
'* Remarque : Hackez-le comme vous le souhaitez *
'* : *
'* Date : 20/10/2020 *
'* Version : 1.0 *
'* Notes : Programme de démonstration pour PCB "LED&KEY" TM1638 qui a 8 *
'* : chiffres LED à 7 segments, 8 boutons poussoirs et 8 *
'* : LED discrètes.*
'* : CETTE VERSION POUR MICROCHIP CURIOSITY BOARD PIC16F18346*
'* : *
'************************************ ********************************
' Il s'agit d'un programme de démonstration primitif qui exerce les fonctions du TM1638 7- Segment
LED driver IC tel qu'il est
utilisé dans la carte de circuit imprimé d'un expérimentateur "LED&KEY" (disponible à partir de
nombreuses sources en ligne). Le LED&KEY PCB
' comprend 8 caractères d'affichage à 7 segments, 8 LED discrètes et 8 interrupteurs à
bouton-poussoir, ainsi que le contrôleur TM1638. Ce
« PCB interface un microcontrôleur avec une interface à 3 fils (données, horloge et stroboscope) et
ressemble à la norme de l'industrie
« SPI de sorte que PIC Basic » s Les commandes SHIFTIN et SHIFTOUT sont utilisables.
'
' La fiche technique du TM1638 fournit les informations essentielles pour les commandes,
l'adressage et la synchronisation. L'expérimentation avec
les modes 'PIC Basic a permis de trouver que le mode 0 est optimal pour SHIFTOUT et que le mode 1
est optimal pour SHIFTIN.
'
' Le programme de démonstration est segmenté en deux parties :
'
' "MAIN" montre l'envoi des états et des caractères de LED ON/OFF à la LED&KEY. Plus précisément,
le programme commence par
'effacer la mémoire du TM1638 en envoyant des caractères "-" via le sous-programme "CLEARALL".
Ensuite, chacune des 8 LED est
allumée individuellement pendant 1/2 seconde en séquence. Ensuite, les chiffres 0 à 7 sont envoyés
à l'affichage et effacés.
' Puis, les chiffres 8 à F sont envoyés à l'écran et effacés.
'
' "READKEY" montre l'utilisation des 8 touches. La LED&KEY est effacée de sorte que l'écran affiche
tous les caractères "-".
' En appuyant sur n'importe quelle touche, le numéro de cette touche ("5" par exemple) apparaît à
l'emplacement d'affichage correspondant, tandis que
' la LED discrète correspondante clignote ce nombre de fois. Un appui momentané sur la touche
induit un tel affichage
et une séquence de clignotement, tandis que le maintien de la touche enfoncée induit un chiffre en
boucle et un clignotement.
'
' La fiche technique du TM1638 indique que les combinaisons de touches ne sont pas possibles avec
les touches câblées de la manière qu'elles
' sont sur la carte LED&KEY (par exemple, toutes partagent "K3").
'
' Le dispositif LED&KEY ressemble à la fiche technique du TM1638'
' connecté aux broches K1 et K2 du TM1638, et avec 8 LED discrètes câblées en anode commune
selon la figure TM1638 (8).
'
' Le TM1638 est conçu pour accueillir des écrans LED à 7 segments qui sont câblés en tant qu'anode
commune ou cathode commune. Ce
'dispositif LED&KEY a ses afficheurs 7 segments câblés en cathode commune.
'
' Les 8 caractères à 7 segments apparaissent ainsi à 8 emplacements d'adresse (chaque adresse
désignant un Grid#) chacun avec 7
bits de données ' (8 y compris la décimale désignée par SEG1-SEG8) tandis que les 8 LED
apparaissent à 8 emplacements d'adresse (chaque adresse
' désignant un Grid#) avec 1 bit de données désigné par la valeur de SEG9.
'
' Certains PCB TM1638 similaires ont des LED bicolores qui utilisent la capacité de SEG10. Ainsi, la
couleur de la LED peut
être modifiée par la valeur des bits de données définis pour SEG9 et Seg10. Ce code de
démonstration ne prend pas en charge les LED bicolores
mais peut être facilement modifié pour le faire.
'
' UNE LECTURE ET UNE COMPRÉHENSION APPROFONDIES DE LA FICHE TECHNIQUE TM1638 EST
UTILE !
'
' Ci-dessous se trouvent les codes de segments hexadécimaux correspondant aux valeurs décimales
'0123456789
' 0x3F 0x06 0x5B 0x4F 0x66 0x6D 0x7D 0x07 0x7F 0x67
'
'****************** ******************Déclarer les variables et les
constantes**************************** ******************
'
' Placez le fichier INCLUDE d'initialisation PIC ici (voir les fichiers de configuration PBP PIC)
INCLUDE "PIC16F18346_Initialize.pbp"
'
ANSELB = 000000 ' Le port B est utilisé pour toutes les broches d'interface
TRISB = 000000

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)

HIGH SDA 'Initialiser la sortie de données


HIGH STB 'Initialiser la sortie stroboscopique

i VAR BYTE'boucle variable à usage multiple


j VAR BYTE 'variable de boucle à usage multiple

'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)

B1 VAR BYTE 'B1 est la première valeur d'octet du clavier


B2 VAR BYTE 'B2 est la valeur du deuxième octet du clavier
B3 VAR BYTE 'B3 est la valeur du troisième octet du clavier
B4 VAR BYTE 'B4 est la valeur du quatrième octet du clavier
KEYNO VAR BYTE 'KEYNO est le numéro de clé ou le code hexadécimal du numéro de clé

'
'************************ ****************************** PRINCIPALE
******************* **************************************

GOSUB CLEARALL' Efface l'affichage avec tous les caractères "-"

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

' Cette boucle envoie les caractères 0 à 7 à l'affichage à 8 chiffres


Pour i = 0 à 7
RECHERCHE i,[$3F,$06,$5B,$4F,$66,$6D,$7D,$07],j 'Recherche le code hexadécimal pour chaque
chiffre d'affichage
Bas STB
SHIFTOUT SDA, SCK, 0, [$C0 + 2 *i,j] 'Envoyer des adresses de nombre pair pour les chiffres à 7
segments et le chiffre de sortie
Élevé STB
PAUSE 500 'Pause 1/2 seconde
Suivant i

GOSUB CLEARALL 'Effacer l'affichage à tous les caractères "-"

'Cette boucle envoie les caractères 8 à F à l'affichage à 8 chiffres


Pour i = 0 à 7
RECHERCHE i,[$7F,$67,$77,$7C,$39,$5E,$79,$71],j 'Recherche le code hexadécimal pour chaque
chiffre d'affichage
LOW STB
SHIFTOUT SDA, SCK, 0, [$C0 + 2*i,j] 'Envoyer des adresses de nombre pair pour les chiffres à 7
segments et le chiffre de sortie
HIGH STB
PAUSE 500 'Pause 1/2 seconde
Suivant i

GOSUB CLEARALL 'Effacer l'affichage à tous Caractères "-"

'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
'************************* ********************************************************
**************************************

READKEY : « READKEY ne traite que les entrées à une seule touche


'Envoyer la commande de lecture de touche et déplacer les 4 octets de la matrice de touches
LOW STB
SHIFTOUT SDA, SCK, 0, [CMD2] 'Envoyer la commande de lecture clavier CMD2 = 42 $
PAUSEUS 10
SHIFTIN SDA, SCK, 1, [B1,B2,B3, B4] 'Décaler les 4 octets du clavier
PAUSEUS 1
HIGH STB
PAUSE 50 'Délai pour permettre le relâchement de

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

'Convertit le numéro de clé 0 à 7 en un code hexadécimal à 7 segments 1 à 8


RECHERCHE i,[$06,$5B,$4F,$66,$6D,$7D,$07,$7F],KEYNO 'Redéfinir KEYNO comme code
hexadécimal d'affichage

LOW STB' Activer la sélection de puce


SHIFTOUT SDA, SCK, 0, [CMD1 ] 'Cela décale le caractère de commande d'écriture 8 bits $44
HIGH STB 'pour l'adressage en mode fixe (accès aléatoire)
PAUSE 1

'Affiche le numéro de clé à l'emplacement d'affichage correspondant et fait clignoter la LED


plusieurs fois
LOW STB
SHIFTOUT SDA, SCK, 0, [$C0+(2*i),KEYNO] 'Déplacer le numéro de clé vers le chiffre à 7 segments
à l'
emplacement de la clé correspondante HIGH STB'
PAUSE 1
Pour j = 0 à i 'Flasher la LED le nombre de fois de la touche
LOW STB
SHIFTOUT SDA, SCK, 0, [$C1+(2*i),$01] 'Masquer l'octet d'adresse et l'octet de données vers la
LED discrète
HIGH STB 'pour allumer la LED pendant 1/4 de seconde
PAUSE 250
LOW STB
SHIFTOUT SDA, SCK, 0, [$C1+(2*i),$00] 'Shiftout octet d'adresse et octet de données vers la LED
discrète
HIGH STB 'pour éteindre la LED pendant 1/4 seconde
PAUSE 250
Suivant j 'Répétez le nombre de fois égal au numéro de clé

LOW STB
SHIFTOUT SDA, SCK, 0, [CMD3] ' La commande de sortie envoie CMD3 qui allume l'afficheur
HIGH STB

GOSUB CLEARALL

GOTO READKEY ' Revenir au début et attendre un autre appuyez sur


la touche '****************************************** Sous-programme EFFACER TOUT **
********************************************************
'Cette section efface les LED et définit les chiffres à 7 segments sur "-" dans la mémoire d'affichage
du
TM1638 CLEARALL :
LOW STB 'Activer la sélection de puce
SHIFTOUT SDA, SCK, 0, [CMD1] 'Ceci décale le caractère de commande d'écriture 8 bits $44
HIGH STB 'pour l'adressage en mode fixe (accès aléatoire)
PAUSE 1

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é

PAUSE 500 « attendre 1/2 seconde après l' effacement de l'affichage


RETURN

» NOTES RELATIVES À LA MATRICE KEY:


» Sur cet ensemble afficheur/touche, les 8 interrupteurs sont câblés dans la colonne matricielle
K3/KS1-KS8 (voir fiche technique TM1638 V1.3
' Figures(3)&(4) en page 7). Cela signifie que les commutateurs S1-S4 activent le lsb "B0" dans les
octets 1-4 respectivement, et les commutateurs
S5-S8 activent le bit "B4" dans les octets 1-4 respectivement.
' et les OR pour créer un seul octet KEYNO avec les 8 bits de commutation présents.
'
' Numéro de bit : B7 B6 B5 B4 B3 B2 B1 B0
' Commutateur #1 0 0 0 0 0 0 0 1 Octet 1 - non décalé vers la gauche
' Commutateur #2 0 0 0 0 0 0 0 1 0 Octet 2 - décalé vers la gauche 1 bit
' Commutateur #3 0 0 0 0 0 1 0 0 Octet 3 - décalé à gauche de 2 bits
' Commutateur #4 0 0 0 0 1 0 0 0 Octet 4 - décalé à gauche de 3 bits
' Commutateur #5 0 0 0 1 0 0 1 0 Octet 1 - non décalé vers la gauche
' Commutateur #6 0 0 1 0 0 0 1 0 Octet 2 - décalé vers la gauche 1 bit
' Commutateur #7 0 1 0 0 0 0 1 0 Octet 3 - décalé vers la gauche 2 bits
' Commutateur #8 1 0 0 0 0 0 1 0 Octet 4 - décalé vers la gauche 3 bits
'
' KEYNO OR's Value 1 1 1 1 1 1 1 1 SEULS LES APPUIES SUR UNE SEULE TOUCHE SONT AUTORISÉS
' DONC SEULEMENT 1 BITS SUR 8 VA = 1
'
' LOOKDOWN RECHERCHE $80 $40 $20 $10 $08 $04 $02 $01 LOOKDOWN convertit KEYNO au
numéro de clé
' VALEUR DE RECHERCHE 7 6 5 4 3 2 1 0
'
' Touche : S8 S7 S6 S5 S4 S3 S2 S1 Numéros de commutateur 1 - 8
' Matrix Loc : K3/KS8 K3/KS6 K3/KS4 K3/KS2 K3/KS7 K3/KS5 K3/KS3 K3/KS1 TM1638 Emplacement de
la matrice
' Numéro d'octet : B4 B3 B2 B1 B4 B3 B2 B1 Lecture de la clé suivante 0x42 commande
' Code hexadécimal de la clé : 0x10 0x10 0x10 0x10 0x01 0x01 0x01 0x01 Code hexadécimal non
décalé
' Décalé à gauche : 0x80 0x40 0x20 0x10 0x08 0x04 0x20 0x01 Code hexadécimal décalé
' Key# Code :0x7F 0x07 0x7D 0x6D 0x66 0x4F 0x5B 0x06 Valeur de chiffre et LED 7 Seg Code
' Chiffre affiché : 8 7 6 5 4 3 2 1
'
'************************************ ** Fin de la lecture du clavier
******************************************** ********

You might also like