I wrote a three wire unidirectional serial communication interface to communicate between two AVRs.
I am getting this error:
/Users/carlbaum/Documents/Arduino/libraries/SimpleSerial/SimpleSerial.cpp: In member function 'int SimpleSerial::begin(byte, byte, byte, boolean)':
/Users/carlbaum/Documents/Arduino/libraries/SimpleSerial/SimpleSerial.cpp:18: error: argument of type 'void (SimpleSerial::)()' does not match 'void (*)()'
Here is the sketch:
#include <SimpleSerial.h>
void setup() {
simple.begin(0, 1, 3, MASTER);
delay(100);
simple.send(50);
delay(100);
simple.send(100);
delay(100);
simple.end();
}
void loop() {
}
The .h:
#ifndef SimpleSerial_h
#define SimpleSerial_h
#include "WProgram.h"
//bite set macros, if we need them
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#ifndef gbi
/* I made this one up. Returns supplied bit of byte. Can be used for
* digital read, such as gbi(PINB, 6)
*/
#define gbi(sfr, bit) (!!(_SFR_BYTE(sfr) & _BV(bit)))
#endif
//DEFINES
#define MASTER 1
#define SLAVE 0
#ifndef INTPIN
#define INTPIN 0 //slave clock line, set on pin 2 default, change to 1 for pin 3
#endif
class SimpleSerial
{
public:
int begin(byte clkpin, byte datapin, byte busypin, boolean mode);
void end();
int send(byte data);
boolean available();
byte read();
private:
void receive();
byte clkpin, datapin, busypin;
boolean mode;
byte rxdata, temp;
byte bits;
boolean buffer;
};
extern SimpleSerial simple;
#endif
and the .cpp:
#include "WProgram.h"
#include "SimpleSerial.h"
int SimpleSerial::begin(byte clkpin, byte datapin, byte busypin, boolean mode) {
bits = 0;
buffer = false;
if(mode) { //MASTER
pinMode(clkpin, OUTPUT);
pinMode(datapin, OUTPUT);
pinMode(busypin, OUTPUT);
return 0;
}
else { //SLAVE, clkpin must be on an ext. int pin
pinMode(clkpin, INPUT);
pinMode(datapin, INPUT);
pinMode(busypin, INPUT);
attachInterrupt(INTPIN, receive, RISING);
temp = 0;
rxdata = 0;
return 0;
}
return 1;
}
void SimpleSerial::end() {
if(mode) { //only master can do this
digitalWrite(busypin, LOW);
digitalWrite(clkpin, LOW);
digitalWrite(datapin, LOW);
busypin = 0; //set everything to 0
clkpin = 0;
datapin = 0;
}
else { //if we are a slave, disable interrupts.
detachInterrupt(INTPIN);
}
}
//Serial Master commands
int SimpleSerial::send(byte data) {
if(mode) { //make sure we are a master
digitalWrite(busypin, HIGH);
delay(1);
shiftOut(datapin, clkpin, MSBFIRST, data); //save code by using builtins
digitalWrite(busypin, LOW); //tell slave we are done.
return 0;
}
return 1; //we are a slave, can't send data
}
//Serial Slave commands
void SimpleSerial::receive() { //ISR triggered by clock rise, private function
buffer = false;
rxdata = 0;
temp |= (digitalRead(datapin) << bits);
if(bits < 7) { //increment 'bits' only if we are under 7 bits
bits++;
}
else { //on the eight bit, reset values for next byte
bits = 0;
rxdata = temp;
temp = 0;
buffer = true;
}
}
boolean SimpleSerial::available() {
return buffer;
}
byte SimpleSerial::read() {
return rxdata;
}
...sorry for having so much!
The problem, obviously, is the attachInterrupt() function in the .cpp file (line 18). The function wants "void (*userFunc)(void)" as its second argument, obviously my receive() function is of the type SimpleSerial. How can I fix it? I basically need the receive function to be called when the clock line rises.
Thanks a lot!
baum