爱特梅尔C引脚操作宏 [英] Atmel C Pin Manipulation Macros

查看:171
本文介绍了爱特梅尔C引脚操作宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在爱特梅尔Ç被编程了一会儿,我已经习惯了所有的C位操作,所以现在我想隐藏它。我想隐藏的位操作,​​不仅使我的code更具可读性,也使其更易于维护和修改的情况下我们的硬件变更或我们做出新的硬件。

So I have been programming in Atmel C for a while and I have gotten used to all the C bit manipulation, so now I want to hide it. I want to hide the bit manipulation not only to make my code more readable but also make it easier to maintain and modify in case our hardware changes or we make new hardware.

所以,我问你什么是爱特梅尔C中的最佳宏基本针操纵。

So I am asking you what are the best macros for Atmel C for basic pin manipulation.

我要寻找的特点是:


  1. 设置一个引脚的输入或输出

  2. 设置输出引脚为高电平或低电平

  3. 读取输入的值引脚

所以,我发现,我可以使用,但没有真正适合我的帐单几个宏。

So I have found a few macros that I could use but none really fit my bill.

链接:

http://www.piconomic.co.za/fwlib/group___a_v_r___p_i_o.html (仍然要保持每个引脚定义多个)

http://www.piconomic.co.za/fwlib/group___a_v_r___p_i_o.html (still have to keep multiple defines per pin)

http://www.starlino.com/port_macro.html (不编译,AVR Studio的6.2)

http://www.starlino.com/port_macro.html (doesn't compile, AVR Studio 6.2)

用C 更改全局变量(最好的一个,在顶部附近下的问题/ * LCD DEFINES * /

Changing a global variable in C (best one, near the top of the question under "/* LCD DEFINES */"

我真的想是这样的:

#define LED1      PA1
#define BUTTON1   PB0



set_output(LED1);
output_high(LED1);
delay_ms(100);
output_low(LED1);


set_input(BUTTON1);
uint8_t tmpVal = get_input(BUTTON1);
if( tmpVal == 1 )
{
    // assuming button IS pressed here
}
else
{
    // assuming button IS NOT pressed here
}

这是最彻底的方法任何想法做到这一点?

Any ideas on the cleanest way to do this?

我能胜任围绕每个引脚几个定义保持,但我觉得这不应该是必要的。不应该PA1和PB0能够告诉我们一切还是他们得到定义成一个单一的价值?

I could handle keeping around a few more defines per pin but I feel like that shouldn't be needed. Shouldn't PA1 and PB0 be able to tell us everything or do they get defined into a single value?

编辑:在Windows上使用爱特梅尔Studio的6.2

谢谢,
罗布R上。

Thanks, Rob R.

推荐答案

算了这样做编译

http://www.starlino.com/port_macro.html

// MACROS FOR EASY PIN HANDLING FOR ATMEL GCC-AVR
//these macros are used indirectly by other macros , mainly for string concatination
#define _SET(type,name,bit)          type ## name  |= _BV(bit)    
#define _CLEAR(type,name,bit)        type ## name  &= ~ _BV(bit)        
#define _TOGGLE(type,name,bit)       type ## name  ^= _BV(bit)    
#define _GET(type,name,bit)          ((type ## name >> bit) &  1)
#define _PUT(type,name,bit,value)    type ## name = ( type ## name & ( ~ _BV(bit)) ) | ( ( 1 & (unsigned char)value ) << bit )

//these macros are used by end user
#define OUTPUT(pin)         _SET(DDR,pin)    
#define INPUT(pin)          _CLEAR(DDR,pin)    
#define HIGH(pin)           _SET(PORT,pin)
#define LOW(pin)            _CLEAR(PORT,pin)    
#define TOGGLE(pin)         _TOGGLE(PORT,pin)    
#define READ(pin)           _GET(PIN,pin)

/*
    BASIC STAMPS STYLE COMMANDS FOR ATMEL GCC-AVR

    Usage Example:
    ———————————————–
    #define pinLed B,5  //define pins like this

    OUTPUT(pinLed);       //typo fixed
    //OUTPUT(pinLED);     //compiles as DDRB |= (1<<5);
    HIGH(pinLed);         //compiles as PORTB |= (1<<5);
    ———————————————–
*/

有只是一个错字,我有固定

There is just one typo which I have fixed

这篇关于爱特梅尔C引脚操作宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆