|
发表于 2008-12-30 15:32:33
|
显示全部楼层
#if /#endif是C里面最重要的宏指令之一,而宏则是C的精华(C的另一精华是指针),起初是难上手,但上了手就上了贼盘,再也跑不掉的.呵呵.
#if 0 / #endif常见于调试代码.调试是将#if 0改成#if 1,代码就有效,等调完了改回#if 0,代码就等同于删除,但比删除好的是,下次要用时还可以随时再加回来---你未必能清楚知道这段代码真的可以永久删掉还是下次仍需要用.随便贴一段我写的代码,你可以看到里面有大量的条件编译:
//ICC-AVR application builder : 2005-1-21 18:37:43
// Target : MCS51 22.184 M8 8M
#include "so12864.h"
//========MCS51========================================================
#if defined(MCS51)
#include <reg51.h>
#include "so12864.h"
#include "string.h"
sbit W_IN = P3^2;
sbit W_OUT = P3^4;
#define HIGH_W_OUT W_OUT = 1
#define LOW_W_OUT W_OUT = 0
//========ATMega8========================================================
#elif defined(ATMega8)
#include <iom8v.h>
#include <macros.h>
#define W_IN (PIND & (1<<2))
#define HIGH_W_OUT PORTD &= ~(1<<3)
#define LOW_W_OUT PORTD |= (1<<3)
#endif
//========END========================================================
unsigned char w_data; //接收时用于存储两次上升沿之间的时长,发送时存储前半周
unsigned char send_busy = 0;//存储发送时后半周
unsigned char recv_timer = 0;
unsigned char w_stat, last_w_stat;
unsigned char jiffies=0;
//========定义中断函数========================================================
#if defined(MCS51)
void clock_timer(void) interrupt 1 using 1
#elif defined(ATMega8)
#pragma interrupt_handler clock_timer:10
void clock_timer(void)
#endif
//========END========================================================
{
//TIMER1 has overflowed 20KHz
#if defined(ATMega8)
TCNT0 = 0xD0; //reload counter value
#endif
if (send_busy){
if(w_data){
w_data--;
LOW_W_OUT;
}else{
send_busy--;
HIGH_W_OUT;
}
}else{
w_stat = W_IN;
if (w_stat != last_w_stat){
last_w_stat = w_stat;
if (w_stat){
w_data = recv_timer;
recv_timer = 0;
}
}
if (~recv_timer)//if(recv_timer != 0xff)
recv_timer++;
}
jiffies++;
}
#if defined(ATMega8)
#define RI (UCSRA & 0x80)
#define TI (UCSRA&(1<<UDRE))
#define SBUF UDR
#endif |
|