Here's the entire Rx code - its the section at the end that needs something.
I am going for ultrafast. Serial read at 115200, write the DACs and encoder states, be ready for next byte coming in.
/* Sketch for Remote Control Receiver board.
Set up to read from Serial port &
send int values to the ports to simulate 4 Quadrature Encoders and 2 switches,
send to 4 DUAL 12-BIT DACs, MCP4922
send back status of a switch over the RS232.
Also show the state of 3 push buttons.
*/
// call libraries
#include <SPI.h>
// declare pins used
// D0, D1 used by Serial.begin
// D11,12,13 - SPI for SS for ADC
byte SlaveSel3 = 10; // SPI SS for ADC 0/1
byte SlaveSel2 = 9; // SPI SS for ADC 2/3
byte Slavesel1 = 8; // SPI SS for ADC 4/5
byte Slavesel0 = 19; // SPI SS for ADC 6/7
byte S3 = 18; // Switch2 output, Hi/Lo
byte S1 = 17; // Switch1 output, Hi/Lo
byte S0 = 16; // Switch0 output, Hi/Lo
// need to change these around
byte enc_clk3 = 15; // now optical encoder1 updown
byte enc_ud3 = 14; // now optical encoder2 updown
byte enc_clk2 = 7; // now mechanical encoder1 pulse
byte enc_ud2 = 6; // now mechanical encoder0 pulse
byte enc_clk1 = 5; // now optical encoder1 pulse
byte enc_ud1 = 4; // now optical encoder0 pulse
byte enc_clk0 = 3; // now mechanical encoder1 updown
byte enc_ud0 = 2; // now mechanical encoder0 updown
// D1, D0 used by Serial
// declare variables - most not actually used now with Direct Port Access ??
byte enc_clk0_state = 1; // the clock pins
byte enc_clk1_state = 1; // the clock pins
byte enc_clk2_state = 1; // the clock pins
byte enc_ud0_state = 1; // the up/down pins
byte enc_ud1_state = 1; // the up/down pins
byte enc_ud2_state = 1; // the up/down pins
byte enc_clk3_state = 1; // the clock pins
byte enc_ud3_state = 1; // the up/down pins
byte ADCsyncbyte = 0x33; // b00110011
byte Encsyncbyte = 0xAA; // b10101010
byte transfer_type;
byte encoder_data;
byte x = 0;
// use as upper 4 bits of highADC = 0/1 for A/B, 1 for buffered, 1 for gain x1, 1 for active (not shutdown)
byte DAC_A_mode = B01110000;
byte DAC_B_mode = B11110000;
void setup(){
// setup the I/O pins
pinMode (DAC_SS[6], OUTPUT);
digitalWrite (DAC_SS[6], HIGH);
pinMode (DAC_SS[4], OUTPUT);
digitalWrite (DAC_SS[4], HIGH);
pinMode (DAC_SS[2], OUTPUT);
digitalWrite (DAC_SS[2], HIGH);
pinMode (DAC_SS[0], OUTPUT);
digitalWrite (DAC_SS[0], HIGH);
pinMode(enc_clk0, OUTPUT);
pinMode(enc_clk1, OUTPUT);
pinMode(enc_clk2, OUTPUT);
pinMode(enc_clk3, OUTPUT);
pinMode(enc_ud0, OUTPUT);
pinMode(enc_ud1, OUTPUT);
pinMode(enc_ud2, OUTPUT);
pinMode(enc_ud3, OUTPUT);
pinMode(S0, OUTPUT);
digitalWrite (S0, LOW);
pinMode(S1, OUTPUT);
digitalWrite (S1, LOW);
pinMode(S3, OUTPUT);
digitalWrite (S3, LOW);
// Open Serial interface
Serial.begin (115200);
// Open SPI interface
SPI.begin (); // leave blank, we are master
SPI.setClockDivider( SPI_CLOCK_DIV2 ); // faster clocking for DACs
}
void loop(){
/* The Flow:
receive Sync Byte
if = 0x33:
read serial, Direct write switch to Port C
read serial, write to DAC
if = 0xAA:
read serial, split & send to PortD, PortC
read S3 state, send back over serial
*/
if (Serial.available()>0){
transfer_type = Serial.read();
if (transfer_type == 0x33){ // switches + a DAC
/*
How data is created in Tx code:
Serial.write (PINC & B00011100); // 3 switches on Port C. PINC = direct port read?
Serial.write (address_count); // find way to put this in top nibble of next command?
Serial.write (highADC & 0x0F); // send it out
Serial.write (lowADC); // send it out
*/
if (Serial.available() >3 ){
PORTC = PORTC | Serial.read() ; // should be b000xxx00, switch data
switch (Serial.read() ) { // should be 0 to 7
case 0:
// read serial UART & SPI.transfer it out
//digitalWrite (DAC_SS[0], LOW);
PORTB = PORTB & B11111011; // PB:2
SPI.transfer (Serial.read() | DAC_A_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
// digitalWrite (DAC_SS[0], HIGH);
PORTB = PORTB & B00000100; // PB:2
break;
case 1:
//digitalWrite (DAC_SS[1], LOW);
PORTB = PORTB & B11111011; // PB:2
SPI.transfer (Serial.read() | DAC_B_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[1], HIGH);
PORTB = PORTB & B00000100; // PB:2
break;
case 2:
// digitalWrite (DAC_SS[2], LOW);
PORTB = PORTB & B11111101; // PB1
SPI.transfer (Serial.read() | DAC_A_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[2], HIGH);
PORTB = PORTB & B00000010; // PB:1
break;
case 3:
//digitalWrite (DAC_SS[3], LOW);
PORTB = PORTB & B11111101; // PB:1
SPI.transfer (Serial.read() | DAC_B_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[3], HIGH);
PORTB = PORTB & B00000010; // PB1
break;
case 4:
//digitalWrite (DAC_SS[4], LOW);
PORTB = PORTB & B11111110; // PB:0
SPI.transfer (Serial.read() | DAC_A_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[4], HIGH);
PORTB = PORTB & B00000001; // PB:0
break;
case 5:
//digitalWrite (DAC_SS[5], LOW);
PORTB = PORTB & B11111110; // PB0
SPI.transfer (Serial.read() | DAC_B_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[5], HIGH);
PORTB = PORTB & B00000001; // PB:0
break;
case 6:
//digitalWrite (DAC_SS[6], LOW);
PORTC = PORTC & B11011111; // PC:5
SPI.transfer (Serial.read() | DAC_A_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[6], HIGH);
PORTC= PORTC & B00100000; // PC:5
break;
case 7:
//digitalWrite (DAC_SS[7], LOW);
PORTC = PORTC & B11011111; // PC:5
SPI.transfer (Serial.read() | DAC_B_mode); // read highADC nibble & mix with control bits, send it out
SPI.transfer (Serial.read()); // read lowADC byte, send it out
//digitalWrite (DAC_SS[7], HIGH);
PORTC= PORTC & B00100000; // PC:5
break;
}
/*
Serial.write (B00000010); // send back sync bit
*/
}
if (transfer_type == 0xAA){ // get in sync, B10101010
/* quadEncoder bit assigments:
BIT7 - optical encoder 1 up/down direction >> PD:7
BIT6 - optical encoder 0 up/down direction >> PD:6
BIT5 - mechanical encoder 1 pulse >> PD:5
BIT4 - mechanical encoder 0 pulse >> PD:4
BIT3 - optical encoder 1 pulse >> PD:3
BIT2 - optical encoder 0 pulse >> PD:2
BIT1 - mechanical encoder 1 up/down direction >> PC:1
BIT0 - mechanical encoder 0 up/down direction >> PC:0
*/
// Take pulse bits low if low, direction bits follow the pulse
// Then take pulse bits back high
encoder_data = Serial.read(); // D:7 Dir, D:6 D, D:5 Pulse, D:4 P, D:3 P, D:2 P, C:1 D , C:0 D
PORTC = PORTC | (encoder_data & B00000011); // port C direction bits, can be high or low
PORTD = PORTD | (encoder_data & B11000011); // port D direction bits, can be high or low, leaveRx/Tx alone
PORTD = PORTD ; // ??? // pull in low pulses
PORTD = PORTD | B00111100; // take pulses back high, leave directions alone
}
/*
Serial.write (B00010010); // send back an ack
*/
}
}
}