I am currently using a PLUS+1 microcontroller for my project, and I am using C1P25 pin with FreqIN input. I am actually developing a simple HIL with my PC and it's going great so far, but one problem I am encountering is trying to "simulate" physical signals such as Digital and FreqIN signals.
So I've opted for using Arduino as an "intermediary" between my PC and PLUS+1 which for Digital signals, I can use Arduino's digital output signals. However, for FreqIN, Im not entirely sure how exactly to approach this. I am actually trying to simulate a speed sensor which I found this forum to be doing exactly what I need.
However, after going through it, I did not truly understand what exactly does the code do and if that's exactly what I am looking for. I am hoping I can get a bit of guidance on how to approach this, and possibly some reference.
Edit: Sorry, I should have included the code here. Thanks for pointing it out
const byte numPulsesMeasured = 16;
const unsigned long timeout = 100000UL; // 100ms timeout, may need to adjust this
byte pulsesCounted = 0;
unsigned long firstPulseTime;
volatile unsigned int interval = 0;
float gearRatio = 1.0;
void setup()
{
attachInterrupt(0, isr, RISING); // every time interrupt triggered (pin 2) run function isr.
while (interval == 0) { } // discard first reading
// insert code to initialize timer 1 here
}
void isr()
{
++pulsesCounted; // tally pulses
if (pulsesCounted == numPulsesMeasured) // once count of 16 reached (1 revolution)
{
unsigned long now = micros(); // save current clock time
unsigned long temp = now - firstPulseTime; // subtract last clock time from current clock time to get microS per revolution
if (temp < timeout)
{
interval = (unsigned int)(temp/numPulsesMeasured); // calculates microsec per pulse
}
firstPulseTime = now; // set last clock time to current clock time
pulsesCounted = 0; // reset pulses counted
}
}
void setOutputInterval(unsigned int interval)
{
// insert code to change timer 1 TOP and OCR registers here
}
void loop()
{
noInterrupts();
interval = 0;
interrupts();
unsigned int localInterval;
unsigned long startTime = micros();
do
{
noInterrupts();
localInterval = interval;
interrupts();
} while (localInterval == 0 && (micros() - startTime < timeout));
// need code to update the gear ration somewhere....
setOutputInterval((unsigned int)(gearRatio * (float)localInterval));
}
Cheers,
Khalid