;______________________________________________________________
LIST P=18F4550 ;directive to define processor
#include <P18F4550.INC> ;processor specific variable definitions
#include "migrate.inc"
Errorlevel -302
;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please
;see the .inc file for futher details on notation. Below are a few examples.
; Oscillator Selection:
;CONFIG FOSC = HS ;XT oscillator, XT used by USB
config FOSC = HSPLL_HS ; HS
config PLLDIV = 5 ; PLL prescaler i.e. 20/5=4 MHz (96MHz PLL)
config CPUDIV = OSC1_PLL2 ; PLL divided by 2 (96/2=48MHz) to feed CPU;
config USBDIV = 2 ; if full speed (FSEN=1) USB is fed by PLL
; divided by 2 (96/2=48MHz)
config IESO = OFF
config PWRT = OFF
config BOR = OFF
config BORV = 2
config VREGEN = ON
config WDT = OFF
config WDTPS = 32768
config MCLRE = OFF
config LPT1OSC = OFF
config PBADEN = OFF
config CCP2MX = OFF
config STVREN = OFF
config LVP = OFF
config ICPRT = OFF
config XINST = OFF
config DEBUG = OFF
config CP0 = OFF
config CP1 = OFF
config CP2 = OFF
config CPB = OFF
config CPD = OFF
config WRT0 = OFF
config WRT1 = OFF
config WRT2 = OFF
config WRT3 = OFF
config WRTB = OFF
config WRTC = OFF
config WRTD = OFF
config EBTR0 = OFF
config EBTR1 = OFF
config EBTR2 = OFF
config EBTRB = OFF
;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used.
; More variables may be needed to store other special function registers used
; in the interrupt routines.
CBLOCK 0x080
WREG_TEMP ;variable used for context saving
STATUS_TEMP ;variable used for context saving
BSR_TEMP ;variable used for context saving
RAND1 ;random file
SENSTAT ;veg status hit-delay-over
BUTTON1 ;start veg
;INT_PORTS
Random
count1
counta
countb
DEALY25
DEALY50
DEALY100
CounterA
CounterB
CounterC
EXAMPLE ;example of a variable in access RAM
ENDC
;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here
ORG 0xf00000
DE "Test Data",0,1,2,3,4,5
;******************************************************************************
;Reset vector
; This code will start executing when a reset occurs.
ORG 0x0000
goto Main1 ;go to start of main code
;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.
ORG 0x0008
bra HighInt ;go to high priority interrupt routine
;******************************************************************************
;Low priority interrupt vector and routine
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.
ORG 0x0018
;movff STATUS,STATUS_TEMP ;save STATUS register
;movff WREG,WREG_TEMP ;save working register
;movff BSR,BSR_TEMP ;save BSR register
; movff BSR_TEMP,BSR ;restore BSR register
; movff WREG_TEMP,WREG ;restore working register
; movff STATUS_TEMP,STATUS ;restore STATUS register
; GOTO HOLD
; retfie
;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here to avoid conflicting with
; the low priority interrupt vector.
HighInt:; *** high priority interrupt code goes here ***
OVER
MOVLW B'00000000' ;all Veg down
MOVWF PORTD
MOVLW B'00000010' ;RC1 time gate reset
MOVWF PORTC
CALL DELAY100
MOVLW b'00000000' ;
MOVWF INTCON;,GIE
CLRF BUTTON1
GOTO HOLD_INT
;retfie FAST
; Start of main program
; The main program code is placed here.
Main1:
GOTO INT_PORTS
Main2
GOTO HOLD
GOTO Main2
;********** INITIALIZING ***********
INT_PORTS
CLRF UCON ;disable usb
CLRF LATA ;clear latches
MOVLW 07h ;turn off compartors
MOVWF CMCON
CLRF PORTA ;******PortA INPUT not used
MOVLW 0Fh
MOVWF ADCON1
MOVLW 0fh ;inputs
MOVWF TRISA
CLRF PORTB ;*****PortB INPUT
CLRF LATB ;clear latches
MOVLW 0Fh ;1111
MOVWF ADCON1
MOVLW b'11111111' ;inputs
MOVWF TRISB
CLRF PORTC ;*****PortC Output
CLRF LATC ;clear latches
MOVLW b'00000000' ;outputs
MOVWF TRISC
CLRF PORTD ;********PortD OUTPUT
CLRF LATD ;clear latches
MOVLW b'00000000' ;outputs
MOVWF TRISD
CLRF PORTE
CLRF LATE ;clear latches
MOVLW 07h ;turn off comparators
MOVWF CMCON
MOVLW b'00000000' ;outputs
MOVWF TRISE
MOVLW b'11011000' ;
MOVWF b'01111111'
MOVWF OSCTUNE
MOVWF b'00000000'
MOVWF WDTCON
MOVWF INTCON;,GIE
bcf RCON,IPEN ;disable priorities and enable Compatibility Mode
GOTO Main2
;***************************
;clear all ports coming back to the hold function for restart.
HOLD_INT
MOVLW b'11011000' ;restore inturrupts
MOVWF INTCON;
CLRF PORTA
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF PORTE
HOLD
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF Random
CLRF BUTTON1
MOVLW b'00000010' ;clear latch RC1=time gate reset
MOVWF PORTC ;PortC Bits only work RCO,RC1,RC2,RC6, RC7
CALL DELAY10
MOVLW B'00000000' ;clear portC
MOVWF PORTC
MOVLW B'00000000' ;clear portB inputs
MOVWF PORTB
GOTO HOLD2
HOLD2 ;inter loop
MOVF PORTB,W ;PORT-B, RB3 (INPUT) Stat Button pressed
MOVWF BUTTON1 ;Button
MOVF BUTTON1,W
XORLW B'00000110' ; Fast-Start Bit0 speed, bit1 start
BTFSC STATUS,Z
GOTO VegFAST ;goto Fast veg
MOVF BUTTON1,W
XORLW B'00000100' ; Slow-start bit0 speed, bit1 start
BTFSC STATUS,Z
GOTO VegSLOW ;goto Slow veg
GOTO HOLD2
VegSLOW ;This is the Main loop to run until timer is complete.
SetupS
MOVLW B'10000000' ;s-clear close relay RC7 can start timer same time also RC7.
MOVWF PORTC
CALL DELAY10
MOVLW B'00000000' ;S-clear open relay RC7
MOVWF PORTC
MOVLW B'00000100' ;(Start Timer) RC2 high start timer card via opto.
MOVWF PORTC
CALL DELAY10
MOVLW B'00000000' ;(Timer Running) RC2 timer remains running
MOVWF PORTC
MOVLW B'00000011' ;Reset RC1 Time Gate,,RC0=enable time gate
MOVWF PORTC
CALL DELAY10
MOVLW B'00000001' ;RC0 time gate enabled remain on to end
MOVWF PORTC
GOTO SlowRAND
;Best Random Code ----------------------------------------------
SlowRAND
movlw .123
movwf Random
ReadS call RandS ;
call RandS ;Call Rand 4 times
call RandS ;
call RandS ;
movf Random,W ;
andlw d'15' ;
sublw d'13' ;allow 0 to 13 only
btfss STATUS,C ; ;
goto ReadS ;if not 15 call Rand one mor time again;;New code
movf Random,W ;move # in Random to count number in table.
andlw .15
call TableS
movwf PORTD
call DelaySlow
goto ReadS
TableS ADDWF PCL, f ;data table for bit pattern on PORT D
retlw b'00001000'
retlw b'00000001'
retlw b'00000010'
retlw b'00000100'
retlw b'00000000'
retlw b'00000100'
retlw b'00000010'
retlw b'00000001'
retlw b'00000010'
retlw b'00000100'
retlw b'00001000'
retlw b'00010000'
retlw b'00000100'
retlw b'00001000'
RandS rlcf Random,w ;shift and move to W
andlw 0x82 ;keep bits 7 and 1
addlw 0x7e ;xor into bit 7
addlw 0x80 ;move to carry bit
rlcf Random,f ;shift into seed
return
DelaySlow
return; added for testing*******Remove
movlw d'250' ;delay 250 ms (4 MHz-clock)
movwf count1
S1 movlw 0xC7 ;change values for speed or call more times
movwf counta
movlw 0x01
movwf countb
Delay_S
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_S
decfsz count1 ,f
goto S1
retlw 0x00
;-------------------------------------------------------------
VegFAST ;This is the Main loop to run until timer is complete.
SetupF
MOVLW B'10000000' ;s-clear close relay RC7
MOVWF PORTC
CALL DELAY10
MOVLW B'00000000' ;S-clear open relay RC7
MOVWF PORTC
MOVLW B'00000100' ;(Start Timer) RC2 high start timer card via opto.
MOVWF PORTC
CALL DELAY10
MOVLW B'00000000' ;(Timer Running) RC2 timer remains running
MOVWF PORTC
MOVLW B'00000011' ;Reset RC1 Time Gate,,RC0=enable time gate
MOVWF PORTC
CALL DELAY10
MOVLW B'00000001' ;RC0 time gate enabled remain on to end
MOVWF PORTC
GOTO FastRAND
;Best Random Code ----------------------------------------------
FastRAND movlw .123
movwf Random
ReadF call RandF ;
call RandF ;Call Rand 4 times
call RandF ;
call RandF ;
movf Random,W ;
andlw d'15' ;
sublw d'13' ;allow 0 to 13 only
btfss STATUS,C ;
goto ReadF ;if not 15 call Rand one mor time again;;New code
movf Random,W ;move # in Random to count number in table.
andlw .15
call TableF
movwf PORTD
call DelayFast
goto ReadF
TableF ADDWF PCL, f ;data table for bit pattern
retlw b'00001000'
retlw b'00000001'
retlw b'00000010'
retlw b'00000100'
retlw b'00000000'
retlw b'00000100'
retlw b'00000010'
retlw b'00000001'
retlw b'00000010'
retlw b'00000100'
retlw b'00001000'
retlw b'00010000'
retlw b'00000100'
retlw b'00001000'
RandF rlcf Random,w ;shift and move to W
andlw 0x82 ;keep bits 7 and 1
addlw 0x7e ;xor into bit 7
addlw 0x80 ;move to carry bit
rlcf Random,f ;shift into seed
return
DelayFast
return; added for testing
movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
F1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_F
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_F
decfsz count1 ,f
goto F1
retlw 0x00
;-------------------------------------------------------------
DELAY10
return ;**********************////??Remove
movlw D'25'
movwf CounterC
movlw D'10'
movwf CounterB
movlw D'1'
movwf CounterA
LOOP10
decfsz CounterA,f
goto LOOP10
decfsz CounterB,f
goto LOOP10
decfsz CounterC,f
goto LOOP10
RETURN
END