View previous topic :: View next topic |
Author |
Message |
hagagyosseh
Joined: 18 Jun 2012 Posts: 2
|
S.O.S unknown delay pic16f877a when awake from sleep int_rb |
Posted: Mon Jun 18, 2012 12:12 pm |
|
|
Hi
I have a problem.
My program runs on endless loop on sleep. When int_rb happens I have 13 ms delay from int to rb_isr.
Why? (Without sleep it jumps without any delay)
Here is a test code !
Code: |
#include <16F877A.h
#fuses xt,wdt,noprotect,noput,nobrownout,nolvp
#use delay(clock=2000000,restart_wdt)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#int_rb
void rb_isr()
{
disable_interrupts(int_rb);
dummy= portb;
counter++;
led=1;
delay_ms(1);
led=0;
RBIF = 0 ;
}
void main()
{
//PORTS initialization
set_tris_a(0b11111101);
set_tris_b(0b11111101);
set_tris_c(0b10010110);
set_tris_d(0b00000000);
set_tris_e(0b00000111);
PORTA=0b00000000;
PORTB=0b00100000;
PORTC=0b00000000;
PORTD=0b00100001;
port_b_pullups(1);
setup_counters(rtcc_internal,wdt_576ms);
enable_interrupts(int_rb);
enable_interrupts(global);
while (1)
{
Restart_wdt();
enable_interrupts(int_rb);
sleep();
delay_cycles(1);
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Mon Jun 18, 2012 12:21 pm |
|
|
You should use the 'insert code' instead of typing it here...
There are several errors, like wrong way brackets, in this program.
As such we cannot 'cut and paste' to try it on our PICs.
You do not need to disable interrupts while in an ISR..
You should NEVER use delay_ms(nnn) while in an ISR..
I don't know why you have a delay_cycles(1) after the sleep() command.
You've never declared any variables( dummy, led,counter,etc.)...
This 'program' can't compile or work as shown..... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Mon Jun 18, 2012 12:53 pm |
|
|
XT fuse at 20MHz.
There will be a delay of perhaps 100uSec, while the oscillator starts. This is normal, but with XT selected, the crystal drive will be so low, that it could easily take several mSec for the oscillator to stabilise. During the whole of this time the oscillator will be running 'slow', so a delay will be many times what you expect....
Delay_cycles(1) after the sleep is good programming. This codes as a 'nop', and since the instruction after the sleep is 'prefetched' avoids any nasties that can result with this giving unexpected results.
Best Wises |
|
|
hagagyosseh
Joined: 18 Jun 2012 Posts: 2
|
|
Posted: Mon Jun 18, 2012 11:58 pm |
|
|
In reply for temtronic
I can't insert the real code because it is on computer without ability to extract it out. the real code does compile and work fine besides the delay I have on int_rb from sleep. The led on isr is just for test to know when the code arrives to isr and measure it with scope. All the variables are declared.
In reply for Ttelmah
I did not understand your answer.
I need to use other fuse declaration instead of XT ?
Is so what declaration I should use on 2MHZ ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Tue Jun 19, 2012 3:01 am |
|
|
Have a look at Microchip AN849. I must admit I assumed you were at 20MHz, (this is why I like to use xxMHz, rather than lots of zero's), but the comment still applies. Particularly look at the section starting half way down the right hand column on Page8. Starting the crystal from sleep, is _hard_, and requires careful design of the oscillator. I'd suspect yours has problems....
Best Wishes |
|
|
|