Searching \ for '[PIC] [EE] making a quartz clock' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/time.htm?key=clock
Search entire site for: '[EE] making a quartz clock'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] [EE] making a quartz clock'
2007\02\24@105617 by picnoob

flavicon
face

ok, i think im gettin this.. 2 more questions: if the crystal is connected to
t0clk, will the freq be divided by 4. and also, how do i wire the 32768Hz
crystal into the t0clk pin?


i cant just execute instructions at 32768Hz, due to my design.

current wiring:  http://www.nabble.com/file/6750/2%20xtals.bmp 2 xtals.bmp
--
View this message in context: www.nabble.com/making-a-quartz-clock-tf3281293.html#a9135079
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

2007\02\24@113812 by Jan-Erik Söderholm

face picon face
picnoob skrev:

> how do i wire the 32768Hz
> crystal into the t0clk pin?

Was this an F84A ?
It has no crystal osc on tmr0...

Jan-Erik.

2007\02\24@114650 by Byron A Jeff

face picon face
On Sat, Feb 24, 2007 at 07:56:09AM -0800, picnoob wrote:
>
> ok, i think im gettin this.. 2 more questions: if the crystal is connected to
> t0clk, will the freq be divided by 4.

There's a prescaler you can use IIRC. I haven't looked at 16F84s in a long
long time.


> and also, how do i wire the 32768Hz crystal into the t0clk pin?

I don't think you can. That's why many of us have been telling you to abandon
the 16F84 and work with a newer part.

Where are you again? Maybe some kind soul out there can drop you some 16F88s
in the post. Or even better some 16F887s.

> i cant just execute instructions at 32768Hz, due to my design.

That's where the timer 1 that most modern PICs are useful. It can have its
own separate crystal from the main processor crystal.

Please consider it. It'll make your work a lot easier.

BAJ

2007\02\24@114753 by Byron A Jeff

face picon face
On Sat, Feb 24, 2007 at 05:37:58PM +0100, Jan-Erik S?derholm wrote:
> picnoob skrev:
>
> > how do i wire the 32768Hz
> > crystal into the t0clk pin?
>
> Was this an F84A ?
> It has no crystal osc on tmr0...

Which is yet another good reason not to use it.

Jan-Erik,

IIRC picnoob is in Sweden, right? Can you drop him some newer parts in the
post?

BAJ

2007\02\24@130342 by Jan-Erik Söderholm

face picon face
Sure,
he can check my web site :
http://www.sodjan.se
http://www.jescab.se/Prod_PIC.html (PIC's I resell)
http://www.jescab.se/Kontakt.htm (mail and other info)
http://www.jescab.se/Kontakt1.htm (english page)

And (*if* he's in Sweden) he should also register on the
Swedish "Elektronikforum" :
http://elektronikforumet.com/forum/index.php
There is a separate forum part for uC/uP.

Jan-Erik.


Byron A Jeff skrev:
{Quote hidden}

2007\02\24@152113 by picnoob

flavicon
face




Jan-Erik Söderholm-2 wrote:
>
> And (*if* he's in Sweden)
>

nope, UK. sweeden would be nice to live in, or south korea.
--
View this message in context: www.nabble.com/making-a-quartz-clock-tf3281293.html#a9137456
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

2007\02\24@152122 by picnoob

flavicon
face




Jan-Erik Söderholm-2 wrote:
>
> And (*if* he's in Sweden)
>

nope, UK. sweeden would be nice to live in, or south korea:)
--
View this message in context: www.nabble.com/making-a-quartz-clock-tf3281293.html#a9137457
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

2007\02\24@152354 by picnoob

flavicon
face



Jan-Erik Söderholm-2 wrote:
>
> And (*if* he's in Sweden)
>

nope, UK. sweeden would be nice to live in, or south korea :)

so, i take it that using a pic16f84a isnt the best idea, im considering a
16f628a, does it have an analogue to digital converter or not, i cant find
out.
--
View this message in context: www.nabble.com/making-a-quartz-clock-tf3281293.html#a9137457
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

2007\02\24@154235 by =?UTF-8?B?SmFuLUVyaWsgU8O2ZGVyaG9sbQ==?= n/a

face picon face
picnoob wrote :

> so, i take it that using a pic16f84a isnt the best idea, im considering a
> 16f628a, does it have an analogue to digital converter or not, i cant find
> out.

Of course you can! Just check the datasheet.
Concider the 16F88 for a full-function 16F in 18-pin DIP
*including* an A/D converter.

2007\02\24@160234 by Byron A Jeff

face picon face
On Sat, Feb 24, 2007 at 09:42:22PM +0100, Jan-Erik S??derholm wrote:
> picnoob wrote :
>
> > so, i take it that using a pic16f84a isnt the best idea, im considering a
> > 16f628a, does it have an analogue to digital converter or not, i cant find
> > out.
>
> Of course you can! Just check the datasheet.

Even better you can check out the PIC parameter page here:

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=74

> Concider the 16F88 for a full-function 16F in 18-pin DIP
> *including* an A/D converter.

I still think a larger package would be better. The 16F886 and 16F887 look
like really excellent parts.

BAJ

2007\02\24@172545 by michael brown

picon face

picnoob wrote:
> Jan-Erik Söderholm-2 wrote:
>>
>> And (*if* he's in Sweden)
>>
>
> nope, UK. sweeden would be nice to live in, or south korea :)
>
> so, i take it that using a pic16f84a isnt the best idea, im
> considering a 16f628a, does it have an analogue to digital converter
> or not, i cant find
> out.

The F628 does not have ADC, it does have a comparator though.  The 16F88 is
what you want for an 18 pin part.  You can think of it like this, sort of,
the F84 was superceded by the F628 which was superceded by the 16F88.  The
F88 has the most memory and peripherals, the lowest power requirements and
the best internal oscillator of the bunch.  It also costs the least.


'[PIC] [EE] making a quartz clock'
2007\03\09@005835 by Rikard Bosnjakovic
picon face
On 2/24/07, Byron A Jeff <spam_OUTbyronTakeThisOuTspamcc.gatech.edu> wrote:

> There's a prescaler you can use IIRC. I haven't looked at 16F84s in a long
> long time.

>From what I can read of the datasheets, a prescaler is some sort of
black magic that scales down Hz to some other value. For a 16F628a
(which I'm going to work on) I can select either 1:1, 1:2, 1:4 or 1:8
in T1CON. I'm going to use a 32768Hz-crystal for counting secs in my
app.

I want a timer-interrupt each second. 32768/4 = 8192. If I pick
prescaler 1:8 I (think I) get 1024, but I'm not sure what this value
is or how I can use it to measure one second of elapsed time.

What should I set the prescaler at, and what does it really do?


--
- Rikard.

2007\03\09@021606 by Marcel Birthelmer

picon face
Rikard,
you can think of the prescaler as a counter. For example, at 1:4, the
you get one count out when you put 4 counts in - it counts to 4
internally and increases the output register (TMR1) when it gets
there, and then starts over.
So you have 32768Hz on the input. If your prescaler is 1:8, you get a
TMR1 interrupt every 4096 cycles. Since 32768 ticks are 1 second, 4096
cycles are 1/8th of a second. So you get eight interrupts per second.
You can then count it down in software to react only to the 8th such
interrupt, and there you go.

- Marcel

On 3/8/07, Rikard Bosnjakovic <.....rikard.bosnjakovicKILLspamspam@spam@gmail.com> wrote:
{Quote hidden}

> -

2007\03\09@025837 by Jinx

face picon face
> >From what I can read of the datasheets, a prescaler is some sort of
> black magic that scales down Hz to some other value. For a 16F628a
> (which I'm going to work on) I can select either 1:1, 1:2, 1:4 or 1:8
> in T1CON. I'm going to use a 32768Hz-crystal for counting secs in
> my app

A pre-scaler is a binary ripple counter (or divider if you want to call it
that) which can have the count limit or division set by s/w. If set to 1:2,
you get one "increment" out for every two that go in, similarly with 1:4,
1:8 and so on. There are diagrams showing the phase relationships but
it's not necessary to understand those to make it work

> I want a timer-interrupt each second. 32768/4 = 8192. If I pick
> prescaler 1:8 I (think I) get 1024, but I'm not sure what this value
> is or how I can use it to measure one second of elapsed time.

When Timer1 is used with an external crystal, the crystal frequency
is not divided by 4, as the main oscillator is. Therefore you want to
count the full 32768 cycles for 1 second. This means you don't have
to use the pre-scaler at all

To take your example above, any count less than 0x0000 (65536) can
be done with a 16-bit timer like TMR1. Very close to 0x0000 can be
tricky, time-wise, but when you're hundreds or thousands of counts
below roll-over it's not generally a problem. The 8192 (0x2000) is
loaded into the timer as -8192, which is 0x100000 - 0x2000 = 0xE000.
So TMR1L would be set to 0x00 and TMR1H to 0xE0, and the pre-
scaler is not needed

The easiest way to get 1 second interrupts out is to clear TMR1L and
set TMR1H to 0x80. ie TMR1 = 0x8000. It will take 32768 (0x8000)
to get TMR1 back up to 0x0000 and generate an interrupt. Then you
reload TMR1H with 0x80 (If the reload is done early in the ISR, then
TMR1H will still be 0x00, and simply BSF TMR1H,7 will make it 0x80,
but I can't imagine you'd be so short of time you'd need to save an
instruction cycle)

TMR1L has already been 0x00 (at the instant of TMR1IF being set)
and is being incremented as part of the second timing, so don't touch
TMR1L

Oh, and don't forget to clear TMR1IF

2007\03\09@071404 by michael brown

picon face
Rikard Bosnjakovic wrote:
> On 2/24/07, Byron A Jeff <.....byronKILLspamspam.....cc.gatech.edu> wrote:
>
>> There's a prescaler you can use IIRC. I haven't looked at 16F84s in
>> a long long time.
>
> From what I can read of the datasheets, a prescaler is some sort of
> black magic that scales down Hz to some other value. For a 16F628a
> (which I'm going to work on) I can select either 1:1, 1:2, 1:4 or 1:8
> in T1CON. I'm going to use a 32768Hz-crystal for counting secs in my
> app.
>
> I want a timer-interrupt each second. 32768/4 = 8192. If I pick
> prescaler 1:8 I (think I) get 1024, but I'm not sure what this value
> is or how I can use it to measure one second of elapsed time.
>
> What should I set the prescaler at, and what does it really do?

The prescalers is just some extra bits that can be tacked onto the
counter that act like a clock divider.  Using a 32kHz crystal to clock
TMR1 will generate an interrupt every 2 seconds with the prescaler set
to 1.  If you were to set the prescaler to 8, then TMR1 would overflow
every 16 seconds instead.  IOW, you don't need to use the prescaler (set
it to 1), what you need to do is modify TMR1H every time an interrupt
occurs.  You should not touch TMR1L if you want accurate timekeeping.

You will simply set the MSB of TMR1H  i.e. bsf  TMR1H, 7 (or you could
just move 0x80 to TMR1H.  This will make TMR1 overflow after 32768 ticks
instead of 65,536.  The result is that you will get one interrupt per
second.

Here's how to do it:    [ Note how TMR1H is only modified right after
TMR1L increments, this is important for perfect timekeeping ]

;---------------------------------------------------------------------------------------------
;       HEADER:
;---------------------------------------------------------------------------------------------
             LIST        P=16f88
             RADIX       DEC
             INCLUDE     "p16f88.inc"
;Program Configuration Register 1
 __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF &
_WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF &
_PWRTE_ON & _WDT_OFF & _INTRC_IO

;Program Configuration Register 2
 __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF

;---------------------------------------------------------------------------------------------
;       EQUATES:
;---------------------------------------------------------------------------------------------


STARTOFRAM    equ           0x20              ;First Usable RAM Location
(Bank 0)
ENDOFRAM      equ           0x6F              ;Last Usable RAM Location
(Bank 0)
FIXEDAREA     equ           0x70              ;Start of shared memory
between banks
BANK1RAM      equ           0xA0              ;Start of Bank 1 RAM
ENDOFBANK1    equ           0xEF              ;End of Bank 1 RAM


             cblock STARTOFRAM
;Application specific variables
temp
             endc



             cblock FIXEDAREA

; Make sure we can always get to these
;
; ISR Register Save Areas
;

INT_FLAGS
W_TEMP
STATUS_TEMP
FSR_TEMP
PCLATH_TEMP

             endc



;---------------------------------------------------------------------------------------------
;       ENRTY POINTS:
;---------------------------------------------------------------------------------------------
             org         0x000
             goto        Start

;IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
;
; ISR
;
;IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

             org         0x004
; Interrupt handler right here
             movwf       W_TEMP              ;Save everything
             swapf       STATUS, W
             movwf       STATUS_TEMP
             movfw       FSR
             movwf       FSR_TEMP
             movfw       PCLATH
             movwf       PCLATH_TEMP

             bcf         STATUS, RP1
             bcf         STATUS, RP0         ; Bank 0

             btfsc       PIR1, TMR1IF        ;Is it a TMR1 interrupt
              goto       Timeout             ;Yes

             goto        IntExit


;-----------------------------------------------------------------------------
;
; ISR routines
;
;-----------------------------------------------------------------------------


;------------------------------------------------------------------------------
Timeout

; toggle LED pin
             movfw       PORTA
             xorlw       0x01
             movwf       PORTA

; wait for TMR1L to increment so that we only change on TMR1H when the
clock input is high
             movfw       TMR1L
             movwf       temp              ; Save a copy of TMR1L
WaitForTMR1ToChange:
             movfw       TMR1L             ;
             xorwf       temp,W            ; Has TMR1L changed?
             skpnz
              goto WaitForTMR1ToChange

; Reload TMR1H for rollovers
;              movlw       0xC0              ; Preload for 1/2 second
rollover
             movlw       0x80              ; Preload for 1 second
rollover
;              movlw       0x00              ; Preload for 2 second
rollover
             movwf       TMR1H

             bcf         PIR1, TMR1IF      ; Clear interrupt flag

             goto        IntExit


;-------------------------------------------------------
IntExit
             movfw       PCLATH_TEMP
             movwf       PCLATH
             movfw       FSR_TEMP
             movwf       FSR
             swapf       STATUS_TEMP, W
             movwf       STATUS
             swapf       W_TEMP, F
             swapf       W_TEMP, W
             retfie



;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;
; Main Level Code
;
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM



Start
             bcf         STATUS, RP1
             bsf         STATUS, RP0         ; Switch to Bank 1

             movlw       b'00000000'         ; All pins output
             movwf       TRISA & 0x7F

             movlw       b'00000000'         ; All pins output
             movwf       TRISB & 0x7F
;              movwf       WPU & 0x7F

             movlw       0x00                ; All pins digital i/o for
now
             movwf       ANSEL & 0x7F

;              movlw       b'01110000'         ;Set internal osc to 8Mhz
             movlw       b'01100000'         ;Set internal osc to 4Mhz
;              movlw       b'00000000'         ;Set internal osc to
31khz
             movwf       OSCCON & 0x7F

             bcf         STATUS, RP1
             bcf         STATUS, RP0         ; Switch to Bank 0

             clrf        PORTA                ; Drive all pins low
             clrf        PORTB

             call        TMR1Init

             bsf         INTCON, GIE         ; Turn on GLOBAL
Interrupts

Main
;              sleep            ; Your choice, makes no difference
             nop
             goto Main


;------------------------------------------------------------------------
;
;  Timers and Stuff
;
;------------------------------------------------------------------------


TMR1Init
;

; Setup for using external crystal
       movlw   b'00001110'             ; TMR1 oscillator off, and timer
stopped, prescale /1
;                 |||||||+-------------- TMR1ON (started later)
;                 ||||||+--------------- TMR1CS (external 32768 crystal)
;                 |||||+---------------- /T1SYNC (unsynchronized for
sleep)
;                 ||||+----------------- T1OSCEN (enabled)
;                 |||+------------------ T1CKPS0
;                 ||+------------------- T1CKPS1
;                 |+-------------------- TMR1GE (T1RUN on 16F88)
;                 +--------------------- T1GINV (Unused on 16F88)

       movwf   T1CON

; Clear the timer registers
       clrf    TMR1H
       clrf    TMR1L

; Clear the interrupt flag
       bcf     PIR1, TMR1IF

; Enable peripheral interrupts
       bsf     INTCON, PEIE

; Enable TMR1 interrupts
       bcf     STATUS, RP1
       bsf     STATUS, RP0             ; Select Bank 1

       bsf     PIE1 & 0x07F, TMR1IE    ;  Turn on TMR1 ints

       bcf     STATUS, RP1
       bcf     STATUS, RP0             ; Select Bank 0

; Turn on Timer 1
       bsf     T1CON, TMR1ON           ; Start timer running

       return

       end

2007\03\09@113603 by Tony Antoniou

picon face
Interesting solution. I've never considered using an interrupt to engage a
subroutine. Particularly with my smaller 12C50x projects, I've always used
the 1:32 prescaler with a 32kHz crystal and checked the change in status of
TMR0,7. It's a pretty short routine with x number of goto routines
proportional to the amount of pseudo-timers I have coded into it to give me
more flexibility than just the one timer. Of course, this concept would
translate to an interrupt-based timer as well.

In the grand scheme of things, I'd say that your solution is more elegant
but for the sake of the argument, I figured I'd throw in my sample solution
as well for all to decide on which would be best for them. It's worked for
me and very accurately at that.


       LIST P=12C508A,F=INHX8M
       INCLUDE <P12C508A.INC>
       __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_ON & _LP_OSC

#DEFINE        ALERT                GPIO,3
#DEFINE        INPUT1        GPIO,2
#DEFINE        OUTPUT1        GPIO,1
#DEFINE        OUTPUT2        GPIO,0

;General purpose RAM
TIMER1                EQU                07H
TIMER2                EQU                08H
COUNT                        EQU                09H
WAIT_HIGH_LOW        EQU                10H

       ORG        00

;First instruction now begins here

INITIALISE
;Set GP0 and GP1 as outputs, all others inputs
       CLRF        GPIO
       MOVLW        B'00111100'
       TRIS        GPIO

;Setup OPTION: No wakeup on pin change, disable weak pull ups (GP0,1,3)
;              Fosc/4 clock source, timer0 clock on low - high transition
;               Prescaler assigned to TMR0, /32
       MOVLW        B'01000100'
       OPTION

       CLRF        TMR0
       CLRF        WAIT_HIGH_LOW
;        BCF        WAIT_HIGH_LOW,0
       BSF        WAIT_HIGH_LOW,1        ;Safeguard against power to any detectors
;        BCF        WAIT_HIGH_LOW,2        ;Flag for when waiting for complete
second (as
                                       ;each clock period is 0.5s duration)
;        BCF        WAIT_HIGH_LOW,3        ;1st sensor release to start timer1
;        BCF        WAIT_HIGH_LOW,4        ;1st sensor set to engage timer on 1st
                                       ;sensor release
       BSF        TEL_MUTE                ;Set TEL_MUTE to No Mute state
       BCF        D_PWR                        ;Set D_PWR to Power Off state
       MOVLW        .05                        ;Set up sensor timer
       MOVWF        TIMER1
       MOVLW        .10                        ;Set up telephone mute timer
       MOVWF        TIMER2
       MOVLW        .05                        ;Set up sensor switching counter
       MOVWF        COUNT

;Time monitoring

T_MONITORING
       CLRWDT
       BTFSC        TMR0,7                ;Check for 0.5s rising edge clock pulse,
skip
                                       ;next cmd if 0
       CALL        TIME_CHECK
       BTFSC        WAIT_HIGH_LOW,2        ;Check if we're waiting for the falling edge

                                       ;0.5s pulse
       CALL        TIME_CHECK


;Alert monitoring

A_MONITORING
       CLRWDT
       BTFSC        ALERT
       GOTO        ACT_TEL_MUTE
       BTFSS        TEL_MUTE
       GOTO        ACT_TEL_MUTE

;INPUT1 monitoring

B_MONITORING
       CLRWDT
       BTFSS        INPUT1
       GOTO        PWR_DOWN
       BTFSC        WAIT_HIGH_LOW,0
       GOTO        PWR_DOWN
       BTFSC        WAIT_HIGH_LOW,1
       GOTO        PWR_DOWN
       GOTO        PWR_UP
       GOTO        T_MONITORING

TIME_CHECK
       CLRWDT
       BSF        WAIT_HIGH_LOW,2                ;We are now waiting for falling edge

                                               ;0.5s pulse
       BTFSC        TMR0,7                        ;Check for other 0.5s falling edge
pulse
       RETURN                                ;No? Go back to where we left off
       BCF        WAIT_HIGH_LOW,2                ;No longer waiting for falling edge
       BTFSC        D_PWR                                ;Don't touch timer if power
supplied
       GOTO        TIMER2_CHK_SET
       BTFSS        WAIT_HIGH_LOW,3                ;Don't touch timer if we're still
       GOTO        TIMER2_CHK_SET                ;waiting for 1st sensor release
       MOVF        TIMER1                        ;Check that timer1 > 0
       BTFSS        STATUS,Z
       DECF        TIMER1                        ;Knock timer1 down by 1 second if >
0

TIMER2_CHK_SET
       CLRWDT
       BTFSC        TEL_MUTE                        ;Don't touch timer if
TEL_MUTE is not 0
       GOTO  A_MONITORING
       MOVF        TIMER2                        ;Check that timer2 > 0
       BTFSS        STATUS,Z
       DECF        TIMER2                        ;Knock timer2 down by 1 second if >
0
       BCF        WAIT_HIGH_LOW,2                ;No longer waiting for falling edge
                                               ;transition
       RETURN


Adios,
Tony

---------------  TAMA - The Strongest Name in Drums  ---------------

-----Original Message-----
From: EraseMEpiclist-bouncesspam_OUTspamTakeThisOuTmit.edu [piclist-bouncesspamspam_OUTmit.edu] On Behalf Of
michael brown
Sent: Friday, 9 March 2007 11:14
To: Microcontroller discussion list - Public.
Subject: Re: [PIC] [EE] making a quartz clock

Here's how to do it:    [ Note how TMR1H is only modified right after
TMR1L increments, this is important for perfect timekeeping ]

2007\03\09@131653 by Brooke Clarke

flavicon
face
Hi:

Another way to generate clock interrupts is to use Timer2.  You set the
prescaler and program PR2 with the 8 bit count value.  When the counter
rolls over it sets the interrupt and loads the PR2 value and keeps
going.  You may want to have the interrupt more frequently than once per
second to allow doing different things in different parts of the second.

Have Fun,

Brooke Clarke

--
w/Java http://www.PRC68.com
w/o Java www.pacificsites.com/~brooke/PRC68COM.shtml
http://www.precisionclock.com

2007\03\09@164108 by Jinx

face picon face
Just a thought Rikard - I forget whether you have 4 or 6 digits. A
2Hz IRQ would let you flash the colon to indicate seconds, or 4Hz
and flash the colon 1 in 4 IRQs (looks more like a 'tick')

More... (looser matching)
- Last day of these posts
- In 2007 , 2008 only
- Today
- New search...