All About Circuits Forum TIMER to count for 1 second
 Register Blogs FAQ Members List Today's Posts Search Today's Posts Mark Forums Read

 The Projects Forum Working on an electronics project and would like some suggestions, help or critiques? If you would like to comment or assist others with their projects, this is the place to do it.

#1
08-29-2011, 09:49 PM
 honeymustard909 New Member Join Date: Jul 2011 Posts: 3
TIMER to count for 1 second

Hi there i was wondering if any of you could help me out here . Basically, i need a Timer to count down a time limit which is used to turn off my device after the specified time limit. My pic18f46k20 is running at 1MHz and it is a 16 bit counter . (65536x(1/250KHz) = approx 1 second. I was wondering if this is a correct approach . The period_counter is to increase the second after the timer finish counting from 0-65536 . However to get 1 second , i have to set the period counter to 4000 instead of just 1 . I thought that the timer takes approx 1 second to finish counting.

INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00000001; // pre scale 1:4
TMR0H = 0x00; // clear timer
TMR0L = 0x00;
T0CONbits.TMR0ON = 1;

void timer (void)
{
if ( INTCONbits.TMR0IF == 1 )
{
TMR0H= 0x00;
TMR0L= 0x00;
period_counter++;
}
}
if ( period_counter > 4000 ) // 1 second
{

LATDbits.LATD7 = 1;
period_counter = 0;
counter_second ++;
}
#2
08-30-2011, 12:11 AM
 honeymustard909 New Member Join Date: Jul 2011 Posts: 3
Timer for 1 second

Why does it takes 4500 to generate a second ?

void main (void)
{
//Init the output ports
TRISD = 0b01111111; // RD7 as output

//Init oscillator frequency
// OSCCON = 0b00110110; // to set the frequency to 16MHz
// OSCTUNE = 0b01011111;

// Init Timer
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
// INTCONbits.TMR0IE = 1; //enable interrupt flag
T0CON = 0b00001000; //counts 16 bits no prescaler use internal oscillator
TMR0H = 0x80; // clear timer
TMR0L = 0x00;
T0CONbits.TMR0ON = 1; // start timer
while (1)
{
timer();
counting();
seconds();
// minutes();
}
}
void timer(void)
{
if (INTCONbits.TMR0IF == 1)
{
TMR0L = 0xFF;
TMR0H = 0xFF;
period_counter++;
}
}
void counting (void)
{
if ( period_counter > 4500 ) // 1 second (4000)
{
LATDbits.LATD7 = 1;
period_counter = 0;
counter_second ++;
}
}
void seconds (void)
{
if (counter_second > 1)
{
LATDbits.LATD7 =0;
counter_second =0;
counter_minute =0;
counter_hour =0;
}
}

 Tags count, timer

 Similar Threads Thread Thread Starter Forum Replies Last Post ChrisN The Projects Forum 4 04-22-2011 10:49 PM daredavel Embedded Systems and Microcontrollers 6 02-22-2010 06:18 PM shoe624 General Electronics Chat 2 12-23-2009 01:08 AM Airasolt The Projects Forum 2 09-10-2009 12:15 PM doodsettledown Homework Help 1 10-08-2008 12:21 AM

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Electronics Forums     General Electronics Chat     The Projects Forum     Homework Help     Electronics Resources Software, Microcomputing, and Communications Forums     Programmer's Corner     Embedded Systems and Microcontrollers     Computing and Networks     Radio and Communications Circuits and Projects     The Completed Projects Collection Abstract Forums     Math     Physics     General Science All About Circuits Commmunity Forums     Off-Topic     The Flea Market     Feedback and Suggestions

All times are GMT. The time now is 04:00 PM.