Hi yall, I am trying to do a rotating heating pan here. So, I used a TB6560 to control a stepper and use a max 6675 thermal-couple to adjust the coil every half second.
My problem now is that my temperature checking timer interrupt will interfere the pulse generation for my stepper function and give me "breaks". From oscilloscope, I can clearly see during the temperature checking, the pulse generation is stopped and will resume after. I use AccelStepper for stepper control which does not use delay function. The code is below, thanks for the help in advance!
#include <AccelStepper.h>
#include <I2Cdev.h>
#include <TimerOne.h>
#include <max6675.h>
#define stepdir 6
#define stepclk 5
#define TIMER_US 500000 // 500mS set timer duration in microseconds
volatile bool in_long_isr = false; // True if in long interrupt
int thermoDO = 9;
int thermoCS = 10;
int thermoCLK = 11;
int coilpin = 13;
const int t_want = 40;
AccelStepper stepper(stepdir,stepclk);
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
void setup()
{
pinMode(stepclk, OUTPUT);
pinMode(stepdir, OUTPUT);
pinMode(coilpin, OUTPUT);
stepper.setMaxSpeed(300);
stepper.setSpeed(300);
Timer1.initialize(TIMER_US); // Initialise timer 1
Timer1.attachInterrupt( timerIsr ); // attach the ISR routine here
Serial.begin(9600);// use Arduino pins
Serial.println("MAX6675 test");
delay(500);
}
void loop()
{
stepper.runSpeed();
}
void timerIsr()
{
// digitalWrite( LED0, digitalRead( LED0 ) ^ 1 ); // Toggle LED 0
volatile long i=0;
interrupts(); // Enable interrupts
//checkcoil();
int temp = thermocouple.readCelsius();
if (temp >= t_want)
digitalWrite(coilpin, LOW);
else if (temp <= t_want)
digitalWrite(coilpin, HIGH);
Serial.print("C = ");
Serial.println(temp);
noInterrupts();
}
Interrupts are enabled immediately on entering the ISR, so you can call Serial and spend lots of time
there without breaking anything, but you must also call stepper.runSpeed() frequently in that ISR
if you want the stepper to work.
If your timer event is only every 1/2 second you can poll for it, a timer interrupt is overkill, but
you still need to make sure runSpeed() is called frequently enough (it seems that the max6675
library isn't workable for this - indeed if you look at it its useing _delay_ms() instead of delayMicroseconds(),
which it ought to be.
It does not make a lot of sense. What happens if the same interrupt occurs before the first one completes.
...R
I can't recall - go to the datasheet for that level of detail. Either it is blocked until the relevant
interrupt flag is cleared or it isn't. I'm pretty sure only one interrupt can be queued for each vector,
but all the different vectors can queue simultaneously, the highest priority one being selected when
interrupts are enabled.
If an ISR can be re-entrantly called you have to program it in a manner safe with that re-entrancy,
typically inhibiting interrupts briefly where necessry to access data-structures and variables atomically.