Myke's Home Page
|
"Random" ExperimentThis experiment demonstrates how TMR0 can be used to provide an essentially random value everytime a button is pressed by a user. This application uses the same TMR0 running with the Debounce switch macro.
The experiment uses the circuit shown below:
The parts needed for this experiment are listed in the table:
Using a breadboard, the experiment is wired using the guide:
If the EMU-II or YAP-II is used, the experiment is wired as:
The source code listed below can be accessed from the CD-ROM by clicking Here.
title "Random - Produce a Random Number Generator with TMR0"
;
; This Code Displays TMR0 on RB0 every time the Button on RA0
; is Pressed.
;
;
; Hardware Notes:
; PIC16F84 Running at 4 MHz
; _MCLR is Pulled Up
; All 8 bits of PORTB are Pulled up and Connected to LEDs
; PORTA.0 is Pulled up with a Momentary On Pulling to Ground
;
; Myke Predko
; 99.12.26
;
LIST R=DEC
ifdef __16F84
INCLUDE "p16f84.inc"
else
ifdef __16F877
INCLUDE "p16f877.inc"
endif
; Register Usage
CBLOCK 0x020 ; Start Registers at End of the Values
Dlay:2 ; Delay Value
ENDC
Up EQU 1 ; Flag Value for Debounce "Up"
Down EQU -1 ; Flag Value for Debounce "Down"
; Macros
Debounce MACRO Direction
if (Direction < 0) ; Going Down
btfsc PORTA, 0
else
btfss PORTA, 0 ; Wait for Button Released
endif
goto $ - 1
movlw 0x0100 - 0x0C4 ; Initialize Dlay for a 20 msec
movwf Dlay ; Delay
movlw 0x0100 - 0x00A
movwf Dlay + 1
bcf STATUS, Z ; Make Sure that Zero is Reset
incfsz Dlay, f
goto $ + 2
incf Dlay + 1, f
if (Direction < 0)
btfsc PORTA, 0
else
btfss PORTA, 0 ; Button Still Released?
endif
goto $ - 11 ; No - Loop Around Again
btfss STATUS, Z ; Zero Flag Set (20 mSecs Past?)
goto $ - 6
endm ; End the Macro
PAGE
ifdef __16F84
__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON
else
__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON & _DEBUG_OFF & _LVP_OFF & _BODEN_OFF
endif
; Note that the WatchDog Timer is OFF
; Mainline of Random
org 0
nop
movlw 0x0FF ; Turn off all the LEDs initially
movwf PORTB
bsf STATUS, RP0
clrf TRISB ^ 0x080 ; Make All 8 PortB Bits Output
movlw 0x0D0 ; Assign Prescaler of 1:1 to TMR0
movwf OPTION_REG ^ 0x080 ; Load the Option Register Value
bcf STATUS, RP0
Loop ; Loop Here
Debounce Up ; Wait for Key to Go Up
nop ; Location for Stopping after
; "Up" Debounce
Debounce Down ; Wait for Key to Go Down
comf TMR0, w ; Output the TMR0 Value
movwf PORTB
goto Loop
end
|