16F628(A) at 16 MHz and TMR0 problem

Started by Wolfgang July 27, 2005
Hi,
i am using "PIC Simulator IDE" from OshonSoft to compile and assemble
my
code for the 16F628. All is working fine. But I have got the problem,
that when I want to have i.e. 50 Hz frequency output to one pin on
port a, i never get the result I am expecting.
I compute like this : clock frequency is 16 Mhz, so internal clock is
4MHz. If I use a prescaler value of 1:4 (i.e PS2, PS1, PS0 = '001')
and a TMR0 value of 178, I should expect the following output
frequency:

78 = 256 - 178

(4MHz : 4) : 78 = 12.821 Hz. so all right. in my ISR another register
becomes decremented and I only toggle the output pin, when that
register is 0.

12.821Hz : 256 = ~50Hz.

Using an oszi, i read out a high time from about 22ms. I cant believe
that this offset from about 10 percent is all what i can expect.

The same PIC and the same IDE create a fairly exact High time from
about 5 ms if i use a "waitms 5" statement.

what is wrong?

code snippets: (vorteilerwert is (PS2, PS1, PS0),i.e. 1)

OPTION_REG = OPTION_REG + vorteilerwert
TMR0 = zaehlerwert
INTCON.T0IE = 1 'enable Timer0 interrupts
INTCON.GIE = 1 'enable all un-masked interrupts
OPTION_REG.T0CS = 0 'set Timer0 clock source to internal
'instruction cycle clock
...

On Interrupt
Save System
INTCON.GIE = 0
TMR0 = zaehlerwert
ASM: decf irqpause,1
ASM: btfsc STATUS, Z
Goto doit
Goto donothing
doit:
Toggle servoaus
donothing:
INTCON.T0IF = 0 'enable new TMR0 interrupts
INTCON.GIE = 1 'enable all un-masked interrupts



Hi Wolfgang

At the end of your interrupt service routine you need
a resume to restore your saved state and return , I
Don't know if this has anytyhing to do with it but
worth trying

Peter van Hoof

--- Wolfgang <wowogiengen@wowo...> wrote:

> Hi,
> i am using "PIC Simulator IDE" from OshonSoft to
> compile and assemble
> my
> code for the 16F628. All is working fine. But I have
> got the problem,
> that when I want to have i.e. 50 Hz frequency output
> to one pin on
> port a, i never get the result I am expecting.
> I compute like this : clock frequency is 16 Mhz, so
> internal clock is
> 4MHz. If I use a prescaler value of 1:4 (i.e PS2,
> PS1, PS0 = '001')
> and a TMR0 value of 178, I should expect the
> following output
> frequency:
>
> 78 = 256 - 178
>
> (4MHz : 4) : 78 = 12.821 Hz. so all right. in my ISR
> another register
> becomes decremented and I only toggle the output
> pin, when that
> register is 0.
>
> 12.821Hz : 256 = ~50Hz.
>
> Using an oszi, i read out a high time from about
> 22ms. I cant believe
> that this offset from about 10 percent is all what i
> can expect.
>
> The same PIC and the same IDE create a fairly exact
> High time from
> about 5 ms if i use a "waitms 5" statement.
>
> what is wrong?
>
> code snippets: (vorteilerwert is (PS2, PS1,
> PS0),i.e. 1)
>
> OPTION_REG = OPTION_REG + vorteilerwert
> TMR0 = zaehlerwert
> INTCON.T0IE = 1 'enable Timer0 interrupts
> INTCON.GIE = 1 'enable all un-masked interrupts
> OPTION_REG.T0CS = 0 'set Timer0 clock source to
> internal
> 'instruction cycle clock
> ...
>
> On Interrupt
> Save System
> INTCON.GIE = 0
> TMR0 = zaehlerwert
> ASM: decf irqpause,1
> ASM: btfsc STATUS, Z
> Goto doit
> Goto donothing
> doit:
> Toggle servoaus
> donothing:
> INTCON.T0IF = 0 'enable new TMR0 interrupts
> INTCON.GIE = 1 'enable all un-masked interrupts >
>




HI Peter,

In the 16F877 PIC and others, you need to adjust your TIMER0 reload value by
a value of 2. It's in the midrange guide.

Alan KM6VV

> -----Original Message-----
> From: piclist@picl... [mailto:piclist@picl...] On Behalf
> Of Peter van Hoof
> Sent: Wednesday, July 27, 2005 11:54 AM
> To: piclist@picl...
> Subject: Re: [piclist] 16F628(A) at 16 MHz and TMR0 problem
>
> Hi Wolfgang
>
> At the end of your interrupt service routine you need
> a resume to restore your saved state and return , I
> Don't know if this has anytyhing to do with it but
> worth trying
>
> Peter van Hoof
>
> --- Wolfgang <wowogiengen@wowo...> wrote:
>
> > Hi,
> > i am using "PIC Simulator IDE" from OshonSoft to
> > compile and assemble
> > my
> > code for the 16F628. All is working fine. But I have
> > got the problem,
> > that when I want to have i.e. 50 Hz frequency output
> > to one pin on
> > port a, i never get the result I am expecting.
> > I compute like this : clock frequency is 16 Mhz, so
> > internal clock is
> > 4MHz. If I use a prescaler value of 1:4 (i.e PS2,
> > PS1, PS0 = '001')
> > and a TMR0 value of 178, I should expect the
> > following output
> > frequency:
> >
> > 78 = 256 - 178
> >
> > (4MHz : 4) : 78 = 12.821 Hz. so all right. in my ISR
> > another register
> > becomes decremented and I only toggle the output
> > pin, when that
> > register is 0.
> >
> > 12.821Hz : 256 = ~50Hz.
> >
> > Using an oszi, i read out a high time from about
> > 22ms. I cant believe
> > that this offset from about 10 percent is all what i
> > can expect.
> >
> > The same PIC and the same IDE create a fairly exact
> > High time from
> > about 5 ms if i use a "waitms 5" statement.
> >
> > what is wrong?
> >
> > code snippets: (vorteilerwert is (PS2, PS1,
> > PS0),i.e. 1)
> >
> > OPTION_REG = OPTION_REG + vorteilerwert
> > TMR0 = zaehlerwert
> > INTCON.T0IE = 1 'enable Timer0 interrupts
> > INTCON.GIE = 1 'enable all un-masked interrupts
> > OPTION_REG.T0CS = 0 'set Timer0 clock source to
> > internal
> > 'instruction cycle clock
> > ...
> >
> > On Interrupt
> > Save System
> > INTCON.GIE = 0
> > TMR0 = zaehlerwert
> > ASM: decf irqpause,1
> > ASM: btfsc STATUS, Z
> > Goto doit
> > Goto donothing
> > doit:
> > Toggle servoaus
> > donothing:
> > INTCON.T0IF = 0 'enable new TMR0 interrupts
> > INTCON.GIE = 1 'enable all un-masked interrupts
> >
> >
> >
> >
> to unsubscribe, go to http://www.yahoogroups.com and follow the
> instructions
> Yahoo! Groups Links


--- In piclist@picl..., Peter van Hoof <pvhoof@y...> wrote:
> Hi Wolfgang
>
> At the end of your interrupt service routine you need
> a resume to restore your saved state and return , I
> Don't know if this has anytyhing to do with it but
> worth trying
>
> Peter van Hoof
>

Ok... Peter...

I have that resume in my programm. I just forgot to paste it into the
snippet. The suggestion with the adjustment of the reload value could
be the solution. I will try this evening

Wolfgang