I am learning embedded C with Atmega128 and Atmel Studio.
I want to write code that shows the temperature and humidity from a sensor.
I am learning Input Capture and my first question is:
I get IC with value 5000(period) but I think 2500 is correct. Am I calculating it right? If so, do you have any idea why IC has the wrong value?
16000.000/64 = 250000
1 second 250000 puls
100 ms x = 2500
Board info:
avr128
16.000.000 external clock.
philips puls generator info:
Repitation 100 mili s
Duration 10 ms
5 volt
My code
volatile uint32_t iccnt=0,ovfcnt=0;
volatile uint32_t myCapt [2],lastCapt;
//__________________________
void initTimer()
{
TCNT1 =0;//Max=65535
TCCR1B |= 1<<CS10 | 1<<CS11 ;
// 1<<CS10 | 1<<CS11 ;//16000000/16=250000
//250000/65535=3.81
//TCCR1B |= 1<<WGM13 |1<<WGM12;
TIMSK |= 1<<TOIE1;
TIFR = 1<<TOV1;
sei();
}
void initInputCapture()
{
// First Capture on rising edge
TCCR1B |=1<<ICES1;
//ic intrrupt flag enable
TIMSK |= 1<< TICIE1;
// set input pin to get signal
}
void initlcd()
{
lcd_init(LCD_DISP_ON_CURSOR_BLINK);
lcd_clrscr();
}
void ShowA(unsigned int a)
{
char aa[11];
if (a >32000) {
itoa(a - 32000,aa,10);
lcd_puts("32000+");
lcd_puts(aa);
} else {
itoa(a,aa,10);
lcd_puts(aa);
}
}
ISR(TIMER1_OVF_vect)
{
ovfcnt++;
}
ISR(TIMER1_CAPT_vect)
{
iccnt++;
myCapt[iccnt % 2] = ICR1 - lastCapt;
lastCapt = ICR1;
}
void ShowCaptureTime2()
{
lcd_clrscr();
ShowA (iccnt);
lcd_puts(": \n");
ShowA(myCapt[(iccnt - 1) % 2]);
for (int j=0;j<(1000) ;j++)
{
for (int i=0;i<4000;i++)
{
}
}
}
//*******************************
//*******************************
int main(void)
{
DDRG = 0b11111111;
int cnt = 0;
initlcd();
initTimer();
initInputCapture();
while(1)
{
ShowCaptureTime2();
}
}