Searching \ for '[PIC]: 16F877 TMR0 Problem' 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: '16F877 TMR0 Problem'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: 16F877 TMR0 Problem'
2002\06\07@144650 by Peter Betts

flavicon
face
Hello,

I've been playing with PIC for a while now and have created a number of
units based on the 16F84 range.
Just lately I've migrated to the PIC16F877 and using the ICD have developed
a graphical display unit with LCD etc etc.
This all works 100% O.K. I'm using HiTECH C.

The problem is this.....

I've been trying to get the TMR0 timer0 to increment once per instruction
cycle on my PIC16F877.
This would at first appear very very simple. I have done this before
without any problem on the PIC16F84. It also works perfectly under
simulation
but.......

The hardware PIC16F877 appears to randomly increment the TMR0 register as
soon as I set TOCS=0 and I cannot see any reason for this.
Even single assembler instructions to set the TMR0 register to a value do
not work.
With TOCS=1 I can set TMR0 to any value I like but of course it doesn't do
anything as I don't have any clock source on T0CKI.

I have included (In the next email!!!)  a sample program below (written for
HITECH C). This code functions perfectly under simulation but neither my
hardware (20MHz) or the ICD demo board (4Mhz) function properly. I also
compiled it for the PIC16F84 and that ran perfectly as well so it only the
PIC16F877 that seems to get upset?

I would not suspect my hardware as it also does this on the "Advanced
Transdata" demo board that came with my ICD.

I have also tried to do this under MPLAB
movlw    0xA
movwf    0x1
but it does not work!!! Unless I have T0CS=1 which is pretty stupid.
TMR0 just goes on blindly changing at random.

I've checked the register bank select bits are being set to bank0 where
TMR0 is located at address 0x1.

I am at a complete loss and cannot work out why the same program works fine
for the 16F84 but not the 16F877 ????
I hope you can help as this has now crippled my product and I'm going
nowhere fast.

Thanks
Pete
spam_OUTpeterTakeThisOuTspamtrlperformance.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\06\07@144900 by Peter Betts

flavicon
face
Here is the sample HiTECH C program I cannot get to work on the PIC16F877

What am I doing wrong?
Thanks
Pete

#include <pic.h>

typedef unsigned char UINT8;
typedef unsigned short UINT16;

#define TIMER_PRESET    50

/***************************************************************************
***
*                              Subroutines
;***************************************************************************
***/
/*-----------------------------
initalisation --------------------------------*/
void    init_pic(void)
   {
   // Configure TMR0 Port and interrupts

   TRISC = 0x0;

   T0CS = 0;       // Timer increments on instruction clock
   T0IE = 1;       // Enable interrupt on TMR0 overflow
   INTEDG = 1;     // Rising edge trigger the interrupt
   INTE = 1;       // enable the external interrupt RB0 change
   GIE = 1;        // Global interrupt enable
   }

/***************************************************************************
***
*                        Interrupt Service Routine

****************************************************************************
*/
static void interrupt
isr(void)
{
/*-------------------------- TMR0 timer
interrupt ---------------------------*/
   if (T0IF) {
       TMR0 = TIMER_PRESET;
       T0IF = 0;          // clear the interrupt flag

   }
}
/* ****************************** END ISR
************************************/

void main(void)
{
   UINT8 counter=0;
   UINT16 counter1=0;
   init_pic();

background_tasks:

   if ( counter1 == 0)
   {
       PORTC = counter;
       counter++;
   }
   counter1++;

   goto    background_tasks;
}

/////////////////////////////  END OF FILE
////////////////////////////////////

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\06\07@153942 by William Bross

flavicon
face
just a guess but I don't see you assigning the prescaler to the WDT, OPTION
register PSA = 1?  If not, the prescaler will default to TMR0 with whatever
PS0-PS2 are set to, 000 being a 1:2 rate.

Bill

At 07:39 PM 06/07/2002 +0100, you wrote:
{Quote hidden}

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\06\10@031948 by Peter Betts

picon face
http://www.piclist.com/postbot.asp?id=piclist\2002\06\07\153942a

Thanks for the reply and yes I hadn't assigned the Prescaler in that example,BUT this is not the real problem.

I have written other code to select the prescaler for the TMR0 but is not that the PIC isn't counting/timing at the correct rate it's more drastic....
*** the TMR0 register is just RANDOMLY changing **** I can see no sequence in the numbers at all. They appear to just change at will on every instruction.

When I run this in the simulator all is well and I get the TMR0 register changing as expected. So why NOT on the real hardware???

I have now written it in assembler and still I must be missing something obvious...

; Use PIC16F84 and set radix system to decimal
       LIST P=16F877, R=DEC

;******************************************************************************
; Include header file
;******************************************************************************
#include "p16f877.inc"

#define TIMER_PRESET    180

;******************************************************************************
;                               equates
;******************************************************************************
; WORDS
status_temp             equ     0x0c
w_temp                  equ     0x0d


;******************************************************************************
;                            START of CODE
;******************************************************************************
       org     0x0                     ; reset vector
       goto    init                    ; jump over subroutines

       org     0x4                     ; Interrupts
       goto    isr                     ; Goto this service routine


;******************************************************************************
;                            initalisation
;******************************************************************************

init    clrf    PORTC                   ; set = 0
       bsf     STATUS,RP0              ; BANK1
       movlw   0x00
       movwf   TRISC

       movlw   b'11011111'             ;
       movwf   OPTION_REG              ; Set Timer ON and use prescaler of 256
       bcf     STATUS,RP0              ; Retrun to BANK0

       movlw   TIMER_PRESET
       movwf   TMR0                    ; Reset Timer0

       bsf     INTCON,T0IE             ; Turn Timer Interrupt On
       bsf     INTCON,INTE             ; Turn RB0 Change Interrupt On
       bsf     INTCON,GIE              ; Turn Global Interrupts On

;******************************************************************************
;                             main program
;******************************************************************************

main

background_loop
       goto    background_loop


;******************************************************************************
;                       Interrupt Service Routines
;******************************************************************************
isr
; --------------------------- save context ------------------------------------
context_save
       movwf   w_temp                  ; save off W and STATUS registers
       swapf   STATUS, w
       movwf   status_temp
       clrf    STATUS                  ; select Bank0

; ************************* TIMER TMR0 Interrupt ******************************
tmr0_int
; ----------------------- Clear Interrupt Condition --------------------------
       bcf     INTCON,T0IF             ; Clear pending interrupt flag

; ------------- Reset TMR0 Timer State for next timed interrupt --------------
       movlw   TIMER_PRESET
       movwf   TMR0                    ; Reset Timer0
tmr0_int_end
; ******************** restore context saved on entry to ISR *******************
isr_end

context_restore
       swapf   status_temp, w          ; restore all registers
       movwf   STATUS
       swapf   w_temp, f
       swapf   w_temp, w

; -------------------- END of interrupt service routine -----------------------
       retfie                          ; Return from interrupt service routine
;******************************************************************************


;******************************************************************************
;******************************************************************************
       end
; ------------------------------ END OF FILE ----------------------------------




---
Peter R Betts
http://www.piclist.com/member/PRB-TRL-AA7
PIC/PICList FAQ: http://www.piclist.com

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestKILLspamspam@spam@mitvma.mit.edu


2002\06\10@112856 by William Bross
flavicon
face
Hi Peter.
I just did an MPLAB sim on your asm version.  It works as you would expect.
I also ran it as a 16C73 on my trusty old PICmaster emulator (won't do
877s) and it works the same as the sim. TMR0 increments by ones and twos as
advertised depending on the instruction type.  BTW, your equates need to be
moved up to 0x20 or higher.  Right now, they are in the middle of the SFRs.

Now for the real question -- is your program really working and your
emulator just isn't showing you or is there really something wrong?  What
you should do is calculate your TMR0 interrupt rate and toggle an output
pin.  Monitor the pin with a scope to see if it happens at the right rate.
If it does, at least you can keep developing with some degree of confidence
until we can sort out why TMR0 doesn't look like it works.  If not, might
be time to call your emulator manufacturer and talk to him.

Bill

At 08:08 AM 06/10/2002 +0100, you wrote:
> http://www.piclist.com/postbot.asp?id=piclist\2002\06\07\153942a
>
>Thanks for the reply and yes I hadn't assigned the Prescaler in that
example,BUT this is not the real problem.
>
>I have written other code to select the prescaler for the TMR0 but is not
that the PIC isn't counting/timing at the correct rate it's more drastic....
>
>*** the TMR0 register is just RANDOMLY changing ****
>I can see no sequence in the numbers at all. They appear to just change at
will on every instruction.
>
>When I run this in the simulator all is well and I get the TMR0 register
changing as expected.
>So why NOT on the real hardware???
>
>I have now written it in assembler and still I must be missing something
obvious...
>
>; Use PIC16F84 and set radix system to decimal
>        LIST P=16F877, R=DEC
>
code snipped...

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestspamKILLspammitvma.mit.edu


2002\06\10@113840 by Alan B. Pearce

face picon face
>*** the TMR0 register is just RANDOMLY changing ****
>I can see no sequence in the numbers at all. They appear to just change at
will on every instruction.
>
>When I run this in the simulator all is well and I get the TMR0 register
changing as expected.
>So why NOT on the real hardware???

Are you running this with an ICD? If you are then remember that the timer
will increment for every instruction that the ICD code executes as well, not
just the destination code. I seem to remember a warning about this somewhere
in the ICD documentation.

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestKILLspamspam.....mitvma.mit.edu


2002\06\10@114718 by Peter Betts

picon face
Bill,

> I just did an MPLAB sim on your asm version.  It works as you
> would expect.

Great, it worked for both the 16F84 and 16F877 sim' targets and the 16F84 hardware debugger but not the 16f877 (with small changes between the software versions for target compatibility).


> equates need to be moved up to 0x20 or higher.  Right now, they are in the
> middle of the SFRs.


Doh! O.K. That was me trying to hack something quick in ASM to prove a point. The Hitech C version should have all that sorted properly. It may be masking something however.


> is your program really working and your emulator just isn't showing you or is there really something
> wrong?  
Ummm... I see. Yes I could execute some LED (pin) toggling in the ISR as a test and measure with my scope.
That would prove its rate is fixed or random.


> until we can sort out why TMR0 doesn't look like it works.  
This might be the case. The code did call the ISR function but I didn't measure the period between calls, only stopped the debugger and checked the TMR0 register value. It may have just randomly hit 0 and triggered or in fact counted up and wrapped around as required.

> If not, might be time to call your emulator manufacturer and talk to him.

Its the MicroChip made ICD (In Circuit Debugger) with the latest (last week) downloaded firmware.

Thanks for your help
Pete

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspam_OUTspamTakeThisOuTmitvma.mit.edu


2002\06\10@115336 by Jonathan Starr

flavicon
face
I have seen something like this before, my timer worked fine then suddenly it would go berserk and shoot off into hyperspace, after trying everything we could think of I noticed a hair-line solder bridge between two outputs, so when one turned on it tried to drag the other one up and the PIC didn't like it at all, but crucially didn't totally stop working either.
It wasn't visible with the naked eye, and we only saw it when we got the board under a microscope, it's worth considering, we got too stuck on the software and didn't consider a hardware problem could possibly cause an apparent software problem :{

Good luck !

Jonathan Starr

R&D Technician
Electronix Limited

Tel +44 (0)1993 700510
Fax +44 (0)1993 700511


e-mail jonathansspamspam_OUTelectronix.co.uk
web http://www.electronix.co.uk
This e-mail has been sent to you by Electronix Ltd.
This communication is intended for the addressee only, is private and confidential, and is subject to the applicable terms and conditions.
If you are not the intended recipient, any disclosure, copying or
distribution of this information is strictly prohibited.

--


{Quote hidden}

example,BUT this is not the real problem.
>
>I have written other code to select the prescaler for the TMR0 but is not
that the PIC isn't counting/timing at the correct rate it's more drastic....
>
>*** the TMR0 register is just RANDOMLY changing ****
>I can see no sequence in the numbers at all. They appear to just change at
will on every instruction.
>
>When I run this in the simulator all is well and I get the TMR0 register
changing as expected.
>So why NOT on the real hardware???
>
>I have now written it in assembler and still I must be missing something
obvious...
>
>; Use PIC16F84 and set radix system to decimal
>        LIST P=16F877, R=DEC
>
code snipped...

--
http://www.piclist.com hint: To leave the PICList
@spam@piclist-unsubscribe-requestKILLspamspammitvma.mit.edu
--
http://www.piclist.com hint: To leave the PICList
KILLspampiclist-unsubscribe-requestKILLspamspammitvma.mit.edu


2002\06\10@115545 by Peter Betts

picon face
> Are you running this with an ICD?

YES.

> the timer will increment for every instruction that the ICD code executes as well, not just the destination code.
Oh know!!!! Really????
That would explain a heck of a lot. Never even considered that. Sees obvious now you mention it.


> I seem to remember a warning about this somewhere in the ICD documentation.

Trust me to miss that little gem.
I'll try to find this in the ICD doc's.

Does the ICD execute extra cycles ONLY when processing a breakpoint or stepping etc, when the debugger has to communicate with the host or does it run a few cycles all the time in the background? (Sorry if this is in the doc's I'm not at home to look them up)

I'm trying to get a constant 10ms interrupt that will sample my ADC inputs and then spend the rest of the time processing the data and displaying on an LCD screen (which is slow!) Obviously I do not want this to vary as it's my main sampling rate.

Thanks for the reply, I think this is the answer.

Pete

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu


2002\06\10@120713 by Alan B. Pearce

face picon face
>Does the ICD execute extra cycles ONLY when processing
>a breakpoint or stepping etc, when the debugger has to
>communicate with the host or does it run a few cycles
>all the time in the background? (Sorry if this is in
>the doc's I'm not at home to look them up)

This should only be a problem while in the ICD code. Just remember that it
is a program in your programming space that has been enabled by a special
bit of hardware that has in effect caused a special interrupt.

>I'm trying to get a constant 10ms interrupt that will
>sample my ADC inputs and then spend the rest of the
>time processing the data and displaying on an LCD
>screen (which is slow!) Obviously I do not want this
>to vary as it's my main sampling rate.

Well I would say that to do this you are best to get the operations to a
point where they work correctly, i.e. the A/D operation, and the LCD
operation, and then dispense with the ICD and have printf(debug message)
messages going over a serial interface to a terminal when you let it rip so
you are not stopping your code.

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestspamBeGonespammitvma.mit.edu


2002\06\10@121518 by Peter Betts

picon face
> Just remember that it is a program in your programming space that has been enabled
> by a special bit of hardware that has in effect caused a special interrupt.

Yes, I forgot that. All to easy to think it's a real emulator.


> Well I would say that to do this you are best to get the
> operations to a
> point where they work correctly, i.e. the A/D operation, and the LCD
> operation, and then dispense with the ICD
Yep. At least now I feel happy about carrying on. Over the weekend I was pulling my hair out.
Seems so simple now I know.

I know all the seperate bits work if I run everything in a big background task. I just needed to get a constant periodic sampling interval to make the numbers displayed meaningful and went off on a tangent.

Thanks for all your help everyone.

Pete
now with a big smile

--
http://www.piclist.com hint: To leave the PICList
TakeThisOuTpiclist-unsubscribe-requestEraseMEspamspam_OUTmitvma.mit.edu


2002\06\10@164928 by Olin Lathrop

face picon face
>>
*** the TMR0 register is just RANDOMLY changing ****
I can see no sequence in the numbers at all. They appear to just change at
will on every instruction.

When I run this in the simulator all is well and I get the TMR0 register
changing as expected.
So why NOT on the real hardware???
<<

How are you "seeing" timer 0 values on the real hardware?  With the in
circuit debugger I perhaps?  I'm not familiar with the in circuit debugger
since I use the ICE, but doesn't that thing execute code on the target for
each "single step"?  If so, that would explain the additional timer 0
increments.  Timer 0 behaves very nicely with the ICE.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestspamTakeThisOuTmitvma.mit.edu


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