Search code examples
avratmel

I want to rename a single bit of a Port


What does code i need to rename a single bit of port? as follows:

#define EN PORTD.0 
#define RS PORTD.1

I am using Avr studio 6 IDE. with c compiler.


Solution

  • You cannot access the bit directly, instead you could use macros to read or write to pin.

    Read Macro

    #define __READ_PIN__(PORT, PIN)    (PORT & (1>>PIN))
    

    Write Macros

    #define __SET_PIN__(PORT, PIN)     (PORT |= (1 << PIN))
    #define __CLEAR_PIN__(PORT, PIN)   (PORT &= ~(1 << PIN))
    

    Then you can define your pins like

    #define EN_PORT    PORTD
    #define EN_PIN     PD0    // OR (#define EN_PIN    0)
    #define RS_PORT    PORTD
    #define RS_PIN     PD1    // OR (#define RS_PIN    1)
    

    Then you could use the read or write pin macros to access this pin

    __SET_PIN__(EN_PORT, EN_PIN);    // Output logic 1 on EN pin
    __CLEAR_PIN__(RS_PORT, RS_PIN);  // Output logic 0 on RS pin
    

    And don't forget to include i/o library

    #include <avr/io.h>