Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC frequency 4MHz to 48kHz BCF PCON, 3

Status
Not open for further replies.

RMIM

Member
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

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:
As with a number of members here I never use breadboards - I don't consider them reliable enough, and they cause more problems than they cure.

There's no need to slow the PIC down though, keypad scanning works perfectly at 4MHz (or at any higher speed as well).

If placing your hand near the circuit affects it, then you've probably left inputs floating?, all inputs require pullup or pulldown resistors, or an external circuit that holds them at a correct logic level.
 
As with a number of members here I never use breadboards - I don't consider them reliable enough, and they cause more problems than they cure.

There's no need to slow the PIC down though, keypad scanning works perfectly at 4MHz (or at any higher speed as well).

If placing your hand near the circuit affects it, then you've probably left inputs floating?, all inputs require pullup or pulldown resistors, or an external circuit that holds them at a correct logic level.

Thanks for the reply Nigel. For me, if it was not for bread boards I think I would have given up electronics up long ago. If I had to solder/de-solder because I put a transistor in the wrong way every time it would be a night mare.

All my input/output pins have been used apart from 1. I will check again.

Lets say I have unused inputs – if I turn them to outputs in software does this help?
 
Be careful....... Laptops are expensive. As little ghostman will tell you....

I would protect this supply as best possible..

Thanks for the reply Ian.
Could I do any damage if I dont have any 240ac about? What the worse I could do? Short the usb + and - ? Is it not propected for shorts?

I'll start using a wall adaptor if it's dangerous to my laptop.
 
No, if you're not checking the pin as an input it has effect, and you shouldn't be testing pins you don't use.

Try posting your schematic.

I could not understand what you were saying.

Lets say im using a 16F268a chip. Then I have 8 RB pins, 8 RA pins and 2 power supply pins.

Lets say I have a really simple circuit – It flashes an LED using RB0, this means RB1 to RB7 and RA0 to RA7 are all unused.

What do I do with all these unused pins? Do I have to physically pull them down/up using resistors to stop them floating?

Can’t I just use software to make them outputs and leave them unconnected? What about software port B pullups?

As for the circuit im working on at the moment all pins but RA4 are being used.

RB0 is connected to a bluetooth serial unit. RB1-RB7 is connected to the 7 wires off the keypad matrix. RA ports are connected to leds
 
Last edited:
Im still not sure how to bring my pic down to 48khz, do I just use the one line
BCF PCON, 3
in bank 1

or is there something else I have to do as well?

Thanks
 
Last edited:
I could not understand what you were saying.

Lets say im using a 16F268a chip. Then I have 8 RB pins, 8 RA pins and 2 power supply pins.

Lets say I have a really simple circuit – It flashes an LED using RB0, this means RB1 to RB7 and RA0 to RA7 are all unused.

What do I do with all these unused pins? Do I have to physically pull them down/up using resistors to stop them floating?

Nothing at all - you 'could' make them outputs, but it really makes little difference.

Can’t I just use software to make them outputs and leave them unconnected? What about software port B pullups?

As for the circuit im working on at the moment all pins but RA4 are being used.

RB0 is connected to a bluetooth serial unit. RB1-RB7 is connected to the 7 wires off the keypad matrix. RA ports are connected to leds

Do you have pull-up/down resistors on the inputs of PortB, or the internal pullups enabled? (just connecting them to a keypad leaves them floating.
 
Nothing at all - you 'could' make them outputs, but it really makes little difference.



Do you have pull-up/down resistors on the inputs of PortB, or the internal pullups enabled? (just connecting them to a keypad leaves them floating.

No, I have just connected them to the keypad - but internal pullups are on (BcF OPTION_REG,7)
 
I think I found the reason why I cant get it to work at 48k - I think I have to change all the timing routines to work at 48k and not 4Mhz.

edit:I have adjusted all the timing routines to work with 48khz, the circuit now works and is less susceptible to my finger touching the insulated wires.

What should I be adding to my circuit/software to stop this erratic behaviour completely?
 
Last edited:
You need to find out what you've done wrong that's causing it - it's NOT something that happens, it's always due to an error - what are you doing with the MCLR pin?.
 
thanks for the reply nigel. i will go back to my circuit and see. it's something i have to work out, as it's not the first circuit I have made to suffer like this.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top