Smal INT0 exercise does not work

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

Dear Community

 

I started a small INT0 exersise.

 

Thats what the code should do:

 

I apply 100Hz signalto INT0 pin

 

I declared a variable uinit32_t counter.

 

In the ISR I increment the counter by 1.

 

In a while loop I check if counter exceeds 1000 counts and if yes I toggle pin4 of portD.

 

I checked with a pin toggle in the ISR that the interrupt gets sensed and it does. I have just wondered about the quite long latency of ca. 3us until the interruppt gets serviced.

 

The debuger does not help much, when I do single step it goes straight to the PORTD toggle (after initialising) after executing PORTD toggle it goes to "if (counter >= 1000)" and the debugger exits single stepping, I can only stop or pause.

 

 

It looks like a simple code,buit I dont know whats wrong.

 

Have you got a tip?

 

regards

 

Juergen

/*
 * ExternInt0.c
 *
 * Created: 18.10.2019 10:36:28
 * Author : jb
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
//uint8_t counter;
uint32_t counter;

ISR (INT0_vect)
{
	counter++;
	//PORTD ^= (1<<4);
}

int main(void)
{
	
    DDRD &= ~(1<<PD2);
	DDRD |= (1<<PD4);
	PORTD = 0x00;
	
	EICRA |= (1<<ISC01) | (1<<ISC00);
	EIMSK |= (1<<INT0);
	
	sei();
	   
	while (1) 
    {
		if (counter >= 1000)
		{		
			PORTD ^= (1<<4);
			counter= 0;	
		}
		
    }
}

 

 

 

 

This topic has a solution.
Last Edited: Sat. Oct 19, 2019 - 07:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Make counter a volatile or static.

Happy Trails,

Mike

JaxCoder.com

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

volatile is the key.

 

Again, this is a standard 'C' thing - not specific to AVR.

 

https://www.avrfreaks.net/forum/tutcoptimization-and-importance-volatile-gcc - although the focus is on GCC, the principle applies to any optimising compiler for any target; ie, pretty much any compiler today.

 

 

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...
Last Edited: Sat. Oct 19, 2019 - 01:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks very much.

 

I thought if I switch off the optimiser it does not matter.

 

I set to volatile and it works.

 

I still have to look into the input signal, some times it just stops working, I went up to 300kHz then I guess it starts loosing interrupts as the output signals become intermittant.

 

I will have to look into it.

 

Thanks for your help

 

regards

 

Juergen

 

 

 

 

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

JB57 wrote:
I thought if I switch off the optimiser it does not matter.

But then it may well take more than 3us to get to the toggle code!!!  Look at the generated code.  But indeed, learn about 'volatile'.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.