View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
looping forever |
Posted: Sun Dec 18, 2011 10:07 am |
|
|
Dear Friends,
I am making a program which has a looping (while (1)) in the main function. Is it good to have this type of programming or it is not "healthy" for the PIC?
In case that one uses sleep() command, what is the command to return back in case of interrupt?
I am using PIC16F687 with a 4MHz crystal |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
|
Posted: Sun Dec 18, 2011 11:02 am |
|
|
The compiler is going to give you a warning, but it's just because your loop will never break out, which is what you want. As for the health of the pic it doesn't mind at all. |
|
|
Battery David
Joined: 01 Feb 2010 Posts: 25
|
|
Posted: Sun Dec 18, 2011 11:07 am |
|
|
When you get an interrupt, the code returns to the statement after the sleep(). This is an easy way to save lots of power. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Dec 18, 2011 3:17 pm |
|
|
If you have an interrupt enabled, on a hardware peripheral that does not use the clock, and do not have the global interrupts enabled, then when the interrupt triggers, the next instruction is executed, and you don't need an interrupt handler.
If you have both the interrupt enabled, and the global interrupts enabled, then the chip will wake up, and call the interrupt handler _which must be present_, then return to the code after the sleep.
You can only wake on peripherals that keep working when you are asleep.
On some latter chips, you can leave the clock oscillator running and feeding the peripherals, while the CPU itself sleeps. On these you can wake up from peripherals that do use the clock.
Best Wishes |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Sun Dec 18, 2011 4:30 pm |
|
|
Thanks for your reply. On the datasheet I've read that during sleep mode, the MCU will have no clock cycles, so the timer 1 will not be running. Please correct me if I am wrong. On the other hand, if I use the internal oscillator instead of an external crystal, I will solve the problem? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Dec 19, 2011 2:30 am |
|
|
The 687 does not support having the CPU clock running during sleep mode.
The internal oscillator will stop.
What it does allow is you to run Timer1, off _it's own external crystal_. Normally 32768Hz, which draws very little power.
You need to have a crystal between the OSC1, and OSC2 pins of Timer1 (with suitable loading capacitors), and setup timer1 with:
setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_1);
Then timer1, will stay running when the processor sleeps.
Best Wishes |
|
|
|