Gorgon_Meducer 发表于 2006-4-25 15:43:06

[古董贴][共享][沉底]最新的通用C语言位操作支持库

#ifndef _USE_AVR_PORT_BIT_H_

#define _USE_AVR_PORT_BIT_H_

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

*函数库说明:AVR位操作通用支持库                         *

*版本:      v1.00                                       *

*作者:      傻孩子                                    *

*创建日期:2006年4月25日                               *

* -------------------------------------------------------- *

*修改:                                                *

*修改日期:                                              *

*版本:                                                *

* -------------------------------------------------------- *

*[版本历史]                                              *

*   前身版本在该版本最终生成之前,经历过针对各种芯片的 *

*               专用版本,这些版本提供了针对PORT的位操作和 *

*               部分PIN、DDR的位操作。                     *

*      v1.00该版本支持几乎所有的芯片,具有完善的宏定义 *

*               系统,相对前身版本最小的资源占用。增加了对 *

*               各种常用变量用户类型的定义。               *

* -------------------------------------------------------- *

*[使用说明]                                              *

*            1、在使用该头文件之前先包含您需要使用的芯片   *

*               的官方头文件,例如:# include <iom8v.h>    *

*            2、如果你不是ICC的用户,请在使用该头文件之前*

*               先绑定各个端口的地址到相应的宏定义上,例如 *

*               # define PORTA (*((volatile char *)0x3b))*

*            3、如果你使用的是非ICC系统,请在调用文件之前*

*               定义宏 _USE_NON_ICC_IDE。                  *

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



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

*   系 统 宏 定 义*

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

#ifndef _USE_NON_ICC_IDE

    # include <macros.h>

#endif

/*------------------*

*   常 数 宏 定 义*

*------------------*/

# define PDIR_OUT            0x01

# define PDIR_IN             0x00

# define High                0x01

# define Low               0x00



/*------------------*

*   功 能 宏 定 义*

*------------------*/

# define SET_BITS_FORMAT(Addr)   (*((volatile PORTBIT *)&Addr))



/*---------兼容性宏定义-----------*/

# define PORTDefine();



/*------------------*

* 硬 件 连 接 定 义 *

*------------------*/



/*---------------- PORT口绑定 ---------------*/

#ifdef PORTA

    # define PORTA_BIT   SET_BITS_FORMAT(PORTA)

#endif

#ifdef PORTB

    # define PORTB_BIT   SET_BITS_FORMAT(PORTB)

#endif

#ifdef PORTC

    # define PORTC_BIT   SET_BITS_FORMAT(PORTC)

#endif

#ifdef PORTD

    # define PORTD_BIT   SET_BITS_FORMAT(PORTD)

#endif

#ifdef PORTE

    # define PORTE_BIT   SET_BITS_FORMAT(PORTE)

#endif

#ifdef PORTF

    # define PORTF_BIT   SET_BITS_FORMAT(PORTF)

#endif

#ifdef PORTG

    # define PORTG_BIT   SET_BITS_FORMAT(PORTG)

#endif



/*---------------- DDR 口绑定 ---------------*/

#ifdef DDRA

    # define DDRA_BIT   SET_BITS_FORMAT(DDRA)

#endif

#ifdef DDRB

    # define DDRB_BIT   SET_BITS_FORMAT(DDRB)

#endif

#ifdef DDRC

    # define DDRC_BIT   SET_BITS_FORMAT(DDRC)

#endif

#ifdef DDRE

    # define DDRE_BIT   SET_BITS_FORMAT(DDRE)

#endif

#ifdef DDRF

    # define DDRF_BIT   SET_BITS_FORMAT(DDRF)

#endif

#ifdef DDRG

    # define DDRG_BIT   SET_BITS_FORMAT(DDRG)

#endif



/*---------------- PIN 口绑定 ---------------*/

#ifdef PINA

    # define PINA_BIT   SET_BITS_FORMAT(PINA)

#endif

#ifdef PINB

    # define PINB_BIT   SET_BITS_FORMAT(PINB)

#endif

#ifdef PINC

    # define PINC_BIT   SET_BITS_FORMAT(PINC)

#endif

#ifdef PINE

    # define PINE_BIT   SET_BITS_FORMAT(PINE)

#endif

#ifdef PINF

    # define PINF_BIT   SET_BITS_FORMAT(PINF)

#endif

#ifdef PING

    # define PING_BIT   SET_BITS_FORMAT(PING)

#endif



/*------------------*

*   端 口 宏 定 义*

*------------------*/



/*-------------PORT位操作定义--------------*/

#ifdef PORTA

    # define _PA0      PORTA_BIT.Bit0

    # define _PA1      PORTA_BIT.Bit1

    # define _PA2      PORTA_BIT.Bit2

    # define _PA3      PORTA_BIT.Bit3

    # define _PA4      PORTA_BIT.Bit4

    # define _PA5      PORTA_BIT.Bit5

    # define _PA6      PORTA_BIT.Bit6

    # define _PA7      PORTA_BIT.Bit7

#endif

#ifdef PORTB

    # define _PB0      PORTB_BIT.Bit0

    # define _PB1      PORTB_BIT.Bit1

    # define _PB2      PORTB_BIT.Bit2

    # define _PB3      PORTB_BIT.Bit3

    # define _PB4      PORTB_BIT.Bit4

    # define _PB5      PORTB_BIT.Bit5

    # define _PB6      PORTB_BIT.Bit6

    # define _PB7      PORTB_BIT.Bit7

#endif

#ifdef PORTC

    # define _PC0      PORTC_BIT.Bit0

    # define _PC1      PORTC_BIT.Bit1

    # define _PC2      PORTC_BIT.Bit2

    # define _PC3      PORTC_BIT.Bit3

    # define _PC4      PORTC_BIT.Bit4

    # define _PC5      PORTC_BIT.Bit5

    # define _PC6      PORTC_BIT.Bit6

    # define _PC7      PORTC_BIT.Bit7

#endif

#ifdef PORTD

    # define _PD0      PORTD_BIT.Bit0

    # define _PD1      PORTD_BIT.Bit1

    # define _PD2      PORTD_BIT.Bit2

    # define _PD3      PORTD_BIT.Bit3

    # define _PD4      PORTD_BIT.Bit4

    # define _PD5      PORTD_BIT.Bit5

    # define _PD6      PORTD_BIT.Bit6

    # define _PD7      PORTD_BIT.Bit7

#endif

#ifdef PORTE

    # define _PE0      PORTE_BIT.Bit0

    # define _PE1      PORTE_BIT.Bit1

    # define _PE2      PORTE_BIT.Bit2

    # define _PE3      PORTE_BIT.Bit3

    # define _PE4      PORTE_BIT.Bit4

    # define _PE5      PORTE_BIT.Bit5

    # define _PE6      PORTE_BIT.Bit6

    # define _PE7      PORTE_BIT.Bit7

#endif

#ifdef PORTF

    # define _PF0      PORTF_BIT.Bit0

    # define _PF1      PORTF_BIT.Bit1

    # define _PF2      PORTF_BIT.Bit2

    # define _PF3      PORTF_BIT.Bit3

    # define _PF4      PORTF_BIT.Bit4

    # define _PF5      PORTF_BIT.Bit5

    # define _PF6      PORTF_BIT.Bit6

    # define _PF7      PORTF_BIT.Bit7

#endif

#ifdef PORTG

    # define _PG0      PORTG_BIT.Bit0

    # define _PG1      PORTG_BIT.Bit1

    # define _PG2      PORTG_BIT.Bit2

    # define _PG3      PORTG_BIT.Bit3

    # define _PG4      PORTG_BIT.Bit4

    # define _PG5      PORTG_BIT.Bit5

    # define _PG6      PORTG_BIT.Bit6

    # define _PG7      PORTG_BIT.Bit7

#endif



/*------------- DDR 位操作定义 -------------*/

#ifdef DDRA

    # define DDR_PA0   DDRA_BIT.Bit0

    # define DDR_PA1   DDRA_BIT.Bit1

    # define DDR_PA2   DDRA_BIT.Bit2

    # define DDR_PA3   DDRA_BIT.Bit3

    # define DDR_PA4   DDRA_BIT.Bit4

    # define DDR_PA5   DDRA_BIT.Bit5

    # define DDR_PA6   DDRA_BIT.Bit6

    # define DDR_PA7   DDRA_BIT.Bit7

#endif

#ifdef DDRB

    # define DDR_PB0   DDRB_BIT.Bit0

    # define DDR_PB1   DDRB_BIT.Bit1

    # define DDR_PB2   DDRB_BIT.Bit2

    # define DDR_PB3   DDRB_BIT.Bit3

    # define DDR_PB4   DDRB_BIT.Bit4

    # define DDR_PB5   DDRB_BIT.Bit5

    # define DDR_PB6   DDRB_BIT.Bit6

    # define DDR_PB7   DDRB_BIT.Bit7

#endif

#ifdef DDRC

    # define DDR_PC0   DDRC_BIT.Bit0

    # define DDR_PC1   DDRC_BIT.Bit1

    # define DDR_PC2   DDRC_BIT.Bit2

    # define DDR_PC3   DDRC_BIT.Bit3

    # define DDR_PC4   DDRC_BIT.Bit4

    # define DDR_PC5   DDRC_BIT.Bit5

    # define DDR_PC6   DDRC_BIT.Bit6

    # define DDR_PC7   DDRC_BIT.Bit7

#endif

#ifdef DDRD

    # define DDR_PD0   DDRD_BIT.Bit0

    # define DDR_PD1   DDRD_BIT.Bit1

    # define DDR_PD2   DDRD_BIT.Bit2

    # define DDR_PD3   DDRD_BIT.Bit3

    # define DDR_PD4   DDRD_BIT.Bit4

    # define DDR_PD5   DDRD_BIT.Bit5

    # define DDR_PD6   DDRD_BIT.Bit6

    # define DDR_PD7   DDRD_BIT.Bit7

#endif

#ifdef DDRE

    # define DDR_PE0   DDRE_BIT.Bit0

    # define DDR_PE1   DDRE_BIT.Bit1

    # define DDR_PE2   DDRE_BIT.Bit2

    # define DDR_PE3   DDRE_BIT.Bit3

    # define DDR_PE4   DDRE_BIT.Bit4

    # define DDR_PE5   DDRE_BIT.Bit5

    # define DDR_PE6   DDRE_BIT.Bit6

    # define DDR_PE7   DDRE_BIT.Bit7

#endif

#ifdef DDRF

    # define DDR_PF0   DDRF_BIT.Bit0

    # define DDR_PF1   DDRF_BIT.Bit1

    # define DDR_PF2   DDRF_BIT.Bit2

    # define DDR_PF3   DDRF_BIT.Bit3

    # define DDR_PF4   DDRF_BIT.Bit4

    # define DDR_PF5   DDRF_BIT.Bit5

    # define DDR_PF6   DDRF_BIT.Bit6

    # define DDR_PF7   DDRF_BIT.Bit7

#endif

#ifdef DDRG

    # define DDR_PG0   DDRG_BIT.Bit0

    # define DDR_PG1   DDRG_BIT.Bit1

    # define DDR_PG2   DDRG_BIT.Bit2

    # define DDR_PG3   DDRG_BIT.Bit3

    # define DDR_PG4   DDRG_BIT.Bit4

    # define DDR_PG5   DDRG_BIT.Bit5

    # define DDR_PG6   DDRG_BIT.Bit6

    # define DDR_PG7   DDRG_BIT.Bit7

#endif



/*------------- PIN 位操作定义 -------------*/

#ifdef PINA

    # define Read_PA0   PINA_BIT.Bit0

    # define Read_PA1   PINA_BIT.Bit1

    # define Read_PA2   PINA_BIT.Bit2

    # define Read_PA3   PINA_BIT.Bit3

    # define Read_PA4   PINA_BIT.Bit4

    # define Read_PA5   PINA_BIT.Bit5

    # define Read_PA6   PINA_BIT.Bit6

    # define Read_PA7   PINA_BIT.Bit7

#endif

#ifdef PINB

    # define Read_PB0   PINB_BIT.Bit0

    # define Read_PB1   PINB_BIT.Bit1

    # define Read_PB2   PINB_BIT.Bit2

    # define Read_PB3   PINB_BIT.Bit3

    # define Read_PB4   PINB_BIT.Bit4

    # define Read_PB5   PINB_BIT.Bit5

    # define Read_PB6   PINB_BIT.Bit6

    # define Read_PB7   PINB_BIT.Bit7

#endif

#ifdef PINC

    # define Read_PC0   PINC_BIT.Bit0

    # define Read_PC1   PINC_BIT.Bit1

    # define Read_PC2   PINC_BIT.Bit2

    # define Read_PC3   PINC_BIT.Bit3

    # define Read_PC4   PINC_BIT.Bit4

    # define Read_PC5   PINC_BIT.Bit5

    # define Read_PC6   PINC_BIT.Bit6

    # define Read_PC7   PINC_BIT.Bit7

#endif

#ifdef PIND

    # define Read_PD0   PIND_BIT.Bit0

    # define Read_PD1   PIND_BIT.Bit1

    # define Read_PD2   PIND_BIT.Bit2

    # define Read_PD3   PIND_BIT.Bit3

    # define Read_PD4   PIND_BIT.Bit4

    # define Read_PD5   PIND_BIT.Bit5

    # define Read_PD6   PIND_BIT.Bit6

    # define Read_PD7   PIND_BIT.Bit7

#endif

#ifdef PINE

    # define Read_PE0   PINE_BIT.Bit0

    # define Read_PE1   PINE_BIT.Bit1

    # define Read_PE2   PINE_BIT.Bit2

    # define Read_PE3   PINE_BIT.Bit3

    # define Read_PE4   PINE_BIT.Bit4

    # define Read_PE5   PINE_BIT.Bit5

    # define Read_PE6   PINE_BIT.Bit6

    # define Read_PE7   PINE_BIT.Bit7

#endif

#ifdef PINF

    # define Read_PF0   PINF_BIT.Bit0

    # define Read_PF1   PINF_BIT.Bit1

    # define Read_PF2   PINF_BIT.Bit2

    # define Read_PF3   PINF_BIT.Bit3

    # define Read_PF4   PINF_BIT.Bit4

    # define Read_PF5   PINF_BIT.Bit5

    # define Read_PF6   PINF_BIT.Bit6

    # define Read_PF7   PINF_BIT.Bit7

#endif

#ifdef PING

    # define Read_PG0   PING_BIT.Bit0

    # define Read_PG1   PING_BIT.Bit1

    # define Read_PG2   PING_BIT.Bit2

    # define Read_PG3   PING_BIT.Bit3

    # define Read_PG4   PING_BIT.Bit4

    # define Read_PG5   PING_BIT.Bit5

    # define Read_PG6   PING_BIT.Bit6

    # define Read_PG7   PING_BIT.Bit7

#endif



/*------------------*

*用户变量类型定义 *

*------------------*/

typedef unsigned int uint16;

typedef unsigned int UINT;

typedef unsigned int UINT16;

typedef unsigned int U16;

typedef unsigned int WORD;



typedef unsigned long uint32;

typedef unsigned long UINT32;

typedef unsigned long U32;

typedef unsigned long DWORD;



typedef char byte;

typedef char BYTE;

typedef char UINT8;

typedef char uint8;

typedef char U8;



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

* 结 构 体 定 义 区 *

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

typedef struct BYTE_BIT

{

    unsigned Bit0:1;

       unsigned Bit1:1;

    unsigned Bit2:1;

       unsigned Bit3:1;

    unsigned Bit4:1;

       unsigned Bit5:1;

    unsigned Bit6:1;

       unsigned Bit7:1;       

}PORTBIT;







#endif




-----此内容被Gorgon Meducer于2006-04-25,15:52:16编辑过

Gorgon_Meducer 发表于 2006-4-25 15:47:18

使用范例:

假设一套M8系统

那么应该按照以下的方法使用以上的头文件



# include <iom8v.h>

# include <RD_UseAVRPORTBit.h>



如果你之前使用过该头文件的前身版本,比方说RD_ATMega8L.h,那么,只需要按照上面的格式替换掉原来的

# include <RD_ATMega8L.h>

就可以了。



欢迎大家共同测试。

rainbow 发表于 2006-4-28 08:45:35

這們好啊!

有用。

收下了。

poqpoqpoq 发表于 2006-5-12 09:19:23

收下,谢谢楼主!很不错。



但好象缺乏PIND定义,可以插入以下代码:



#ifdef PIND

    # define PIND_BIT   SET_BITS_FORMAT(PIND)

#endif






-----此内容被poqpoqpoq于2006-05-12,09:22:54编辑过

alisha 发表于 2006-5-12 09:59:10

楼主出的都是经典啊!!

不顶怎么行!!!

flfihc 发表于 2006-5-12 10:11:11

顶,对楼主的敬仰犹如滔滔江水连绵不绝!!!

呵呵!

kanprin 发表于 2006-5-12 10:14:06

呵呵,很好的位操作典范。

帮忙顶一下了。

Gorgon_Meducer 发表于 2006-5-12 10:14:39

前面的版本出现重大错误,对不起大家,现在把最新的粘贴上来。

ifndef _USE_BITS_H_

#define _USE_BITS_H_

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

*函数库说明:系统位段操作函数库                        *

*版本:      v1.00                                       *

*作者:      傻孩子                                    *

*创建时间:2006年5月4日                              *

* -------------------------------------------------------- *

*修改:                                                *

*修改日期:                                              *

*版本:                                                *

* -------------------------------------------------------- *

* [版本历史]                                             *

*      v1.00   包含了从32位到8位对16位8位4位1位的各种映射*

*            允许通过功能宏定义中定义的宏拆分任意的地址*

* -------------------------------------------------------- *

* [使用说明]                                             *

*          1、该头文件属于基层头文件。                     *

*          2、兼容32位16位8位单片机系统。                  *

*          3、饱含该头文件以后,可以通过功能宏定义中定义的 *

*             宏拆分任意地址,也可以直接使用后面自定义的位 *

*             段类型定义新的位段变量或者指针。             *

*          4、需要注意的是,使用功能宏定义中的宏只是将指定 *

*             变量的地址强行邦定为指定类型的位段,并不真正 *

*             分配一个地址空间,如果指定类型的位段长度和地 *

*             址实际长度不相同将会导致不可预期的后果。   *

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



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

*   系 统 宏 定 义*

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



/*------------------*

*   常 数 宏 定 义*

*------------------*/



/*------------------*

*   功 能 宏 定 义*

*------------------*/

# define SET_BIT8_FORMAT(Addr)   (*((volatile BYTEBIT *)&Addr))

# define SET_BYTE_DIV_2(Addr)      (*((volatile BYTE2HBYTE *)&Addr))



# define SET_BIT16_FORMAT(Addr)    (*((volatile WORDBIT *)&Addr))

# define SET_WORD_DIV_2(Addr)      (*((volatile WORD2BYTE *)&Addr))

# define SET_WORD_DIV_4(Addr)      (*((volatile WORD2HBYTE *)&Addr))



# define SET_BIT32_FORMAT(Addr)    (*((volatile DWORDBIT *)&Addr))

# define SET_DWORD_DIV_2(Addr)   (*((volatile DWORD2WORD *)&Addr))

# define SET_DWORD_DIV_4(Addr)   (*((volatile DWORD2BYTE *)&Addr))

# define SET_DWORK_DIV_8(Addr)   (*((volatile DWORD2HBYTE *)&Addr))



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

* 结 构 体 定 义 区 *

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



/*------------------*

*   8位变量位映射   *

*------------------*/

typedef struct BYTE_BIT

{

    unsigned BIT0:1;

    unsigned BIT1:1;

    unsigned BIT2:1;

    unsigned BIT3:1;

    unsigned BIT4:1;

    unsigned BIT5:1;

    unsigned BIT6:1;

    unsigned BIT7:1;

}BYTEBIT;



/*------------------*

*   16位变量位映射*

*------------------*/

typedef struct WORD_BIT

{

    unsigned BIT0:1;

    unsigned BIT1:1;

    unsigned BIT2:1;

    unsigned BIT3:1;

    unsigned BIT4:1;

    unsigned BIT5:1;

    unsigned BIT6:1;

    unsigned BIT7:1;

   

    unsigned BIT8:1;

    unsigned BIT9:1;

    unsigned BIT10:1;

    unsigned BIT11:1;

    unsigned BIT12:1;

    unsigned BIT13:1;

    unsigned BIT14:1;

    unsigned BIT15:1;

}WORDBIT;



/*------------------*

*32位变量位映射   *

*------------------*/

typedef struct DWORD_BIT

{

    unsigned BIT0:1;

    unsigned BIT1:1;

    unsigned BIT2:1;

    unsigned BIT3:1;

    unsigned BIT4:1;

    unsigned BIT5:1;

    unsigned BIT6:1;

    unsigned BIT7:1;

   

    unsigned BIT8:1;

    unsigned BIT9:1;

    unsigned BIT10:1;

    unsigned BIT11:1;

    unsigned BIT12:1;

    unsigned BIT13:1;

    unsigned BIT14:1;

    unsigned BIT15:1;

   

    unsigned BIT16:1;

    unsigned BIT17:1;

    unsigned BIT18:1;

    unsigned BIT19:1;

    unsigned BIT20:1;

    unsigned BIT21:1;

    unsigned BIT22:1;

    unsigned BIT23:1;

   

    unsigned BIT24:1;

    unsigned BIT25:1;

    unsigned BIT26:1;

    unsigned BIT27:1;

    unsigned BIT28:1;

    unsigned BIT29:1;

    unsigned BIT30:1;

    unsigned BIT31:1;

}DWORDBIT;



/*------------------*

*32位变量4分映射*

*------------------*/

typedef struct DWORD_BYTE

{

    unsigned BYTEA:8;

    unsigned BYTEB:8;

    unsigned BYTEC:8;

    unsigned BYTED:8;

}DWORD2BYTE;



/*------------------*

*32位变量2分映射*

*------------------*/

typedef struct DWORD_WORD

{

    unsigned WORDA:16;

    unsigned WORDB:16;

}DWORD2WORD;



/*------------------*

*32位变量16分映射 *

*------------------*/

typedef struct DWORD_HBYTE

{

    unsigned BYTEAL:4;

                unsigned BYTEAH:4;

               

                unsigned BYTEBL:4;

                unsigned BYTEBH:4;

               

                unsigned BYTECL:4;

                unsigned BYTECH:4;

               

                unsigned BYTEDL:4;

                unsigned BYTEDH:4;

               

}DWORD2HBYTE;



/*------------------*

* 16位变量2分映射   *

*------------------*/

typedef struct WORD_BYTE

{

    unsigned BYTEA:8;

    unsigned BYTEB:8;

}WORD2BYTE;



/*------------------*

*16位变量4分映射*

*------------------*/

typedef struct WORD_HBYTE

{

    unsigned BYTEAL:4;

                unsigned BYTEAH:4;

               

                unsigned BYTEBL:4;

                unsigned BYTEBH:4;

}WORD2HBYTE;



/*------------------*

*8位变量2分映射   *

*------------------*/

typedef struct BYTE_DIV2

{

    unsigned BYTEAL:4;

    unsigned BYTEAH:4;

}BYTE2HBYTE;



#endif

Gorgon_Meducer 发表于 2006-5-12 10:15:13

#ifndef _USE_AVR_PORT_BIT_H_

#define _USE_AVR_PORT_BIT_H_

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

*函数库说明:AVR位操作通用支持库                         *

*版本:      v1.01                                       *

*作者:      傻孩子                                    *

*创建日期:2006年4月25日                               *

* -------------------------------------------------------- *

*[支持库]                                                *

*库名称:    RD_UseBITs.h                              *

*需要版本:v1.00 &abv                                  *

*函数库说明:系统位段操作函数库                        *

*                                                          *

*库名称:    RD_MacroAndConst.h                        *

*需要版本:v0.01 &abv                                  *

*函数库说明:系统常用宏定义库                            *

* -------------------------------------------------------- *

*修改:      傻孩子                                    *

*修改日期:2006年5月4日                              *

*版本:      v1.01                                       *

* -------------------------------------------------------- *

*[版本历史]                                              *

*   前身版本在该版本最终生成之前,经历过针对各种芯片的 *

*               专用版本,这些版本提供了针对PORT的位操作和 *

*               部分PIN、DDR的位操作。                     *

*      v1.00该版本支持几乎所有的芯片,具有完善的宏定义 *

*               系统,相对前身版本最小的资源占用。增加了对 *

*               各种常用变量用户类型的定义。               *

*      v1.01增加了关于布尔型变量类型的宏定义。         *

*               修改了头文件的位段映射模式。通过统一的头文 *

*               件RD_UseBITs.h来实现位段类型的定义和声明。 *

* -------------------------------------------------------- *

*[使用说明]                                              *

*            1、在使用该头文件之前先包含您需要使用的芯片   *

*               的官方头文件,例如:# include <iom8v.h>    *

*            2、如果你不是ICC的用户,请在使用该头文件之前*

*               先绑定各个端口的地址到相应的宏定义上,例如 *

*               # define PORTA (*((volatile char *)0x3b))*

*            3、如果你使用的是非ICC系统,请在调用文件之前*

*               定义宏 _USE_NON_ICC_IDE。                  *

*            4、可以使用SET_BITS_FORMAT(变量)把任意变量拆*

*               成位定义的模式。                           *

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

# include <RD_UseBITs.h>

# include <RD_MacroAndConst.h>

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

*   系 统 宏 定 义*

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



/*------------------*

*   常 数 宏 定 义*

*------------------*/

# define PDIR_OUT            0x01

# define PDIR_IN             0x00

# define High                0x01

# define Low               0x00



#ifndef True

    # define True            0x01

#endif

#ifndef False

    # define False         0x00

#endif



/*---------兼容性宏定义-----------*/

# define PORTDefine();



/*------------------*

* 硬 件 连 接 定 义 *

*------------------*/



/*---------------- PORT口绑定 ---------------*/

#ifdef PORTA

    # define PORTA_BIT   SET_BIT8_FORMAT(PORTA)

#endif

#ifdef PORTB

    # define PORTB_BIT   SET_BIT8_FORMAT(PORTB)

#endif

#ifdef PORTC

    # define PORTC_BIT   SET_BIT8_FORMAT(PORTC)

#endif

#ifdef PORTD

    # define PORTD_BIT   SET_BIT8_FORMAT(PORTD)

#endif

#ifdef PORTE

    # define PORTE_BIT   SET_BIT8_FORMAT(PORTE)

#endif

#ifdef PORTF

    # define PORTF_BIT   SET_BIT8_FORMAT(PORTF)

#endif

#ifdef PORTG

    # define PORTG_BIT   SET_BIT8_FORMAT(PORTG)

#endif



/*---------------- DDR 口绑定 ---------------*/

#ifdef DDRA

    # define DDRA_BIT   SET_BIT8_FORMAT(DDRA)

#endif

#ifdef DDRB

    # define DDRB_BIT   SET_BIT8_FORMAT(DDRB)

#endif

#ifdef DDRC

    # define DDRC_BIT   SET_BIT8_FORMAT(DDRC)

#endif

#ifdef DDRD

    # define DDRD_BIT   SET_BIT8_FORMAT(DDRD)

#endif

#ifdef DDRE

    # define DDRE_BIT   SET_BIT8_FORMAT(DDRE)

#endif

#ifdef DDRF

    # define DDRF_BIT   SET_BIT8_FORMAT(DDRF)

#endif

#ifdef DDRG

    # define DDRG_BIT   SET_BIT8_FORMAT(DDRG)

#endif



/*---------------- PIN 口绑定 ---------------*/

#ifdef PINA

    # define PINA_BIT   SET_BIT8_FORMAT(PINA)

#endif

#ifdef PINB

    # define PINB_BIT   SET_BIT8_FORMAT(PINB)

#endif

#ifdef PINC

    # define PINC_BIT   SET_BIT8_FORMAT(PINC)

#endif

#ifdef PIND

    # define PIND_BIT   SET_BIT8_FORMAT(PIND)

#endif

#ifdef PINE

    # define PINE_BIT   SET_BIT8_FORMAT(PINE)

#endif

#ifdef PINF

    # define PINF_BIT   SET_BIT8_FORMAT(PINF)

#endif

#ifdef PING

    # define PING_BIT   SET_BIT8_FORMAT(PING)

#endif



/*------------------*

*   端 口 宏 定 义*

*------------------*/



/*-------------PORT位操作定义--------------*/

#ifdef PORTA

    # define _PA0      PORTA_BIT.BIT0

    # define _PA1      PORTA_BIT.BIT1

    # define _PA2      PORTA_BIT.BIT2

    # define _PA3      PORTA_BIT.BIT3

    # define _PA4      PORTA_BIT.BIT4

    # define _PA5      PORTA_BIT.BIT5

    # define _PA6      PORTA_BIT.BIT6

    # define _PA7      PORTA_BIT.BIT7

#endif

#ifdef PORTB

    # define _PB0      PORTB_BIT.BIT0

    # define _PB1      PORTB_BIT.BIT1

    # define _PB2      PORTB_BIT.BIT2

    # define _PB3      PORTB_BIT.BIT3

    # define _PB4      PORTB_BIT.BIT4

    # define _PB5      PORTB_BIT.BIT5

    # define _PB6      PORTB_BIT.BIT6

    # define _PB7      PORTB_BIT.BIT7

#endif

#ifdef PORTC

    # define _PC0      PORTC_BIT.BIT0

    # define _PC1      PORTC_BIT.BIT1

    # define _PC2      PORTC_BIT.BIT2

    # define _PC3      PORTC_BIT.BIT3

    # define _PC4      PORTC_BIT.BIT4

    # define _PC5      PORTC_BIT.BIT5

    # define _PC6      PORTC_BIT.BIT6

    # define _PC7      PORTC_BIT.BIT7

#endif

#ifdef PORTD

    # define _PD0      PORTD_BIT.BIT0

    # define _PD1      PORTD_BIT.BIT1

    # define _PD2      PORTD_BIT.BIT2

    # define _PD3      PORTD_BIT.BIT3

    # define _PD4      PORTD_BIT.BIT4

    # define _PD5      PORTD_BIT.BIT5

    # define _PD6      PORTD_BIT.BIT6

    # define _PD7      PORTD_BIT.BIT7

#endif

#ifdef PORTE

    # define _PE0      PORTE_BIT.BIT0

    # define _PE1      PORTE_BIT.BIT1

    # define _PE2      PORTE_BIT.BIT2

    # define _PE3      PORTE_BIT.BIT3

    # define _PE4      PORTE_BIT.BIT4

    # define _PE5      PORTE_BIT.BIT5

    # define _PE6      PORTE_BIT.BIT6

    # define _PE7      PORTE_BIT.BIT7

#endif

#ifdef PORTF

    # define _PF0      PORTF_BIT.BIT0

    # define _PF1      PORTF_BIT.BIT1

    # define _PF2      PORTF_BIT.BIT2

    # define _PF3      PORTF_BIT.BIT3

    # define _PF4      PORTF_BIT.BIT4

    # define _PF5      PORTF_BIT.BIT5

    # define _PF6      PORTF_BIT.BIT6

    # define _PF7      PORTF_BIT.BIT7

#endif

#ifdef PORTG

    # define _PG0      PORTG_BIT.BIT0

    # define _PG1      PORTG_BIT.BIT1

    # define _PG2      PORTG_BIT.BIT2

    # define _PG3      PORTG_BIT.BIT3

    # define _PG4      PORTG_BIT.BIT4

    # define _PG5      PORTG_BIT.BIT5

    # define _PG6      PORTG_BIT.BIT6

    # define _PG7      PORTG_BIT.BIT7

#endif



/*------------- DDR 位操作定义 -------------*/

#ifdef DDRA

    # define DDR_PA0   DDRA_BIT.BIT0

    # define DDR_PA1   DDRA_BIT.BIT1

    # define DDR_PA2   DDRA_BIT.BIT2

    # define DDR_PA3   DDRA_BIT.BIT3

    # define DDR_PA4   DDRA_BIT.BIT4

    # define DDR_PA5   DDRA_BIT.BIT5

    # define DDR_PA6   DDRA_BIT.BIT6

    # define DDR_PA7   DDRA_BIT.BIT7

#endif

#ifdef DDRB

    # define DDR_PB0   DDRB_BIT.BIT0

    # define DDR_PB1   DDRB_BIT.BIT1

    # define DDR_PB2   DDRB_BIT.BIT2

    # define DDR_PB3   DDRB_BIT.BIT3

    # define DDR_PB4   DDRB_BIT.BIT4

    # define DDR_PB5   DDRB_BIT.BIT5

    # define DDR_PB6   DDRB_BIT.BIT6

    # define DDR_PB7   DDRB_BIT.BIT7

#endif

#ifdef DDRC

    # define DDR_PC0   DDRC_BIT.BIT0

    # define DDR_PC1   DDRC_BIT.BIT1

    # define DDR_PC2   DDRC_BIT.BIT2

    # define DDR_PC3   DDRC_BIT.BIT3

    # define DDR_PC4   DDRC_BIT.BIT4

    # define DDR_PC5   DDRC_BIT.BIT5

    # define DDR_PC6   DDRC_BIT.BIT6

    # define DDR_PC7   DDRC_BIT.BIT7

#endif

#ifdef DDRD

    # define DDR_PD0   DDRD_BIT.BIT0

    # define DDR_PD1   DDRD_BIT.BIT1

    # define DDR_PD2   DDRD_BIT.BIT2

    # define DDR_PD3   DDRD_BIT.BIT3

    # define DDR_PD4   DDRD_BIT.BIT4

    # define DDR_PD5   DDRD_BIT.BIT5

    # define DDR_PD6   DDRD_BIT.BIT6

    # define DDR_PD7   DDRD_BIT.BIT7

#endif

#ifdef DDRE

    # define DDR_PE0   DDRE_BIT.BIT0

    # define DDR_PE1   DDRE_BIT.BIT1

    # define DDR_PE2   DDRE_BIT.BIT2

    # define DDR_PE3   DDRE_BIT.BIT3

    # define DDR_PE4   DDRE_BIT.BIT4

    # define DDR_PE5   DDRE_BIT.BIT5

    # define DDR_PE6   DDRE_BIT.BIT6

    # define DDR_PE7   DDRE_BIT.BIT7

#endif

#ifdef DDRF

    # define DDR_PF0   DDRF_BIT.BIT0

    # define DDR_PF1   DDRF_BIT.BIT1

    # define DDR_PF2   DDRF_BIT.BIT2

    # define DDR_PF3   DDRF_BIT.BIT3

    # define DDR_PF4   DDRF_BIT.BIT4

    # define DDR_PF5   DDRF_BIT.BIT5

    # define DDR_PF6   DDRF_BIT.BIT6

    # define DDR_PF7   DDRF_BIT.BIT7

#endif

#ifdef DDRG

    # define DDR_PG0   DDRG_BIT.BIT0

    # define DDR_PG1   DDRG_BIT.BIT1

    # define DDR_PG2   DDRG_BIT.BIT2

    # define DDR_PG3   DDRG_BIT.BIT3

    # define DDR_PG4   DDRG_BIT.BIT4

    # define DDR_PG5   DDRG_BIT.BIT5

    # define DDR_PG6   DDRG_BIT.BIT6

    # define DDR_PG7   DDRG_BIT.BIT7

#endif



/*------------- PIN 位操作定义 -------------*/

#ifdef PINA

    # define Read_PA0   PINA_BIT.BIT0

    # define Read_PA1   PINA_BIT.BIT1

    # define Read_PA2   PINA_BIT.BIT2

    # define Read_PA3   PINA_BIT.BIT3

    # define Read_PA4   PINA_BIT.BIT4

    # define Read_PA5   PINA_BIT.BIT5

    # define Read_PA6   PINA_BIT.BIT6

    # define Read_PA7   PINA_BIT.BIT7

#endif

#ifdef PINB

    # define Read_PB0   PINB_BIT.BIT0

    # define Read_PB1   PINB_BIT.BIT1

    # define Read_PB2   PINB_BIT.BIT2

    # define Read_PB3   PINB_BIT.BIT3

    # define Read_PB4   PINB_BIT.BIT4

    # define Read_PB5   PINB_BIT.BIT5

    # define Read_PB6   PINB_BIT.BIT6

    # define Read_PB7   PINB_BIT.BIT7

#endif

#ifdef PINC

    # define Read_PC0   PINC_BIT.BIT0

    # define Read_PC1   PINC_BIT.BIT1

    # define Read_PC2   PINC_BIT.BIT2

    # define Read_PC3   PINC_BIT.BIT3

    # define Read_PC4   PINC_BIT.BIT4

    # define Read_PC5   PINC_BIT.BIT5

    # define Read_PC6   PINC_BIT.BIT6

    # define Read_PC7   PINC_BIT.BIT7

#endif

#ifdef PIND

    # define Read_PD0   PIND_BIT.BIT0

    # define Read_PD1   PIND_BIT.BIT1

    # define Read_PD2   PIND_BIT.BIT2

    # define Read_PD3   PIND_BIT.BIT3

    # define Read_PD4   PIND_BIT.BIT4

    # define Read_PD5   PIND_BIT.BIT5

    # define Read_PD6   PIND_BIT.BIT6

    # define Read_PD7   PIND_BIT.BIT7

#endif

#ifdef PINE

    # define Read_PE0   PINE_BIT.BIT0

    # define Read_PE1   PINE_BIT.BIT1

    # define Read_PE2   PINE_BIT.BIT2

    # define Read_PE3   PINE_BIT.BIT3

    # define Read_PE4   PINE_BIT.BIT4

    # define Read_PE5   PINE_BIT.BIT5

    # define Read_PE6   PINE_BIT.BIT6

    # define Read_PE7   PINE_BIT.BIT7

#endif

#ifdef PINF

    # define Read_PF0   PINF_BIT.BIT0

    # define Read_PF1   PINF_BIT.BIT1

    # define Read_PF2   PINF_BIT.BIT2

    # define Read_PF3   PINF_BIT.BIT3

    # define Read_PF4   PINF_BIT.BIT4

    # define Read_PF5   PINF_BIT.BIT5

    # define Read_PF6   PINF_BIT.BIT6

    # define Read_PF7   PINF_BIT.BIT7

#endif

#ifdef PING

    # define Read_PG0   PING_BIT.BIT0

    # define Read_PG1   PING_BIT.BIT1

    # define Read_PG2   PING_BIT.BIT2

    # define Read_PG3   PING_BIT.BIT3

    # define Read_PG4   PING_BIT.BIT4

    # define Read_PG5   PING_BIT.BIT5

    # define Read_PG6   PING_BIT.BIT6

    # define Read_PG7   PING_BIT.BIT7

#endif



#endif

Gorgon_Meducer 发表于 2006-5-12 10:15:46

#ifndef _USE_MACRO_AND_CONST_H_

#define _USE_MACRO_AND_CONST_H_

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

*函数库说明:系统常用宏定义库                            *

*版本:      v0.01                                       *

*作者:      傻孩子                                    *

*创建日期:2006年5月4日                              *

* -------------------------------------------------------- *

*修改:                                                *

*修改日期:                                              *

*版本:                                                *

* -------------------------------------------------------- *

*[版本历史]                                              *

* -------------------------------------------------------- *

*[使用说明]                                              *

*         1、该库文件属于基础库文件。                  *

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



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

*   系 统 宏 定 义*

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

#ifndef _USE_NON_ICC_IDE

    # include <macros.h>

#endif



/*------------------*

*   常 数 宏 定 义*

*------------------*/

# define True    0x01

# define False   0x00

# define High    0x01

# define Low   0x00



# define Null    (&NULL)         



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

* 全 局 变 量 声 明 *

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

const unsigned long NULL = 0;



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

*用户变量类型定义 *

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

typedef unsigned int uint16;

typedef unsigned int UINT;

typedef unsigned int UINT16;

typedef unsigned int U16;

typedef unsigned int WORD;



typedef unsigned long uint32;

typedef unsigned long UINT32;

typedef unsigned long U32;

typedef unsigned long DWORD;



typedef char byte;

typedef char BYTE;

typedef char UINT8;

typedef char uint8;

typedef char U8;

typedef char BOOL;



#endif

alisha 发表于 2006-5-17 16:16:57

楼主,OURAVR之大家典范啊!

能多传点通用库文件就好了。



问一句,俺想要个TWI双机通讯的程序,自己折腾了4天了,也总是FAIL,都不知道从哪下手查了,看DATASHEET都有点麻木了。

fsclub 发表于 2006-5-17 16:34:41

谢谢共享!!

yerdak 发表于 2006-5-17 21:13:44

#define BIT(x)        (1 << (x))

#define SETBIT(x, y)              (x |= y)

#define CLRBIT(x, y)            (x &= ~y)

#define CHKBIT(x, y)            (x & y)

#define BIT7 0x80

#define BIT6 0x40

#define BIT5 0x20

#define BIT4 0x10

#define BIT3 0x08

#define BIT2 0x04

#define BIT1 0x02

#define BIT0 0x01





我是这样干的,也来献丑一下。使用时就 set(portb,bit7)

楼主的比我这强多了,佩服。

Gorgon_Meducer 发表于 2006-5-17 22:48:41

呵呵,楼上的不要这样说哈。你的方法代码效率要高一点——当然牺牲了一点灵活性。

wuwy0168 发表于 2007-5-31 14:50:39

俺是这样的

#define BIT(x)   (1 << (x))

#define sbi(x, y) (x |= BIT(y))

#define cbi(x, y) (x &= ~BIT(y))

guantingwei 发表于 2007-5-31 18:15:49

可以参考飞思卡尔的位定义

也是在头文件中定义的结构与联合

操作很方便

xiejun 发表于 2007-6-1 08:17:45

我用在KEIL C中,好像不行。原因可能是因为端口的地址和RAM的地址相同了,反正我用来操作,看到的是RAM在变化,IO口倒不能。如果能操作就好了,以后程序移植就省好多事了。

lihuyong 发表于 2007-9-24 11:08:00

留个脚印,仔细研究

Chenxg 发表于 2008-3-15 16:54:11

顶,对楼主的敬仰犹如滔滔江水连绵不绝!!!

TTLife 发表于 2008-3-15 17:00:19

同17楼。。。。。

spy2008 发表于 2008-3-15 17:07:36

仔细看看

classv 发表于 2008-3-19 16:40:33

同意15楼的,瑞萨的也是那样定义的,按位或字节访问都很方便,如P1.PORT.BIT.B7=1跟P1.PORT.BYTE|=(1<<7)是一样的.

classv 发表于 2008-3-19 16:45:07

用结构提的方法在AVR里试过一次,好象不太行,高低位刚好相反,按BIT的调整过来,按BYTE的有反了

manbkk 发表于 2008-5-17 22:42:04

谢谢!留个脚印.

zds_share 发表于 2008-6-10 22:26:22

mark

Gorgon_Meducer 发表于 2008-6-10 23:12:47

此帖为古董帖,请大家不要再挖古墓。
该头文件的最新版本在我的专栏里面已经置顶了……
页: [1]
查看完整版本: [古董贴][共享][沉底]最新的通用C语言位操作支持库