Searching \ for '[PIC] 16F873 30 second timout' 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/devices.htm?key=16F
Search entire site for: '16F873 30 second timout'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] 16F873 30 second timout'
2007\07\16@204755 by Jinx

face picon face
> is there a good way of having a counter for these 30 seconds??

Yes. There are some excellent ways of making a 30 second
counter. If you can supply a little more detail about how your
873 is set up we can suggest which one of these excellent ways
would suit you best ;-)

2007\07\20@215033 by Carolina Dieguez

picon face
Thanks a lot for your help...

Assembly code trying to express what I have the intention to do follows:

       processor        pic16f873A
       include        p16f873A.inc
       __config    _XT_OSC & _WDT_OFF & _PWRTE_ON

       errorlevel    -306


       cblock        0x20
               Frecuency
               Period
               TimeH                         ; Time in which the signal is
high
               TimeL                          ; Time in which the signal is
low
               Loop 1
               Loop 2
       endc

       goto            Main

       Main            org        h'80'

       movlw        h'06'
       movwf        PCLATH
       pagesel     Table
       movlw       d'100'
       movwf       Counter                  ; inicialize counter
       movlw        Frecuency             ; load the index used to enter
the table and as a result have the corresponding value of period

       call           Table
       nop

       movwf        Period                   ; value retrieved from the
table is loaded to Period
       movlw        TimeH
       sublw         Period                   ; substract TimeH from Period
and the result is saved in register W.
       movwf        TimeL                    ; then, the previous result is
saved in TimeL
       call            Flash_Led
end

       org        h'600'
Table:
       addwf        pcl, f
                                      ; by now we are just taking into
account the frequencies between 4Hz and 35Hz
       retlw        d'250'        ; 250 mseg that corresponds to 4Hz
       retlw        d'200'
       retlw        d'167'
       retlw        d'143'
       retlw        d'125'        ; 125 mseg that corresponds to 8Hz
       retlw        d'111'
       retlw        d'100'
       retlw        d'90'
       retlw        d'83'
       retlw        d'76'
       retlw        d'71'
       retlw        d'67'
       retlw        d'63'        ; 63 mseg that corresponds to 16Hz
       retlw        d'59
       retlw        d'55'
       retlw        d'53'
       retlw        d'50'
       retlw        d'48'
       retlw        d'45'
       retlw        d'43'
       retlw        d'42'
       retlw        d'40'
       retlw        d'38'
       retlw        d'37'
       retlw        d'35'
       retlw        d'34'
       retlw        d'33'
       retlw        d'32'
       retlw        d'31'        ;32 mseg that corresponds to 32Hz
       retlw        d'30'
       retlw        d'29'
       retlw        d'28'        ;28 mseg that corresponds to 35Hz

       return

;*************************************************************************************


High    movlw   TimeH         ; time in which the pulse is high, let's say
10 mseg
          movwf   Loop1
Outer   movlw   200
          movwf   Loop2
Inner    nop
          nop
          decfsz  Loop2, f
          goto     Inner          ; Inner loop = 5 usec.
          decfsz  Loop1, f
          goto     Outer
          return

;*************************************************************************************

Low     movlw   TimeL
          movwf   Loop1         ; Loop1 is loaded with the milisecond value
retrieved from table
Outer   movlw   200
          movwf   Loop2
Inner    nop
          nop
          decfsz  Loop2, f
          goto     Inner          ; Inner loop = 5 usec.
          decfsz  Loop1, f
          goto    Outer
          return

;*************************************************************************************

Flash_Led  bsf    portb, 7              ; Port B as output
                call    High
                bcf     portb, 7
                call    Low
                decfsz  Counter, 1        ; while counter does not get to
zero, led is flashing.
                goto   Led_on
                return

;**************************************************************************************

The fact is that you may find many other mistakes...

I've included a counter that is being decremented while a square signal is
generated. Depends on the counter whether the signal stops or continues.
However, instead of this counter, I need to have a timer that in case these
pulses have been reproduced for up to 30 seconds, a "time out" makes it
stop. I understand I need a timer interruption to do that (and use Timer 0
because I'll use PWM module later).

The fact is that a combination of a timer interruption and another delay
subrrutine would have not to affect on my previous time delay rutines (these
receive frequency information from RS 232 reception - that is not expressed
here)

I really hope you can help me. In case I was not clear enough, please let me
know.

Carolina.

On 7/16/07, Jinx <spam_OUTjoecolquittTakeThisOuTspamclear.net.nz> wrote:
>
> > is there a good way of having a counter for these 30 seconds??
>
> Yes. There are some excellent ways of making a 30 second
> counter. If you can supply a little more detail about how your
> 873 is set up we can suggest which one of these excellent ways
> would suit you best ;-)
>
> -

2007\07\20@230420 by Jinx

face picon face
> I've included a counter that is being decremented while a square
> signal is generated. Depends on the counter whether the signal
> stops or continues. However, instead of this counter, I need to
> have a timer that in case these pulses have been reproduced for
> up to 30 seconds, a "time out" makes it stop. I understand I
> need a timer interruption to do that (and use Timer 0 because
> I'll use PWM module later)

I think I understand what you want to do. Make square waves
for 30 seconds and then stop ?

Some possibilities -

(1) TMR0 + pre-scaler + counter
(2) TMR1 + pre-scaler + counter
(3) TMR1 with a 32kHz crystal

(1) & (2) TMR0 / TMR1

You didn't say what frequency you're running. I'll assume 4MHz as
you've got XT selected => 1us cycle time

30 seconds = 30,000,000us. If TMR0 pre-scaler = 256 then
=> (30000000/65536) = 457 rollovers + 50048 cycles

So you could set TMR0 to free run and detect TMR0IF=1 (with
or without interrupts). Increment a counter and look for the second
time it reaches (457-256) = 201, then load TMR0 with -195 to do
the last part (just over 50ms) of the 30 seconds. Because the
pre-scaler is set to 256, you will be off by 128us, as 50048/256
= a 195.5 count in TMR0. You could switch the pre-scaler to a
lower value (128 ?) to get the timing more accurate

TMR0 with 1:256 pre-scaler is the same as TMR1 without a pre-
scaler. TMR1 can have a 1:8 pre-scaler

As above, 30000000/65536/8 = 57 rollovers + 14448 cycles

(3) TMR1 + crystal

This might be the easiest. TMR1 with no pre-scaler running at
32768Hz will rollover every 2 seconds. Re-load TMR1 with
0x8000 will make that 1s, 0xC000 for 0.5s and so on

> (and use Timer 0 because I'll use PWM module later)

You might want to check whether PWM will go as slowly as
you need

2007\07\21@120158 by Carolina Dieguez

picon face
Thanks Jinx, I hope I could have at least 25 % of your knowledge on PICS
some time!
Below my comments...

On 7/21/07, Jinx <.....joecolquittKILLspamspam@spam@clear.net.nz> wrote:
{Quote hidden}

Exactly. What annoys me much is that I want to make use of this time out and
at the same time -simultaneously- I need to control the signal I'm
generating because it will have not always the same frequency neither the
same duty cycle. It depends on the  value retrieved from the table. (By the
way, this poor table was meant to convert frequency values, received from
RS232 transmission, to period values so as to generate these pulses -with
duty cycle information also received from RS232-).

Some possibilities -
{Quote hidden}

I really appreciate all your explanation. It is very clear for me, in fact I
was thinking make a try with the first alternative but those 457 rollovers
scare me:o)

What may be not clear enough for me is the way I can implement the third
option (32 KHz crystal).

> (and use Timer 0 because I'll use PWM module later)
>
> You might want to check whether PWM will go as slowly as
> you need


Yes, at the very beginning I thought I could use this module to generate my
signal but possile frequencies are between 2 - 120 Hz.

-

2007\07\21@163008 by Rikard Bosnjakovic

picon face
On 21/07/07, Carolina Dieguez <caro.dieguezspamKILLspamgmail.com> wrote:

> What may be not clear enough for me is the way I can implement the third
> option (32 KHz crystal).

Hook up an external 32kHz-crystal and use it for Timer1. When
receiving a Timer1-interrupt, you set the TMR1H-register (in the ISR)
to the value you want. For example, to 0x80 as Jinx wrote for 1 sec
overflows:

ISR:

      btfsc   PIR1, TMR1IF                    ; check for Timer1-overflow
        goto   timer1_action

isr_exit:
     ...
    retfie

timer1_action:
       bcf     PIR1, TMR1IF                    ; Reset interrupt flag
       movlw   0x80                            ; set TMR1H to 0x80
(TMR1 = 0x8000)
       movwf   TMR1H
       .... some other code
      goto isr_exit


If TMR1 is cleared, overflows will occur every 2 secs (for
32kHz-crystals with no prescaler).


--
- Rikard - http://bos.hack.org/cv/

2007\07\21@175453 by Jinx

face picon face
> What may be not clear enough for me is the way I can implement
> the third option (32 KHz crystal).

Put a 32kHz crystal across pins T1OSO (11) and T1OSI (12) with
caps to ground from those pins, 33pF is about right

I can find an 18F452 project using TMR1 oscillator (the F877 one I
was looking for is tucked away on a CD). Should be the same

bsf   t1con,t1oscen ; enable oscillator
bsf   t1con,tmr1cs ; select count source
bsf   t1con,tmr1on ; start

As you're maybe not doing anything too critical, I think use the TMR1
interrupt

2007\07\21@200624 by Carolina Dieguez

picon face
Great! But... Isn't it that this 32 KHz crystal can create some conflict
seeing there is also a 4 Mhz crystal as well??

I have the impression this is a silly question...

On 7/21/07, Jinx <.....joecolquittKILLspamspam.....clear.net.nz> wrote:
{Quote hidden}

> -

2007\07\21@205900 by Jinx

face picon face
> Great! But... Isn't it that this 32 KHz crystal can create some
> conflict seeing there is also a 4 Mhz crystal as well??

No, not at all. T1OSC is completely separate in the hardware
from OSC1/2. You can drive TMR1 with a crystal/resonator/
ExtClkIn and run the rest of the PIC with a different clock.
So TMR1 output can tick away at 1Hz or whatever whilst the
rest of the chip might be running at full speed. Plus T1OSC will
run while the rest of the PIC is asleep. This is useful for perhaps
a data logger, where you want to keep the power consumption
down (T1OSC uses about 20uA) but wake periodically to read
sensors, eg ADC and some maths, and then store or transmit
results. Or a time-of-day clock, perhaps usually running on mains
power, with T1OSC as the back-up timekeeper when on reserve
(battery) power. TMR1 IRQ wakes the PIC to increment TOD
registers, then the PIC goes back to sleep. You might use the 1:2
pre-scaler to generate a 0.25Hz (15 IRQ/min) interrupt from a free-
running TMR1. In a simple application that might reduce overall
current consumption from say 2mAh (no sleep) to just a little over
the TIOSC current, assuming 900 IRQs / hour waking for 0.1ms,
The job still gets done, accurately timed intervals, but with 1/100th
of the power consumption

2007\07\22@073612 by Rikard Bosnjakovic

picon face
On 22/07/07, Carolina Dieguez <EraseMEcaro.dieguezspam_OUTspamTakeThisOuTgmail.com> wrote:

> Great! But... Isn't it that this 32 KHz crystal can create some conflict
> seeing there is also a 4 Mhz crystal as well??

The 4MHz-crystal is for timing the PIC (system clock). The 32kHz is
entirely separate from the system clock and will not do anything
except making the Timer1 overflow at the times you want (by setting
TMR1).

However, if one by some strange reason would want the 32kHz to act as
a system clock, then the scenario would be something completely
different (and pointless).


--
- Rikard - http://bos.hack.org/cv/

2007\07\22@082213 by Jinx

face picon face
> want the 32kHz to act as a system clock, then the scenario
> would be something completely different (and pointless)

That's a little debatable. Say you have both the PIC and T1OSC
running at 32kHz. As I mentioned, the PIC can be put to sleep
and TMR1 will continue to interrupt. The interesting part comes
when you calculate how much power the PIC uses when it wakes
up to do what it's gotta do. A slow core clock (32kHz) uses
less power but the PIC is awake longer. A fast core clock uses
more power but is active for a much shorter time. For example
the F877 manual says

20MHz (HS) / 5.5V 7mA
32kHz (LP) / 3.0V 20uA

Extrapolating the XT figure of 1.6mA (5.5V) and 0.6mA (3.0V)
you might expect LP to be 53uA @ 5.5V + 20uA for TMR1

If the PIC woke for 500 instructions once per hour

32kHz => 61ms @ 73uA => 0.073/(3600*16.4) = 1.24nAh
20MHz => 0.1ms @ 7mA => 7/(3600*10000) = 0.19nAh

+ the SLEEP current, which is a heck more at 20,000nAh

I think nAh is the correct unit. Time * current = 0.0000044 and
0.0000007 respectively

2007\07\22@100052 by Gerhard Fiedler

picon face
Jinx wrote:

> Extrapolating the XT figure of 1.6mA (5.5V) and 0.6mA (3.0V)
> you might expect LP to be 53uA @ 5.5V + 20uA for TMR1
>
> If the PIC woke for 500 instructions once per hour
>
> 32kHz => 61ms @ 73uA => 0.073/(3600*16.4) = 1.24nAh
> 20MHz => 0.1ms @ 7mA => 7/(3600*10000) = 0.19nAh
>
>  + the SLEEP current, which is a heck more at 20,000nAh
>
> I think nAh is the correct unit.

It is. It's easier to see if you take the units along with all
calculations:

32kHz: q = 61ms * 73uA = 4453 nAs = 4453 nAs / (3600 s/h) = 1.24nAh
20MHz: q = 100us * 7mA = 700 nAs =  700 nAs / (3600 s/h) = 0.19nAh


Interesting result. Basically, it seems to say that the "overhead" of
having the PIC running at all is relevant, and therefore the economy with
lower speeds is not quite proportional.

It's different though if you have time dependent events and the PIC spends
part of its wake time waiting for something to happen (because then the
number of instruction cycles is not the same in both cases).

Gerhard

2007\07\22@101904 by Rikard Bosnjakovic

picon face
On 22/07/07, Jinx <joecolquittspamspam_OUTclear.net.nz> wrote:

> That's a little debatable.

You're right, ofcourse. What I meant, and forgot to write, is when you
have 32kHz as system clock and 4MHz as Timer1. That is, the opposite
of Carolina's setup. Using an external clock faster than the system
clock is a bit pointless, since you won't be able to catch all the
ticks.

Atleast if I'm thinking correctly.


--
- Rikard - http://bos.hack.org/cv/

2007\07\22@175046 by Jinx

face picon face
> Using an external clock faster than the system clock is a bit
> pointless, since you won't be able to catch all the ticks

That would be true in some cases. Perhaps if you had a frequency
counter for example. Although TxCKI can increment its timer at,
I think, up to 50MHz, for frequent sampling and processing, as
well as incrementing counters outside of of TMRx H:L, you might
want something a little faster than 32kHz. Generally 32kHz is still
plenty fast enough to do maths and update an LCD at a rate that
would be acceptable to the eye

2007\07\22@175046 by Jinx

face picon face
> Basically, it seems to say that the "overhead" of having the PIC
> running at all is relevant, and therefore the economy with lower
> speeds is not quite proportional

In the case of 32k vs 20MHz there's a 1:610 reduction in speed
but only a (guesstimate) 1:96 reduction in consumption. For the
older style 877 anyway. I'm sure the figures would be better for
nW parts

2007\07\22@223122 by Carolina Dieguez

picon face
Thanks to you all!!!!

Let me tell you Iearn a lot with these discussions... During this week, I'll
try to implement at least the two of those timer alternatives  you gave me.
Then, I'll tell you the results.

But first, I have to fixed some problems I had with the table, something I
thought it was the easiest part:o(((

Write you soon!
                        Carolina.

On 7/22/07, Jinx <@spam@joecolquittKILLspamspamclear.net.nz> wrote:
>
> > Basically, it seems to say that the "overhead" of having the PIC
> > running at all is relevant, and therefore the economy with lower
> > speeds is not quite proportional
>
> In the case of 32k vs 20MHz there's a 1:610 reduction in speed
> but only a (guesstimate) 1:96 reduction in consumption. For the
> older style 877 anyway. I'm sure the figures would be better for
> nW parts
>
> -

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