View previous topic :: View next topic |
Author |
Message |
jojos
Joined: 30 Apr 2007 Posts: 64
|
delay_ms instruction problem |
Posted: Fri Jan 18, 2008 3:25 am |
|
|
Hello.I use PiC 18F2520. CCS version 3.249
I noticed that when i use delay_ms() instruction the interrupts from e.g. Timer1
doesn't work correctly.TO enter the interrupt service routine
it must first complete the delay time from the delay_ms instruction
I want when the Timer reach its FFFF value ,and produce its interrupt,the program to
go into the interrupt service routine and not to wait to complete the
delay_ms time.
How can i achieve that?Maybe a software delay routine of my own? |
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 18, 2008 4:20 am |
|
|
It sounds as if you are using delays inside an interrupt routine somewhere.
If so, interrupts _will_ be disabled during the delays, to prevent re-entracy (you cannot call code from inside itself on the PIC, and if delays are using inside the interrupt, which could therefore result in delays inside delays if the interrupt occurred inside the delay).
1) Avoid using delays in interrupts, unless absolutely neccessary. If _short_ delays are needed, use the 'delay_cycles' instruction.
2) Try to consider using delays of another sort (use a 'tick' timer interrupt, and count this, rather than waiting inside the interrupt).
2) If you absolutely _must_ have delays, then add a separate '#use delay' declaration for the interrupt code. This will result in separate code being generated for the delaysin the interrupts, and prevent interrupts being disabled in the 'main' code. A search here, will find examples of how to do this.
Best Wishes |
|
|
jojos
Joined: 30 Apr 2007 Posts: 64
|
|
Posted: Fri Jan 18, 2008 6:50 am |
|
|
Found the problem.Inside interrupt routine i called A/D conversion to catch some button pressing.Inside A/D routine i used these delay calls
Code: |
ADON=1;
delay_ms(1);
WaitAdc:
GO=1;
//delay_cycles(10);
Wait:
if(GO !=0) //Wait until conversion finishes
goto Wait;
.
.
.
.
.
|
I removed these delay calls and everything works good.I dont like though the fact i had to remove these delay calls.I believe they are necessary for the A/D to work well(i am not sure).
Thanks anyway for the response. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jan 18, 2008 7:29 am |
|
|
In that small snapshot of code you have 2 labels and 1 goto.
I am assuming you are writing in C.
try
while (GO) {}
for the loop!
Someone is bound to say that goto is quicker. If you therefore need speed write in assembly. Although goto is allowed in C it is not good practise to use it.
And generally speaking there is always a nicer way to do it than using a GOTO even if it requires a little more code. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Fri Jan 18, 2008 8:38 am |
|
|
Many new to progamming see don't use goto as the eleventh commandment.
While{} clauses can and often are just as obtuse. Since both goto and while{} are useful then both should be used. The issue with c is pointers. A misused or misunderstood pointer can cause much more grief than a misused goto or over convoluted while{} statements.
Quote: |
Wayne_ wrote:
Remove the & from &CANmsg[0] and &CANmsg[1]
CANmsg[0] is the same as a pointer to the first entry and CANmsg[1] to the second.
No, if you remove the ampersand then the structure will be passed, not the address of the structure. The posters problem lies elsewhere, the code as he has shown is (syntax wise anyway) ok.
|
|
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 18, 2008 10:39 am |
|
|
A a comment though, why not just use the read_adc command?. This already does exactly what the posted code was doing. Unless there is something 'wrong' with the CCS code, use it. It makes code simpler, and reduces the number of likely fault locations.
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jan 21, 2008 2:59 am |
|
|
Douglas Kennedy
"Many new to programming see don't use goto as the eleventh commandment. "
Referring to me ? LOL
And to quote a mistake I posted in another thread. WOW.
Using goto is BAD practice in C.
Mainly because misused goto's will cause stack problems due to jumping out of functions and not returning from them properly.
I would have thought it was the new programmer or a script kiddy that would be more likely to use a GOTO. Someone who has moved to C from a language which incorporates GOTO's as standard practice. Such as BASIC. |
|
|
|