USART: example needed for interrupt-driven transmission

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

I have problems sending data through the USART.

The data should be sent in packets of 14 bytes.
But the process simply does not stop.

I am sure that something is wrong with my concept of sending data inerrupt driven by UDRE

This is how I send the first byte:

_UART_TX_bytecounter=sizeof(struct UART_Message);
		_UART_TX_pointer=(uint8_t *) &_UART_TX_tmpMessage;	//Startwert setzen
		
		#ifdef __AVR_AT90CAN128__
			//volatile tmp=*UART_TX_Control.READ_Pointer;
			//UDR1=tmp;
			UCSR1B = UCSR1B | (1<<UDRIE1) | (1<<TXEN1);
		
			// Interrupt freigeben
			//...danach werden die restlichen Bytes werden durch Interupt gesendet.
		#elif __AVR_ATmega32__
			UCSRB = UCSRB |	(1<<UDRIE);
		#endif
		
		UDR1=*_UART_TX_pointer;	//erstes Byte übertragen
		_UART_TX_bytecounter--;
		_UART_TX_pointer++;

ANd this is the irq-code (partly):

if(RTS_LINE_FT232R==USART_READY)
	{	//Gegenseite empfangsbereit
		// es !dürfen! (weitere) Zeichen gesendet werden ...
		tmp=*_UART_TX_pointer;
		#ifdef __AVR_AT90CAN128__
			//UDR1=*UART_TX_Control.READ_Pointer;	//Datenbyte in das Register UDR1 schreiben
			UDR1=tmp;	//Datenbyte in das Register UDR1 schreiben
		#elif defined __AVR_ATmega32__
			UDR=*UART_TX_Control.READ_Pointer;
		#endif
	}
	else
	{	//Gegenseite *NICHT* empfangsbereit !
		//vorerst die Übetragung beenden. Der Marker für
		#ifdef __AVR_AT90CAN128__
			CLEARBIT(UCSR1B,UDRIE1);	//Interrupt löschen
		#elif defined __AVR_ATmega32__
			CLEARBIT(UCSRB,UDRIE);
		#endif
		UART_TX_Control.QueueRunning=0;	// Ãœbetragung ist offiziell beendet (?)
		return;	//Pointer wird NICHT erhöht	
	}

	
	//UART_TX_Control.BufferBytesRemaining++; //Zeichen erfolgreich gesendet
	_UART_TX_bytecounter--;
	_UART_TX_pointer++;	//Pointer um eins erhöhen ...
	
	if(_UART_TX_bytecounter==0)
	{
		//Paket vollständig übertragen
		#ifdef __AVR_AT90CAN128__
			CLEARBIT(UCSR1B,UDRIE1);	//Interrupt löschen
			CLEARBIT(UCSR1B,TXEN1);
		#elif defined __AVR_ATmega32__
			CLEARBIT(UCSRB,UDRIE);
		#endif
		UART_TX_Control.QueueRunning=0;	// Ãœbetragung ist offiziell beendet
		
		//UART_StartPacketTransmission(); //temporarily disabled. Should send the next packet if necessary
//falls noch weitere Pakete anstehen -> weitermachen
	}

Any suggestions?

I program like a man:
COPY CON: > firmware.hex

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

Turn off the transmit complete interrupt after the last char in the transmit buffer/array is sent? Enable it when adding chars into the transmit buffer/array. Receive interrupt is on all the time.

Imagecraft compiler user

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

Quote:

Any suggestions?

Remove all the irrelevant lines.
Format the code neatly.
Print it out on a sheet of paper.

Then compare with a textbook example.

Seriously, unused conditionals only complicate matters.

Once you have it working for a single case, you can introduce conditional complexity.

David.

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

there are many, many such examples of AVR serial UART buffered interrupt code. On this forum in the projects area. On the 'net, plentiful.