Interrupt does not fire after first pass

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

Hey All,

Trying to get the frequency counter to work on the XMEGA32e5, and I have run into a problem.  The interrupt flag fires as soon as I launch the app, and I step through the ISR in Studio and watch the flag clear on ISR exit.  But the ISR never fires again after even though all the interrupt enables are in fact enabled.  I cannot figure it out.

 

Now this is a bit of a kludge from the AVR1306 example and my setting it up to be GCC.  I don't know why Atmel wrote almost all their example code in IAR, yet STUDIO is GCC....THey are not the same obviously.

 

Anyway here is what I have:

/*
 * Frequency_Counter.c
 *
 * Created: 1/8/2020 12:54:40 PM
 * Author : jgmDESIGNS
 */ 

#define F_CPU           2000000UL
#define CPU_PRESCALER   1

#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <avr/interrupt.h>

volatile uint16_t freq;



// Timer/Counter TCC4 Compare/Capture A interrupt service routine
void TCC4_CCA_vect(void)
{
	static uint32_t frequency;
	static uint32_t dutyCycle;
	static uint16_t totalPeriod;
	static uint16_t highPeriod;
	static uint16_t thisCapture;
	// Ensure that the Compare/Capture A interrupt flag is cleared
	if (TCC4.INTFLAGS & TC4_CCAIF_bm) TCC4.INTFLAGS=TC4_CCAIF_bm;
	// Write your code here
	thisCapture = TCC4.CCA;

	/*  Save total period based on rising edge and reset counter. */
	if ( thisCapture & 0x8000 ) {
		totalPeriod = thisCapture & 0x7FFF;
		TCC4.CNT = 0;
	}
	/* Calculate duty cycle based on time from reset and falling edge. */
	else {
		highPeriod = thisCapture;
	}

	dutyCycle = ( ( ( highPeriod * 100 ) / totalPeriod ) + dutyCycle ) / 2;
	frequency = ( ( ( F_CPU / CPU_PRESCALER ) / totalPeriod ) + frequency ) / 2;

	freq = frequency;
}


void XMEGA_INIT(void)
{
	//event system setup
	EVSYS.CH0MUX = EVSYS_CHMUX_PORTD_PIN0_gc;
	EVSYS.CH0CTRL = EVSYS_DIGFILT_1SAMPLE_gc;	//SYSTEM DEFAULTS
	EVSYS.DFCTRL = EVSYS_PRESCFILT_CH04_gc | (0<<EVSYS_FILTSEL_bp) | EVSYS_PRESC_CLKPER_8_gc;
	
	
	//Timer Counter Type 4 setup
	
	TCC4.CTRLA = TC_CLKSEL_DIV1_gc;
	TCC4.CTRLB = 0;		//SYSTEM DEFAULTS
	TCC4.CTRLC = 0;     //SYSTEM DEFAULTS
	TCC4.CTRLD = TC_EVACT_PWF_gc | TC_EVSEL_CH0_gc;
	TCC4.CTRLE = TC_CCAMODE_CAPT_gc;
	TCC4.INTCTRLA = 0;	//SYSTEM DEFAULTS
	TCC4.INTCTRLB = TC_CCAINTLVL_LO_gc;
	// Clear the interrupt flags
	TCC4.INTFLAGS=TCC4.INTFLAGS;
	// Set Counter register
	TCC4.CNT=0x0000;
	// Set Period register
	TCC4.PER=0x7FFF;
	// Set channel A Compare/Capture register
	TCC4.CCA=0x0000;
	// Set channel B Compare/Capture register
	TCC4.CCB=0x0000;
	// Set channel C Compare/Capture register
	TCC4.CCC=0x0000;
	// Set channel D Compare/Capture register
	TCC4.CCD=0x0000;
	
	//NTERRUPT SETUP
	uint8_t n;
	// Low level interrupt: On
	// Round-robin scheduling for low level interrupt: On
	// Medium level interrupt: Off
	// High level interrupt: Off
	// The interrupt vectors will be placed at the start of the Application FLASH section
	n=(PMIC.CTRL & (~(PMIC_RREN_bm | PMIC_IVSEL_bm | PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm))) |
	PMIC_LOLVLEN_bm ;
	CCP=CCP_IOREG_gc;
	PMIC.CTRL=n;
	// Set the default priority for round-robin scheduling
	PMIC.INTPRI=0x00;
}


int main(void)
{
	XMEGA_INIT();		//CONFIG XMEGA
	sei();

    /* Replace with your application code */
    while (1) 
    {
		freq = freq;
    }
}

The 'magic number' 0 for the CTRL registers in TCC4 is just to make sure they are at the power up condition.

 

If someone can spot why this is not functioning I would appreciate it.  The ISR stuff is from the AVR1306 example which was done for the XMEGA128A.

 

Essentially all I want to do is show the frequency in the "FREQ" variable.  THats it.

 

Yes, I know I could use two counters, but I am intending on using them for other things, and this feature is supposed to be part of TCC4 so.........

 

Jim

This topic has a solution.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Thu. Jan 9, 2020 - 12:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
I step through the ISR in Studio

Does the problem only occur when stepping in the debugger ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No.  I put a breakpoint in the ISR to show that  the ISR hits and let it run.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Sorry if wrong.
I can't find the ISR in your code.

void TCC4_CCA_vect(void)

Isn't this an ordinary function?

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

kabasan wrote:

Sorry if wrong.
I can't find the ISR in your code.

void TCC4_CCA_vect(void)

Isn't this an ordinary function?

 

OH S****! LOOK AT THAT! angry

 

Will fix and get back with a reply.

 

Thanks!

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Well OK, I at least now have the ISP firing properly!

 

Sadly the ISR does not seem to provide a correct frequency count.  All I get for FREQ is 0xFFF.

 

At least teh ISR is firing.  Now to figure out whats wrong.

 

THanks

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

List what I noticed.

1. Do not run the timer until all settings are completed. That is, CTRLA writes last.
2. The cycle and duty can be obtained by CCA and CCB without complicated calculations.

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

Yeah, I have been learning that things re not always as difficult as they seem.

 

kabasan wrote:
2. The cycle and duty can be obtained by CCA and CCB without complicated calculations.

 

THats what I am trying to figure out now.  I am more interested in getting the Period(T) through CCA though.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user