Myke's Home Page

Book CD-ROM Home

File Copying/Harddrive Setup

Development Tools

Experiments

Projects

Useful Code Snippets and Macros

Introduction to Electronics

Introduction to Programming

Datasheets

PCBs

Links

McGraw-Hill Professional Publishing

"Random" Experiment

This 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:

Part Description Required for the YAP-II/EMU-II?
PICmicro® MCU PIC16F84-04/P
PIC16F877-04/P
In Socket
Vdd/Vss Decoupling Capacitor 0.1 uF (Any Type) No
_MCLR Pull Up Resistor 10K, 1/4 Watt No
4 MHz Ceramic Resonator Three Leaded Ceramic Resonator with Built in 27-33pF Capacitors No
RA0 Pull Up 10K, 1/4 Watt No - "BUT1" Used
RA0 Push Button Momentary On/Modified for Breadboard No - "BUT1" Used
PORTB LED Current Limiting Resistors 8x 220W, 1/4 Watt No - "LED1" Used
PORTB LED 10 LED "Bargraph" Recommended No - "LED1" through "LED9" Used
Breadboard Any Type No
+5 Volt "Vcc" Power Supply Any Type No

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
              

Click Here to look at the twenty ninth experiment - Sleep