# high tech c and geting time in milli or micro seconds?

**URL:** <https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771>\
**Category:** MicroMod\
**Tags:** avr-atmel\
**Created:** [October 16, 2008, 9:08pm UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771 "2008-10-16T21:08:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![duhjalpmig](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@duhjalpmig](https://community.sparkfun.com/u/duhjalpmig)\
**Post date:** [October 16, 2008, 9:08pm UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/1 "2008-10-16T21:08:40Z")

</div>

i would like to get the time since my pic started in milli or micro seconds is that possible?

how would i do it?

am using a pic16f88 with the internal 8MHz oscillator and i compile my code with “Hi-Tech C”

---

<div class="post-metadata">

**Author:** ![jasonharper](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/jasonharper/32/12350_2.png) [@jasonharper](https://community.sparkfun.com/u/jasonharper)\
**Post date:** [October 16, 2008, 9:17pm UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/2 "2008-10-16T21:17:49Z")

</div>

You could start a timer and use it for that purpose.

With an 8 MHz clock, Timer 1 with a prescale of 2 would count in microseconds (plus or minus a few percent, due to the inaccuracy of the internal osc). It would roll over every 65 milliseconds.

---

<div class="post-metadata">

**Author:** ![duhjalpmig](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@duhjalpmig](https://community.sparkfun.com/u/duhjalpmig)\
**Post date:** [October 17, 2008, 2:46pm UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/3 "2008-10-17T14:46:48Z")

</div>

> jasonharper:  
> It would roll over every 65 milliseconds.

What do you mean with that?

---

<div class="post-metadata">

**Author:** ![orbital\_fox](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@orbital\_fox](https://community.sparkfun.com/u/orbital_fox)\
**Post date:** [October 17, 2008, 2:47pm UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/4 "2008-10-17T14:47:50Z")

</div>

I think he means count at steps of 64 ms

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [October 17, 2008, 3:17pm UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/5 "2008-10-17T15:17:46Z")

</div>

You need to maintain a counter in memory with your software that is incremented every time the hardware counter overflows. By varying the prescaler, counter load value and software counter size you can have any resolution you need, within reason.

Leon

---

<div class="post-metadata">

**Author:** ![duhjalpmig](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@duhjalpmig](https://community.sparkfun.com/u/duhjalpmig)\
**Post date:** [October 21, 2008, 4:08am UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/6 "2008-10-21T04:08:49Z")

</div>

i hate to sound annoying or so but i don’t understand how to set one of these timers in C.

is there any example code anywhere, witch tells my what each function does.

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [October 21, 2008, 4:45am UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/7 "2008-10-21T04:45:30Z")

</div>

Here is how it is done in C18 using the timer library:

```auto
/*
** Timer0.c
** Timer test program for PIC18F4520
**
**
*/

//8 MHz clock and 1/16 prescaler
//

#include <p18f4520.h>
#include <timers.h>

//function prototypes
void init(void);
void dly(unsigned int);

// Defines
#define LED LATAbits.LATA0
#define PB PORTAbits.RA1

// Macros
#define nop() _asm nop _endasm

void main(void)
{
	init();
	while (1)  
	{
		LED = 1;
		dly(0xFFF0); //516 ms delay
		LED = 0;           
		dly(60000); //3.6 s delay 
		nop();          
	}
}

void init(void)
{
	// run internal oscillator at 8 MHz
	OSCCON = OSCCON | 0x70;
	while (OSCCONbits.IOFS == 0) ;
	PORTA = 0x00;
	ADCON1 = 0x0F; // set up PORTA to be digital I/Os
	TRISA = 0x02; // PORTA<7:2,0> outputs PORTA<1> input
	OpenTimer0( TIMER_INT_OFF &
            	T0_16BIT &
            	T0_SOURCE_INT &
            	T0_PS_1_16 );
}

void dly(unsigned int c)
{
	INTCONbits.TMR0IF = 0;
	WriteTimer0(c);
	while (INTCONbits.TMR0IF == 0)
		;
}

```

Leon

---

<div class="post-metadata">

**Author:** ![stevech](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@stevech](https://community.sparkfun.com/u/stevech)\
**Post date:** [October 22, 2008, 3:03am UTC](https://community.sparkfun.com/t/high-tech-c-and-geting-time-in-milli-or-micro-seconds/8771/8 "2008-10-22T03:03:06Z")

</div>

> duhjalpmig:  
> i would like to get the time since my pic started in milli or micro seconds is that possible?
> 
> how would i do it?
> 
> am using a pic16f88 with the internal 8MHz oscillator and i compile my code with “Hi-Tech C”

PIC - wrong forum
