calculate 1 micro second with timer/counter 1

Discussion in 'Homework Help' started by Nasi Mahi, Aug 10, 2016.

  1. Nasi Mahi

    Thread Starter New Member

    Aug 6, 2016
    6
    0
    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 (Text):
    1. #include <mega32.h>
    2. #include <delay.h>
    3. #asm
    4.     .equ __lcd_port=0x12
    5. #endasm
    6. #include <lcd.h>
    7. #include <stdio.h>
    8. // Declare your global variables here
    9. char count=0;
    10. char c=0,A[];
    11. // Timer1 overflow interrupt service routine
    12. interrupt [TIM1_OVF] void timer1_ovf_isr(void)
    13. {
    14. count++;
    15. if (count==2097120){
    16. TCNT1H=0x2000 >> 8;
    17. TCNT1L=0x2000 & 0xff;
    18. count=0;
    19. c++;
    20. }
    21. // Reinitialize Timer1 value
    22. TCNT1H=0x2000 >> 8;
    23. TCNT1L=0x2000 & 0xff;
    24. // Place your code here
    25.  
    26. }
    27.  
    28. void main(void)
    29. {
    30. // Declare your local variables here
    31. lcd_init(16);
    32.  
    33. // Timer/Counter 1 initialization
    34. // Clock source: System Clock
    35. // Clock value: 31/250 kHz
    36. // Mode: Normal top=0xFFFF
    37. // OC1A output: Disconnected
    38. // OC1B output: Disconnected
    39. // Noise Canceler: Off
    40. // Input Capture on Falling Edge
    41. // Timer Period: 1/835 s
    42. // Timer1 Overflow Interrupt: On
    43. // Input Capture Interrupt: Off
    44. // Compare A Match Interrupt: Off
    45. // Compare B Match Interrupt: Off
    46. TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
    47. TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
    48. TCNT1H=0x20;
    49. TCNT1L=0x00;
    50. ICR1H=0x00;
    51. ICR1L=0x00;
    52. OCR1AH=0x00;
    53. OCR1AL=0x00;
    54. OCR1BH=0x00;
    55. OCR1BL=0x00;
    56.  
    57.  
    58. // Timer(s)/Counter(s) Interrupt(s) initialization
    59. TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
    60.  
    61.  
    62. // Global enable interrupts
    63. #asm("sei")
    64.  
    65. while (1)
    66.       {
    67.       // Place your code here
    68.       sprintf(A,"Counter: %03u ",c);
    69.         lcd_gotoxy(0,0);
    70.         lcd_puts(A);
    71.         delay_ms(100);
    72.  
    73.       }
    74. }
    75.  
     
  2. Nasi Mahi

    Thread Starter New Member

    Aug 6, 2016
    6
    0
    Is there a better way for calculate micro second??
     
  3. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,893
    375
    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
     
  4. Nasi Mahi

    Thread Starter New Member

    Aug 6, 2016
    6
    0
    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
     
  5. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,676
    899
    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
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    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.
     
Loading...