1 second loop timer with 10MHz crystal

Status
Not open for further replies.

krtodoroff

Newbie level 5
Joined
Jan 10, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,418
I use this code with is not mine, but for my rogram i need 10MHz crystal.
Pleace help me how to change it. I'm using PIC 16F877A and 10Mhz crystal. My compiler is Mikroc.

code:

#define XTALHz 4000000 // 4 Mhz. (can trim this to fine-tune clock speed)



//=============================================================================
// POLL TIMER
//=============================================================================
void poll_timer(void)
{
//-----------------------------------------------------
// this function must be called often, don't allow more
// than 131 mS between calls.
// 8Mhz; TMR1 1:4 prescale overflows every 7.6Hz (131mS)
// (if 20Mhz TMR1 1:4 prescale overflows every 19Hz (53mS))
//
// TMR1 is at 1:4 prescaler, so 1 second = xtal Mhz / 16.
//-----------------------------------------------------
// if TMR1 overflowed, check if 1 second has passed
if(PIR1.TMR1IF)
{
// clear overflow flag
PIR1.TMR1IF = 0;
// add another overflow period to total
period += 65536;
// test if reached 1 second yet...
if(period >=(XTALHz / 16) )
{
period -= ((XTALHz / 16) );
new_second = 1;
}
}
}





poll_timer();

if (new_second)
{
new_second = 0;
....
}
...
} // Endless loop while
 
Last edited:

this tutorial is perfect, but when i set 2.5 MIPS clock whit TMR 1:1 , one second is 839 ms
I dont know wat to do.

here this code:

void poll_timer(void)
{
if(TMR0.TMR0IF)
{

PIR1.TMR0IF = 0;
period += 256;

if(period >= 2500000)
{
period -= 2500000;
new_second = 1;
}
}

}
in main
OPTION_REG = 0b00000000;
TMR0 = 1 ;
....

I dont know whats wrong in this tutorial, or in me.
Pleace HELP
 

All your variables should be UNSIGNED type.

I've implemented Roman's technique on 16F887 @ 20 MHz without any problem (of course, all the incrementing is done in the interrupt routine), in Proton+. 20 MHz was just for proof of concept as I run the PIC on it's internal oscillator @ 4 MHz.

So in your main program you should test only your new_second variable and do what you need to do at the 1 sec period.

At the moment I rum on a small breadboard a kind of thermostat comprising 2x16 LCD, DS18B20 and 16F628A on it's internal oscillator, displaying time (clock) and actual room temperature. Clock is drifting, of course, as the internal oscillator is voltage and temperature dependent, but, just as proof of concept, it runs satisfactory.
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…