I'm trying to figure out a way to randomly pop individual ports on a 16f628a.
I've looked at some of the examples but they aren't very clear in how they work.
What I'd like to have ideally is just a single random bit value so I can turn on individual ports with the method below (code below is still untested, and somewhat pseudo):
Am I even going about this the right way?
Thanks!!!
I've looked at some of the examples but they aren't very clear in how they work.
What I'd like to have ideally is just a single random bit value so I can turn on individual ports with the method below (code below is still untested, and somewhat pseudo):
Code:
; Based on the 'Tutorial 1.6' by Nigel Goodwin
LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
__config 0x3D18 ;sets the configuration settings (oscillator type etc.)
cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc
LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDTRIS Equ PORTB ;set constant for TRIS register
SWPORT Equ PORTA ;set constant SWPORT = 'PORTA'
SWTRIS Equ PORTA ;set constant for TRIS register
; ===============================
; set constants for the switches
; ===============================
SWFOOT Equ 7 ;foot switch
SWRESET Equ 6 ;back reset switch
SWTEST Equ 5 ;internal test switch
; ===============================
; set constants for the outputs
; ===============================
LEDB0 Equ 0 ;blink port 1
LEDB1 Equ 1 ;blink port 2
LEDB2 Equ 2 ;blink port 3
LEDB3 Equ 3 ;blink port 4
LEDPWR Equ 7 ;power led
BUZZER Equ 6 ;buzzer
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
; ===============================
; Initialize Ports
; ===============================
bsf STATUS, RP0 ;select bank 1
movlw b'00000000' ;set PortB all outputs
movwf LEDTRIS
bcf STATUS, RP0 ;select bank 0
clrf LEDPORT ;set all outputs low
movlw b'11111111' ;set PortA all inputs
movwf SWTRIS
; ===============================
; Initialize LEDs
; ===============================
bsf LEDPORT, LEDPWR ;turn power light on
; ===============================
; Main Loop
; ===============================
Main
btfss SWPORT, SWTEST ;Check for internal test button press
call BlinkTest ;This line gets skipped over if the switch isn't pushed
<generate_random_bit>
<if random bit = 1 then>
bsf LEDPORT, LEDB0 ;turn blink light on
<if random bit = 0 then>
bcf LEDPORT, LEDB0 ;turn blink light off
call Delay ;this waits for a while!
goto Main ;Endless Loop
BlinkTest
; ============================================================
; This routine just scans through all the ports on LEDPORT in
; a pre-defined pattern for testing
; ============================================================
movlw b'11111111' ;initial port test
movwf LEDPORT
call Delay ;this waits for a while!
call Delay ;this waits for a while!
call Delay ;this waits for a while!
call Delay ;this waits for a while!
movlw b'00000000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000001'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000010'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000100'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00001000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00010000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00001000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000100'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000010'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000001'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000000'
movwf LEDPORT
call Delay ;this waits for a while!
; ============================================================
; check to see if the reset button is being held.
; If it is, jump out of the test loop back into the main loop
; ============================================================
btfss SWPORT, SWRESET ; Check for button push, and reset if necessary
call Main ; This line gets skipped over if the switch isn't pushed
goto BlinkTest ;go back and do it again
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
end
Am I even going about this the right way?
Thanks!!!
Last edited: