Problem by using transmission of string or char in external interrupt

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

hi

i want to send string by USART in external interrupt .

my project is :

one bluetooth module connected to external interrupt . , .....

 

when i am connecting to module ,ext interrupt work correctly but i use usart transmission on ISR(INT1_vect) , several sequence of string send to me .

 

my code :

/*
 * PROBLE_USART_INT_1.c
 *
 * Created: 10/25/2018 08:01:09 AM
 * Author : HF
 */ 
//device : atmega328 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
 
void UART_init(uint32_t baud){
	
	UBRR0H = (((F_CPU)/(baud*16UL)-1)>>8);                    // shift the register right by 8 bits for UBRRH
	UBRR0L = ((F_CPU)/(baud*16UL)-1);                        // set baud rate value for UBRRL
	
	UCSR0B|= (1<<TXEN0)|(1<<RXEN0);                // enable receiver and transmitter
	UCSR0C|= (1<<UCSZ00)|(1<<UCSZ01);             // 8bit data format
}

void UART_transmit_char (unsigned char data)
{
	while(!(UCSR0A & (1<<UDRE0)));
	UDR0 = data;                            // load data in the register
}


void UART_transmit_string(char *command){
	
	while(*command != 0x00){
		UART_transmit_char(*command);
	command++;}
}

char UART_recieve (void)
{
	while ( !(UCSR0A & (1<<RXC0)) );                 // wait while data is being received
	return UDR0;                                   // return 8-bit data
}


int main(void)
{
		        
UART_init(9600);
//setting external interrupt 1 for BLT connection
EICRA |= (1<<ISC10)|(1<<ISC11);  //The rising edge of INT1 generates an interrupt request.
EIMSK |= (1<<INT1);  //Enable interrupt 1
sei();
    /* Replace with your application code */
    while (1) 
    {
    }
}

ISR(INT1_vect){
	
	UART_transmit_string("HELLO HOSSEIN");
}

output after connect to bluetooth mdule :

HELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEINHELLO HOSSEIN

 

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

Bad design. Never do "long work" like UART transmission from within an ISR. On an AVR you'll keep interrupts disabled for far too long. Instead have the ISR set a flag then have the foreground code trigger the UART activity from that flag.

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

I think you're seeing 'contact bounce'. It's not a good idea to use an external interrupt for mechanical switches.

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

clawson wrote:

Bad design. Never do "long work" like UART transmission from within an ISR. On an AVR you'll keep interrupts disabled for far too long. Instead have the ISR set a flag then have the foreground code trigger the UART activity from that flag.

 

thanks , so you say that me how can find one connection from bluetooth module ?