PIN CHANGE INTERRUPT

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

Hi,

The following code has an external interrupt(which is a button on PD2).When the button is pressed an ISR is enabled  which increments the variable and prints its value through UART.

But when I press the button its printing garbage value on COOL TERM.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>

#define F_CPU 8000000UL
#define BAUD 9600
#define MYUBBR F_CPU/16/BAUD-1

volatile int value = 0;

void USART_Init(unsigned int ubrr){
	
	// Setting the baud rate to ubrr
	UCSR0A = 0<<U2X0;
	UBRR0H = (unsigned char)(ubrr >> 8);
	UBRR0L = (unsigned char)ubrr;
	//Setting the frame format to 8 data bit, no parity bit, 1 stop bit
	UCSR0C = 3<<UCSZ00;
	UCSR0B = (1<<TXEN0)|(1<<RXEN0);
}


void USART_Transmit( unsigned int data ){
	
	while (!(UCSR0A & (1<<UDRE0)));
	UDR0 = data;
}


int main(void){

   CLKPR = (1 << CLKPCE);// Setting system clock to 8MHz
   CLKPR = 0;
   USART_Init(MYUBBR);

   PCMSK2 |= (1<<PCINT18);  //PCMSKx register high to enable pin change detection
   PCICR |= (1<<PCIE2);    //enable the interrupt for the corresponding vector
   sei();				  //enable interrupt
   while(1)
   {
	
   }
}

ISR(PCINT2_vect){
	
	value++;
	USART_Transmit(value);  
	
}

Kindly point out the mistake. 

 

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

I think you should convert value of the 'value' variable to a string and send this string via USART.

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

Don't use button at Ext Int because button do have bouncing.
Also don't do send UART inside any ISR except UART ISR.
And yes, don't send var directly, use string instead.
.
MG

I don't know why I'm still doing this hobby

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

Thanks .That worked.