CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Timer1 and sleep and WDT

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

Timer1 and sleep and WDT
PostPosted: Thu Sep 01, 2011 2:14 pm     Reply with quote

There is a significant problem.
These codes must be 8 seconds, a time to wake up the PIC.
and wakes up every sec and min values should increase. but sec and min values are not increased, when one reads the eeprom. I wonder why?
not: is used the external oscillator for timer1. Compiler version 4.119
respects.

Code:

#include <18F14K50.h>
#device ADC=10
#fuses HS,WDT2048,NOLVP,NODEBUG,CPUDIV4,NOMCLR,NOPUT,NOBROWNOUT,USBDIV1
#use delay(clock=12000000)
byte sec=0,min=0;

/////////////////////////////////////////////////////////
#int_TIMER1
void timer1_isr()
{
sec++;
set_timer1(32769);
if(sec>59) {sec=0; min++;}
}
   
//////////////////////////////////////////////////////////
void main(void)

   setup_timer_1(T1_EXTERNAL_SYNC | T1_DIV_BY_1 | T1_CLK_OUT);
   set_timer1(32769);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL); 
  setup_wdt(wdt_on);
while(true)
{
output_high(pin_c3);
delay_ms(200);
output_low(pin_c3);
delay_ms(200);
write_eeprom(0,sec);
write_eeprom(1,min);
      sleep();     

     }//while
}//main
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 2:36 pm     Reply with quote

Quote:

not: is used the external oscillator for timer1.

What does this mean ? Do you have a 32.768 KHz watch crystal and
capacitors connected to the Timer1 oscillator pins ? You need it.
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 2:42 pm     Reply with quote

yes. i have 32.768 KHz watch crystal and
capacitors connected to the Timer1 oscillator pins ..
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 2:54 pm     Reply with quote

Several comments:
1) _SYNC_ implies the T1 oscillator change is synchronised to the master clock. Won't work when the master oscillator stops. What does sleep do to the master oscillator?....
2) Look at the EEPROM write life. How long would it potentially take to kill the EEPROM writing every time round the loop?.
3) What is your actual crystal on the main oscillator?. You have the PLL disabled, and a division by four selected, and the clock rate set as 12MHz. Would imply a 48MHz external clock. Is this what you have?.

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 3:03 pm     Reply with quote

I wrote to see u EEprom values. printf would generate the same results are taken.12 MHz is not the problem. 48 MHz is the same. Thanks. but not solved.
When there is no sleep and the WDT is running smoothly. I used to wake up the WDT is always a problem.
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 5:31 pm     Reply with quote

I changed the settings for the oscillator. result is the same.
sleep (idle) works when you have made. but the sleep () does not work in

setup_timer_1(T1_EXTERNAL_SYNC | T1_DIV_BY_1 | T1_CLK_OUT);
setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 | T1_CLK_OUT); working
but Timer1 wake-up , not WDT


Last edited by respected on Thu Sep 01, 2011 5:44 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 5:38 pm     Reply with quote

I made it work with rs232 output. I didn't test it with eeprom.
I used vs. 4.119. I put a 32.768 KHz watch crystal on the T1OSCI and
T1OSCO pins, with 22 pf capacitors. I put a 12 MHz crystal on the CLKIN
and CLKOUT pins, also with 22 pf capacitors. I connected the USART Tx
pin to a MAX232 chip and then to my PC.

I edited the program slightly as shown below in the comments.

I get this output on the TeraTerm window:
Quote:

00:00
00:01
00:02
00:03
00:04
00:05
00:06
00:07
00:08
00:09



Code:

#include <18F14K50.h>
#device ADC=10
#fuses HS,WDT2048,NOLVP,NODEBUG,CPUDIV4,NOMCLR,NOPUT,NOBROWNOUT,USBDIV1
#use delay(clock=12000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, ERRORS)

byte sec=0,min=0;

/////////////////////////////////////////////////////////
#int_TIMER1
void timer1_isr()
{
sec++;
set_timer1(32769);
if(sec>59) {sec=0; min++;}
}
   
//////////////////////////////////////////////////////////
void main(void)



setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_CLK_OUT);  // *** REMOVED SYNC
set_timer1(32769);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL); 
setup_wdt(wdt_on);

while(true)
{
output_high(pin_c3);
delay_ms(200);
output_low(pin_c3);
delay_ms(200);

//write_eeprom(0,sec);   // *** COMMENTED OUT ***
//write_eeprom(1,min);   // *** COMMENTED OUT ***
printf("%02u:%02u \n\r", min, sec);  // *** ADDED ***
delay_ms(3);                         // *** ADDED ***

sleep();     

}//while

}//main
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 3:16 am     Reply with quote

Dear pcm programmer,
The simulation is running, but timer1 wake-up, not wdt. (on the circuit)
In this case does not sleep, because 1 minute wake-up.
I want to wake up in 8 minutes.

thanks. respects
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 4:04 am     Reply with quote

Just count.....

The WDT, is an inaccurate timer. Variation as much as 300%, over temperature/voltage/chip versions. To get anything accurate, you have to use a timer, not the WDT. The timer here wakes every two seconds (if you don't set the counter forward). The wake only involves a few dozen instructions, so power is tiny. Just let the chip wake every two seconds, and count the wake-ups till you get to your eight minutes. You can reduce the number of instructions involved in the wake up by _not_ having an interrupt handler. So:
Code:

#include <18F14K50.h>
#device ADC=10
#fuses HS,WDT2048,NOLVP,NODEBUG,CPUDIV4,NOMCLR,NOPUT,NOBROWNOUT,USBDIV1
#use delay(clock=12000000)
int16 ticks;

//////////////////////////////////////////////////////////
void main(void) {

   setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_CLK_OUT);  // *** REMOVED SYNC
   set_timer1(0);
   enable_interrupts(INT_TIMER1);
   //enable_interrupts(GLOBAL); //Remove so interrupt handler is not called

   do {
      ticks=0;
      output_high(pin_c3);
      delay_ms(200);
      output_low(pin_c3);
      do {
         sleep();
         delay_cycles(1); //Instruction pre-fetched - NOP
         clear_interrupt(INT_TIMER1);
      } while(++ticks<240);  //Just wake up for this loop every 2 seconds   
   } while(TRUE); //Main loop every eight minutes.
}

This will just raise the output line for 200mSec every 8 minutes, and will sleep 99.9% of the time, with corresponding power savings.

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 5:02 am     Reply with quote

a different approach. going to try it. thanks
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 7:52 am     Reply with quote

yes. no problem.
i don't use WDT. i use timer1 . it's worked
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 12:55 pm     Reply with quote

For reference, here is sample code to sleep for a specified amount of time:
http://www.ccsinfo.com/forum/viewtopic.php?t=45458&start=5
It uses Timer1 with an external watch crystal and capacitors.
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 2:30 pm     Reply with quote

Thank you for your interest PCM.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group