Interrupt Problem in C++ Atmel Studio

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

Hi.

I decided to write an application by C++ in atmel Studio. The micro controller that I using is Xmega32A4u. 

 

First, I created a class for USART and I could Send Data correctly.

 

But I do not know How and where I should write ISR function for receiving data.

This Is my code:

 

<code>

/*
* Xmega_Usart_Class.cpp
*
* Created: 5/19/2016 3:16:04 PM
* Author: Nima Talebpour
*/

#include <avr/io.h>

#include "../Includes/Xmega_Usart_Class.h"

//USARTD0,PORTD,0
Xmega_Usart_Class::Xmega_Usart_Class(USART_t* ArgUsartName,PORT_t* ArgUsartPort,int ArgUsartPortNum)
{
    UsartName=ArgUsartName;
    UsartPort=ArgUsartPort;
    UsartPortNum=ArgUsartPortNum;
}

Xmega_Usart_Class::~Xmega_Usart_Class()
{
}

//USART_CHSIZE_8BIT_gc,USART_PMODE_DISABLED_gc, false
void Xmega_Usart_Class::USART_Init(int BSEL,int BSCALE,USART_CHSIZE_t UsartChSize,USART_PMODE_t UsartMode,bool IsTwoStopBits)
{
    if(UsartPortNum==0)
    {
        // (TX) as output
        UsartPort->DIRSET   = PIN3_bm;
        // (RX) as input
        UsartPort->DIRCLR   = PIN2_bm;
    }
    else
    {
        // (TX) as output
        UsartPort->DIRSET   = PIN7_bm;
        // (RX) as input
        UsartPort->DIRCLR   = PIN6_bm;
    }
    
    UsartName->CTRLC = (uint8_t) UsartChSize | UsartMode |  (IsTwoStopBits ? USART_SBMODE_bm : 0);
    
    
    // Enable RXC interrupt
    UsartName->CTRLA = (UsartName->CTRLA & ~USART_RXCINTLVL_gm) | USART_RXCINTLVL_LO_gc;
    
    UsartName->BAUDCTRLA =(uint8_t)BSEL;
    UsartName->BAUDCTRLB =(BSCALE << USART_BSCALE0_bp)|(BSEL >> 8);

    /* Enable both RX and TX. */
    UsartName->CTRLB |= USART_RXEN_bm;
    UsartName->CTRLB |= USART_TXEN_bm;
}

void Xmega_Usart_Class::USART_PutC(uint8_t Ch)
{
    UsartName->DATA = Ch;
    while (!((UsartName->STATUS & USART_DREIF_bm) != 0));
}

char Xmega_Usart_Class::USART_GetC()
{
    return UsartName->DATA ;
}

void Xmega_Usart_Class::USART_PutS(uint8_t* Str)
{
    for (int i=0;Str[i]!='\0';i++)
    {
        USART_PutC(Str[i]);
    }
}

</code>

 

/*
* Xmega_Usart_Class.h
*
* Created: 5/19/2016 3:16:04 PM
* Author: Nima Talebpour
*/

#ifndef __XMEGA_USART_CLASS_H__
#define __XMEGA_USART_CLASS_H__

class Xmega_Usart_Class
{        
    //variables
    public:
    USART_t* UsartName;
    PORT_t* UsartPort;
    int UsartPortNum;
    protected:
    private:

    //functions
    public:
    Xmega_Usart_Class(USART_t* UsartName,PORT_t* UsartPort,int UsartPortNum);
    ~Xmega_Usart_Class();
    void USART_Init(int BSEL,int BSCALE,USART_CHSIZE_t UsartChSize,USART_PMODE_t UsartMode,bool IsTwoStopBits);
    void USART_PutC(uint8_t Ch);
    void USART_PutS(uint8_t* Str);
    char USART_GetC();
    protected:
    private:
    Xmega_Usart_Class( const Xmega_Usart_Class &c );
    Xmega_Usart_Class& operator=( const Xmega_Usart_Class &c );

}; //Xmega_Usart_Class

#endif //__XMEGA_USART_CLASS_H__

 

</code>

 

The problem Is, that I do not know where I should use this:

 

<code>

ISR( USARTC0_RXC_vect )
{
 
}

</code>

Last Edited: Thu. May 19, 2016 - 10:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I looked at the Arduino libraries for that answer.

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

Kartman wrote:
I looked at the Arduino libraries for that answer.

 

Could you please show me an example ?

 

thank you

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

An example of how to Google?
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial0.cpp
Line 49
You'll need to look a little deeper fo find how Serial is declared.

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

I'm having the same problem.

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

What don't you understand with the example I gave?

 

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

I have no problem with that example. But I need an example to interrupt at xmega.

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

Atmel have application notes showing how to use all the Xmega peripherals. 

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

but there is not any application note which show how we can use ISR function in C++.

 

If you know any example or document please tel me.

 

thanks.

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

Not sure I understand your question. The AVR-LibC interrupt handling using ISR() is the same whether you do it in C or C++.