在写到 pwm音阶程序的时候,在代码中有#if...#endif的用法问题,相关音阶的代码如下:

1 /*******************************************************/
2 /* pwm音阶程序 */
3 /*******************************************************/
4
5
6 #include <hidef.h> /* common defines and macros */
7 #include "derivative.h" /* derivative-specific definitions */
8
9 #define LEDCPU PORTK_PK4
10 #define LEDCPU_dir DDRK_DDRK4 //
11 #define BUS_CLOCK 32000000
12 #define OSC_CLOCK 16000000
13
14
15 unsigned int sounghigh[10]={ 16 4048,3816,3402,3030,2866,2551,2273,2024,1912,1704
17 }; 18
19 unsigned int soundtone[8]={ 20 1,2,3,4,5,6,7,8
21 }; 22
23 unsigned int soundlong[8]={ 24 1,1,1,1,1,1,1,1
25 }; 26
27 unsigned int t; 28
29
30 /*******************************************************/
31 /* 初始化锁相环 */
32 /*******************************************************/
33 void INIT_PLL(void) { 34 CLKSEL &= 0x7f; 35 PLLCTL &= 0x8f; 36 CRGINT &= 0xdf; 37
38 #if(BUS_CLOCK == 40000000)
39 SYNR = 0x44; 40 #elif(BUS_CLOCK == 32000000)
41 SYNR = 0x43; 42 #elif(BUS_CLOCK == 24000000)
43 SYNR = 0x42; 44 #endif
45
46 REFDV = 0X81; 47 PLLCTL |= 0X70; 48 asm NOP; 49 asm NOP; 50 while(!(CRGFLG&0x08)); 51 CLKSEL |= 0x80; 52 } 53
54
55 /*******************************************************/
56 /* 初始化PWM */
57 /*******************************************************/
58 void init_pwm(void) { 59 PWMCTL_CON01 = 1; 60 PWMPOL_PPOL1 = 1; 61 PWMPRCLK = 0X55; 62 PWMCLK = 0X00; 63 PWMPER01 = 3816; 64 PWME_PWME1 = 1; 65 } 66
67 /*******************************************************/
68 /* 延时函数 */
69 /*******************************************************/
70 void delay(void) { 71 unsigned int i,j; 72 for(i=0;i<10;i++) 73 for(j=0;j<50000;j++); 74 } 75
76 /*******************************************************/
77 /* 延时函数 */
78 /*******************************************************/
79 void delay2(unsigned int n) { 80 unsigned int i,j,k; 81 for(k=0;k<n;k++) 82 for(i=0;i<50;i++) 83 for(j=0;j<50000;j++); 84 } 85
86 /*******************************************************/
87 /* 主函数 */
88 /*******************************************************/
89
90 void main(void) { 91 DisableInterrupts; 92 INIT_PLL(); 93 init_pwm(); 94 LEDCPU_dir=1; 95 LEDCPU=0; 96 EnableInterrupts; 97
98 for(;;) { 99 for(t=0;t<8;t++) { 100 PWMPER01=sounghigh[soundtone[t]]; 101 PWMDTY01=sounghigh[soundtone[t]]/2; 102 delay2(soundlong[t]); 103 PWMDTY01=0; 104 delay(); 105 } 106 delay2(5); 107
108 for(t=8;t>0;t--) { 109 PWMPER01=sounghigh[soundtone[t-1]]; 110 PWMDTY01=sounghigh[soundtone[t]]/2; 111 delay2(soundlong[t]); 112 PWMDTY01=0; 113 delay(); 114 } 115 delay2(5); 116 } 117
118
119 EnableInterrupts; 120
121
122 for(;;) { 123 _FEED_COP(); /* feeds the dog */
124 } /* loop forever */
125
126 }
下面笔者来说说 #if...#endif的用法;
#if...#endif是C++种的条件编译预处理命令
有两种格式:
1:#ifdef 标示符
程序段1
#else
程序段2
#endif
表示:如果标示符已经被#define命令定义过,则编译程序段1,否则编译程序段2
2:#if 表达式
程序段1
#else
程序段2
#endif
表示:如果表达式为真,则编译程序段1,否则编译程序段2.
你写出的命令应该属于格式1
它的意思是,如果在程序中没有对**进行过#defined ** 的定义。则编译下面的语句,否则不编译。这中情况多用在需要经常移植的程序模块上,或是调试的时候用.
结合使用 #if 与 #else、#elif、#endif、#define 和 #undef 指令,可以根据一个或多个符号是否存在来包含或排除代码。 在编译调试版本的代码或针对特定配置进行编译时,这会很有用。
以 #if 指令开始的条件指令必须用 #endif 指令显式终止。
#define 使您可以定义一个符号,通过将该符号用作传递给 #if 指令的表达式,使该表达式计算为 true。
也可以用 /define 编译器选项来定义符号。 可以用 #undef 来取消定义符号。
用 /define 或 #define 定义的符号与具有同一名称的变量不冲突。 即,不应将变量名传递到预处理器指令,并且只能用预处理器指令计算符号。
用 #define 创建的符号的范围是在其中定义该符号的文件。