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

internal timer wake-up problem

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

internal timer wake-up problem
PostPosted: Wed Nov 07, 2012 5:26 am     Reply with quote

Below program internal timer wake-up after 18 seconds.

Why it is so ?

After 18 seconds it start normally. why ?


Code:
#include <18F2520.h>
#include <string.h>
#fuses INTRC_IO,NOWDT,NOLVP,PUT
#use delay(clock=31000)

#define EN     PIN_A1
#define RS     PIN_A2


#define INTS_PER_SECOND 15.1371 // (31000/(4*2*256))
INT16 seconds;     
BYTE INT_count;   
#INT_rtcc         
void clock_isr()
{
   if (--INT_count == 0)
   {
      ++seconds;     
      INT_count = INTS_PER_SECOND;
   }
}

void lcd_cmd(CHAR a);  // function declaration
void lcd_print(CHAR rstr[]);

CHAR msg[16];
 
void main ()
{
   set_timer0 (0) ;
   setup_counters (RTCC_INTERNAL, RTCC_DIV_2|RTCC_8_BIT);
   enable_interrupts (INT_RTCC) ;
   enable_interrupts (GLOBAL) ;
   
   output_low (RS);//set LCD to command mode

   lcd_cmd (0x30);
   lcd_cmd (0x01);
   lcd_cmd (0x02);
   lcd_cmd (0x0c);
   lcd_cmd (0x06);   
   
   seconds =0;   
   WHILE (1)
   {     
       output_low (RS);//set LCD to command mode   
       lcd_cmd (0x02);//move cursor to home position   
       sprintf (msg, "SECONDS %LU  ",  seconds ) ;     
       if (seconds==1)
           {
            output_high(pin_a3);
           }
       lcd_print (msg);   
   }

}

void lcd_cmd(CHAR a)
{
   output_b (a);
   delay_ms (10);
   output_high (EN);
   delay_ms (10);
   output_low (EN);
}


void lcd_print(CHAR rstr[])
{
   INT i;
   
   FOR (i=0; rstr[i]!='\0';  i++)
   {
      output_high (rs);
      lcd_cmd (rstr[i]);
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19466

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 5:38 am     Reply with quote

Well. The program never goes to sleep, so it doesn't "wake up after 18 seconds". Presumably you mean it starts updating the time?.

Remember, definitions like:

byte int_count;

does not initialise the variable. What it contains is random garbage that just happens to be in the RAM. So the actual count given, could be anything from 0 to 255....

byte int_count=INTS_PER_SECOND;

would initialise it.

Same applies to seconds, etc...

Best Wishes
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 6:15 am     Reply with quote

When I power, it shows seconds in the display. After 18 seconds only, it displays the counts. eg: 0,1,2,3,4.......

Why it is waiting for 18 seconds to start the count ? Any problem with the code?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Nov 07, 2012 6:36 am     Reply with quote

whats WRONG ??
hard to know where to start really.
beyond what Ttelmah noted:
Quote:

INT_count = INTS_PER_SECOND;


one of the above is an 8 bit integer - the other is def'd as a float.

and the assignment works , HOW??

and the supposed function of the int handler is what ??
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 6:53 am     Reply with quote

#define INTS_PER_SECOND 15

Now I have the changed it as integer.

Please check the example : EX_STWT.C
Ttelmah



Joined: 11 Mar 2010
Posts: 19466

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 9:09 am     Reply with quote

asmboy wrote:
whats WRONG ??
hard to know where to start really.
beyond what Ttelmah noted:
Quote:

INT_count = INTS_PER_SECOND;


one of the above is an 8 bit integer - the other is def'd as a float.

and the assignment works , HOW??

and the supposed function of the int handler is what ??


Won't matter C 'casts' values automatically. This works both ways.
Remember the #define doesn't actually generate any variable or anything, it is just a text substitution. However it is potentially confusing.

Other thing to think of is how slow the chip really is running. Just 7750 instructions per second. The boot sequence will easily be 100+ instructions, Then the LCD initialisation will take probably about 300mSec. The code in the printf, to actually generate the resulting value, could easily take a second of more to actually execute, so suddenly you are up to a couple of extra seconds before the first value displays. The delays from printf etc., will be there in every subsequent call, but will be constant, so the actual interval between the displays will be close to 'right', but the display could easily be a couple of seconds _after_ the number actually updated.

Best Wishes
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Thu Nov 08, 2012 12:53 am     Reply with quote

Ttelmah answered this in his first reply. INT_count isn't initialised anywhere. Odds are, it starts at zero.

Next, INT_count is predecremented in clock_isr. Subtracting one from zero produces 255 for unsigned 8-bit integers. How long will that take to decrement back down to zero? 255 / 15 = 17 seconds!

Moral of the story: always initialise your variables before using them.
_________________
Andrew
Ttelmah



Joined: 11 Mar 2010
Posts: 19466

View user's profile Send private message

PostPosted: Thu Nov 08, 2012 2:42 am     Reply with quote

Yes, and if you add perhaps a second till the value actually displays (because of how slow the processor is), you are at 18+ seconds.

Best Wishes
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Thu Nov 08, 2012 3:31 am     Reply with quote

Thank you so much Smile .. it's working now. Smile
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