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??
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);
}
}