Hi,
I am using Arduino to receive, process commands and derive another machine. I am using Arduino UNO
See the code below:
#include <SPI.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SoftwareSerial.h> // This feature is for second serial port
SoftwareSerial mySerial(2,3); // RX, TX
/*------- SPI Pin assignments ----------
SCK 13
SDO 11
SDI 12
CNV 05 (SS)
FREFH Out 09
RESETn 06
Soft Serial Rx 02
Soft Serial Tx 03
---------------------------------------*/
char *ptr = NULL;
uint16_t receivedData; // We will read data in this for displaying later
uint16_t sendData; // Unsigned Integer of 16 bit width
uint16_t sendData1;// Included for testing
const byte CLOCKOUT = 9;
#define slaveSelect 5
#define resetOutput 6
SPISettings settingsA(1000000, MSBFIRST, SPI_MODE2); // Mode 2 means, SCK will remain 1 in inactive, and will sample on negative edge, will work on 8 MHz
/*---------------------------This is the function that will generate the SPI transaction------------------------*/
void SPITransactionSettingsA(uint16_t a) {
PORTD = PORTD | (B00100000); // Writting to pin 5 // digitalWrite (slaveSelect, HIGH);
SPI.beginTransaction(settingsA);
receivedData = SPI.transfer16(a);
SPI.endTransaction();
PORTD = PORTD & ~(B00100000); // Writting to pin 5 // Previously digitalWrite(slaveSelect, LOW);
//Serial.print("The received data from SPI is:"); // Will print values in Hexadecimal
//Serial.print(receivedData, HEX); // Will print values in Hexadecimal
//Serial.print("\n"); // Will print values in Hexadecimal
}
void setup() {
Serial.begin(115200);
mySerial.begin(115200); // Previously 154000
SPI.begin();
pinMode(slaveSelect,OUTPUT);
pinMode (CLOCKOUT, OUTPUT);
TCCR1A = bit (COM1A0);
TCCR1B = bit (WGM12) | bit (CS10);
OCR1A = 0;
pinMode(resetOutput,OUTPUT);
PORTD = PORTD &~(B1111111);
PORTD = PORTD | (B01000000); // Putting Reset to 1 initially
}
void loop() {
if(Serial.available()){
String data = Serial.readString();
unsigned long StartTime = millis();
if ((data.substring(0,5)) == "RESET"){
PORTD = PORTD & ~(B01000000);
PORTD = PORTD | (B01000000);
}
else {
String IntermediateData = data.substring(0,4);
sendData = strtoul(IntermediateData.c_str(),&ptr,16);
SPITransactionSettingsA(sendData);
}
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;
mySerial.println(ElapsedTime);//(sendData1); // This line I included just to see whether or where it prints
}
}
Problems: When I send command with an interval of 1010ms, everything works fine. But as I decrease time, even to 1009ms, only first command gets executed.
I tried to measure the timing of execution of program, but it reports that the loop executed in 0 time. So here are two problems:
- What time it is taking to execute the function SPITransactionSettingsA.
- Why 1010ms, why cant it process faster?
Thanks in advance.