Random schakelaar


 


Schema:

Print (PDF file): PDF file met print layout
Source code:

;;



;; 
;;    random.asm, a PIC 16C84 (or PIC 16F84) program to randomly switch on
;;    and off a LED.
;;    Copyright (C) 2000  Geert Van Espen (geert.van.espen@bigfoot.com)
;;
;;    The LED stays on for a minimum of 8 minutes and a maximum of 15
;;    minutes, then it stays of for a minimum of 16 minutes and a maximum
;;    of 31 minutes. Thus the average on-time is 11 minutes 30 seconds,
;;    and the average off time is 23 minutes 30 seconds.
;;
;;    The LED should be connected at RB0 (pin 6) with a series resistor of
;;    470 Ohm (at Vdd = 5V).
;;
;;    I actually replaced the LED with a transistor, relay (and protection
;;    diode). In this way I switch on and off my 220V aquarium lights in a
;;    random fashion.
;;
;;    This program uses the watchdog feature of the PIC, in combination
;;    with the SLEEP command for timing purposes. If you are using
;;    a home-made programmer for programming the device, you must use
;;    a command like this to do it right:
;;
;;    TOPIC -RWG random.hex
;;
;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the11-1307  USA
;;
        
        processor 16c84
        include "p16c84.inc"

                                ; general purpose register allocation


werk    equ     0x0c            ; werkregister
ddd     equ     0x14            ; debug only
t1      equ     0x0d            ; telwerk 1
t2      equ     0x0e            ; telwerk 2
        
ranl    equ     0x10            ; 24-bit random number
ranm    equ     0x11
ranh    equ     0x12


m_input equ     b'00000000'     ; input mask, which bits are input

        org     0x00            ; reset vector
        goto    main

        dt      "2000-06-04 geert.van.espen@bigfoot.com random"

reinitialise:                   ; set up ports
        ;clrf    TMR0
        ;bsf     STATUS,RP0      ; select register bank 1
        bcf     STATUS,RP1
        bsf     STATUS,RP0
        clrwdt
        movlw   b'00000000'     ; assign 1:1 prescaler to WDT
        movwf   OPTION_REG
        movlw   m_input
        movwf   TRISB
        bcf     STATUS,RP0      ; select register bank 0

        clrf    PORTB           ; LED uit


; LED even aan en uit laten knipperen om te laten zien dat het
; apparaat werkt
        movlw   0xff
        movwf   PORTB

        movf    ranl,w          ; knipper willekeurig aantal keer
        andlw   7               ; knipper max. 8 keer
        movwf   t1
        incf    t1,f
        movwf   ddd             ; debug only
functiontest:
        movlw   0x19            ; 25 x 20 ms = 0.5 seconde periodetijd
        movwf   t2
        
functiontest1:
        sleep                   ;
        decfsz  t2,f            ;
        goto    functiontest1   ;

        movlw   0xff            ;
        btfsc   PORTB,0         ;
        movlw   0               ;
        movwf   PORTB           ;
        decfsz  t1,f            ;
        goto    functiontest    ;
        
        clrf    PORTB           ;

        retlw   0               ; return with 0 in w

initialise:                     ; clear the random number seed
        ;movlw   0xa5
        movf    PORTA,w         ; zet ongeinitialiseerd register in w
        movwf   ranh            ;
        movwf   ranm            ;
        movwf   ranl            ;
        retlw   0               ; return with 0 in w

zet_aan:
        movlw   0xff            ; LED aan
        movwf   PORTB           ;
        movf    ranl,w          ; haal random waarde op en zet in w
        andlw   7               ; beperk 0 -> 7
        addlw   8               ; 8 -> 15
        call    wachtlus        ;

        retlw   0               ; return with 0 in w

zet_uit:
        clrf    PORTB           ; LED uit
        movf    ranl,w          ; haal random waarde op en zet in w
        andlw   0xf             ; beperk 0 -> 15
        addlw   0x10            ; 8 -> 15
        call    wachtlus        ;

        retlw   0               ; return with 0 in w

wachtlus:
; wacht x minuten, met x in w
        movwf   werk            ; zet w in werk
        movwf   ddd             ; debug purposes

slaper:
        movlw   0x65            ;
        movwf   t1              ;
slapertje:
        movlw   0x1f            ;
        movwf   t2              ;
slapertje2:
        sleep
        decfsz  t2,f            ;
        goto    slapertje2      ;
        decfsz  t1,f            ;
        goto    slapertje       ;
        decfsz  werk,f          ;
        goto    slaper          ;

        retlw   0               ; return with 0 in w

random:                         ; find next random number
        movf    ranl,w          ; check total value zero
        iorwf   ranh,w          ;
        btfsc   STATUS,Z        ;
        comf    ranh,f          ; invert some bits if so
        movlw   0x80            ;
        btfsc   ranh,6          ;
        xorwf   ranh,f          ;
        btfsc   ranh,4          ;
        xorwf   ranh,f          ;
        btfsc   ranl,3          ;
        xorwf   ranh,f          ;
        bcf     STATUS,C        ; clear the carry flag
        btfsc   ranh,7          ; if the high bit is set ...
        bsf     STATUS,C        ; ... set also the carry flag
        rlf     ranl,f          ;
        rlf     ranm,f          ;
        rlf     ranh,f          ;
        retlw   0               ; return with 0 in w

main:
        ;btfsc   STATUS,NOT_TO   ; if this is a reset or power-up ...
        call    initialise      ; ... clear random number seed
        call    reinitialise    ; set port drivers and WDT prescaler

main_1:
        call    random          ; find next random number
        call    zet_aan         ; LED aan zetten gedurende willek. tijd
        call    random          ; find next random number
        call    zet_uit         ; LED uit zetten gedurende willek. tijd
        goto    main_1          ; en weer opnieuw!

        ;; 
        ;;
        
        end


Het apparaat ingebouwd in een stukje PVC afvoerbuis.

Foto van het afgewerkte apparaat.

  Home         Terug naar Electronica projecten