#define总结

本文详细介绍了C语言预处理指令的多种用法,包括定义常数、简单函数、宏及多行宏等,并探讨了它们在条件编译中的应用。

1.定义简单的常数:定义常量,便于修改(切不可在后面加上分号!)
#define N 1000
 等效于 const int N = 1000; 但略有不同,define只是简单替换,而不是作为一个量来使用.

2. 定义简单的函数:注意多使用括号

define可以像函数那样接受一些参数,如下:

#define max(x,y) (x)>(y)?(x):(y);

这个定义就将返回两个数中较大的那个,看到了吗?因为这个“函数”没有类型检查,就好像一个函数模板似的,当然,它绝对没有模板那么安全就是了。可以作为一个简单的模板来使用而已。但是这样做的话存在隐患,例子如下:

#define Add(a,b) a+b;

在一般使用的时候是没有问题的,但是如果遇到如:c * Add(a,b) * d的时候就会出现问题,代数式的本意是a+b然后去和c,d相乘,但是因为使用了define(它只是一个简单的替换),所以式子实际上变成了

c*a + b*d

另外举一个例子:

#define pin int*

pin a,b;

本意是a和b都是int型指针,但是实际上变成int* a,b;

a是int型指针,而b是int型变量。

这是应该使用typedef来代替define,这样a和b就都是int型指针了。

所以我们在定义的时候,养成一个良好的习惯,建议所有的层次都要加括号。


3. 定义单行宏:主要有以下三种用法.
   1) 前加##或后加##,将标记作为一个合法的标识符的一部分.注意,不是字符串.多用于多行的宏定义中.例如:
#define A(x)  T_##x
则 int A(1) = 10; //等效于int T_1 = 10;
#define A(x)  Tx##__
则 int A(1) = 10; //等效于int T1__ = 10;
   2) 前加#@,将标记转换为相应的字符,注意:仅对单一标记转换有效
 #define B(x) #@x
 则B(a)即’a’,B(1)即’1’.
 但B(abc)却不甚有效.B(abc) 即'c',就是最后一个字符。参数最多只能是四位数。
   3) 前加#,将标记转换为字符串.
 #define C(x) #x
 则C(1+1) 即 ”1+1”.

4. 定义多行宏:注意斜杠的使用,最后一行不能用斜杠.
 #define DECLARE_RTTI(thisClass, superClass)/
  virtual const char* GetClassName() const/ 
  {return #thisClass;}/
  static int isTypeOf(const char* type)/
  {/
   if(!strcmp(#thisClass, type)/
    return 1;/
   return superClass::isTypeOf(type);/
   return 0;/
  }/
  virtual int isA(const char* type)/
  {/
   return thisClass::isTypeOf(type);/
  }/
  static thisClass* SafeDownCast(DitkObject* o)/
  {/
   if(o&&o->isA(#thisClass))/
    return static_cast<thisClass*>(o);/
   return NULL;/
  }

5. 用于条件编译:(常用形式)
 #ifndef _AAA_H
 #define _AAA_H
 //c/c++代码
 #endif

在大规模的开发过程中,特别是跨平台和系统的软件里,define最重要的功能是条件编译。就是:

#ifdef WINDOWS
......
......

#endif

#ifdef LINUX
......
......
#endif
可以在编译的时候通过#define设置编译环境

5.如何定义宏、取消宏

//定义宏
#define [MacroName] [MacroValue]

//取消宏
#undef [MacroName]

普通宏
#define PI (3.1415926)

带参数的宏
#define max(a,b) ((a)>(b)? (a),(b))

关键是十分容易产生错误,包括机器和人理解上的差异等等。

 

6.条件编译

#ifdef XXX…(#else) …#endif

例如 #ifdef DV22_AUX_INPUT

#define AUX_MODE 3 

#else

#define AUY_MODE 3

#endif

#ifndef XXX … (#else) … #endif

 

7.头文件(.h)可以被头文件或C文件包含;

重复包含(重复定义)

由于头文件包含可以嵌套,那么C文件就有可能包含多次同一个头文件,就可能出现重复定义的问题的。

通过条件编译开关来避免重复包含(重复定义)

例如

#ifndef __headerfileXXX__

#define __headerfileXXX__

文件内容

#endif 

 

8. 一些注意事项:
  1) 不能重复定义.除非定义完全相同.#define A(x) … 和#define A 是重复定义.

  2) 可以只定义符号,不定义值.如#define AAA

 

 

转自:http://www.cnblogs.com/phonefans/archive/2009/02/25/1398121.html

main.c(20): error C202: &#39;PWM2T2&#39;: undefined identifier main.c(21): error C202: &#39;PWM3T2&#39;: undefined identifier main.c(22): error C202: &#39;PWM4T2&#39;: undefined identifier main.c(23): error C202: &#39;PWM5T2&#39;: undefined identifier main.c(24): error C202: &#39;PWM6T2&#39;: undefined identifier main.c(25): error C202: &#39;PWM7T2&#39;: undefined identifier main.c(37): error C202: &#39;PWM2T1&#39;: undefined identifier main.c(38): error C202: &#39;PWM2T2&#39;: undefined identifier main.c(42): error C202: &#39;PWMC&#39;: undefined identifier代码运行时出现这些问题,应该是头文件的定义和代码中的定义不符合,我给你部分头文件中关于PWM的代码,你看看怎么修改sfr B = 0xF0; sfr PWMCFG = 0xF1; sfr PCA_PWM0 = 0xF2; sfr PCA_PWM1 = 0xF3; sfr PCA_PWM2 = 0xF4; sfr PWMCR = 0xF5; sfr PWMIF = 0xF6; sfr PWMFDCR = 0xF7; sfr P7 = 0xF8; sfr CH = 0xF9; sfr CCAP0H = 0xFA; sfr CCAP1H = 0xFB; sfr CCAP2H = 0xFC; #define PWMCH 0xFFF0 #define PWMCL 0xFFF1 #define PWMCKS 0xFFF2 #define PWM2T1H 0xFF00 #define PWM2T1L 0xFF01 #define PWM2T2H 0xFF02 #define PWM2T2L 0xFF03 #define PWM2CR 0xFF04 #define PWM3T1H 0xFF10 #define PWM3T1L 0xFF11 #define PWM3T2H 0xFF12 #define PWM3T2L 0xFF13 #define PWM3CR 0xFF14 #define PWM4T1H 0xFF20 #define PWM4T1L 0xFF21 #define PWM4T2H 0xFF22 #define PWM4T2L 0xFF23 #define PWM4CR 0xFF24 #define PWM5T1H 0xFF30 #define PWM5T1L 0xFF31 #define PWM5T2H 0xFF32 #define PWM5T2L 0xFF33 #define PWM5CR 0xFF34 #define PWM6T1H 0xFF40 #define PWM6T1L 0xFF41 #define PWM6T2H 0xFF42 #define PWM6T2L 0xFF43 #define PWM6CR 0xFF44 #define PWM7T1H 0xFF50 #define PWM7T1L 0xFF51 #define PWM7T2H 0xFF52 #define PWM7T2L 0xFF53 #define PWM7CR 0xFF54 #define PWM2_ID 0 #define PWM3_ID 1 #define PWM4_ID 2 #define PWM5_ID 3 #define PWM6_ID 4 #define PWM7_ID 5 #define PwmClk_1T 0 #define PwmClk_2T 1 #define PwmClk_3T 2 #define PwmClk_4T 3 #define PwmClk_5T 4 #define PwmClk_6T 5 #define PwmClk_7T 6 #define PwmClk_8T 7 #define PwmClk_9T 8 #define PwmClk_10T 9 #define PwmClk_11T 10 #define PwmClk_12T 11 #define PwmClk_13T 12 #define PwmClk_14T 13 #define PwmClk_15T 14 #define PwmClk_16T 15 #define PwmClk_T2 16 #define PWMn_PS_1 0x00 #define PWMn_PS_2 0x08 #define EPWMnI_Enable 0x04 #define ECnT2SI_Enable 0x02 #define ECnT1SI_Enable 0x01 #define PWM2_P37 0x00 #define PWM2_P27 0x08 #define PWM3_P21 0x00 #define PWM3_P45 0x08 #define PWM4_P22 0x00 #define PWM4_P44 0x08 #define PWM5_P23 0x00 #define PWM5_P42 0x08 #define PWM6_P16 0x00 #define PWM6_P07 0x08 #define PWM7_P17 0x00 #define PWM7_P06 0x08 #define EAXSFR() P_SW2 |= 0x80 #define EAXRAM() P_SW2 &= ~0x80 #define C2INI 0x01 #define C3INI 0x02 #define C4INI 0x04 #define C5INI 0x08 #define C6INI 0x10 #define C7INI 0x20 #define CBTADC 0x40 #define ENPWM 0x80 #define ECBI 0x40 #define ENC7O 0x20 #define ENC6O 0x10 #define ENC5O 0x08 #define ENC4O 0x04 #define ENC3O 0x02 #define ENC2O 0x01 #define PWM_Enable() PWMCR |= 0x80 #define PWM_Disable() PWMCR &= ~0x80 #define CBIF 0x40 #define C7IF 0x20 #define C6IF 0x10 #define C5IF 0x08 #define C4IF 0x04 #define C3IF 0x02 #define C2IF 0x01 #define ENFD 0x20 #define FLTFLIO 0x10 #define EFDI 0x08 #define FDCMP 0x04 #define FDIO 0x02 #define FDIF 0x01 #define PWM_FaultDetect_Enable() PWMFDCR |= 0x20 #define PWM_FaultDetect_Disable() PWMFDCR &= ~0x20
最新发布
08-05
你遇到的错误是因为 **PWM 寄存器没有在头文件中定义为 `sfr` 或 `sfr16`**,而是使用了宏定义(如 `#define PWM2T2 0xFF03`),而你代码中直接使用了 `PWM2T2 = x;` 这样的语句,编译器无法识别这些标识符。 --- ## ✅ 问题分析 你的头文件中: ```c #define PWM2T2 0xFF03 ``` 但你代码中: ```c PWM2T2 = x; ``` 这在标准 C 编译器中是非法的,因为 `PWM2T2` 并不是一个变量或寄存器,只是一个地址常量。你需要使用指针访问这些地址。 --- ## ✅ 解决方案 ### ✅ 方法一:使用宏定义访问寄存器(推荐) 在头文件中使用宏定义,代码中使用指针访问: ```c #define PWM2T2 (*((volatile unsigned short *)0xFF02)) // PWM2T2L + PWM2T2H #define PWM3T2 (*((volatile unsigned short *)0xFF12)) #define PWM4T2 (*((volatile unsigned short *)0xFF22)) #define PWM5T2 (*((volatile unsigned short *)0xFF32)) #define PWM6T2 (*((volatile unsigned short *)0xFF42)) #define PWM7T2 (*((volatile unsigned short *)0xFF52)) ``` 解释: - `0xFF02` 是 `PWM2T2L` 的地址 - `0xFF03` 是 `PWM2T2H` 的地址 - 定义为 `unsigned short` 类型的指针后,写入 `PWM2T2 = x;` 会自动写入高低字节 --- ### ✅ 修改后的 `SetPWM()` 函数如下: ```c void SetPWM(u8 cs, u16 x) { P_SW2 |= 0x80; // 打开扩展寄存器访问 switch(cs) { case 2: PWM2T2 = x; break; case 3: PWM3T2 = x; break; case 4: PWM4T2 = x; break; case 5: PWM5T2 = x; break; case 6: PWM6T2 = x; break; case 7: PWM7T2 = x; break; } P_SW2 &= ~0x80; // 关闭扩展寄存器访问 } ``` --- ### ✅ 同样,`PWM_config()` 中的寄存器也需要定义: ```c #define PWM2T1 (*((volatile unsigned short *)0xFF00)) // PWM2T1L + PWM2T1H #define PWMC (*((volatile unsigned short *)0xFFF3)) // 假设 PWMC 地址为 0xFFF3 ``` 或者如果你知道 `PWMC` 是 8 位寄存器,可以这样定义: ```c #define PWMC (*((volatile unsigned char *)0xFFF3)) ``` --- ## ✅ 最终完整修复后的代码 ```c #include <STC15Fxxxx.h> typedef unsigned char u8; typedef unsigned short u16; // 主频和 PWM 参数 #define FOSC 24000000L // 主频 24MHz #define PWM_FREQ 1000 // PWM 频率为 1kHz #define MAX_DUTY 100 // 占空比最大值 // PWM 寄存器定义(使用指针方式访问) #define PWM2T1 (*((volatile unsigned short *)0xFF00)) // PWM2T1L + PWM2T1H #define PWM2T2 (*((volatile unsigned short *)0xFF02)) // PWM2T2L + PWM2T2H #define PWM3T2 (*((volatile unsigned short *)0xFF12)) #define PWM4T2 (*((volatile unsigned short *)0xFF22)) #define PWM5T2 (*((volatile unsigned short *)0xFF32)) #define PWM6T2 (*((volatile unsigned short *)0xFF42)) #define PWM7T2 (*((volatile unsigned short *)0xFF52)) #define PWMC (*((volatile unsigned char *)0xFFF3)) // 假设 PWMC 地址为 0xFFF3 // PWM 时钟源选择 #define PwmClk_9T 0x08 // 9T 模式(PWMCKS 寄存器值) // 设置 PWM 周期和占空比 void SetPWM(u8 cs, u16 x) { P_SW2 |= 0x80; // 打开扩展寄存器访问 switch(cs) { case 2: PWM2T2 = x; break; case 3: PWM3T2 = x; break; case 4: PWM4T2 = x; break; case 5: PWM5T2 = x; break; case 6: PWM6T2 = x; break; case 7: PWM7T2 = x; break; } P_SW2 &= ~0x80; // 关闭扩展寄存器访问 } // 配置 PWM 模块 void PWM_config(void) { P_SW2 |= 0x80; // 打开扩展寄存器访问 *(volatile unsigned char *)0xFFF2 = PwmClk_9T; // PWMCKS = 0xFFF2 PWM2T1 = 0; // PWM2 起始值 PWM2T2 = (FOSC / (PWM_FREQ * 10)) - 1; // 设置周期(频率为 1kHz) *(volatile unsigned char *)0xFF04 = PWMn_PS_2; // PWM2CR = 0xFF04 PWMC = MAX_DUTY; // PWM 总周期单位(100 表示 100 个单位) *(volatile unsigned char *)0xF1 = 0; // PWMCFG = 0xF1 *(volatile unsigned char *)0xF5 = ENPWM | ENC2O; // PWMCR = 0xF5 P_SW2 &= ~0x80; // 关闭扩展寄存器访问 } // 初始化 P2.7 为推挽输出 void GPIO_Init(void) { P2M1 &= ~0x80; // P2.7 设置为推挽输出 P2M0 |= 0x80; } // 延时函数(毫秒级) void DelayMs(u16 ms) { u16 i, j; for(i = ms; i > 0; i--) for(j = 1350; j > 0; j--); } // 主函数 void main(void) { u8 duty = 0; GPIO_Init(); // 初始化 GPIO PWM_config(); // 初始化 PWM 模块 while(1) { for(duty = 0; duty <= MAX_DUTY; duty += 5) { SetPWM(2, duty); // 设置 PWM2 占空比 DelayMs(500); // 每次延时 500ms } } } ``` --- ## ✅ 总结修改点 | 修改点 | 说明 | |--------|------| | `PWM2T2 = x;` | 原来是未定义标识符,现在通过指针方式访问地址 | | `PWMC = MAX_DUTY;` | 原来是未定义标识符,现在通过指针或宏定义访问 | | `PWMCKS`、`PWM2CR` 等寄存器 | 用 `*(volatile unsigned char *)地址` 方式访问 | | `P_SW2 |= 0x80;` | 保持不变,用于访问扩展寄存器 | --- ## ✅ Proteus 仿真建议 - 确保芯片型号为 **STC15W4K32S4** - 设置主频为 **24MHz** - 添加晶振和两个 22pF 电容 - P2.7 接示波器 A 通道 - GND 接示波器 B 通道 - 使用 Proteus 的示波器查看波形变化 --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值