0

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();     

    }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
samsoft
  • 21
  • 7
  • "16MHz external clock" doesn't say much, since apparently the timer clock is inherited from the system bus clock. I don't know this part, but according to RTFM for register XDIV: "The XTAL Divide Control Register is used to divide the Source clock frequency by a number in the range 2 - 129." If I understand this correctly, the fastest bus clock you can get on this MCU is the external clock divided by 2. – Lundin Dec 03 '15 at 15:53
  • 1
    Where do you set up system clock and other such fundamental things? Is this done in some compiler-delivered code which it spews out to execute before main? Try to get rid of such files: it is always best to actually understand your hardware, instead of blindly relying on some icky start-up code made by the compiler vendor. – Lundin Dec 03 '15 at 15:54
  • @Lundin - if bit 7 is zero the crystal is not divided. – Chris Stratton Dec 04 '15 at 05:16
  • You very very likely do not want floating point arithmetics on ATmega. Even less `double`. It bloats your code and is **slow**. Use fixed-point/integer arithmetic. – too honest for this site Dec 06 '15 at 15:40
  • thanks all, I could find the answer yesterday. code has no problem. Pulse generator work not correktly! incorrect input!!!! – samsoft Dec 09 '15 at 12:57
  • Sounds like we should close it as "could not be reproduced" since the problem was with external circuitry. – Chris Stratton Dec 10 '15 at 04:19

1 Answers1

0

I could find the answer yesterday. code has no problem. Pulse generator work not correktly! incorrect input!!!! – samsoft

Armali
  • 18,255
  • 14
  • 57
  • 171