Overflow Callback not called

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <avr/io.h>
#include <util/delay.h>
#include <asf.h>
#include "tc.h"
static void my_callback(void);


int main(void)
{
   pmic_init();
   sysclk_init();
   board_init();
   PORTR_DIRSET= 0x03;
   PORTE_DIRCLR = 0x20;
   
   while (1)
   {
	   
	if ((PORTE.IN & PIN5_bm))
	{   
	tc_enable(&TCC0);

   cpu_irq_enable();
   
   tc_set_overflow_interrupt_callback(&TCC0, my_callback);
   
   tc_set_wgm(&TCC0,TC_WG_NORMAL);
   tc_write_period(&TCC0, 31250);
   
   tc_set_overflow_interrupt_level(&TCC0,TC_INT_LVL_LO);
   tc_set_resolution(&TCC0, 31250);
	}

   
   }
   
   

	
 }

static void my_callback(void)
{
	if (!(PORTE.IN & PIN5_bm))
	{
		PORTR_OUTCLR = PIN0_bm;
		_delay_ms(400);
		PORTR_OUTSET = PIN0_bm;
		tc_clear_overflow(&TCC0);
	}
	// User code to execute when the overflow occurs here
	else if (PORTE.IN & PIN5_bm)
	{
		PORTR_OUTCLR = PIN1_bm;
		_delay_ms(400);
		PORTR_OUTSET = PIN1_bm;
		tc_clear_overflow(&TCC0);
	}
}

Only when i put condition of a button press the overflow callback function is not getting called,if i normally do it without any button press condition its working fine and toggling the LED.I'm using atxmega256a3bu xplained.

Nitin Shenoy

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

Why are you doing the initialisation stuff repeatedly in the while(1) loop? Surely you set up the callback, initialise the timer and so on just once?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int main(void)
{
   pmic_init();
   sysclk_init();
   board_init();
   PORTR_DIRSET= 0x03;
   PORTE_DIRCLR = 0x20;
   
	   
	if (!(PORTE.IN & PIN5_bm))
	{   
	tc_enable(&TCC0);

   cpu_irq_enable();
   
   tc_set_overflow_interrupt_callback(&TCC0, my_callback);
   
   tc_set_wgm(&TCC0,TC_WG_NORMAL);
   tc_write_period(&TCC0, 2000);
   
   tc_set_overflow_interrupt_level(&TCC0,TC_INT_LVL_LO);
   tc_set_resolution(&TCC0, 31250);
	}

something like this?

 

Nitin Shenoy

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

Don't write C code - DESIGN the solution first. This usually involves some one time initialisation and then some repeated activity in a loop (in fact this is so common that Arduino actually invites you to implement the two parts as setup() and loop()!). When you have a clear design then you can start to implement the C code.

 

Clearly things like configuring the timer and enabling interrupts are going to be part of the "one time" stuff but why this would be predicated on the state of E5 is a complete mystery ? No doubt your design will clarify this.

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

Unfortunately I've seen keypad debouncing done like that. Well something like that anyway:

 

This is what I've seen:

On pressing a key, a pin change ISR reads the keypad state and starts a one-shot timer. When that one-shot expires the key state is read again and if matches the first read, the key is considered debounced.

 

PS: This question differs only slightly from your https://www.avrfreaks.net/forum/getting-stuck-while post. You really do seem to be struggling.

 

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

Yeah that's the exact operation I want to do and also the previous one i solved by reading around, but this one seems to get some issues.

Nitin Shenoy