calculate 1 micro second with timer/counter 1

Thread Starter

Nasi Mahi

Joined Aug 6, 2016
6
I want to calculate 1 micro second with timer 1 and show in lcd but my program doesn't work,I use IC clock source with 8MHz and clock value /256 so
8000000/256=31.25KHz
1/31.25=32us
32*65535 =2097120 //time for one overflow
2097120/256=8192 //Initial value (timer value=0x2000)
what am I missing??
Code:
#include <mega32.h>
#include <delay.h>
#asm
    .equ __lcd_port=0x12
#endasm
#include <lcd.h>
#include <stdio.h>
// Declare your global variables here
char count=0;
char c=0,A[];
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
count++;
if (count==2097120){
TCNT1H=0x2000 >> 8;
TCNT1L=0x2000 & 0xff;
count=0;
c++;
}
// Reinitialize Timer1 value
TCNT1H=0x2000 >> 8;
TCNT1L=0x2000 & 0xff;
// Place your code here

}

void main(void)
{
// Declare your local variables here
lcd_init(16);

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31/250 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 1/835 s
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
TCNT1H=0x20;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;


// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);


// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here 
      sprintf(A,"Counter: %03u ",c);
        lcd_gotoxy(0,0);
        lcd_puts(A);
        delay_ms(100);

      }
}
 

AlbertHall

Joined Jun 4, 2014
12,625
First, I presume you mean a count of seconds, not microseconds.
You don't say which processor and compiler you are using and I don't recognise the timer setting constants so I can't check them.
If this is a Microchip PIC processor you are using then, commonly, the timer clock is the Xtal frequency divided by four.
You have declared count as a char so the line if (count==2097120) will never be true.

For timing one second see: http://www.romanblack.com/one_sec.htm
 

Thread Starter

Nasi Mahi

Joined Aug 6, 2016
6
First, I presume you mean a count of seconds, not microseconds.
You don't say which processor and compiler you are using and I don't recognise the timer setting constants so I can't check them.
If this is a Microchip PIC processor you are using then, commonly, the timer clock is the Xtal frequency divided by four.
You have declared count as a char so the line if (count==2097120) will never be true.

For timing one second see: http://www.romanblack.com/one_sec.htm
no micro second I maked mistake my clock system 8MHz and I must choose clock value /8 so I have 1 micro second my timer value must be 0xFF ,,it is very bad mistake
 

jpanhalt

Joined Jan 18, 2008
11,087
An 8-Mhz clock with a pre-scale or post-scale of 1:2 will give 1-us "ticks". Your counter will then show the number of microseconds.

Is that what you are trying to do? We don't know your chip, but assuming it is a typical PIC, TMR1 is a 16-bit timer. Thus, you will only count to 65535 before it rolls over. You can then count the number of times it rolls over to get longer timing intervals.

John
 

MrChips

Joined Oct 2, 2009
34,807
Line #1 tells me you are using an Atmel ATmega32.

You are making a number of mistakes.
A 16-bit counter will count 65536 clock pulses before it overflows, not 65535.

You have declared count and c as data type char which is 8-bit value.

Lines 16 and 17 waste time unless you are using an optimizing compiler.
In any case, do not reset the timer. Allow your timer to free-run at 1MHz.

If you wish to time an external event, use input capture.

If you wish fixed time intervals, use output compare.

It is not clear what you mean when you say "I want to calculate 1 micro second".

You cannot "calculate" 1 micro second.
 
Top