View previous topic :: View next topic |
Author |
Message |
johnl
Joined: 30 Sep 2003 Posts: 120
|
16F1823 WDT wake up from sleep |
Posted: Mon Nov 19, 2012 10:55 am |
|
|
If the A/D reads a voltage above a threshold, the processor is to sleep and wake up every 4 seconds (WDT timeout) to read the voltage again. If below the threshold, it should stay awake and flash an LED.
The code works except for the waking up part. It never restarts. Any clues would be appreciated. Code: |
#include <16F1823.h>
#device ADC=10
#include <string.h>
#include <STDLIB.H>
#fuses NOMCLR,WDT,PUT,INTRC_IO,NOBROWNOUT,NOLVP,NOIESO, NODEBUG, NOFCMEN
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_A0)
#use standard_io(A)
#define LED_ON output_low(PIN_A5)
#define LED_OFF output_high(PIN_A5)
#byte OPTION_REG = 0x95
#byte WPUC = 0x20E
#byte WPUA = 0x20C
#byte APFCON = 0x11D
void main()
{
long V;
setup_wdt( WDT_4S);
APFCON = 4; // Serial out on pin A0
setup_oscillator(OSC_8MHZ);
OPTION_REG = 0b00000000;
WPUC = 0b00010000;;
WPUA = 0b00001000; //pullup on pin A3
setup_comparator(NC_NC_NC_NC);
set_tris_c(0b1111);
set_tris_a(0b011110);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(6);
setup_adc_ports( sAN6 |VSS_VDD );
LED_ON;
delay_ms(20); //short flash on restart
LED_OFF;
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
disable_interrupts( INT_TIMER1 );
disable_interrupts(GLOBAL);
while(1)
{
V = (long)read_adc();
printf("\r\n ADC = %ld", V);
if (V < 800)
{
LED_ON; delay_ms(400); LED_OFF; delay_ms(1600);
restart_wdt();
}
if (V > 799)
{
printf("\r\nGoing to sleep");
// minimize I/O currents before sleep
output_high(PIN_C4);
output_low(PIN_A0);
output_high(PIN_A2);
sleep();
reset_cpu();
}
delay_ms(1000);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 19, 2012 3:25 pm |
|
|
The first thing is that you didn't give your compiler version, so there's no
way for us to check if your version has a bug.
The 2nd thing is that to test WDT wake-up from sleep, it's not necessary
to have such a large program. I cut your code down to the short
program shown below. It worked. The blink period was about 4.8 seconds
which is about 4 seconds for the WDT timeout, and .5 seconds for the
delay. The WDT is not exact.
I ran this program on a Microchip "Low Pin Count" board running at +5v
with an 16F1823 installed. I kept your fuses the same.
Code: |
#include <16F1823.h>
#fuses NOMCLR,WDT,PUT,INTRC_IO,NOBROWNOUT,NOLVP,NOIESO, NODEBUG, NOFCMEN
#use delay(clock=8M)
//========================================
void main()
{
setup_wdt(WDT_4S);
output_high(PIN_C1); // Led on
delay_ms(500);
output_low(PIN_C1); // Led off
sleep();
reset_cpu();
while(1);
}
|
|
|
|
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Mon Nov 19, 2012 4:05 pm |
|
|
I ran your program as is except I changed the LED pin to A5.
It doesn't work. The LED comes on, then goes off with no further activity. The compiler is version 4.121. Thanks.
Code: |
#include <16F1823.h>
#fuses NOMCLR,WDT,PUT,INTRC_IO,NOBROWNOUT,NOLVP,NOIESO, NODEBUG, NOFCMEN
#use delay(clock=8M)
//========================================
void main()
{
setup_wdt(WDT_4S);
output_low(PIN_A5); // Led on
delay_ms(500);
output_high(PIN_A5); // Led off
sleep();
reset_cpu();
while(1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 19, 2012 4:29 pm |
|
|
You also changed the LED polarity. On my board, a logic high turns the
LED on.
I tested the program with vs. 4.121 (with LED lines set for my board)
and it works. The LED blinks once every 4.8 seconds.
Questions:
1. What is the value of the LED series resistor that you are using ?
2. What voltage is connected to the non-PIC side of the LED ?
3. What's your PIC's Vdd voltage ?
4. What programmer and IDE are you using ? |
|
|
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Mon Nov 19, 2012 4:38 pm |
|
|
I switched the LED lines around because the LED is connected to turn on when the output is low (LED anode tied to VCC.)
1. 220 ohms
2,3. 5.17 volts (from a USB port) bypassed with a 1uF and 0.1 Uf cap.
4. ICD2 with low VPP voltage adapter and MPLAB IDE 8.83 . |
|
|
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Mon Nov 19, 2012 4:52 pm |
|
|
Just noticed that in MPLAB, the configuration comes up as WDT disabled.
I was in the debug build mode.
Now it works. Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 19, 2012 5:10 pm |
|
|
An important thing that I do, is to look at the list of fuses created by
compiling the program. This is given at the end of .LST file. If you
looked at that early on, you would have immediately seen the DEBUG
fuse was active. Even though you have NODEBUG in your #fuses,
the Release/Debug drop-down box in MPLAB will over-ride this setting
and put DEBUG in your actual compiled fuses (as shown in the .LST file). |
|
|
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Mon Nov 19, 2012 5:15 pm |
|
|
PCM programmer wrote: | An important thing that I do, is to look at the list of fuses created by
compiling the program. This is given at the end of .LST file. If you
looked at that early on, you would have immediately seen the DEBUG
fuse was active. Even though you have NODEBUG in your #fuses,
the Release/Debug drop-down box in MPLAB will over-ride this setting
and put DEBUG in your actual compiled fuses (as shown in the .LST file). |
Yes, I've wasted time before on this issue... should've known better.
Thanks again. |
|
|
|