HI All,
I’m having a little trouble with setting my 16F268A to a lower frequency.
From what I have tried to work out, I think I have to include the line
BCF PCON, 3 ;oscillator to 48k
to select the lower frequency.
I have added this line and now my circuit does not work. When I remove the line, my circuit works just fine.
I guess there is more to it than just including this line.
My system is a matrix pad that waits for a 4 digit code then energises a latch relay which turns on an LED. If the code is wrong or star/hash is pressed it turns the LED off.
I’m sure my code is not the best way of doing it, I did not have time to look up the correct way, but my code does work as far as I know, I just want to slow the pic down. I'll fix the code later once I work out the problem with the frequency.
Also my circuit is a bit erratic [brining a finger near the wires causes erratic behaviour/breadboard], I tried my best to throw a load of 100nF caps at it, still nothing. I then had the idea of wrapping the 7 wires coming of the key pad in foil then grounding them – this seems to have done the trick.
Is this behaviour due to using a bread board with wires going everywhere? Is it due to the frequency of the PIC? This is one of my reasons for wanting to slow the PIC down. Is this correct? Will it make my circuit more stable? Do breadboards make unstable circuits?
My power supply comes off my laptops USB
Thanks
I’m having a little trouble with setting my 16F268A to a lower frequency.
From what I have tried to work out, I think I have to include the line
BCF PCON, 3 ;oscillator to 48k
to select the lower frequency.
I have added this line and now my circuit does not work. When I remove the line, my circuit works just fine.
I guess there is more to it than just including this line.
My system is a matrix pad that waits for a 4 digit code then energises a latch relay which turns on an LED. If the code is wrong or star/hash is pressed it turns the LED off.
I’m sure my code is not the best way of doing it, I did not have time to look up the correct way, but my code does work as far as I know, I just want to slow the pic down. I'll fix the code later once I work out the problem with the frequency.
Also my circuit is a bit erratic [brining a finger near the wires causes erratic behaviour/breadboard], I tried my best to throw a load of 100nF caps at it, still nothing. I then had the idea of wrapping the 7 wires coming of the key pad in foil then grounding them – this seems to have done the trick.
Is this behaviour due to using a bread board with wires going everywhere? Is it due to the frequency of the PIC? This is one of my reasons for wanting to slow the PIC down. Is this correct? Will it make my circuit more stable? Do breadboards make unstable circuits?
My power supply comes off my laptops USB
Thanks
Code:
LIST p=16F628a ;tell assembler what chip we are using
include "P16F628a.inc" ;include the defaults for the chip
__CONFIG _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
cblock 20h
Ncoin ;32
d1
d2
d3
d4
b1
b2
b3
f1 ;40
f2
f3
t1
t2
t3
t4
one1
one2
one3
h1 ;50
h2
h3
ms1
ms2
pin1 ;55
pin2 ;56
pin3 ;57
pin4 ;58
key1
key2 ;60
key3
key4
pinCount ;63
store ;64
quarterSec1
quarterSec2
endc
org 000h
goto SETUP
; org 004h
; goto INTERRUPT
SETUP:
;;;;;;;;;;;;;;BANK 1 BELOW;;;;;;;;;;;;;;
BSF STATUS,5 ;BANK 1
BCF PCON, 3 ;oscillator to 48k
BcF OPTION_REG,7 ;RBPU: PORT B PULL UPS 1=OFF 0=ON
BCF OPTION_REG,6 ;INTEDG: INTERRUPT EDGE
BCF OPTION_REG,5 ;T0CS: BIT5 CLOCK SOURCE, EXTERNAL = 1 INTERNAL CYCLE = 0
BCF OPTION_REG,4 ;T0SE: EDGE TRIGGER FOR EXTERNAL TRIGGER
BCF OPTION_REG,3 ;PSA: ASSIGNS PRESCALER TO Timer0=0 watch dog=1
BcF OPTION_REG,2 ;PS2:
BcF OPTION_REG,1 ;PS1:
BcF OPTION_REG,0 ;PS0: pre-scaler
movlw b'11000111'
movwf TRISB ;set PortB INPUTS/OUTPUTS
movlw b'00000000'
movwf TRISA ;set PortA INPUTS/OUTPUTS
;;;;;;;;;;;;;;BANK 0 BELOW;;;;;;;;;;;;;;
BCF STATUS,RP0 ;BANK 0
BcF INTCON,GIE ;Enable general interrupts GIE=bit 7
BCF INTCON,PEIE ;bit6 PERIPHERAL INTERRUPTS
BCF INTCON,T0IE ;bit5 TMR0 OVERFLOW INTERRUPT 1=ENABLE
BcF INTCON,INTE ;bit4 EXTERNAL INTERRUPT RB0 1=ON 0=OFF
BCF INTCON,RBIE ;bit3 PORT CHANGE RB INTERRUPT 0=OFF
BSF INTCON,T0IF ;bit2 TMR0 OVERFLOW INTERRUPT FLAG, MUST BE CLEARED IN SOFTWARE
BCF INTCON,INTF ;bit1 EXTERNAL INTERRUPT FLAG for RB0, must clear in software*** before *RETFIE*
BSF INTCON,0 ;bit0 RB <7:4> pin have changed state = 1 (must be cleared in software)
movlw 0x07 ; TURN OFF PORTa COMPARATOR movwf CMCON
movwf CMCON
call ON
call onesec
call OFF
bsf PORTB,3
bSf PORTB,4 ;inject 0 down wire 4
bsf PORTB,5
CLRF PORTA
CLRF pinCount
movlw b'11111111'
movwf PORTA
call onesec
movlw b'00000000'
movwf PORTA
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pin:
movlw d'1'
movwf pin1
movlw d'9'
movwf pin2
movlw d'8'
movwf pin3
movlw d'6'
movwf pin4
key_check:
bsf PORTB,3
bcf PORTB,4 ;inject 0 down wire 4
bsf PORTB,5
BTFSS PORTB,1
goto one
BTFSS PORTB,2
goto four
BTFSS PORTB,7
goto seven
BTFSS PORTB,6
goto star
bcf PORTB,3 ;inject 0 down wire 3
bsf PORTB,4
bsf PORTB,5
BTFSS PORTB,1
goto two
BTFSS PORTB,2
goto five
BTFSS PORTB,7
goto eight
BTFSS PORTB,6
goto zero
bsf PORTB,3
bsf PORTB,4
bcf PORTB,5 ;inject 0 down wire 5
BTFSS PORTB,1
goto three
BTFSS PORTB,2
goto six
BTFSS PORTB,7
goto nine
BTFSS PORTB,6
goto hash
goto key_check
one:
incf pinCount
movlw d'1'
movwf store
goto position
two:
incf pinCount
movlw d'2'
movwf store
goto position
three:
incf pinCount
movlw d'3'
movwf store
goto position
four:
incf pinCount
movlw d'4'
movwf store
goto position
five:
incf pinCount
movlw d'5'
movwf store
goto position
six:
incf pinCount
movlw d'6'
movwf store
goto position
seven:
incf pinCount
movlw d'7'
movwf store
goto position
eight:
incf pinCount
movlw d'8'
movwf store
goto position
nine:
incf pinCount
movlw d'9'
movwf store
goto position
zero:
incf pinCount
movlw d'10'
movwf store
goto position
star:
clrf pinCount
CALL OFF
goto key_check
hash:
clrf pinCount
CALL OFF
goto key_check
position:
movlw b'11000110' ;MASK PORTS (to check no keys are presses)
ANDWF PORTB,W ;APPLY MASK
XORLW b'11000110'
btfss STATUS,Z
goto $-4
movlw d'1'
xorwf pinCount,W
btfsc STATUS,Z
goto p1
;;;;;;;;;;;;;;;;;;;;;;;;;
movlw d'2'
xorwf pinCount,W
btfsc STATUS,Z
goto p2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
movlw d'3'
xorwf pinCount,W
btfsc STATUS,Z
goto p3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
movlw d'4'
xorwf pinCount,W
btfsc STATUS,Z
goto p4
p1:
bSf PORTA,3
call ms100
bcf PORTA,3
movfw store
movwf key1
goto key_check
p2:
bSf PORTA,0
call ms100
bcf PORTA,0
movfw store
movwf key2
goto key_check
p3:
bSf PORTA,7
call ms100
bcf PORTA,7
movfw store
movwf key3
goto key_check
p4:
bSf PORTA,6
call ms100
bcf PORTA,6
movfw store
movwf key4
goto compare
compare:
clrf pinCount
movfw pin1
xorwf key1
btfsc STATUS,Z
goto $+2
goto hash
movfw pin2
xorwf key2
btfsc STATUS,Z
goto $+2
goto hash
movfw pin3
xorwf key3
btfsc STATUS,Z
goto $+2
goto hash
movfw pin4
xorwf key4
btfsc STATUS,Z
goto $+2
goto hash
call ON
goto key_check
ON:
BsF PORTA,1
call ms
BcF PORTA,1
RETURN
OFF:
BsF PORTA,2
call ms
BcF PORTA,2
RETURN
debounce:
movlw 0x08 ;pause to debounce switch
movwf b1
movlw 0x2F
movwf b2
movlw 0x03
movwf b3
bounce_0
decfsz b1, f
goto $+2
decfsz b2, f
goto $+2
decfsz b3, f
goto bounce_0
;3 cycles
goto $+1
nop
return
ms:
;19993 cycles
movlw 0x9E
movwf ms1
movlw 0x10
movwf ms2
ms_0
decfsz ms1, f
goto $+2
decfsz ms2, f
goto ms_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
onesec:
;999990 cycles
movlw 0x07
movwf one1
movlw 0x2F
movwf one2
movlw 0x03
movwf one3
onesec_0
decfsz one1, f
goto $+2
decfsz one2, f
goto $+2
decfsz one3, f
goto onesec_0
;6 cycles
goto $+1
goto $+1
goto $+1
;4 cycles (including call)
return
halfsec:
;499994 cycles
movlw 0x03
movwf h1
movlw 0x18
movwf h2
movlw 0x02
movwf h3
halfsec_0
decfsz h1, f
goto $+2
decfsz h2, f
goto $+2
decfsz h3, f
goto halfsec_0
;2 cycles
goto $+1
;4 cycles (including call)
return
ms100
;99993 cycles
movlw 0x1E
movwf quarterSec1
movlw 0x4F
movwf quarterSec2
ms100_0
decfsz quarterSec1, f
goto $+2
decfsz quarterSec2, f
goto ms100_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
ms250
;249993 cycles
movlw 0x4E
movwf quarterSec1
movlw 0xC4
movwf quarterSec2
ms250_0
decfsz quarterSec1, f
goto $+2
decfsz quarterSec2, f
goto ms250_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
end
Last edited: