0 Members and 1 Guest are viewing this topic.
// MACROs#define PIN2L !(PIND & 0B00000100)#define PIN3L !(PIND & 0B00001000)#define CLR(x,y) (x&=(~(1<<y)))#define SET(x,y) (x|=(1<<y))byte get;byte bitCount = 8;void bitTransceive(){ get >>= 1; if (PIN2L) { get |= 128; } bitCount--;}void setup(){ CLR(DDRD,2); //pinMode 2 is input CLR(DDRD,3); //pinMode 3 is input Serial.begin(115200); attachInterrupt(1,bitTransceive,FALLING);}void loop(){ if (!bitCount){ // Pull down clock line to make sure the calculator does not send while the atmega // is processing the incoming data. SET(DDRD,3); Serial.println(get); get = 0; bitCount = 8; CLR(DDRD,3); // Released the clockline. }}
#define _N 255// MACROs#define PIN2L !(PIND & 0B00000100)#define PIN3L !(PIND & 0B00001000)#define CLR(x,y) (x&=(~(1<<y)))#define SET(x,y) (x|=(1<<y))byte data;byte bitCount = 8;// State _N (255) : Do nothing and wait for incoming interrupt. Carry out other tasks in the mean time.// State 0 : Slave receives (Receive bytes from master) // State 1 : Slave sends (Send bytes to master)void bitReceive(){ if(bitCount){ data >>= 1; if (PIN2L) { data |= 128; } bitCount--; }}void bitSend(){ if(bitCount) { //Set portD DDRD |= 4; if(data & 1) PORTD &= 251; else PORTD |= 4; data >>= 1; bitCount--; }}void setup(){ CLR(DDRD,2); //pinMode 2 is input CLR(DDRD,3); //pinMode 3 is input attachInterrupt(1,bitReceive,FALLING);}void loop(){ byte state = _N, address, cell[128]; while(1) { if (!bitCount){ // Pull down clock line to make sure the calculator does not send while the atmega // is processing the incoming data. DDRD |= 8; switch (state) { case 255: { bitCount = 8; address = data >> 1; state = data & 1; // When the mode is Slave Transmit the function bitSend() is attached to the interrupt. if (state){ data = cell[address]; attachInterrupt(1,bitSend,FALLING); } break; } case 0: { cell[address] = data; bitCount = 8; // Return to mode _N. state = _N; break; } case 1: { bitCount = 8; // Re-attach the bitReceive function to the external interrupt on pin 3 and // return to mode _N. attachInterrupt(1,bitReceive,FALLING); state = _N; break; } } // Release the clockline and data line. DDRD &= 243; PORTD &= 251; } }}