AVR口宏定义.doc

上传人:李医生 文档编号:8938052 上传时间:2021-01-26 格式:DOC 页数:8 大小:29.50KB
返回 下载 相关 举报
AVR口宏定义.doc_第1页
第1页 / 共8页
AVR口宏定义.doc_第2页
第2页 / 共8页
AVR口宏定义.doc_第3页
第3页 / 共8页
AVR口宏定义.doc_第4页
第4页 / 共8页
AVR口宏定义.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《AVR口宏定义.doc》由会员分享,可在线阅读,更多相关《AVR口宏定义.doc(8页珍藏版)》请在三一文库上搜索。

1、重点讲述avr单片机的io口操作。很多朋友都是由51单片机走向嵌入式系统,经历了51-43051-pic51-avr,这样一些转换,本人粗略学了51,后直接专攻avr,为此有一些心得,和一些雕虫小技的小伎俩,希望能抛砖引玉,引发同行反思,在工作中提供举一反三后的便利。 想像51一样,在winavr中直接写上sbitKEY1=p11, 然后用下列语句扫描键盘吗? if(KEY1=0)keyval=1; 请看下面的宏定义,其中位段的手法来源于网络,本人纯属借鉴。之后的#,宏链接符,纯属自创, 各位先行使用以下宏后,有问题直接联系本人qq21332560讨论:注明验证信息:io口 /定义新的数据类型

2、,方便进行IO端口操作。 #ifndef _bit_h#define _bit_htypedef struct /定义一个8字节的位段 bit07是每个位段名称 1代表一位 PBIT就是整个位段的名称 unsigned bit0 : 1 ; unsigned bit1 : 1 ; unsigned bit2 : 1 ; unsigned bit3 : 1 ; unsigned bit4 : 1 ; unsigned bit5 : 1 ; unsigned bit6 : 1 ; unsigned bit7 : 1 ; PBIT;#define PORTABIT (*(volatile PBIT

3、*)0x3B)/*0x3B是PORTA的地址 (volatile PBIT *)0x3B 就是将这个地址强制类型装换为我们前面定义过的位段volatile关键字还不懂的话去查点资料前面加个*号 不用我说也知道什么意思吧*/#define DDRABIT (*(volatile PBIT *)0x3A) #define PINABIT (*(volatile PBIT *)0x39)#define PORTBBIT (*(volatile PBIT *)0x38) #define DDRBBIT (*(volatile PBIT *)0x37) #define PINBBIT (*(volati

4、le PBIT *)0x36)#define PORTCBIT (*(volatile PBIT *)0x35) #define DDRCBIT (*(volatile PBIT *)0x34) #define PINCBIT (*(volatile PBIT *)0x33)#define PORTDBIT (*(volatile PBIT *)0x32) #define DDRDBIT (*(volatile PBIT *)0x31) #define PINDBIT (*(volatile PBIT *)0x30)/*使用方法:要将PORTA的第一位输出为高电平DDRABIT.bit0=1;

5、PORTABIT.bit0=1;或者为了便于程序的移植我们一般用宏定义#define power PORTABIT.bit0power=1;#define power_out() DDRABIT.bit0=1*/当然PORTABIT.bit0敲起来有点长 可以用宏定义把他改小一点#define PORTABIT0 PORTABIT.bit0/可以改成自己看的习惯的 但不要与系统头文件中定义过的冲突 比如PA0 PORTA0#define PORTABIT1 PORTABIT.bit1#define PORTABIT2 PORTABIT.bit2#define PORTABIT3 PORTABI

6、T.bit3#define PORTABIT4 PORTABIT.bit4#define PORTABIT5 PORTABIT.bit5#define PORTABIT6 PORTABIT.bit6#define PORTABIT7 PORTABIT.bit7#define PORTBBIT0 PORTBBIT.bit0#define PORTBBIT1 PORTBBIT.bit1#define PORTBBIT2 PORTBBIT.bit2#define PORTBBIT3 PORTBBIT.bit3#define PORTBBIT4 PORTBBIT.bit4#define PORTBBI

7、T5 PORTBBIT.bit5#define PORTBBIT6 PORTBBIT.bit6#define PORTBBIT7 PORTBBIT.bit7#define PORTCBIT0 PORTCBIT.bit0#define PORTCBIT1 PORTCBIT.bit1#define PORTCBIT2 PORTCBIT.bit2#define PORTCBIT3 PORTCBIT.bit3#define PORTCBIT4 PORTCBIT.bit4#define PORTCBIT5 PORTCBIT.bit5#define PORTCBIT6 PORTCBIT.bit6#defi

8、ne PORTCBIT7 PORTCBIT.bit7#define PORTDBIT0 PORTDBIT.bit0#define PORTDBIT1 PORTDBIT.bit1#define PORTDBIT2 PORTDBIT.bit2#define PORTDBIT3 PORTDBIT.bit3#define PORTDBIT4 PORTDBIT.bit4#define PORTDBIT5 PORTDBIT.bit5#define PORTDBIT6 PORTDBIT.bit6#define PORTDBIT7 PORTDBIT.bit7/*#define DDRABIT0 DDRABIT

9、.bit0#define DDRABIT1 DDRABIT.bit1#define DDRABIT2 DDRABIT.bit2#define DDRABIT3 DDRABIT.bit3#define DDRABIT4 DDRABIT.bit4#define DDRABIT5 DDRABIT.bit5#define DDRABIT6 DDRABIT.bit6#define DDRABIT7 DDRABIT.bit7#define DDRBBIT0 DDRBBIT.bit0#define DDRBBIT1 DDRBBIT.bit1#define DDRBBIT2 DDRBBIT.bit2#defi

10、ne DDRBBIT3 DDRBBIT.bit3#define DDRBBIT4 DDRBBIT.bit4#define DDRBBIT5 DDRBBIT.bit5#define DDRBBIT6 DDRBBIT.bit6#define DDRBBIT7 DDRBBIT.bit7#define DDRCBIT0 DDRCBIT.bit0#define DDRCBIT1 DDRCBIT.bit1#define DDRCBIT2 DDRCBIT.bit2#define DDRCBIT3 DDRCBIT.bit3#define DDRCBIT4 DDRCBIT.bit4#define DDRCBIT

11、5 DDRCBIT.bit5#define DDRCBIT6 DDRCBIT.bit6#define DDRCBIT7 DDRCBIT.bit7#define DDRDBIT0 DDRDBIT.bit0#define DDRDBIT1 DDRDBIT.bit1#define DDRDBIT2 DDRDBIT.bit2#define DDRDBIT3 DDRDBIT.bit3#define DDRDBIT4 DDRDBIT.bit4#define DDRDBIT5 DDRDBIT.bit5#define DDRDBIT6 DDRDBIT.bit6#define DDRDBIT7 DDRDBIT.

12、bit7/*#define PINABIT0 PINABIT.bit0#define PINABIT1 PINABIT.bit1#define PINABIT2 PINABIT.bit2#define PINABIT3 PINABIT.bit3#define PINABIT4 PINABIT.bit4#define PINABIT5 PINABIT.bit5#define PINABIT6 PINABIT.bit6#define PINABIT7 PINABIT.bit7#define PINBBIT0 PINBBIT.bit0#define PINBBIT1 PINBBIT.bit1#def

13、ine PINBBIT2 PINBBIT.bit2#define PINBBIT3 PINBBIT.bit3#define PINBBIT4 PINBBIT.bit4#define PINBBIT5 PINBBIT.bit5#define PINBBIT6 PINBBIT.bit6#define PINBBIT7 PINBBIT.bit7#define PINCBIT0 PINCBIT.bit0#define PINCBIT1 PINCBIT.bit1#define PINCBIT2 PINCBIT.bit2#define PINCBIT3 PINCBIT.bit3#define PINCBI

14、T4 PINCBIT.bit4#define PINCBIT5 PINCBIT.bit5#define PINCBIT6 PINCBIT.bit6#define PINCBIT7 PINCBIT.bit7#define PINDBIT0 PINDBIT.bit0#define PINDBIT1 PINDBIT.bit1#define PINDBIT2 PINDBIT.bit2#define PINDBIT3 PINDBIT.bit3#define PINDBIT4 PINDBIT.bit4#define PINDBIT5 PINDBIT.bit5#define PINDBIT6 PINDBIT

15、.bit6#define PINDBIT7 PINDBIT.bit7#endif/*-*/ICC-AVR application builder :/ Target : M8/ Crystal: 1.000Mhz/*-*/*-*/防止重复定义#ifndef _ICCAVRIO_H_#define _ICCAVRIO_H_/*-*/定义新的数据类型,方便进行IO端口操作。/定义一个8字节的位域 bit07是每个位域名称, 1代表一位, bit_field就是整个位域的名称typedef struct unsigned bit0: 1 ; unsigned bit1: 1 ; unsigned b

16、it2: 1 ; unsigned bit3: 1 ; unsigned bit4: 1 ; unsigned bit5: 1 ; unsigned bit6: 1 ; unsigned bit7: 1 ;bit_field;#define GET_BIT(adr) (*( volatile bit_field * )(&adr)/*-*/设置是否自动初始化IO方向寄存器/自动初始化IO方向寄存器无需在初始化程序中用PORTA=0X.;形式来初始化io控制寄存器,/同时也不争的证明了avr单片机的端口输入/出切换功能#define AUTOINIT 1/ 0 /手动配置IO方向寄存器/ 1 /

17、自动配置IO方向寄存器/*-*/输出端口设置#if (AUTOINIT = 1) /自动初始化IO方向寄存器#define PORT(m, n) GET_BIT(DDR#m).bit#n = 1; GET_BIT(PORT#m).bit#n#define P_OUT(m, n) DDR#m |= n; PORT#m#else /需手动配置IO方向寄存器#define PORT(m, n) GET_BIT(PORT#m).bit#n#endif/*-*/输入端口设置(检测输入端口)#if (AUTOINIT = 1) /自动初始化IO方向寄存器#define PIN(m, n) (!(GET_B

18、IT(DDR#m).bit#n = 0) & (GET_BIT(PORT#m).bit#n = 1) & GET_BIT(PIN#m).bit#n)#define P_IN(m, n) (DDR#m &= n, PORT#m = n, PIN#m & n)#else /需手动配置IO方向寄存器#define PIN(m, n) GET_BIT(PIN#m).bit#n#endif/*-*/端口输入输出方向设置/方便直观操作 自由设定单个io口的方向#define DRA(n) GET_BIT(DDRA).bit#n #define DRB(n) GET_BIT(DDRB).bit#n#defi

19、ne DRC(n) GET_BIT(DDRC).bit#n#define DRD(n) GET_BIT(DDRD).bit#n#define DDR(m, n) GET_BIT(DDR#m).bit#n/*-*/#endif/方便直观操作自由设定单个io口的方向 #defineDRA(n)GET_BIT(DDRA).bit#n #defineDRB(n)GET_BIT(DDRB).bit#n #defineDRC(n)GET_BIT(DDRC).bit#n #defineDRD(n)GET_BIT(DDRD).bit#n #defineDDR(m,n)GET_BIT(DDR#m).bit#n

20、在我们实际项目中,需要用到按键输入,继电器,SPI器件输出, 两者分别为输入,和输出之用,这时候可以方面的在各自c文件对应的.h文件中写下如下语句: #defineKEY1PIN(C,3)/定义三个按键,使能上拉 #defineKEY2PIN(C,4) #defineKEY3PIN(C,5) #defineSCLK_SPIPORT(B,5)/定义spi口的两个控制引脚 #defineCS_SPIPORT(C,0) 上述PINPORT自动化定义的方法中,有些不足,如:在DS18B20这样的应用中,需要切换引脚的输入输出,就必须为18B20的引脚安排两套定义, 类似于:#defineDS18B20

21、_INPIN(A,1) #defineDS18B20_OUTPORT(A,1) 此外:PORT和PIN的自动化定义中,含有DDR的操作,凡是用到PIN和PORT定义过的端口的地方都需要重复DDR操作,带来冗余代码。 希望朋友们能够用得上#definePIN(m,n) (!(u08)(GET_BIT(DDR#m).bit#n=0)&(u08)(GET_BIT(PORT#m).bit#n=1)&GET_BIT(PIN#m).bit#n) 清零DDR_BIT口, 读DDR_BIT口,为0,继续,为1,返回0? 置一PORT_BIT, 读PORT_BIT,为1,继续,为0,返回0? 读PIN_BIT,

22、为1返回1,为0返回0, 这是干什么? #defineDS18B20_INPIN(A,1) #defineDS18B20_OUTPORT(A,1) if(DS18B20_IN) DS18B20_OUT=1; else DS18B20_OUT=0; if(DS18B20_IN) 72: d198 cbi 0x1a,1 ;26 74: d199 sbic 0x1a,1 ;26 76: 06c0 rjmp .+12 ;0x84 78: d99a sbi 0x1b,1 ;27 7a: c99b sbis 0x19,1 ;25 7c: 03c0 rjmp .+6 ;0x84 DS18B20_OUT=1;

23、 7e: d19a sbi 0x1a,1 ;26 80: d99a sbi 0x1b,1 ;27 82: 0895 ret else DS18B20_OUT=0; 84: d19a sbi 0x1a,1 ;26 86: d998 cbi 0x1b,1 ;27 个人感觉可读性,可移植性,效率并不好。 没必要PORT与PIN,DDR一起联动,PORT与PIN,DDR分开操作反而更好。 另外,IO口输出,应该是先操作PORT,再操作DDR, 这比先操作DDR,再操作PORT要好。 个人用法: #defineDS18B20_DDRIO_BIT_ALIAS(&DDRA,1) #defineDS18B20

24、_INIO_BIT_ALIAS(&PINA,1) #defineDS18B20_OUTIO_BIT_ALIAS(&PORTA,1) DS18B20_DDR=0; 72: d198 cbi 0x1a,1 ;26 if(DS18B20_IN) 74: c99b sbis 0x19,1 ;25 76: 02c0 rjmp .+4 ;0x7c DS18B20_OUT=1; 78: d99a sbi 0x1b,1 ;27 7a: 01c0 rjmp .+2 ;0x7e DS18B20_DDR=1; else DS18B20_OUT=0; 7c: d998 cbi 0x1b,1 ;27 DS18B20_DDR=1; 7e: d19a sbi 0x1a,1 ;26

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1