[solved - simulator bug]Simulating external interrupts

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

Hi friends

I am using Proteus software for simulating prototype device and I encounter a problem with external interrupts. I don`t know weather it is a fault of simulator or it is mine.

Problem:

I am sending one interrupt from slave uC to master uC and it seems interrupts are triggered very often.

My settings:

Master uC:

EICRA|=(1<<ISC01)|(1<<ISC11)|(1<<ISC21); //falling edge generates interrupt
	EIMSK|=(INT0)|(INT1)|(INT2);
ISR(INT1_vect) // Napiecie niezrownowazenia
{
	
	
		nap_nez=PINA;
		nap_nez|=((PINB&((1<<PINB4)|(1<<PINB5)))<<4);
		nap_nez&=((PINB|~(1<<PINB4)|(1<<PINB5))<<4); //poprawna sprawa
		
		Usart_Tx (nap_nez);
		Usart_Tx (nap_nez>>5);
		
	
	
	
}

ISR(INT2_vect) // Poprawka
{
		poprawka=PINA;
		poprawka|=((PINB&((1<<PINB4)|(1<<PINB5)))<<4);
		poprawka&=((PINB|~(1<<PINB4)|(1<<PINB5))<<4); //poprawna sprawa
		
		Usart_Tx(poprawka);
		Usart_Tx(poprawka>>8);
}



Slave uC:

(pins which are connected to triggeriing pins of master uC)

	DDRE|=(1<<PINE0)|(1<<PINE1)|(1<<PINE2);
	PORTE|=(1<<PINE0)|(1<<PINE1)|(1<<PINE2);

Now :

I set external interrupts to trigger for falling edge.
I set pulling up.
I don`t use any mechanical switches so there should be no way of bouncing
I also don`t think so there are ploblems with inductances

What I do wrong? Is it simulator problem ?
Do you think it is better to change interrupts for normal reading ports?

Please help

Last Edited: Tue. Nov 22, 2011 - 03:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Posting all the code would help, the few snippets you posted doesn't show us main, how your code flow works, etc. And which micro are you using?

Why are you ORing PINA with parts of PINB?? Then it looks like you AND the same values off??

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

I am using two atmeaga64. Interrupts are on PD0, PD1, PD2. When I connect outputs from slave uC which are set always for high master uC starts triggering?

Code of the master uC:

#include 
#include 
#include    //Standard Integer Types
#include "HD44780.h"
#include "HD44780.c"
#include      //you need this for itoa function which is not written in io.h
#include 	// itoa is not a standard function, its found in stdlib.h

//UART:
#define USART_BAUDRATE 250000 //250.0 (kbps) 0 % Error
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

//handling inputs:
#define KEY_PIN		PINB 
#define KEY_PORT	PORTB
#define KEY0		0
#define KEY1		1
#define KEY2		2
#define KEY3		3
#define KEY4		4
#define KEY5		5
#define KEY6		6
#define KEY7		7


//Zmienne globalne:
volatile uint8_t key_state;				
volatile uint8_t key_press;		

volatile 	uint16_t wynik, nap_nez, poprawka;	

uint8_t mode;

			

int16_t num;
uint32_t num2;
uint32_t delta_t;
uint32_t delta_l;


char buffer[10];

volatile uint8_t LCD_flag;

ISR(TIMER0_OVF_vect)
{


	
	static uint8_t ct0, ct1;
	static uint8_t i;
	

  TCNT0 = 	(uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 );	// preload for 10ms
  //Here we have another aspect of C programming which is called casting
  //Converting a negative float value to unsigned was undefined by the C-standard.
  //Only converting negative float to signed and signed to unsigned was defined.

  i = key_state ^ ~KEY_PIN;		// key changed ?
  ct0 = ~( ct0 & i );			// reset or count ct0
  ct1 = ct0 ^ (ct1 & i);		// reset or count ct1
  //ct2 = (ct0 & ct1) ^ (ct2 & i);
  //ct3 = (ct0 & ct1 & ct2) ^ (ct3 & i);
  i &= ct0 & ct1;				// count until roll over ?
  key_state ^= i;				// then toggle debounced state
  key_press |= key_state & i;	// 0->1: key press detect


}   

void Usart_Tx ( char ch)
{
  while ((UCSR0A & (1<<UDR0)) == 0) {};
  UDR0 = ch;
}

void Usart_SendString (char * p) //p as pointer ;)
{
	while (*p) 
	{
		Usart_Tx(*p);
		p++;
	} 

}




uint8_t get_key_press( uint8_t key_mask )
{
  cli();					// read and clear atomic !
  key_mask &= key_press;   // read key(s)
  key_press ^= key_mask;   // clear key(s)
  sei();
  return key_mask;
}



ISR(INT0_vect) //wynik
{
	wynik=PINA;
	wynik|=((PINB&((1<<PINB4)|(1<<PINB5)))<<4);
	wynik&=((PINB|~(1<<PINB4)|(1<<PINB5))<<4); //poprawna sprawa
	
	if (mode==0) //rozwazyc zmiane na kazde zbocze wtedy tego nie bedzie
	{
		PORTF|=1<<PINF0;
		LCD_Clear();
		LCD_GoTo(0,0);
		itoa(wynik,buffer,10);
		LCD_WriteText(buffer);
		
		
		
		
		
	}
	
	Usart_Tx (wynik);
	Usart_Tx (wynik>>5);
		
	
		
}



ISR(INT1_vect) // Napiecie niezrownowazenia
{
	
	
		nap_nez=PINA;
		nap_nez|=((PINB&((1<<PINB4)|(1<<PINB5)))<<4);
		nap_nez&=((PINB|~(1<<PINB4)|(1<<PINB5))<<4); //poprawna sprawa
		
		Usart_Tx (nap_nez);
		Usart_Tx (nap_nez>>5);
		
	
	
	
}

ISR(INT2_vect) // Poprawka
{
		poprawka=PINA;
		poprawka|=((PINB&((1<<PINB4)|(1<<PINB5)))<<4);
		poprawka&=((PINB|~(1<<PINB4)|(1<<PINB5))<<4); //poprawna sprawa
		
		Usart_Tx(poprawka);
		Usart_Tx(poprawka>>8);
}







int main(void)
{	 
	PORTA|=0xff;
	PORTB|=0xff;
	
	PORTD|=(1<<PIND0)|(1<<PIND1)|(1<<PIND2);
	DDRD|=(1<<PIND4)|(1<<PIND5);
	PORTD|=(1<<PIND4)|(1<<PIND5);
	
	//dalej ogarnać nieuzywane piny...
	
	///////       //   // / / /////
	
	//Zewnętrzne przerwania
	
	EICRA|=(1<<ISC01)|(1<<ISC11)|(1<<ISC21); //falling edge generates interrupt
	EIMSK|=(INT0)|(INT1)|(INT2);
		
	//Timer0
	TCCR0|= (1<<CS02)|(1<<CS01)|(1<<CS00);		// divide by 1024
	TIMSK|= (1<<TOIE0);				// enable timer interrupt

	//UART:
	
	//turn on the transmission and reception circuity:
	UCSR0B |= (1<< RXEN0)|(1<<TXEN0);
	
	// use 5-bit character sizes:
	UCSR0C&=~(1<<UCSZ00)|(1<<UCSZ01);
	
	// Load the baud rate value into the UBRR registers:
	UBRR0L = BAUD_PRESCALE;
	UBRR0H = (BAUD_PRESCALE >> 8);
	
	
	_delay_ms(200); //Napięcie narpierw pojawia się na uC wiec zanim rozpoczniemy funkcje inicjacji LCD 
	LCD_Initalize();
	_delay_ms(20);
	LCD_WriteText("      LACP      "); // podwujne wyswietlanie to zasluga AVRISPmkII ;)
	LCD_GoTo(0,1);
	LCD_WriteText("Adam ");
	
	for (uint8_t i=0;i<=4;i++)
	{
		_delay_ms(250);
	}
	LCD_Clear();
	LCD_GoTo(0,0);
	LCD_WriteText("-> single conv.");
	LCD_GoTo(0,1);
	LCD_WriteText("   plural conv.");
		
	
	sei(); //obsługa przerwań globalnych włączona
 
	
	
	
for(;;)
{

if (get_key_press(1<<PINB4)) //jesli wcisniety przycisk mode
{
	LCD_Clear();
	mode^=1;
	
	
	if (mode==0)
	{
		LCD_Clear();
		LCD_GoTo(0,0);
		LCD_WriteText("-> single conv.");
		LCD_GoTo(0,1);
		LCD_WriteText("   plural conv.");
	}
	else
	{
		LCD_Clear();
		LCD_GoTo(0,0);
		LCD_WriteText("   single conv.");
		LCD_GoTo(0,1);
		LCD_WriteText("-> plural conv.");
	}
}

if (get_key_press(1<<PINB5)) //jesli wcisniety przycisk start
{
	if (mode==0)
	{
		PORTD&=~(1<<PIND4);
		
	}
	else
	{
		PORTD&=~(1<<PIND5);
	}

}










}


}



Slave uC for testing purposes


int main(void)
{	 

	//jeszcze raz zweryfikowac za jakis czas
	
	DDRA=0xff; //ustaw port A jako wyjścia...
	DDRB|=(1<<PINB6)|(1<<PINB7);
	
	DDRC=0xff;
	DDRD=0xff;
	
	
	
	PORTE|=(1<<PINE4)|(1<<PINE5)|(1<<PINE6);
	
	DDRE|=(1<<PINE0)|(1<<PINE1)|(1<<PINE2);
	PORTE|=(1<<PINE0)|(1<<PINE1)|(1<<PINE2);
	
	
	PORTF|=(1<<PINF0)|(1<<PINF1)|(1<<PINF2);
	
		
	//Nieuzywane piny //do zweryfikowania co jesli bede chcial uzyc JTAG
	
	//PORTB|=(1<<PINB6);
	
	//PORTF|=(1<<PINF3);
	//PORTG|=(1<<PING3)|(1<<PING4);
	
	//Zewnętrzne przerwania
	
	EICRB|=(1<<ISC41)|(1<<ISC51); //falling edge generates interrupt
	EIMSK|=(INT4)|(INT5);
	
	//Timer1
	TCCR1B|= (1<<WGM12); //CTC mode OCR1A as TOP
	
	
	// turn off all switches
	
	PORTC|=(1<<PINC0);
	
	PORTD&=~(1<<PIND2); //SW0
	PORTD|=(1<<PIND3);
	
	PORTD&=~(1<<PIND4); //SW1,4
	PORTD|=(1<<PIND5);
	
	PORTD&=~(1<<PIND6); //SW2,3
	PORTD|=(1<<PIND7);
	
	for (uint8_t i=0;i<=4;i++)
	{
		_delay_ms(250);
		_delay_ms(250);
		_delay_ms(250);
		_delay_ms(250);
	}
	
	
	
	
for(;;)
{

// czeka na przerwania
	
}


}



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

Quote:
TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 ); // preload for 10ms
If you are doing this, then why not use CTC mode?

Regards,
Steve A.

The Board helps those that help themselves.

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

I have got a message from the Labcenter company and indeed it was problem of their simulator. Sorry guys for bugging you unnecessarily.

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

Quote:

I have got a message from the Labcenter company

Holy mother of God - don't tell me someone actually paid for a copy of Proteus? Whatever next?

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

Maybe the message was that certain versions of Proteus do have bugs (allegedly)...