Interfacing of Atmega32 using USART with TX and RX Interrupt

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

Hi,

Please give code for USART. Just transmit and receive any random data using TX and RX Interrupts.

Thanx

harman

Last Edited: Sun. Nov 20, 2016 - 05:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moved to proper forum.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

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

sp_usa1 wrote:

Hi,

Please give code for USART. Just transmit and receive any random data using TX and RX Interrupts.

Thanx

 

I certainly hope that you are not going to repeat the marathon that was your previous one.

 

https://www.avrfreaks.net/forum/t...

 

 

Ross McKenzie, Melbourne Australia

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

 

What micro?

What language?

What clock frequency?

What is receiving the data?

What PCB, (commercial, your own, or breadboard)?

What baud rate?

What have you tried so far?

What do you receive with your current code?

.

.

.

JC

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

sp_usa1 wrote:
Just transmit and receive any random data using TX and RX Interrupts.
Have you actually thought about WHY you would want to do that? In my experience I can quite understand the need for interrupt buffered RX because you have no control over when the other end may asynchronously start to send you data and you may be involved in some intricate job at the time. So yes a FIFO on RXC probably makes a lot of sense. But does the transmission REALLY need to be interrupt driven. Most of the time you are sending things like <"the variable is %d", value> or stuff like that. Does it really matter if the AVR is tied up during this period?

 

So I'd certainly look at using a circular buffer/ring buffer/FIFO for the TX (and those search terms will hit 100's of threads, projects and tutorials here showing you how to do that) but I'd think about whether you want buffered TX or not.

 

PS I love Jay's pulling teeth picture - that is going to have to get a lot of re-use methinks!

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

LOL -- if one is sending and receiving random characters, how will you ever know whether it is working or not?

 

I think OP means "arbitrary" rather than "random"?  IOW, "Can someone point me to any old example of ISR-driven USART use?"  Still language and toolchain would be useful.

 

Might a trip to the Tutorials forum be suggested?

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.

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

theusch wrote:

Might a trip to the Tutorials forum be suggested?

 

Unthinkable, that may require some effort.

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

It's already been suggested to the OP in another thread he/ she posted. Rather than actually looking in the tutorials forum looks like the OP opted to simply post pretty much the same question in the tutorials forum... *sigh*

 

To the OP: I'm not sure why you're asking for code to do this, for starters no one here is going to do your homework for you, especially not without you showing any sign that you've so much as given it a go yourself first. On top of that, you're asking for something that's been done a billion times before, if you so much as googled the subject you might just find a complete working code sample, so to ask 'Freaks to find you the code is just plain lazy...

 

Tell you what, I'll google it for you - what d'ya know, a tutorial posted on AVR Freaks is the top hit...

 

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

What Should I modify in the following program to add Receive interrupt in receive function?

 

 

 

 

#include <avr/io.h>

#include <util/delay.h>

#include <stdlib.h>

#include "lcd.h"                            

  

#define BAUD 9600                         

#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)    

  

#ifndef F_CPU

#define F_CPU 16000000UL                  

#endif

  

// function to initialize UART

void uart_init (void)

{

    UBRRH=(BAUDRATE>>8);

    UBRRL=BAUDRATE;                         

    UCSRB|=(1<<TXEN)|(1<<RXEN);            

    UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);

}

  

 

void uart_transmit (unsigned char data)

{

    while (!( UCSRA & (1<<UDRE)));           

    UDR = data;                             

}

  

 

unsigned char uart_recieve (void)

{

    while(!(UCSRA) & (1<<RXC));           

    return UDR;                            

}

  

 

int main (void)

{

    unsigned char a;

    char buffer[10];

  

    uart_init();                            

    lcd_init(LCD_DISP_ON_CURSOR);           

    lcd_home();                             

  

    while(1)

    {

        a=uart_recieve();                   

        itoa(a,buffer,10);                 

        lcd_clrscr();                       

        lcd_home();                         

        lcd_puts(buffer);                   

        _delay_ms(100);                     

    }

  

    return 0;

}

 

harman

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

Have you been to the tutorial forum and SEARCHED as I suggested to you previously? There are articles in the Tutorial Forum that will teach you this stuff. That is the whole point of the Tutorial Forum - for mentors to share some knowledge to learners. Dean Camera (abcminiuser) jas written articles both about simple, polled use of the UART and also about how to do it using interrupts.

As I think I have also suggested previously some of your Google search terms are "ring buffer", "circular buffer" and "FIFO". Those are three terms that refer to the same thing. It's a buffer into which data is written and from which data is extracted. The first things written will be the first thing extracted (so it's chronological). This is why it is called FIFO (First In, First Out). The buffer is only usually something like 16 or 20 or 32 characters long so that when the end is reached the data storage/removal loops back to the beginning. That is why it is called a "ring" or "circular".

 

For receiving UART you will set up an RXC interrupt. Each time a character arrives and generates "RXC" the interrupt will be called. All it does is write the received byte (UDR) into a FIFO. Meanwhile, elsewhere in the code (main) you will have "consumer" code that will want to use the characters that have arrived. It will extract bytes FROM the FIFO (of available). To do this the FIFO has separate write (used by the interrupt) and read (used by the consumer code) pointers. The buffer is empty when the write and read pointers are the same. If they are not the same there are arrived characters to be consumed.

 

You can use such a system on transmit too. The main() code is a producer and writes to a FIFO (a different one to that used for receive!), it then triggers a UDRE interrupt which then reads and sends anything yet unsent in the buffer. When the "current" character is transmitted another UDRE interrupt occurs. This process repeats as long as there are characters in that buffer (read not equal to write). When all are sent it goes inactive until next time.

 

There are TONS of tutorials and internet articles about all this stuff. If you want to be an engineer you have to learn how to use Google. The only stumbling block (often) is not knowing what the search terms are. Except I just told you (for the second time).

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

Please give corrections in this code

harman

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/* initialize UART */
void InitUART( unsigned char baudrate )
{
    unsigned char x;
    UBRR = baudrate; /* set the baud rate */
    /* enable UART receiver and transmitter, and
    receive interrupt */
    UCSRB = ( (1<<RXCIE) | (1<<RXEN) | (1<<TXEN) );
    x = 0; /* flush receive buffer */
    UART_RxTail = x;
    UART_RxHead = x;
    UART_TxTail = x;
    UART_TxHead = x;
}

/* interrupt handlers */
void UART_RX_interrupt( void )
{
    unsigned char data;
    unsigned char tmphead;
    data = UDR; /* read the received data */
    /* calculate buffer index */
    tmphead = ( UART_RxHead + 1 ) & UART_RX_BUFFER_MASK;
    UART_RxHead = tmphead; /* store new index */
    if ( tmphead == UART_RxTail )
    {
        /* ERROR! Receive buffer overflow */
    }
    UART_RxBuf[tmphead] = data; /* store received data in buffer */
}

void UART_TX_interrupt( void )
{
    unsigned char tmptail;

    /* check if all data is transmitted */
    if ( UART_TxHead != UART_TxTail )
    {
        /* calculate buffer index */
        tmptail = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK;
        UART_TxTail = tmptail; /* store new index */
        UDR = UART_TxBuf[tmptail]; /* start transmition */
    }
    else
    {
        UCSRB &= ~(1<<UDRIE); /* disable UDRE interrupt */
    }
}

/* Read and write functions */
unsigned char ReceiveByte( void )
{
    unsigned char tmptail;

    while ( UART_RxHead == UART_RxTail ) /* wait for incomming data */
        ;
    tmptail = ( UART_RxTail + 1 ) & UART_RX_BUFFER_MASK;/* calculate buffer index */
    UART_RxTail = tmptail; /* store new index */
    return UART_RxBuf[tmptail]; /* return data */
}

void TransmitByte( unsigned char data )
{
    unsigned char tmphead;
    /* calculate buffer index */
    tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK;
    /* wait for free space in buffer */

    while ( tmphead == UART_TxTail )
        ;
    UART_TxBuf[tmphead] = data; /* store data in buffer */
    UART_TxHead = tmphead; /* store new index */
    UCSRB |= (1<<UDRIE); /* enable UDRE interrupt */
}

 

Have fun!

 

FF = PI > S.E.T

 

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

You aren't showing the definition of the globals in that and what about atomic access?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/* UART Buffer Defines */
#define UART_RX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1 )
#define UART_TX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */
#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1 )

#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif

/* Static Variables */
static unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART_RxHead;
static volatile unsigned char UART_RxTail;
static unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];
static volatile unsigned char UART_TxHead;
static volatile unsigned char UART_TxTail;

The OP only asked for "code"!  He will still need to provide the proper interrupt linkage to his "unstated" toolchain.

 

 

FF = PI > S.E.T

 

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

.

 

 

Imagecraft compiler user

Last Edited: Fri. Nov 25, 2016 - 05:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just love it when Cliff tries to educate people and Bob just feeds them with working code...

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

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

Image result for teach a man to fish

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

Amen!

 

Ross McKenzie, Melbourne Australia

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

.

 

Imagecraft compiler user

Last Edited: Fri. Nov 25, 2016 - 05:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see you're getting an early start on your next banishment.  Let's see, how can I egg you on to hasten the process... Nah, you'll manage it all by yourself.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

 

 

 

Imagecraft compiler user

Last Edited: Fri. Nov 25, 2016 - 05:34 PM