本帖最后由 CC2530 于 2011-5-25 20:29 编辑
IAR AVR编译
C++:#include <intrinsics.h>
#include <stddef.h>
#include <ioavr.h>
#ifndef _BV
#define _BV(A) (1U<<(A))
#endif
#ifndef __always_inline__
#define __always_inline__ _Pragma("inline=forced") inline
#endif
typedef unsigned char uint8;
#define __AVR_GPIO_ASSIGN(A, B, C) \
__always_inline__ static void Set(void) {PORT##B |= _BV(C);}\
__always_inline__ static void Clr(void) {PORT##B &= ~_BV(C);}\
__always_inline__ static void MakeOut(void) {DDR##B |= _BV(C);}\
__always_inline__ static void MakeIn(void) {DDR##B &= ~_BV(C);}\
__always_inline__ static void MakeOutHigh(void) {Set(); MakeOut();}\
__always_inline__ static void MakeOutLow(void) {Clr(); MakeOut();}\
__always_inline__ static void MakeInHighImpedance(void) {DDR##B &= ~_BV(C); PORT##B &= ~_BV(C);}\
__always_inline__ static void MakeInPullUp(void) {DDR##B &= ~_BV(C); PORT##B |= _BV(C);}\
__always_inline__ static void Toggle(void) {PORT##B ^= _BV(C);}\
__always_inline__ static uint8 ReadIn(void) {return ((PIN##B & _BV(C)) != 0);}\
__always_inline__ static uint8 ReadOut(void) {return ((PORT##B & _BV(C)) != 0);}\
__always_inline__ static uint8 ReadDir(void) {return ((DDR##B & _BV(C)) != 0);}\
__always_inline__ static uint8 Read(void) {return ReadIn();}\
__always_inline__ static void Out(uint8 x) {(x)?(Set())Clr());}\
__always_inline__ static uint8 IsOutHigh(void) {return ReadOut();}\
__always_inline__ static uint8 IsOutLow(void) {return !ReadOut();}\
#define __AVR_GPIO_ASSIGN_POLARITY(A, B, C,X) \
__AVR_GPIO_ASSIGN(A, B, C)\
__always_inline__ static void On(void) { (X)?(Set())Clr());}\
__always_inline__ static void Off(void) {(!X)?(Set())Clr());}\
__always_inline__ static void MakeOutOn(void) {On();MakeOut();}\
__always_inline__ static void MakeOutOff(void) {Off();MakeOut();}\
__always_inline__ static uint8 IsOn(void) {return (X)?IsOutHigh():IsOutLow();}\
__always_inline__ static uint8 IsOff(void) {return (!X)?IsOutHigh():IsOutLow();}
#define AVR_GPIO_ASSIGN(A, B, C) \
static class class##A{\
public:\
__AVR_GPIO_ASSIGN(A, B, C)\
} A;
#define AVR_GPIO_ASSIGN_POLARITY(A, B, C,X) \
static class class##A{\
public:\
__AVR_GPIO_ASSIGN_POLARITY(A, B, C,X)\
} A;
AVR_GPIO_ASSIGN_POLARITY(LED_0,D,0,1); //LED_0D0:高电平亮
AVR_GPIO_ASSIGN_POLARITY(LED_1,D,1,0); //LED_1D1:低电平亮
AVR_GPIO_ASSIGN(LED_2,D,2); //LED_2D2
volatile uint8 read;
void test(void)
{
LED_0.MakeOut(); //LED_0设置为输出
LED_0.Set(); //LED_0输出高
LED_0.Clr(); //LED_0输出低
LED_0.Toggle(); //LED_0翻转
LED_0.On(); //LED_0亮
LED_0.Off(); //LED_0灭
(&LED_0)->MakeOut(); //LED_0设置为输出
(&LED_0)->Set(); //LED_0输出高
(&LED_0)->Clr(); //LED_0输出低
(&LED_0)->Toggle(); //LED_0翻转
(&LED_0)->On(); //LED_0亮
(&LED_0)->Off(); //LED_0灭
LED_1.MakeOut(); //LED_1设置为输出
LED_1.Set(); //LED_1输出高
LED_1.Clr(); //LED_1输出低
LED_1.Toggle(); //LED_1翻转
LED_1.On(); //LED_1亮
LED_1.Off(); //LED_1灭
(&LED_1)->MakeOut(); //LED_1设置为输出
(&LED_1)->Set(); //LED_1输出高
(&LED_1)->Clr(); //LED_1输出低
(&LED_1)->Toggle(); //LED_1翻转
(&LED_1)->On(); //LED_1亮
(&LED_1)->Off(); //LED_1灭
LED_2.MakeInHighImpedance(); //LED_2设置为高阻输入
LED_2.MakeInPullUp(); //LED_2设置为上拉输入
LED_2.MakeOutHigh(); //LED_2设置为输出高
LED_2.MakeOutLow(); //LED_2设置为上拉输入
read=LED_2.ReadIn(); //读取LED_2引脚点品
read=LED_2.ReadDir(); //读取LED_2方向
read=LED_2.ReadOut(); //如果LED_2端口寄存器
(&LED_2)->MakeInHighImpedance(); //LED_2设置为高阻输入
(&LED_2)->MakeInPullUp(); //LED_2设置为上拉输入
(&LED_2)->MakeOutHigh(); //LED_2设置为输出高
(&LED_2)->MakeOutLow(); //LED_2设置为上拉输入
read=(&LED_2)->ReadIn(); //读取LED_2引脚点品
read=(&LED_2)->ReadDir(); //读取LED_2方向
read=(&LED_2)->ReadOut(); //如果LED_2端口寄存器
}
汇编: RSEG CODE:CODE:NOROOT(1)
// 65 void test(void)
??test:
// 66 {
// 67 LED_0.MakeOut(); //LED_0设置为输出
SBI 0x11, 0x00
// 68 LED_0.Set(); //LED_0输出高
SBI 0x12, 0x00
// 69 LED_0.Clr(); //LED_0输出低
CBI 0x12, 0x00
// 70 LED_0.Toggle(); //LED_0翻转
LDI R16, 1
IN R17, 0x12
EOR R17, R16
OUT 0x12, R17
// 71 LED_0.On(); //LED_0亮
SBI 0x12, 0x00
// 72 LED_0.Off(); //LED_0灭
CBI 0x12, 0x00
// 73
// 74 (&LED_0)->MakeOut(); //LED_0设置为输出
SBI 0x11, 0x00
// 75 (&LED_0)->Set(); //LED_0输出高
SBI 0x12, 0x00
// 76 (&LED_0)->Clr(); //LED_0输出低
CBI 0x12, 0x00
// 77 (&LED_0)->Toggle(); //LED_0翻转
IN R17, 0x12
EOR R17, R16
OUT 0x12, R17
// 78 (&LED_0)->On(); //LED_0亮
SBI 0x12, 0x00
// 79 (&LED_0)->Off(); //LED_0灭
CBI 0x12, 0x00
// 80
// 81
// 82 LED_1.MakeOut(); //LED_1设置为输出
SBI 0x11, 0x01
// 83 LED_1.Set(); //LED_1输出高
SBI 0x12, 0x01
// 84 LED_1.Clr(); //LED_1输出低
CBI 0x12, 0x01
// 85 LED_1.Toggle(); //LED_1翻转
LDI R16, 2
IN R17, 0x12
EOR R17, R16
OUT 0x12, R17
// 86 LED_1.On(); //LED_1亮
CBI 0x12, 0x01
// 87 LED_1.Off(); //LED_1灭
SBI 0x12, 0x01
// 88
// 89 (&LED_1)->MakeOut(); //LED_1设置为输出
SBI 0x11, 0x01
// 90 (&LED_1)->Set(); //LED_1输出高
SBI 0x12, 0x01
// 91 (&LED_1)->Clr(); //LED_1输出低
CBI 0x12, 0x01
// 92 (&LED_1)->Toggle(); //LED_1翻转
IN R17, 0x12
EOR R17, R16
OUT 0x12, R17
// 93 (&LED_1)->On(); //LED_1亮
CBI 0x12, 0x01
// 94 (&LED_1)->Off(); //LED_1灭
SBI 0x12, 0x01
// 95
// 96 LED_2.MakeInHighImpedance(); //LED_2设置为高阻输入
CBI 0x11, 0x02
CBI 0x12, 0x02
// 97 LED_2.MakeInPullUp(); //LED_2设置为上拉输入
CBI 0x11, 0x02
SBI 0x12, 0x02
// 98 LED_2.MakeOutHigh(); //LED_2设置为输出高
SBI 0x12, 0x02
SBI 0x11, 0x02
// 99 LED_2.MakeOutLow(); //LED_2设置为上拉输入
CBI 0x12, 0x02
SBI 0x11, 0x02
// 100
// 101 read=LED_2.ReadIn(); //读取LED_2引脚点品
SBIS 0x10, 0x02
RJMP ??test_1
LDI R16, 1
RJMP ??test_2
??test_1:
LDI R16, 0
??test_2:
STS `read`, R16
// 102 read=LED_2.ReadDir(); //读取LED_2方向
SBIS 0x11, 0x02
RJMP ??test_3
LDI R16, 1
RJMP ??test_4
??test_3:
LDI R16, 0
??test_4:
STS `read`, R16
// 103 read=LED_2.ReadOut(); //如果LED_2端口寄存器
SBIS 0x12, 0x02
RJMP ??test_5
LDI R16, 1
RJMP ??test_6
??test_5:
LDI R16, 0
??test_6:
STS `read`, R16
// 104
// 105 (&LED_2)->MakeInHighImpedance(); //LED_2设置为高阻输入
CBI 0x11, 0x02
CBI 0x12, 0x02
// 106 (&LED_2)->MakeInPullUp(); //LED_2设置为上拉输入
CBI 0x11, 0x02
SBI 0x12, 0x02
// 107 (&LED_2)->MakeOutHigh(); //LED_2设置为输出高
SBI 0x12, 0x02
SBI 0x11, 0x02
// 108 (&LED_2)->MakeOutLow(); //LED_2设置为上拉输入
CBI 0x12, 0x02
SBI 0x11, 0x02
// 109
// 110 read=(&LED_2)->ReadIn(); //读取LED_2引脚点品
SBIS 0x10, 0x02
RJMP ??test_7
LDI R16, 1
RJMP ??test_8
??test_7:
LDI R16, 0
??test_8:
STS `read`, R16
// 111 read=(&LED_2)->ReadDir(); //读取LED_2方向
SBIS 0x11, 0x02
RJMP ??test_9
LDI R16, 1
RJMP ??test_10
??test_9:
LDI R16, 0
??test_10:
STS `read`, R16
// 112 read=(&LED_2)->ReadOut(); //如果LED_2端口寄存器
SBIS 0x12, 0x02
RJMP ??test_11
LDI R16, 1
RJMP ??test_12
??test_11:
LDI R16, 0
??test_12:
STS `read`, R16
// 113
// 114 }
RET
|