条件编译宏通常是以#ifdef 或者#ifndef开头 ,以#endif结尾,在编译满足条件的就被编译,不符合条件的就不被编译,即所谓的条件编译。它不可代替条件语句及条件转移语句,同样条件语句及条件转移语句也不可能代替条件编译宏。条件编译宏常用于元件的驱动固件,可针对不同的工作模式,选择编译,可建设编译后程序的大小。例如LCD1602,4线、8线工作模式驱动代码的选择编译。本文将以STC32G 软件延时程序为例,详细讲解,条件编译宏的应用。
我们知道STC32单片机内部集成高精度R/C时钟,可不需要外部晶振。ISP编程时4MHz~33MHfanwei范围时钟频率可选择设置。我们可以依据实际需求,选择设置单片机的系统时钟频率。软件延时程序与单片机的系统时钟频率相关,如果我们针对每一系统时钟频率写一延时程序,管理·起来就很麻烦。这里我们使用条件编译宏就可巧妙地解决这一问题。
下面是软件延时程序的头文件的部分代码
#ifndef __STC32G_DELYAY_H_
#define __STC32G_DELYAY_H_
#include "mtype.h"
#include "STC32G_Timer.h"
#include "STC32G_INT.h"
#include "config.h"
/****************************************
Function: Delayxus(ui16 x);
Return value: void
Discription: soft delay x us
Example:
Delayxus(1);
****************************************/
void Delayxus(ui16 x);
/****************************************
Function: Delayxms(ui16 x);
Return value: void
Discription: soft delay x ms at fsclk
Example:
Delay1ms(1);
****************************************/
void Delayxms(ui16 x);
#endif
下面是STC32G_delay .c源代码
//****************************************
void Delayxus(ui16 x)
{
ui16 i;
#ifdef F11_0592MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
#endif
#ifdef F12MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
#endif
#ifdef F18_432MHz
while(x--)
{
_nop_();
i = 2UL;
while(i--);
}
#endif
#ifdef F20MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 2UL;
while(i--);
}
#endif
#ifdef F22_1184MHz
while(x--)
{
_nop_();
i = 3UL;
while(i--);
}
#endif
#ifdef F24MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 3UL;
while(i--);
}
#endif
#ifdef F27MHz
while(x--)
{
_nop_();
_nop_();
i = 4UL;
while(i--);
}
#endif
#ifdef F30MHz
while(x--)
{
_nop_();
i = 5UL;
while(i--);
}
#endif
#ifdef F33MHz
while(x--)
{
i = 6UL;
while(i--);
}
#endif
#ifdef F33_1776MHz
while(x--)
{
i = 6UL;
while(i--);
}
#endif
#ifdef F35MHz
while(x--)
{
_nop_();
_nop_();
i = 6UL;
while(i--);
}
#endif
}
//End of Delayxus(ui16 x)
//****************************************/
void Delayxms(ui16 x)
{
ui16 i,j;
#ifdef F11_0592MHz
while(x--)
{
_nop_();
_nop_();
i = 2762UL;
while(i--);
}
#endif
#ifdef F12MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 2997UL;
while(i--i);
}
#endif
#ifdef F18_432MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 4605UL;
while(i--);
}
#endif
#ifdef F20MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 4997UL;
while(i--);
}
#endif
#ifdef F22_1184MHz
while(x--)
{
_nop_();
i = 5527UL;
while(i--);
}
#endif
#ifdef F24MHz
whilee(x--)
{
_nop_();
_nop_();
_nop_();
i = 5997UL;
while (i--);
}
#endif
#ifdef F27MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 6747UL;
while (i--);
}
#endif
#ifdef F30MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 7497UL;
while(i--);
}
#endif
#ifdef F33MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 8247UL;
while (i) i--;
}
#endif
#ifdef F33_1776MHz
while(x--)
{
i = 8292UL;
while(i--);
}
#endif
#ifdef F35MHz
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 8747UL;
while(i--);
}
#endif
}
//End of Delayxms(ui16 x)
下面是config.h的代码,在 config.h中选择配置系统时钟,在下载程序时选择的系统时钟须与该配置一致。
#ifndef __CONFIG_H__
#define __CONFIG_H__
#define PRINTF_SEGLED //printfÊä³öÖض¨Ïòµ½ISPÏÂÔØÈí¼þÖеÄ7¶ÎÊýÂë¹Ü
//#define PRINTF_HID //printfÊä³öÖ±½ÓÖض¨Ïòµ½USB HID½Ó¿Ú
#define FOSC 30000000UL
//#define F11_0592MHz
//#define F12MHz
//#define F18_432MHz //18.432MHz
//#define F20MHz
//#defimne F22_1184MHz //22.1184MHz
//#define F24MHz
//#define F27MHz
#define F30MHz
//#define F33MHz
//#define F33_1776MHz //33.1776MHz
//#define F35MHz
//********************************************************
void SysInit(); //init System speed fastest
#endif
以上程序已通过实际验证。