Wednesday, January 15, 2014

ArduinoPins.h converts Atmega328 pins numbers to Arduino board pin numbers

I've been having fun with different Arduino compatible implementations including being part of a team that created an Arduino Shrimp based kit for the Brighton Maker Faire based on the details for The Shrimp. http://shrimping.it/blog/shrimp/

When working with the raw ATmega328 chip the pin numbers do not match the Arduino pin numbers.  So, we had to continuousy translate between the 2 for the standard Arduino IDE to be used

Thinking about it today I thought it would be useful to have a .h file that converts the Atmega328 pin numbers to the Arduino pin numbers so I put together a little .h file with the conversion in it.

You can download the file ArduinoPins.h with the example code shown below at the following link  http://bit.ly/1hscbh6 (Google Drive link shortened)


// Example using library to convert Atmega328 pins to Arduino ID pins
#include "ArduinoPins.h"

int led = Atmega_P19;  // Atmega_P19 is the same as Arduino Pin 13

void setup ()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);   
}
void loop()
{
   Serial.println(led); // Output to the Serial Monitor the Arduino pin number so you can see it
   digitalWrite(led,HIGH);
   delay(1000);
   digitalWrite(led,LOW);
   delay(1000);
}


ArduinoPins.h

/*************************************************
 * Public Constants to convert Atmega pin numbers
 * to Arduino IDE pin numbers
 * Means no need to manually convert the pins
 *************************************************/

// Defintion for the Digital Pins
#define Atmega_P2    0  // RX
#define Atmega_P3    1  // TX
#define Atmega_P4    2
#define Atmega_P5    3  // PWM
#define Atmega_P6    4
#define Atmega_P11   5  // PWM
#define Atmega_P12   6  // PWM
#define Atmega_P13   7
#define Atmega_P14   8
#define Atmega_P15  9  // PWM
#define Atmega_P16  10  // PWM
#define Atmega_P17  11  // PWM
#define Atmega_P18  12
#define Atmega_P19  13

// Definition for Analog Pins
#define Atmega_P23  0
#define Atmega_P24  1
#define Atmega_P25  2
#define Atmega_P26  3
#define Atmega_P27  4
#define Atmega_P28  5

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.