[PIC] Timer 2 question...

5 views
Skip to first unread message

PICdude

unread,
Jun 11, 2010, 10:19:58 PM6/11/10
to pic...@mit.edu
I'm reading through Microchip's AN1171 (Touch switch setup), and one
example for setting up Timer 2 (on a 16F with CapSense) shows this...

T2CON = 0x04; // T2ON, prescaler & postscaler = 1:1
T2CON = 0x01; // adjust prescaler
PR2 = 0xB4; // w/pres.1:1, 0xB4 sets 125us scan rate.
TMR2IF = 0x00;
TMR2IE = 0x01;
...

Is there a reason for the second line? T2CON is set, then immediately
changed again? I figured I'd ask in case there's some errata I missed
where perhaps the prescaler is supposed to be set to 1:1 while
switching on T2, etc. Or maybe this is some C-specific workaround?
Or am I reading too far into what is really a typo?

Cheers,
-Neil.


--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

ivp

unread,
Jun 12, 2010, 12:22:25 AM6/12/10
to Microcontroller discussion list - Public.
> T2CON = 0x04; // T2ON, prescaler & postscaler = 1:1
> T2CON = 0x01; // adjust prescaler

I'm sure it's a typo. Doesn't make sense otherwise. Not uncommon
in Microchip code examples unfortunately

Olin Lathrop

unread,
Jun 12, 2010, 8:13:18 AM6/12/10
to Microcontroller discussion list - Public.
PICdude wrote:
> T2CON = 0x04; // T2ON, prescaler & postscaler = 1:1
> T2CON = 0x01; // adjust prescaler
> PR2 = 0xB4; // w/pres.1:1, 0xB4 sets 125us scan rate.
> TMR2IF = 0x00;
> TMR2IE = 0x01;
> ...
>
> Is there a reason for the second line? T2CON is set, then immediately
> changed again? I figured I'd ask in case there's some errata I missed
> where perhaps the prescaler is supposed to be set to 1:1 while
> switching on T2, etc. Or maybe this is some C-specific workaround?
> Or am I reading too far into what is really a typo?

Microchip makes great chips, but their code generally sucks. Unless the
data or errata documents say something about changing the prescaler from 1:1
only after the timer is on (and I'd be rather surprised if they do), then
this is just a stupidity.

The comments also don't tell you as much as they should. When you write
this, you should document every bit. You should never set a collection of
individual bits to a HEX number like this. Do something like this (from
16F876, may be different on your chip):

T2CON = 0b00010101;
//X------- unused
//-0010--- postscaler 1:3
//-----1-- timer on
//------01 prescaler 1:4

Now we can see that the second line actually turns the timer off in addition
to changing the prescaler. I think it's pretty clear this code is just bad.
Run away. You're better off writing your own than looking at this bugware.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014. Gold level PIC consultants since 2000.

RussellMc

unread,
Jun 12, 2010, 9:45:25 AM6/12/10
to Microcontroller discussion list - Public.
*
Summary: Text which relies on the use of constant width font to carry some
of its content may have its message lost on less experienced people if the
need to view it correctly is not noted. Murphy, Gates and a host of players
conspire to fool them.

__________

Olin said:*

The comments also don't tell you as much as they should. When you write
this, you should document every bit. You should never set a collection of
individual bits to a HEX number like this. Do something like this (from
16F876, may be different on your chip):

T2CON = 0b00010101;
//X------- unused
//-0010--- postscaler 1:3
//-----1-- timer on
//------01 prescaler 1:4

Now we can see that the second line actually turns the timer off in addition
to changing the prescaler. I think it's pretty clear this code is just bad.
Run away. You're better off writing your own than looking at this bugware.

*Russell notes:*
*
*
The following may be useful to the uninitiated.
Olin's excellent example was visually blunted for me by the email system.
Some others may not realise that 'blunting ' has occurred.

People SHOULD [tm] persist in examining an example which is puzzlingly
inobvious until the mental background processes provide the aha moment and
what is unclear sudenly becomes blindingly obvious. However, the human
tendency is to skim past inobvious examples ununderstood* and miss the
point.

So: Note that Olin's 4 lines of 'code' above need to be viewed in fixed
width font (Courier or similar) to properly convey the point being made. If
that was immediately and blindingly obvious to you AND your examples always
survive intact when passed through the email system (as Olin's one didn't
for me) then move along, nothing of interest here, these are not the ones
you want ... .

Otherwise - I knew exactly what Olin was intending as I've seen him use
exactly the same sort of an example before and on occasion have done
likewise. Despite this it took several seconds of looking at it for the
point to sink in. I'm (more than usually) sleep deprived at present and I
can't guarantee how well Olin's point would stand out to the uninitiated -
but I was amazed at the difference in appearance when I changed it to
Courier font 'just to see' - even though I knew what would happen.

I'm using GMail in Rich Text mode, and it does some funny things to text
depending on path history, browser used at source, O/S etc. This may well
have started as constant width. On receipt to me the lines of - - - - -
appeared to lose their gaps and to be short continuous lines of inobvious
length.
A screen dump at larger size shows that this was not the case - but that
what eyes and brains saw.

To have most certainty that such examples have the widest possible audience,
adding something like "view in fixed width font" (even if it is SENT in
fixed width font) would be helpful. If one doesn't mind losing the audience
members who haven't reached this level then this may otherwise work as a
good filter, losing the newer members, and me after several days of low
sleep :-).

Attached image shows the two versions.
Nothing new, but a useful comparison.
Shrink back to normal display size for best reality fit.


Russell

Should ununderstood" be replaced with "derstood" ? :-).

Z Variable and fixed font example 70.jpg

Michael Watterson

unread,
Jun 12, 2010, 10:03:49 AM6/12/10
to Microcontroller discussion list - Public.
RussellMc wrote:
> *
> Summary: Text which relies on the use of constant width font to carry some
> of its content may have its message lost on less experienced people if the
> need to view it correctly is not noted. Murphy, Gates and a host of players
> conspire to fool them.
>
> __________
>
> Olin said:*
>
> The comments also don't tell you as much as they should. When you write
> this, you should document every bit. You should never set a collection of
> individual bits to a HEX number like this. Do something like this (from
> 16F876, may be different on your chip):
>
> T2CON = 0b00010101;
> //X------- unused
> //-0010--- postscaler 1:3
> //-----1-- timer on
> //------01 prescaler 1:4
>
>
Works fine in "Thunderbird". Worked in Eudora, and before that Netscape.
Earlier than 1994 I think I only had plain text.

Also I always select "Plain Text" in Thunderbird rather than HTML or
Both when sending to mailing lists

Dario Greggio

unread,
Jun 12, 2010, 10:24:06 AM6/12/10
to Microcontroller discussion list - Public.
Michael Watterson ha scritto:

> Works fine in "Thunderbird". Worked in Eudora, and before that Netscape.
> Earlier than 1994 I think I only had plain text.
>
> Also I always select "Plain Text" in Thunderbird rather than HTML or
> Both when sending to mailing lists


agreed on both as yesterday :)

--

Ciao, Dario
--
Cyberdyne

Reply all
Reply to author
Forward
0 new messages