Étape 2: Code
Afin d’avoir la batterie dernière aussi longtemps que possible, le circuit devait utiliser le moins de puissance que je pourrais comprendre, tout en gardant l’idée vivante. Alors, j’ai décidé de réduire l’activité initiale à une moyenne de 1 flash LED, ou le moteur se déplacent toutes les 6 secondes.
Le code a 12 « activités » au hasard, allant de 1 LED mise en marche, le moteur allumer pour différentes périodes de temps ou de directives, de manière aléatoire attendre État. Événements varient de 3 secondes d’écart à plus de 40 secondes d’écart, basée sur l’événement aléatoire généré.
CODE
;===============================================================================
Contrôleur de disque
;
; -----------
; VCC -> |1 8| <-Vss
; M <> – GPIO5 |2 7| GPIO0---> DEL1
; M <> – GPIO4 |3 6| GPIO1---> LED2
; GPIO3 -> |4 5| GPIO2---> DEL3
; -----------
;
;===============================================================================
; Historique des révisions & Notes :
; En-tête Initial v1.0, Code 19/05/09
;
;
; (C) 5/2009
; Ce code peut être utilisé pour les personnels d’apprentissage/demande/modification.
; Toute utilisation de ce code dans les produits commerciaux viole cette version freeware.
; Pour questions ou commentaires, contactez circuit dot mage at yahoo dot com..
;-------------------------------------------------------------------------------
#include P12C672. INC
;===============================================================================
; Définit
;-------------------------------------------------------------------------------
;===============================================================================
; Données
;-------------------------------------------------------------------------------
; Variables de maintien fois
count1 equ 20
count2 equ 21
retarder l’equ 22
Randlo equ 23
Ludovic equ 24
Wtemp equ 25
Temp2 equ 26
Rand equ 27
Count3 equ 28
;===============================================================================
; Réinitialiser les vecteurs
;
; VÉRIFIER LA CONFIG. BITS AVANT LA GRAVURE!!!
; INTOSC
; MCLR : ACTIVÉ
; PWRUP : ACTIVÉ
; TOUS LES AUTRES : DÉSACTIVER!!
;
;-------------------------------------------------------------------------------
RESET_ADDR EQU 0 X 00
org RESET_ADDR
Goto début
;===============================================================================
; Commencez ici !
;-------------------------------------------------------------------------------
début
; Ports e/s de config
BCF STATUS, RP1
BSF STATUS, RP0
movlw h « 08 » ; RA < 0-7 > sorties, PGIO3 toujours d’entrée
Tris GPIO
movlw h « 07 » ; GPIO réglée en mode numérique
movwf ADCON1
; Réglez le minuteur interne
movlw h'CF' ; Source interne Tmr0, prescale TMR0 1: 256
movwf OPTION_REG
movlw h « 00 »
movwf INTCON ; Désactiver les interruptions TMR0,
BCF STATUS, RP0
; Initialiser des registres
CLRF GPIO
CLRF count1
CLRF count2
movlw 045h
movwf Randlo
movlw 030h
movwf Laurence
; attendre 1 seconde.
appel debounce ; 0,2 sec
appel debounce
appel debounce
appel debounce
appel debounce
;===============================================================================
; Principal
;-------------------------------------------------------------------------------
principal
appel twosec ; min 2 sec entre chaque action
RRF Laurence, W
xorwf Randlo, W
movwf Wtemp
swapf Wtemp
RLF Laurence, W
xorwf Laurence, W ; LSB = xorwf(Q12,Q3)
xorwf Wtemp
RLF Wtemp
RLF Randlo
RLF Laurence
movfw Wtemp ; bande 16 aléatoire jusqu'à 7
andlw 0x0F
movwf rand
; sélection aléatoire de routine
xorlw 0 x 00 ; 0 ?
btfsc STATUS, Z
Goto flash1 ; Oui. Appelez 0E
movfw rand
xorlw 0 x 01 ; 1 ?
btfsc STATUS, Z
Goto flash2 ; Oui. Composez le 1er
movfw rand
xorlw 0 x 02 ; 2 ?
btfsc STATUS, Z
Goto flash3 ; Oui. Composez le 2ème
movfw rand
xorlw 0 x 03 ; 3 ?
btfsc STATUS, Z
Goto flashall ; Oui. Appel 3ème
movfw rand
xorlw 0 x 04 ; 4 ?
btfsc STATUS, Z
Goto movels ; Oui. Composez le 4ème
movfw rand
xorlw 0 x 05 ; 5 ?
btfsc STATUS, Z
Goto movell ; Oui. Composez le 5e
movfw rand
xorlw 0 x 06 ; 6 ?
btfsc STATUS, Z
Déménageurs de Goto ; Oui. Composez le 6e
movfw rand
xorlw 0 x 07 ; 7 ?
btfsc STATUS, Z
Goto moverl ; Oui. Appelez 7
movfw rand
xorlw 0 x 08 ; 8 ?
btfsc STATUS, Z
Goto moveburst ; Oui. Composez le 8e
movfw rand
xorlw 0 x 09 ; 9 ?
btfsc STATUS, Z
Goto Wait1 ; Oui. Composez le 9e
movfw rand
xorlw 0x0A ; A ?
btfsc STATUS, Z
Goto Wait2 ; Oui. Appel Ath
movfw rand
xorlw 0x0B ; B ?
btfsc STATUS, Z
Goto Wait3 ; Oui. Appel Bth
Goto rien ; 1/4 du temps, nothing pour 10 secs.
flash1
BSF GPIO, 0
appel debounce
BCF GPIO, 0
Goto principal
flash2
BSF GPIO, 1
appel debounce
BCF GPIO, 1
Goto principal
Flash3
BSF GPIO, 2
appel debounce
BCF GPIO, 2
Goto principal
flashall
BSF GPIO, 0
BSF GPIO, 1
BSF GPIO, 2
appel debounce
appel debounce
BCF GPIO, 0
BCF GPIO, 1
BCF GPIO, 2
Goto principal
movels
BSF GPIO, 4
BCF GPIO, 5
appel debounce
BCF GPIO, 4
Goto principal
movell
BSF GPIO, 4
BCF GPIO, 5
appel debounce
appel debounce
BCF GPIO, 4
Goto principal
déménageurs
BCF GPIO, 4
BSF GPIO, 5
appel debounce
BCF GPIO, 5
Goto principal
moverl
BCF GPIO, 4
BSF GPIO, 5
appel debounce
appel debounce
BCF GPIO, 5
Goto principal
moveburst
BCF GPIO, 4
BSF GPIO, 5
appel debounce ; se déplacer à droite 3 fois, courtes rafales.
BCF GPIO, 5
appel debounce
appel debounce
BSF GPIO, 5
appel debounce
appel debounce
BCF GPIO, 5
appel debounce
appel debounce
BSF GPIO, 5
appel debounce
BCF GPIO, 5
appel debounce
appel debounce
appel debounce
appel debounce
BSF GPIO, 4 ; se déplacer à gauche 3 fois, de courtes rafales.
BCF GPIO, 5
appel debounce
appel debounce
BCF GPIO, 4
appel debounce
appel debounce
BSF GPIO, 4
appel debounce
appel debounce
BCF GPIO, 4
appel debounce
appel debounce
BSF GPIO, 4
appel debounce
appel debounce
BCF GPIO, 4
appel debounce
appel debounce
Goto principal
Wait1 ; Patientez 1
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
Goto principal
Wait2 ; Attendez 0,6 secondes
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
Goto principal
Wait3 ; Attendez 4 secondes
appel twosec
appel twosec
Goto principal
rien
movlw.50 ; Délai de 10 secondes au Total
movwf count3
nothing_loop
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
decfsz count3, F
Goto nothing_loop
Goto principal
;===============================================================================
; attendre 2 secondes
;-------------------------------------------------------------------------------
twosec
movlw.10 ; Retard de 2 secondes au Total
movwf count3
twosec_loop
movlw.255 ; Délai pour debounce 2/10 de seconde.
movwf count2
appel pon_wait
decfsz count3, F
Goto twosec_loop
retour
;===============================================================================
; Debounce signal
; 4 cycles de charge et d’appeler, 2 cycles pour retourner.
; 4 Tc Mhz:: count2 = 255 -> 0,2 s
;-------------------------------------------------------------------------------
Debounce
movlw 127 ; Délai pour debounce 1/10 de seconde.
movwf count2
appel pon_wait
retour
;-------------------------------------------------------------------------------
; count1 = 255 d:: 775 cycles à 0, + 3 cycles à retourner.
;-------------------------------------------------------------------------------
pon_wait
big_loopS
movlw.255
movwf count1
short_loopS
decfsz count1, F
Goto short_loopS
decfsz count2, F
Goto big_loopS
retour
fin