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.

Help with programming a pic to oscillate

Status
Not open for further replies.

alifred

Member
Hi,

I am trying to create a 1hz pulse to run a clock off.
I have a PIC16F627, and can I program it to oscillate at 1hz?
I do have a 32.768KHz crystal, could I use this with the PIC?

Apparently the PIC has an inbuilt RC oscillator, could I use this, or is it not accurate enough?

I do not have any experience with programing pic's, but I do have a serial port PIC controller with software for writing the programs to the PIC.

Thanks for any help! :)

Fred
 
Well, at 32.768Khz, the PIC will execute 8,192 instructions per second, so you can just write a program with a sufficient delay. You'll just have to be careful about how many cycles are used by the code.
 
Hi Fred,

Welcome to the forum.

The 16F627 can produce a very accurate 1Hz signal especially when it has a 32.768kHz crystal. Are you confident you can program the chip? Whatever, if you are willing to put in the effort I'm sure we can easily sort out your 1Hz signal.

Mike.
 
Hi Mike (Pommie),

I am fairly confident with programming a PIC.
I have programmed one before with a program I got off the internet, but I do not know how to write one.

Thanks for responding,

Fred
 
Hi Fred,

It's 2AM here and so time for bed. However, if you post a description of what you are trying to achieve then I'm sure the members here can come up with some helpful suggestions.

Mike.
 
Hi,

I am trying to make a clock. I have tried using lots of different ways to produce a 1hz pulse. They all failed!!

I have got a PIC that I want to use to generate the pulse.

I want one of the PIC's output's to be the clock pulse, and the crystal to be connected to the CLKIN and CLKOUT pins, preferably with no other components!

If someone could maybe have a go at starting to write a program, I would be most grateful!

Cheers

Fred
 
Last edited:
Hi,

If you think I need some capacitors for the crystal, could you recommend some capacitors to go with a certain crystal?

You say I would have to use SOME capacitors. How many, and in what configuration? Could you post a circuit diagram?

Cheers

Fred
 
*Crossed post with eng1*

Fred,

have a look at the datasheet for your PIC16F627, here:

https://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010207

and refer to section 14.2 for crystal selection.

Bill suggests 12pf caps and the original Microchip Demo boards, that I have, use 22pf caps for the 32.678khz crystal. The datasheet suggests 68 - 100pf though. The datasheet notes that higher values increase stability, but also increase start-up time. You are also advised to consult the crystal manufacturer's datasheet. Go figure. :D

Anyway, you will need 2 caps for your 16F627. The crystal connects to pins 15 & 16 then the caps are connected, one to each pin and then both to ground.


HTH.
 
Last edited:
*Crossed post with eng1*
No problem, Mickster ;)

Which demoboard do you have exactly? Do they use the 32 kHz crystal as the system oscillator? I've noticed that they suggest 33 pF capacitors (this value is nearer to the 22 pF caps you mentioned) if you connect the crystal to the TMR1 oscillator. Thanks
 
Last edited:
eng1,

amongst a couple of others, I have the PICkit2 28 pin Demo Board (DM164120-3) which has the 32.768khz crystal with 22pf caps, along with 16F886.

This board is also used with the PICkit Serial Analyser for a Real Time Clock demo.
 
Hi,

I am trying to create a 1hz pulse to run a clock off.
I have a PIC16F627, and can I program it to oscillate at 1hz?
I do have a 32.768KHz crystal, could I use this with the PIC?

Apparently the PIC has an inbuilt RC oscillator, could I use this, or is it not accurate enough?

I do not have any experience with programing pic's, but I do have a serial port PIC controller with software for writing the programs to the PIC.

Thanks for any help! :)

Fred
Why don't you use the PIC for the whole clock and not just the 1Hz time base?
 
Here you go, this should produce a 1Hz signal on RB0 with a 32768Hz crystal.

Code:
	LIST	p=16F627
	include "P16F627.inc"
	__CONFIG _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF

	org	0x00		;start of program
init
	banksel	TRISA		;bank 1
	bcf	TRISB,0 	;PortB,0 output
	banksel	PORTA		;bank 0
	movlw	b'0000001'	;turn on timer 1
	movwf	T1CON
main	btfss	TMR1H,4		;copy timer 1 bit 12
	bcf	PORTB,0		;to portB bit 0
	btfsc	TMR1H,4
	bsf	PORTB,0
	goto	main

	end

As you can see it really is a waste of a pic chip and maybe you should consider the suggestion of building a complete clock with it.

Mike.
 
Last edited:
Hi Mike,

Thanks for that code. I will try it and get back to you.
I have already assembled the rest of the clock using CMOS 4510/4511 so would rather use them than rewire the rest of the clock.

Fred
 
eng1,

amongst a couple of others, I have the PICkit2 28 pin Demo Board (DM164120-3) which has the 32.768khz crystal with 22pf caps, along with 16F886.

Datasheets usually suggest smaller caps when the crystal is driven by the TMR1 module. In your demoboard you should have another crystal that is used for the system clock (20 MHz); so 22 pF caps should be ok with the 32 kHz crystal there. But if you use the 32 kHz crystal for the main clock, 68-100 pF caps look more suitable according to the datasheet of the PIC16F627 (and other PICs) - as we both have suggested earlier. Many people say that those values are not that critical, but I've always done as written in the datasheet, so I can't tell you.

Also, I experienced some problems when specifying the XT oscillator in the configuration word when I was actually using a HS oscillator. That might apply for LP crystals as well? I would use
Code:
 __CONFIG _WDT_OFF & _BODEN_ON & _PWRTE_ON & _LP_OSC & _MCLRE_ON & _LVP_OFF
 
Eng1,

Good point on the XT crystal. It should, as you point out, be LP.

Mike.
 
Thanks,

Do the capacitors with the crystal have to be the same rating?

I have a 15pF and a 22pF one. Would these be too low a value?

Thanks

Fred
 
I only have a 38k crystal and 33pF capacitors and just tried it on a breadboard and it worked fine. I suspect that you will get a more stable and probably more accurate oscillator if you use the recommended 60pF caps. If your just breadboarding it then try it and see. If your soldering them in place I think I'd hold out for the right ones.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top