代码
micromcu.h
/*
Project:
File: macromcu.h
Description: IO macro for AVR mcu and avrgcc compiler.
Use these macro to control IO.
Author: Shaoziyang.
shaoziyang@gmail.com
Version: 1.0
Date: 2011.Mar
*/
#ifndef _MACRO_MCU_H_
#define _MACRO_MCU_H_ 1
#include
#define PIN_OUTPUT 1
#define PIN_INPUT 0
#define PIN_HIGH 1
#define PIN_LOW 0
#define MACRO_CONCAT2_EXPAND(a, b) a
#define MACRO_CONCAT2(a, b) MACRO_CONCAT2_EXPAND(a, b)
#define MACRO_CONCAT3_EXPAND(a, b, c) a
#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 PINDIR(pin, DIR) \
{\
MACRO_CONCAT2(DDR, MACRO_ARG21(pin)) &= ~(1 << MACRO_ARG22(pin));\
MACRO_CONCAT2(DDR, MACRO_ARG21(pin)) |= (DIR << MACRO_ARG22(pin));\
}
#define PINOUT(pin, OUT) \
{ \
MACRO_CONCAT2(PORT, MACRO_ARG21(pin)) &= ~(1 << MACRO_ARG22(pin));\
MACRO_CONCAT2(PORT, MACRO_ARG21(pin)) |= (OUT << MACRO_ARG22(pin));\
}
#define PININ(pin) MACRO_CONCAT2(PIN, MACRO_ARG21(pin)) & (1 << MACRO_ARG22(pin))
#define PINSET(pin) MACRO_CONCAT2(PORT, MACRO_ARG21(pin)) |= (1 << MACRO_ARG22(pin))
#define PINCLR(pin) MACRO_CONCAT2(PORT, MACRO_ARG21(pin)) &= ~(1 << MACRO_ARG22(pin))
#define PININV(pin) MACRO_CONCAT2(PORT, MACRO_ARG21(pin)) ^= (1 << MACRO_ARG22(pin))
#endif
hardware.h
#ifndef _HARDWARE_H_
#define _HARDWARE_H_ 1
#include
#include "cfg.h"
#include "macromcu.h"
extern void IO_init();
#endif
hardware.c
#include "cfg.h"
#include "macromcu.h"
void IO_init()
{
PINDIR(LED, PIN_OUTPUT);
PINCLR(LED);
}
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;
}
运行:
开发工匠 2024-6-20 10:01