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.

external xtal using 16f628 problem

Status
Not open for further replies.

skyrock

New Member
hi all,

i'm having some difficulty setting up the external xtal working with 16f628.. can anyone help?

i've connected a 4mhz crystal with 2 ceramic 22pf capacitors to RA7 & RA6. The code is as below. the program supposed to turn on PORTB but it didn't.

Code:
	LIST	p=16F628		;tell assembler what chip we are using
	include "P16F628.inc"		;include the defaults for the chip
	ERRORLEVEL	0,	-302	;suppress bank selection messages
	__config _XT_OSC &_MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF & _CP_OFF & _DATA_CP_OFF	
	
	cblock 0x20
	CNT
	endc

	ORG	0x00
	goto MAIN

MAIN
	bsf		STATUS, RP0
	clrf	TRISB
	clrf	TRISA
	bcf		STATUS, RP0
	movlw	b'11110010'
	movwf	PORTB
	clrf	CNT
LOOP
	goto	LOOP
 
Hi,
Do you still have PIC16F628? Or it is 628A?
What do you get on PORTB?
Do you have MCLR pulled up? Or you can enable internal pull up in the configuration.
You should disable the comparators if they're not used. That's okay here because you're not using PORTA.
 
skyrock said:
the program supposed to turn on PORTB but it didn't.
Code looks ok. Is your MCLR pulled up properly?

But if you want to turn on all bits in PortB, use
Code:
	movlw	0xff
	movwf	PORTB
instead of
Code:
	movlw	b'11110010'
	movwf	PORTB

What is this for?
Code:
	clrf	CNT

These
Code:
	bsf		STATUS, RP0
	bcf		STATUS, RP0
can be replaced with these
Code:
	banksel	TRISA
	banksel	PORTA
It's a nicer way of doing it. Easier to tell exactly what your bank change is.
 
I had a 1k resistor connected from +5v to RA5, is that OK? What else do I need to set up with the hardware?

The END statement was lefted out when I paste the code here.. Added code to turn off comparator:

Code:
	movlw	b'00000111'			;turn off comparator
	movwf	CMCON

PORTB has no signal at all.. I attaced 1 LED to each port.. so, doesnt matter with the code.. I just used this code to try out how to set up external osc..

I'm actually using 628.. I thought I had a 628a all the time because I asked specifically for 628a.. but the guy who sold me didnt tell me he doesnt have it and simply gave me 628..
 

Attachments

  • pullup.JPG
    pullup.JPG
    7.3 KB · Views: 199
Last edited:
Yes, I do have resistors for each LED..

edit: have I made the circuit right for crystal?
 

Attachments

  • crystal.JPG
    crystal.JPG
    8.3 KB · Views: 310
Last edited:
Everything looks fine to me. Have you checked the connection of the hardware? Have you tried with another PIC?
 
I think it's a connection problem.. when I pressed the connector wire.. it actually started working.. I think i'll have to rework the board then...

Thanks alot..
 
Does it take around 10 second to power up the crystal?

pls check if the code below would actually work?

Code:
	LIST	p=16F628		;tell assembler what chip we are using
	include "P16F628.inc"		;include the defaults for the chip
	ERRORLEVEL	0,	-302	;suppress bank selection messages
	__config _XT_OSC &_MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF & _CP_OFF & _DATA_CP_OFF
	
	cblock 0x20
	CNT
	endc

	ORG	0x00
	goto MAIN

	ORG 0x04
	goto ISR

MAIN
	bsf		STATUS, RP0
	clrf	TRISB
	clrf	TRISA
	movlw	b'10000101'
	movwf	OPTION_REG
	bcf		STATUS, RP0
	movlw	b'00000111'			;turn off comparator
	movwf	CMCON
	movlw	b'11110010'
	movwf	PORTB
	bsf		INTCON,	T0IE
	movlw	.178
	movwf	TMR0
	bsf		INTCON,GIE
	clrf	CNT
LOOP
	goto	LOOP

ISR
	movlw	.178
	movwf	TMR0
	bcf		INTCON, T0IF
	incf	CNT, F
	movlw	.210
	subwf	CNT,W
	btfss	STATUS, Z
	retfie
	btfss	PORTB,4
	goto	$+3
	bcf		PORTB,4
	goto	$+2
	bsf		PORTB,4
	clrf	CNT
	retfie

END
 
Last edited:
hi skyrocket,
Put your program thru my Oshonsoft Sim works OK.:)

The ISR is called, it executes the first half of the ISR and returns OK, when the CNT is reached it drops into the second half of the ISR OK and returns.

EDIT: PORTB.4 toggles On/Off

I would suggest you Enable PWRTE to give that 75mSec hold off delay until everything has stabilised after switching ON.
A good idea when using a LCD.!
 
Last edited:
but when I put the code onto the circuit it didnt work..

it takes a very long time to have the LEDs light up.. I guess about 30 seconds.. and then the LED doesn't change..
 
skyrock said:
but when I put the code onto the circuit it didnt work..

it takes a very long time to have the LEDs light up.. I guess about 30 seconds.. and then the LED doesn't change..

Hi,
It sounds as though you have a slow running osc.????

I will check thru again later... its lunchtime.!:)
 
The circuit just worked. I was touching one of the capacitor's leg and it started flashing, and then not working anymore.. so, i guess the cap must be broken. Let me get a new one tomorrow to see if it would work.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top