Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FastShift Test
/* FastShift Proof-Of-Concept Sketch
*
* Author: Angelo Stavrow <contact@angelostavrow.com>
* Last Updated: 6 January, 2012
*/
// Define slave select lines.
const int SS0 = 2; // Digital pin 2, slave 0
const int SS1 = 3; // Digital pin 3, slave 1
const int SS2 = 4; // Digital pin 4, slave 2
const int SS3 = 5; // Digital pin 5, slave 3
// Define instructions for reading CPU port 5 on the MAX1464.
const byte PT5_READ = B01010100; // 0x54 (Port 5)
const byte SEND2DHR = B00101000; // 0x28
const byte READ_DHR = B00011001; // 0x19
const int dataDelay = 1;
const int clockDelay = 2;
void setup()
{
// Start the serial port.
Serial.begin(115200);
// Set the pin modes for the data transfer bus connections
// and the slave select lines.
pinMode(11, OUTPUT);
pinMode(12, INPUT);
pinMode(13, OUTPUT);
pinMode(SS0, OUTPUT);
pinMode(SS1, OUTPUT);
pinMode(SS2, OUTPUT);
pinMode(SS3, OUTPUT);
}
uint8_t fastShiftIn(uint8_t bitOrder) {
uint8_t value = 0;
uint8_t i;
// Define masks for setting the SCK pin high or low.
uint8_t clkHi = PORTB | B00100000; // Directly set PB5 / digital pin 13 to HIGH.
uint8_t clkLo = PORTB & B11011111; // Directly set PB5 / digital pin 13 to LOW.
for (i = 0; i < 8; ++i) {
// Set the clock pin high.
PORTB = clkHi;
if (bitOrder == LSBFIRST) {
value |= !!(PINB & B00010000) << i;
delayMicroseconds(dataDelay);
} else {
value |= !!(PINB & B00010000) << (7 - i);
delayMicroseconds(dataDelay);
}
// Leave in for 250kHz clock; comment out for 500kHz clock
delayMicroseconds(clockDelay);
// Set the clock pin low.
PORTB = clkLo;
}
return value;
}
void fastShiftOut(uint8_t dataToSend, uint8_t bitOrder) {
uint8_t i;
// Define masks for setting the MOSI pin high or low.
uint8_t bitHi = PORTB | B00001000; // Directly set PB3 / digital pin 11 to HIGH.
uint8_t bitLo = PORTB & B11110111; // Directly set PB3 / digital pin 11 to LOW.
// Define masks for setting the SCK pin high or low.
uint8_t clkHi = PORTB | B00100000; // Directly set PB5 / digital pin 13 to HIGH.
uint8_t clkLo = PORTB & B11011111; // Directly set PB5 / digital pin 13 to LOW.
// Send the data one bit at a time.
for (i = 0; i < 8; ++i) {
if (bitOrder == LSBFIRST) {
uint8_t bitToSend = (dataToSend & (1 << i));
if (bitToSend == 0) {
PORTB = bitLo;
delayMicroseconds(dataDelay);
} else {
PORTB = bitHi;
delayMicroseconds(dataDelay);
}
} else {
uint8_t bitToSend = (dataToSend & (1 << (7 - i)));
if (bitToSend == 0) {
PORTB = bitLo;
delayMicroseconds(dataDelay);
} else {
PORTB = bitHi;
delayMicroseconds(dataDelay);
}
}
// Set the clock pin high.
PORTB = clkHi;
// Leave in for 250kHz clock; comment out for 500kHz clock
delayMicroseconds(clockDelay);
// Set the clock pin low.
PORTB = clkLo;
}
}
void writeByte(uint8_t slaveNumber, uint8_t dataToWrite) {
// Set slave select pin low.
digitalWrite(slaveNumber, LOW);
// Shift out the data to write one bit at a time.
fastShiftOut(dataToWrite, LSBFIRST);
// Set slave select pin high.
digitalWrite(slaveNumber, HIGH);
}
void loop()
{
// Get data from specified port.
writeByte(SS3, PT5_READ);
// Transfer data from specified port to DHR.
writeByte(SS3, SEND2DHR);
// Read data from DHR.
writeByte(SS3, READ_DHR);
// Get the first byte.
digitalWrite(SS3, LOW);
uint8_t hiByte = 0;
hiByte = fastShiftIn(MSBFIRST);
// Get the second byte.
uint8_t loByte = 0;
loByte = fastShiftIn(MSBFIRST);
digitalWrite(SS3, HIGH);
// Combine the two into a 16-bit word.
int readData = word(hiByte, loByte);
// Print the raw data to the serial monitor.
Serial.println(readData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment