Keil C251条件编译宏的应用实例

       条件编译宏通常是以#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

以上程序已通过实际验证。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bill66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值