What is your macro like for dealing with IO ?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am currently working on a good macro for setting bits and getting bits from registers. Here is my current progress blog/self/2015/07/11/v2.0-avr-register-macros.html . And this is what it looks like in practice.

 

// Example blink code for Arduino UNO 16MHz (atmega328p)
#define F_CPU 16000000UL // For delay.h to function
#include <avr/io.h> // PIN, DDR, PORT defs
#include <util/delay.h> //_delay_ms()
#include "./AVRAugment_io.h"
##define LED13 B5 // Only need to change this to swap pins
#define DDR_13  SET_REG( DDR, LED13, 0xFF ) //IN=0x00, OUT=0xFF
#define ON_13   SET_REG( PORT, LED13, 0xFF ) //LOW=0x00, HIGH=0xFF
#define OFF_13  SET_REG( PORT, LED13, 0x00 ) //LOW=0x00, HIGH=0xFF
int main(void)
{
    DDR_13;
    ON_13;
    while(1)
    {
        _delay_ms(2000);
        OFF_13;
        _delay_ms(2000);
        ON_13;
    }
} 

 

Some thing I would like to know, since this method can only set one bit at a time for registers that are defined by Pxn (used for DDR, PORT, PIN).  Is Pxn and DDR PORT PIN pinout the only one that has any cross relations in terms of "x=letter" "n=number"? (e.g. can I get away with only supporting DDR, PIN, PORT, thus simpler macro. Or should it still remain general?)

 

Maybe since this is limited to setting one bit at a time, I should rename from "SET_REG" to "SETPIN_REG" to avoid confusion.

 

----------

 

Or maybe you have a better approach entirely that allows for directly saying "#define LEDPIN B5" or similar? Also would be nice if the same approach can be used for PWM etc...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

God gave you pinMode() and digitalWrite(). Why not use them?

Yes, the Codevision extension makes life very convenient e.g. PORTB.5=0 or DDRB.5=1 but it is not standard C.
You can do something similar with the facilities in "sbit.h"
It is also possible to use the paste operator from the C preprocessor but you need to be very careful how it gets evaluated.
This subject comes up several times a week.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Thanks for the prompt reply. I can assure you that I did make a quick search before posting, but probably wasn't using the right terminology.

 

I presume you meant this post on bit manipulation, in regards to sbit.h , I'll give that a read now. About the sbit.h , I recall in atmel application notes on 8bit C programming of AVRs that there are some issues in regards to compiler differences in section "Bitfield in a C Struct". Also my intention was to select pins based on how it is named in datasheets (like B5, instead of saying "bit 5 of DDR5" "bit 5 of PORT" etc... ), but that shouldn't be too hard to adapt sbit.h to allow for this (if bitfield struct is not an issue).

Last Edited: Sat. Jul 11, 2015 - 08:43 AM