View previous topic :: View next topic |
Author |
Message |
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
Timer can't run after reset. :confused: |
Posted: Sun Sep 30, 2012 12:45 pm |
|
|
Hi everyone,
I have a problem with a basic program in CCS C with PIC16F877A (shift led from left to right depend on timer0). It's ok when I run it normally, but when I press reset button (connect to MCLR) then timer can't run anymore.
I ran it in Proteus and debug in step by step mode in MPLAB, I found that after I press reset button, program run ok (line by line), but when it run to a line to setup timer, it's crashed and stop immediately.
Code: | setup_timer_0(RTCC_DIV_256|RTCC_INTERNAL); |
It's seem that I can't access to Timer0 register after I press reset button.
Please help me !!!
Sorry for my poor English. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 30, 2012 1:41 pm |
|
|
Does the MCLR pin have a pull-up resistor on it ? It needs one.
Add a 10K pull-up to MCLR. |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Sun Sep 30, 2012 1:55 pm |
|
|
Thanks man, but ....yes, i did it, I mean that program is already reset but when it runs to a line to setup timer (interrupt) it's crashed.
Do you think that problem cause by previous interrupt is broken suddenly ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Sep 30, 2012 1:57 pm |
|
|
Are you using the watchdog?.
If so, first possibility. Watchdog timeout.
Make sure you add a restart_wdt at the start of the code.
What compiler version?.
There is a very specific instruction sequence needed when the prescaler is switched to/from the watchdog/timer. The compiler should generate this, but since what is needed changes slightly according to what the existing setting is, this may be a register that is not correctly set to the power on value when you do a reset. So, tell us the compiler version and we can check whether the sequence used does avoid this problem. This causes a device reset, so sounds like it could be the problem.
Best Wishes |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Sun Sep 30, 2012 2:00 pm |
|
|
My Compiler version is 4.105 and ....no , I don't use wdt, just use normal timer. |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Sun Sep 30, 2012 2:28 pm |
|
|
Any one can help me ?? I found that this problem also appear when I use interrupt (I tested with rb4-7 interrupt). When program run to a line to enable global interrupt, occur appear same as my #1, why ? Those problem only appear when I press reset button. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 30, 2012 2:48 pm |
|
|
Post a small test program that shows the problem with setup_timer_0.
Example of small test program:
http://www.ccsinfo.com/forum/viewtopic.php?t=37807&start=3
Note that it has the #include for the PIC, #fuses, #use delay, main(), etc.
It's a complete program.
But make your test program have much less code. Then post it. |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Sun Sep 30, 2012 5:31 pm |
|
|
ok, for ex my program structure will like this:
Code: | #include <16f877a.h>
#device PIC16f877a*=16
#fuses NOWDT,PUT,HS,NOPROTECT
#use delay(clock = 20000000)
#use fast_io(b)
#int_timer0
void timer()
{
clear_interrupt(int_timer0);
disable_interrupts(GLOBAL);
//do something
enable_interrupts(GLOBAL);
set_timer0(0);
}
#int_rb
void interrupt()
{
clear_interrupt(int_rb);
disable_interrupts(GLOBAL);
//do something
enable_interrupts(GLOBAL);
}
void main()
{
set_tris_b(0b11110000);
enable_interrupts(int_timer0);
enable_interrupts(int_rb);
setup_timer_0(RTCC_DIV_4|RTCC_INTERNAL);
set_timer0(0);
enable_interrupts(GLOBAL); <<< when I run step by step I found that program will be crashed after run this line
while(1)
{
//do something
}
}
|
Compile ok, run ok at first time, but problem appear when press reset (when global interrupt flag turn to 1), so that I must stop and restart proteus if I want to rerun program...@@ |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 30, 2012 6:28 pm |
|
|
Your program has many problems. But first I want to ask, which PIC
pin is connected to the "reset" button ? |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Sun Sep 30, 2012 6:50 pm |
|
|
please show me problem , :( , i'm new to ccs . I connect RE3/MCLR to reset button with pull up resistor 10k . |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Oct 01, 2012 2:23 am |
|
|
I'll list two 'deadly' problems with the code.
1) On the PIC, the _hardware_ disables interrupts when the handler is called. The user must _never_ _ever_ enable the GLOBAL interrupt inside the handler. Doing so _will_ crash the code at some point if any interrupt triggers before the code actually returns.
2) INT_RB, _can not be cleared, until it's cause is cleared_. To clear INT_RB, you _must_ read from PORTB to reset the internal latch.
As it stands, INT_RB will remain permanently set, since you are not reading the port, and when you enable_interrupts(GLOBAL), the code will loop back into itself, overwriting the stack, and causing a lockup/crash.
You do realise the INT_TIMER0, implies that timer0, _has_ wrapped to zero.
The timer interrupt will also cause a problem, but to a fractionally lesser extent, only giving a crash if INT_RB also occurs.
Best Wishes |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Mon Oct 01, 2012 5:40 am |
|
|
I'm not really understand how to " clear INT_RB, you _must_ read from PORTB to reset the internal latch. ".
Can you give me an example to do that ? Because I think timer0 and int_rb will be reset after MCLR active ?
Sorry for my poor of knowledge.
Thanks. |
|
|
falcon1877
Joined: 30 Sep 2012 Posts: 24
|
|
Posted: Mon Oct 01, 2012 9:43 am |
|
|
Anyone can give me a solution ? :( |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Oct 01, 2012 9:59 am |
|
|
Is this a Proteus simulation or real hardware?
Mike |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Oct 01, 2012 11:29 am |
|
|
Mike...last line says it all.....Proteus...!!!
I had to reread it as well....
One solution is to use real hardware.then us real world types can really help...
Another is to get rid of proteus,well knwn to be full of bugs,erros,bad drcs.
Yet another, rewrite Proteus to correctly simulate your PIC... |
|
|
|