代码
demo.c
#include "hardware.h"void delay()
{volatile unsigned int n;for(n = 0; n < 50000; n++);
}int main(void)
{IO_init();while(1){PINSET(LED);delay();PINCLR(LED);delay();}return 0;
}
cfg.h
#ifndef _CFG_H_
#define _CFG_H_//ϵͳʱÖÓƵÂÊ
#define F_CPU 8000000UL//LEDÏÔʾ
#define LED P0, 0#endif
hardware.h
#ifndef _HARDWARE_H_
#define _HARDWARE_H_ 1#include <reg51.h>#include "cfg.h"
#include "macromcu.h"DEFPIN(LED);extern void IO_init();#endif
micromcu.h
#ifndef _MACRO_MCU_H_
#define _MACRO_MCU_H_ 1#include <reg51.h>#define PIN_OUTPUT 1
#define PIN_INPUT 1#define PIN_HIGH 1
#define PIN_LOW 0#define MACRO_CONCAT2_EXPAND(a, b) a ## b
#define MACRO_CONCAT2(a, b) MACRO_CONCAT2_EXPAND(a, b)
#define MACRO_CONCAT3_EXPAND(a, b, c) a ## b ## c
#define MACRO_CONCAT3(a, b, c) MACRO_CONCAT3_EXPAND(a, b, c)#define MACRO_ARG21(a, b) a
#define MACRO_ARG22(a, b) b#define PIN_PRRFIX PIN_
#define DEFPIN(pin) sbit MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin)) = MACRO_ARG21(pin) ^ MACRO_ARG22(pin)#define PINDIR(pin, DIR) MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin)) = 1
#define PINOUT(pin, OUT) MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin)) = OUT
#define PININ(pin) MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin))#define PINSET(pin) MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin)) = 1
#define PINCLR(pin) MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin)) = 0
#define PININV(pin) MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin)) = !MACRO_CONCAT3(PIN_PRRFIX, MACRO_ARG21(pin), MACRO_ARG22(pin))#endif
hardware.c
#include "cfg.h"
#include "macromcu.h"
#include "hardware.h"void IO_init()
{PINDIR(LED, PIN_OUTPUT);PINCLR(LED);}
仿真效果图: