Created New question...I am having GPRS module with ttl serial output and rfid reader with wiegand output ...both things works fine individually ..when i merged them together SoftwareSerial and wiegand code giving errors of multiple pin definations....i want to use both of them..how can i do this?need suggestions
code:
// WiegandReader
// credit to Mike Cook
// http://www.thebox.myzen.co.uk/Hardware/Crazy_People.html
// version 0.05.00
#include "pins_arduino.h"
#define ID_LEN 26
#define DATA_PIN_0 4
#define DATA_PIN_1 5
/*
pins_arduino.h is an extension to the interrupt support for arduino.
add pin change interrupts to the external interrupts, giving a way
for users to have interrupts drive off of any pin.
Refer to avr-gcc header files, arduino source and atmega datasheet.
Theory: all IO pins on Atmega168 are covered by Pin Change Interrupts.
The PCINT corresponding to the pin must be enabled and masked, and
an ISR routine provided. Since PCINTs are per port, not per pin, the ISR
must use some logic to actually implement a per-pin interrupt service.
Pin to interrupt map:
D0-D7 = PCINT 16-23 = PCIR2 = PD = PCIE2 = pcmsk2
D8-D13 = PCINT 0-5 = PCIR0 = PB = PCIE0 = pcmsk0
A0-A5 (D14-D19) = PCINT 8-13 = PCIR1 = PC = PCIE1 = pcmsk1
*/
////////////////////////////////////////////////////////
// interrupt code
////////////////////////////////////////////////////////
volatile uint8_t *port_to_pcmask[] = { &PCMSK0, &PCMSK1, &PCMSK2 };
typedef void (*voidFuncPtr)(void);
volatile static voidFuncPtr PCintFunc[24] = { NULL };
volatile static uint8_t PCintLast[3];
////////////////////////////////////////
// attach an interrupt to a specific pin using pin change interrupts.
void PCattachInterrupt(uint8_t pin, void (*userFunc)(void), int mode)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
uint8_t slot;
volatile uint8_t *pcmask;
if (mode != CHANGE)
{
return;
}
// map pin to PCIR register
if (port == NOT_A_PORT)
{
return;
}
else
{
port -= 2;
pcmask = port_to_pcmask[port];
}
slot = port * 8 + (pin % 8);
PCintFunc[slot] = userFunc;
// set the mask
*pcmask |= bit;
// enable the interrupt
PCICR |= 0x01 << port;
}
////////////////////////////////////////
void PCdetachInterrupt(uint8_t pin)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *pcmask;
// map pin to PCIR register
if (port == NOT_A_PORT)
{
return;
}
else
{
port -= 2;
pcmask = port_to_pcmask[port];
}
// disable the mask.
*pcmask &= ~bit;
// if that's the last one, disable the interrupt.
if (*pcmask == 0)
{
PCICR &= ~(0x01 << port);
}
}
////////////////////////////////////////
// common code for isr handler. "port" is the PCINT number.
// there isn't really a good way to back-map ports and masks to pins.
static void PCint(uint8_t port)
{
uint8_t bit;
uint8_t curr;
uint8_t mask;
uint8_t pin;
// get the pin states for the indicated port.
curr = *portInputRegister(port+2);
mask = curr ^ PCintLast[port];
PCintLast[port] = curr;
// mask is pins that have changed. screen out non pcint pins.
if ((mask &= *port_to_pcmask[port]) == 0)
{
return;
}
// mask is pcint pins that have changed.
for (uint8_t i=0; i < 8; i++)
{
bit = 0x01 << i;
if (bit & mask)
{
pin = port * 8 + i;
if (PCintFunc[pin] != NULL)
{
PCintFunc[pin]();
}
}
}
}
////////////////////////////////////////
SIGNAL(PCINT0_vect)
{
PCint(0);
}
////////////////////////////////////////
SIGNAL(PCINT1_vect)
{
PCint(1);
}
////////////////////////////////////////
SIGNAL(PCINT2_vect)
{
PCint(2);
}
////////////////////////////////////////////////////////
// end of interrupt code
////////////////////////////////////////////////////////
volatile long reader1 = 0;
volatile int reader1Count = 0;
////////////////////////////////////////
void read0(void)
{
if(digitalRead(DATA_PIN_0) == LOW)
{
reader1Count++;
reader1 = reader1 << 1;
Serial.print("0");
}
}
////////////////////////////////////////
void read1(void)
{
if(digitalRead(DATA_PIN_1) == LOW)
{
reader1Count++;
reader1 = reader1 << 1;
reader1 |= 1;
Serial.print("1");
}
}
////////////////////////////////////////
void setup()
{
Serial.begin(57000);
// Attach pin change interrupt service routines from the Wiegand RFID readers
PCattachInterrupt(DATA_PIN_0, read0, CHANGE);
PCattachInterrupt(DATA_PIN_1, read1, CHANGE);
delay(10);
// the interrupt in the Atmel processor misses out the first negative pulse as the inputs are already high,
// so this gives a pulse to each reader input line to get the interrupts working properly.
// Then clear out the reader variables.
// The readers are open collector sitting normally at a one so this is OK
for(int i = 4; i<10; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, HIGH); // enable internal pull up causing a one
digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
pinMode(i, INPUT);
digitalWrite(i, HIGH); // enable internal pull up
}
delay(10);
// clear the reader input variables
reader1 = 0;
reader1Count = 0;
digitalWrite(13, HIGH); // show Arduino has finished initialisation
Serial.println("");
Serial.println("****************");
Serial.println("setup done");
Serial.println("****************");
}
////////////////////////////////////////
void loop()
{
if(reader1Count >= ID_LEN)
{
Serial.println("");
Serial.println("---------------------");
Serial.print("bits: ");
Serial.println(reader1Count);
Serial.print("data: ");
Serial.println(reader1 & 0xfffffff);
Serial.print("hex: ");
Serial.println(reader1, HEX);
Serial.println("---------------------");
reader1 = 0;
reader1Count = 0;
}
}