Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Internal clock problem?

Status
Not open for further replies.

hemnath

Advanced Member level 3
Joined
Jun 24, 2012
Messages
702
Helped
61
Reputation
120
Reaction score
57
Trophy points
1,308
Location
Chennai
Activity points
6,588
I'm using internal clock in PIC18F2520 using CCS c compiler. I'm displaying a string "SECONDS" and displays counting numbers from 0,1,2,3...
When i power, it shows "SECONDS 0" in the display and after 18 seconds only, it starts incrementing its value. Why is it so? Please help.. Once it's starts incrementing, it counts for every second,thats normal.

HTML:
#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                   //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]);
   }
}
 

As a guess, I would think the problem is that you don't initialize the "seconds" variable or timer ready for it's first count. The interrupt occurs when the timer has reached terminal value and rolled over to zero so if it starts frm zero it has to count it's full range of values before the first interrupt occurs.

Warning - it looks like you are using the internal 31KHz clock - bear in mind it isn't very accurate or stable if you are using it for critical timing applications.

Brian.
 

Thanks for your replay. But i had already solved the problem and forgot to mark it as solved.

Thanks...
 

Error in code:
In your application,
1 second is on INT_count value upto 15.
as INT_count has initially zero value it will first decrease by 1 and its value become 255.
initial time taken will be 256/15=17.06S.


Solution:
initialize the global variable.
BYTE INT_count = INTS_PER_SECOND;
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top