日志档案

发表于 2007-9-18 23:15:59

1

标签: winavr  friend  operator  overloading  

AVR C++中友元的使用及操作符重载

AVR C++中友元的使用及操作符重载

作者: TestCode

. 友元的使用

C++中,不能直接调用中断函数,因此就不能将中断函数声明为类的成员函数。然而C++中,类的数据成员一般声明为private。中断函数对类的私有成员的访问可以通过友元机制来实现。

 

以下以Atmega32为例:

/***********************************************************************************

Name         :  uart.h

Description  :  Header file for Mega32 Uart driver.

Author       :  2007-09-17 - TestCode

Compiler     :  WINAVR Version: 20070525

************************************************************************************/

#ifndef _C_Com_H_

#define _C_Com_H_

 

#ifdef __cplusplus

extern "C"

{

#endif

 

#include

#include

#include

#include "buffer.h"

#include

#include "iodev.h"

 

/*****************************************************************************

*                                   Definitions

*/

#ifndef FALSE

#define FALSE                 (0)

#endif

#ifndef TRUE

#define TRUE                  (1)

#endif

//

#define BAUD_9600       (9600)

#define BAUD_115K       (115200)

#define BAUD_230K       (230400)

#define BAUD_460K       (460800)

#define BAUD_920K       (921600)

 

//

#define TAB                           (0x09)

#define BACKSPACE             (0x08)

#define SPACE                 (0x20)

#define PERIOD                (0x2E)

#define CR              (0x0D)

#define LF              (0x0A)

#define ESCAPE          (0x1B)

 

void USART_RXC_vect(void) __attribute__ ((signal));

//void USART_TXC_vect(void) __attribute__ ((signal));

 

class Buffer;

class IO;

 

class C_Com

{

private:

    Buffer uartRxBuffer;

    static C_Com* pUart;

    long BAUDRATE;

   

public:

            C_Com(void);

    void Init(long baud);

    void put_c(unsigned char c);

    char Send(unsigned char* pBuffer, unsigned int uiSize);

    char Send(unsigned int uiSize);//Send UartBuffer String

    char GetByte(void);

        friend void USART_RXC_vect(void);

        //friend void USART_TXC_vect(void);

        friend class IO;

        friend class IO_P;

};

 

#ifdef __cplusplus

}

#endif

 

#endif

 

通过把USART_RXC_vect中断函数定义为C_Com的友元,USART_RXC_vect可以对C_Com的私有成员进行操作。

 

. 操作符重载

提到重载,大家很自然想到成员函数重载,函数重载实现赋给同一个函数名多个含义。以下例子中,C++中通过重新定义运算符,不仅g++生成的代码也十分简洁高效,很直观地实现串口驱动的输入及输出。

/***********************************************************************************

Name         :  iodev.h

Description  :  Header file for io device.

Author       :  2007-09-17 - TestCode

Compiler     :  WINAVR Version: 20070525

************************************************************************************/

#ifndef IO_H_

#define IO_H_

#include "avr/io.h"

#include

#include "uart.h"

#include

 

#ifdef __cplusplus

extern "C"

{

#endif

 

class C_Com;

class IO;

class IO_P;

 

extern IO cout, cin;

extern IO_P cout_P;

 

// Debug class.

class IO

{

public:

            /// Sending strings.

            IO& operator << (char const*);

            /// Sending unsigned integers.

            IO& operator << (unsigned int);

            IO& operator >> (char*);  // Read strings.

};

 

class IO_P

{

public:

            IO_P& operator << (PGM_P data);  // Read strings.

};

 

#ifdef __cplusplus

}

#endif

 

#endif /*_IO_H_*/

 

. 测试结果:

 

/***********************************************************************************

Name         :  main.cpp

Description  :  Mega32 Uart c++ sample.

Author       :  2007-09-17 - TestCode

Compiler     :  WINAVR Version: 20070525

************************************************************************************/

#include

#include "uart.h"

#include

#include "iodev.h"

 

prog_char FlashString[] PROGMEM ="Flash String";

 

int main()

{

            unsigned int Test = 123;

            char str[32];

           

            cout<<"HelloWorld\r\n"<<<"\r\n";

            cout_P<

            cout_P<

            while(1)

            {

                        cin>>str;

                        cout<<"Your type in: "<<<"\r\n";

            }

            return 0;

}

 

测试结果:

点击看大图

系统分类: 单片机   |   用户分类: AVR C++   |   来源: 原创   |   【推荐给朋友】

    阅读(1516)    回复(3)  

投一票您将和博主都有获奖机会!

  • 彩云

    2007-9-19 9:11:59

    好厉害啊。。。。学习了。收藏!

  • zhaoyushan

    2007-10-22 12:46:18

    perfect

     

  • feng423

    2007-10-27 9:46:00

    现在正在学习avr,以后多多指教啊