Getting stuck at while

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "tc.h"
#include "led.h"

 

static void ticktimer_callback(void);

 

int main(void)
{
    tc_enable(&TCC0);
    tc_set_overflow_interrupt_callback(&TCC0, ticktimer_callback);
    tc_set_wgm(&TCC0, TC_WG_NORMAL);
    tc_write_period(&TCC0, 1000);
    tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);
    tc_write_clock_source(&TCC0, TC_CLKSEL_DIV1_gc);
 

    while (1){}
    
}

static void ticktimer_callback()
{
    LED_Toggle(LED1_GPIO);
    
}

 

The program is not coming out of while loop

This topic has a solution.

Nitin Shenoy

Last Edited: Tue. Feb 5, 2019 - 10:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It will come out of the while loop when the 1 changes to 0.  Seriously, what did you expect to happen?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried all that just here in this code I didn't mention it. The code automatically goes into the disassembly and gets stuck there.

Nitin Shenoy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you actually saying "the interrupt never triggers"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Missing

cpu_irq_enable(); 

when comparing with http://asf.atmel.com/docs/latest...

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yeah..basically thats what is happening

#include <avr/io.h>
#include "tc.h"
static void my_callback(void);

int main(void)
{
   pmic_init();
   sysclk_init();
   tc_enable(&TCC0);

   tc_set_overflow_interrupt_callback(&TCC0, my_callback);
   tc_set_wgm(&TCC0, TC_WG_NORMAL);
   tc_write_period(&TCC0, 624);
   tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);
   cpu_irq_enable();
   tc_write_clock_source(&TCC0, TC_CLKSEL_DIV256_gc);
   
   PORTR_DIRSET= 0x02;
                
    while (1) 
    {
    }
}

static void my_callback()
{
    
    if(TCC0.CNT == 624)
    {
        PORTR_OUTSET=0x02;
    }
    
    // User code to execute when the overflow occurs here
    //Important to clear Interrupt Flag
    tc_clear_overflow(&TCC0);
}

 

this is my new program

Nitin Shenoy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#include <avr/io.h>
#include "tc.h"
static void my_callback(void);

int main(void)
{
   pmic_init();
   sysclk_init();
   tc_enable(&TCC0);

   tc_set_overflow_interrupt_callback(&TCC0, my_callback);
   tc_set_wgm(&TCC0, TC_WG_NORMAL);
   tc_write_period(&TCC0, 624);
   tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);
   cpu_irq_enable();
   tc_write_clock_source(&TCC0, TC_CLKSEL_DIV256_gc);
   
   PORTR_DIRSET= 0x02;
                
    while (1) 
    {
    }
}

static void my_callback()
{
    
    if(TCC0.CNT == 624)
    {
        PORTR_OUTSET=0x02;
    }
    
    // User code to execute when the overflow occurs here
    //Important to clear Interrupt Flag
    tc_clear_overflow(&TCC0);
}

 

 

Still the same..

Nitin Shenoy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah,it never triggers.

Nitin Shenoy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What happens to the timer registers in the simulator if you step the code? Do you see the timer running? Do you see the timer reach the overflow? Is the overflow flag set then?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Q1

In your original #1 callback, you wrote:

    LED_Toggle(LED1_GPIO); 

Shouldn't the replacement be ?

    PORTR_OUTTGL=0x02;

 

Q2

In your chip, is TCC0 8-bit timer or 16-bit timer ?

if it's 8-bit then

    if(TCC0.CNT == 624)

will never become true.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It will not become true ever, the period is 624 and that value is not going to be readable from CNT in the overflow handler. As mentioned here btw:

https://www.avrfreaks.net/forum/...

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"TCC0.CNT is not going to be 624 in the overflow callback, it's reset to 0 before that (and with fast enough clocking it could already be >  0).

EDIT: btw, why check anyway? You already know there was an overflow, this is the overflow callback after all.

/Lars"

 

What do you suggest that i do here?, 16bit timer, I've used with a prescaler of 256 and FCPU is 16MHz and the delay is 10ms

Nitin Shenoy

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use this 

tc_set_resolution(&TCC0, any prescale value);

instead of 

tc_write_clock_source(&TCC0, TC_CLKSEL_DIV1_gc);

 

and 

 

while(1);

 

to exit from it

Nitin Shenoy