原创 AVR单片机宏的例子

2024-6-17 10:54 144 1 2 分类: MCU/ 嵌入式 文集: AVR
代码
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 ## 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 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

/*
  Project:     AVR demo
  File:        hardware.h
  Description: Simple hardware driver head file, 
               For AVR mcu and avrgcc compiler. 
  Author:      Shaoziyang.
               shaoziyang@gmail.com
  Version:     1.0
  Date:        2011.Mar
  
*/

#ifndef _HARDWARE_H_
#define _HARDWARE_H_ 1

#include 

#include "cfg.h"
#include "macromcu.h"


extern void IO_init();


#endif

hardware.c

/*
  Project:     AVR demo
  File:        hardware.h
  Description: Simple hardware driver, 
               For AVR mcu and avrgcc compiler. 
  Author:      Shaoziyang.
               shaoziyang@gmail.com
  Version:     1.0
  Date:        2011.Mar
  
*/

#include "cfg.h"
#include "macromcu.h"

void IO_init()
{
  PINDIR(LED, PIN_OUTPUT);
  PINCLR(LED);

}





demo.c

/*
  Project:     AVR demo
  File:        demo.c
  Description: demo project main file.
               For AVR mcu and avrgcc compiler. 
  Author:      Shaoziyang.
               shaoziyang@gmail.com
  Version:     1.0
  Date:        2011.Mar
  
*/

#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;
}


运行:


文章评论1条评论)

登录后参与讨论

开发工匠 2024-6-20 10:01

学习和参考了
相关推荐阅读
蓝蓝的天 2024-06-23 08:59
改进的软件串口接收子程序
代码;stduart.c/* 《AVR专题精选》随书例程 3.通信接口使用技巧 项目:改进的延时法实现半双工软件串口 文件:sfuart.c 说明:软件串口驱...
蓝蓝的天 2024-06-22 06:53
destroy信号演示
代码;#include #include #include #include void destroy(GtkWidget *button, gpointer data) { ...
蓝蓝的天 2024-06-21 20:30
容器之固定容器构件演示
代码:#include #include #include #include int main(int argc, char *argv[]) { gtk_init(&argc...
蓝蓝的天 2024-06-21 19:51
界面的宽度与高度
代码:#include #include #include static void set_label(GtkWidget *window) { char citem[100];...
蓝蓝的天 2024-06-20 14:39
菜单构件
代码;#include #include #include GtkWidget *create_menu4(void) { GtkWidget *menu4; GtkWidget ...
蓝蓝的天 2024-06-20 13:46
常用界面构建创建文件夹对话框
代码;#include int main(int argc, char *argv[]) { GtkWidget *dialog; gchar *filename; gi...
EE直播间
更多
我要评论
1
1
关闭 站长推荐上一条 /5 下一条