Need help with ISR - Nano Every

Hi,

If someone can help me to adapt the code below to Arduino nano every?

//**************************************************************//
//  Name    : DMX 4 ch Dimmer                                   //
//  Author  : Dan Fredell                                       //
//  Date    : 16 April, 2013                                    //
//  Version : 1.1                                               //
//  Website :                                                   //
//**********************************z ******************************
/*File modifacations: 
 * hardware/arduino/boards.txt uno.build.f_cpu=20000000L
 * It tells the software that the hardware is running at 20MHz 
 * 
 * In cores/arduino/HardwareSerial.cpp remove 42 lines, 
 *   this allows us to make our own intrupt for DMX signal in
 * From 
 * #if !defined(USART0_RX_vect) && defined(USART1_RX_vect) 
 * to 
 * #if defined(USART1_RX_vect)
 */


//double dim = 0;    // Dimming level (0-128)  0 = on, 128 = 0ff
int AC_pin[4] = {  
  6,7,8,12};  // Output to Opto Triac
int LED_pin[4] = {  
  5,9,10,11};  // Output to Opto Triac
int brightness[4];
int LEDBrightness[4];
boolean zeroCross[]={
  false,false,false,false};
int PrevLEDValue[]={
  0,0,0,0};
unsigned long startZeroTime=0;
unsigned long timeElapsed=0;//Time until end of cycle

volatile uint8_t  DmxRxField[4]; //array of DMX vals (raw)  DMX Max:255 Min:0
volatile uint16_t DmxAddress;    //start address
enum {
  IDLE, BREAK, STARTB, STARTADR}; //DMX states
volatile uint8_t gDmxState;

void setup() {
  delay(500);//Wait for the system to charge
  //Setup DMX
  gDmxState= IDLE; // initial state

  DmxAddress = 1; // The desired DMX Start Adress. Starts at 1
  Serial.begin(250000);//Read DMX signal via RX

  //Setup Outputs
  attachInterrupt(1,zeroCrossInterupt,FALLING);
  for(int ch=0;ch<4;ch++){
    pinMode(AC_pin[ch],OUTPUT);
    pinMode(LED_pin[ch],OUTPUT);
    digitalWrite(AC_pin[ch],HIGH);
    analogWrite(LED_pin[ch],255);
    delay(1000);
    digitalWrite(AC_pin[ch],LOW);
    analogWrite(LED_pin[ch],0);
  }
}

void zeroCrossInterupt(){
  startZeroTime=micros();
  gDmxState= IDLE;
  for(int ch=0;ch<4;ch++){
    zeroCross[ch]=true;
    brightness[ch]=map(DmxRxField[ch],0,265,8000,0);
  }
}

void loop() {

  timeElapsed=micros()-startZeroTime;
  /**
   * This is the main loop. I use timeElapsed instead of delay to allow for more then 1 output.
   * The +50 is to elimnate random noisy high and low values
   * DmxRxField[ch] > 10 because at DMX val8 it flickers
   */

  for(int ch=0;ch<4;ch++){
    if(zeroCross[ch] && timeElapsed > brightness[ch] && timeElapsed < brightness[ch]+50 && DmxRxField[ch]>10){
      digitalWrite(AC_pin[ch], HIGH);  // fire the Triac
      delayMicroseconds(10);       //pause briefly to ensure the triac turned on
      digitalWrite(AC_pin[ch], LOW);   // turn off the Triac gate (triac will not turn off until next zero cross)
      zeroCross[ch]=false;
    }
    if(PrevLEDValue[ch]!=DmxRxField[ch]){//Change the LED value only if it changed
      analogWrite(LED_pin[ch],DmxRxField[ch]);
      PrevLEDValue[ch]=DmxRxField[ch];
    }
  }
}


//ISR method from http://playground.arduino.cc/DMX/Ardmx
ISR(USART_RX_vect) {
  static  uint16_t DmxCount;
  uint8_t  USARTstate= UCSR0A;    //get state before data!
  uint8_t  DmxByte   = UDR0;          //get data
  uint8_t  DmxState  = gDmxState; //just load once from SRAM to increase speed

  if (DmxState == STARTADR)
  {
    DmxRxField[DmxCount++]= DmxByte;        //get channel
    if (DmxCount >= sizeof(DmxRxField)) //all ch received?
    {
      gDmxState= IDLE;        //wait for next break
    }
  }
  else if (USARTstate &(1<<FE0))               //check for break
  {
    DmxCount =  DmxAddress;         //reset channel counter (count channels before start address)
    gDmxState= BREAK;
  }

  else if (DmxState == BREAK)
  {
    if (DmxByte == 0) gDmxState= STARTB;  //normal start code detected
    else                      gDmxState= IDLE;
  }

  else if (DmxState == STARTB)
  {
    if (--DmxCount == 0)    //start address reached?
    {
      DmxCount= 1;            //set up counter for required channels
      DmxRxField[0]= DmxByte; //get 1st DMX channel of device
      gDmxState= STARTADR;
    }
  }
}

I found this project on internet here. With my Arduino Uno and 4 dimmer modules **this ones **and rs 485 module - the code works well. With nano every I have to change the core files and here I have difficulty.
Sorry, this is because I just bought new x3 nano every boards for this project, but didn't think to have this problem.

Thank you in advance
Stefan

Add before the ISR routine:

#ifndef __XMEGA__
#define UCSR0A USART1_STATUS
#define UDR0 USART1_RXDATAL
#define FE0 USART_FERR_bp
#define USART_RX_vect USART1_RXC_vect
#endif

Note that this uses "Serial1", which I believe is the serial port connected to the pins of the Every ("Serial" is connected only to the USB port), so you'll need to change that in setup() as well. And you still need to find the ISR in the arduino core and remove it (but possibly only for Serial1)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.