UART operations with Interrupt0 on ATmega328

Hi Folks,

I have been missing data while using Interrupt0 on ATmega328 , what would be the possible remedy to it?

Posting your code?

How to use this forum

Read this before posting a programming question

@ Nick and rest of the fellowship! this is the code:

#define Relay0 4;
#define Relay1 5;
#define Relay2 6;
#define Relay3 7;

//--Variables--//
String Data;
int AC_LOAD = 3;    // Output to Opto Triac pin
int dimming = 128;  // Dimming level (0-128)  0 = ON, 128 = OFF

void setup()
{
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  attachInterrupt(0, zero_crosss_int, RISING);  // Choose the zero cross interrupt # from the table above
  Serial.begin(9600);
  for (int i=4; i < 8; i++){
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);
   }  
}



       if(chk == 's'){
         Serial.println(Data);
         Data = Data.replace('s', ' ');
         Data = Data.trim(); 
         char DataChar[4];//char Array to store the char conversion values from DataToInt
         Data.toCharArray(DataChar, sizeof(DataChar));
         intData = atoi(DataChar);
         if (intData == 0) {
             digitalWrite(4, HIGH); 
             Serial.print("here");
         }
         if (intData == 1) {
             digitalWrite(5, HIGH); 
         }
         if (intData == 2) {
             digitalWrite(6, HIGH); 
         }
         if (intData == 3) {
             digitalWrite(7, HIGH); 
         }
         if (intData == 5) {
             digitalWrite(4, LOW); 
         }
         if (intData == 6) {
             digitalWrite(5, LOW); 
         }
         if (intData == 7) {
             digitalWrite(6, LOW); 
         }
         if (intData == 8) {
             digitalWrite(7, LOW); 
         }
       }
}


void clearing() {
        //Serial.print("Clearing ALL");
        for(int i=0;i<21;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();    
}

http://www.gammon.com.au/interrupts

What registers need to be initialised for getting the UART interrupt work:

ISR(USART_RX_vect) {
   
 }

I couldn't get from the datasheet

EDIT:

Now it compiles fine with the (USART_RXC_vect) ,

@ Sir Nick there was a problem in the code when the ISR is defined as (USART_RX_vect) whereas if its (USART_RXC_vect) then it compiles fine for me? why?

it throws the following error with (USART_RX_vect)

core.a(HardwareSerial.cpp.o): In function __vector_18': C:\Users\nishant\Downloads\arduino-0022\hardware\arduino\cores\arduino/HardwareSerial.cpp:81: multiple definition of_vector_18' Bluetooth_Dimmer.cpp.o:C:\Users\nishant\AppData\Local\Temp\build7066994282110513459.tmp/Bluetooth_Dimmer_.cpp:40: first defined here

it throws the following error with (USART_RX_vect)

Yes, well HardwareSerial already implements that interrupt so you can’t have one as well.

If you miss-spell it, the compiler accepts it, but nothing happens, because there is no such interrupt.

Put it another way, there already is a UART interrupt.

Did you read my message about not using delay?

Sir Nick,

Yes I read your message of not using a delay, I moved that delaying code snippet to another function and perhaps now run it on a Boolean true.

I think I got the problem , even if UART uses interrupts Im not getting the serial.reads fine just because of the priority case as the priority of interrupt0 's priority is very high as compared to the USART_RX_vect priority so it will continue to miss the serial.reads until unless I move that interrupt0 to any other interrupt on other pins so that the serial.reads get priority.

Correct?

#define Relay3 7;

Oh dear!

NI$HANT: I think I got the problem , even if UART uses interrupts Im not getting the serial.reads fine just because of the priority case as the priority of interrupt0 's priority is very high as compared to the USART_RX_vect priority so it will continue to miss the serial.reads until unless I move that interrupt0 to any other interrupt on other pins so that the serial.reads get priority.

Perhaps if you post your new code. I don't read minds, you know.

NI$HANT: I moved that delaying code snippet to another function and perhaps now run it on a Boolean true.

This is just mumbo-jumbo without seeing your code.

NI$HANT: Sir Nick

My name is Nick Gammon, you can call me Nick.

Nick, here is the code:

#define Relay0 4;
#define Relay1 5;
#define Relay2 6;
#define Relay3 7;



void loop() {
       while(Serial.available() > 0) // Don't read unless
    // there you know there is data
  {
      inChar = Serial.read(); // Read a character//read the character
      if(inChar =='<') //not sure what to put in if statement to run until end
      {
        started = true;
        ended = false;
        index=0;
      }
      else if(inChar =='>')
      {
        ended = true;
        break;
      } 
      if(started)
      { 
       inData[index] = inChar; // Store it
       index++; // Increment where to write next
       inData[index] = '\0'; // Null terminate the string    
      }
  } 
  if (ended) 
  {
      ended = false;
      Data = inData;
      clearing();
      process();
  }
}

void process() {
       Data = Data.replace('<', ' ');
       Data = Data.replace('>', ' ');
       Data = Data.trim();
       char chk = Data.charAt(0);
       if(chk == 'd'){
         Serial.println(Data);
         Data = Data.replace('d', ' ');
         Data = Data.trim(); 
         char DataChar[4];//char Array to store the char conversion values from DataToInt
         Data.toCharArray(DataChar, sizeof(DataChar));
         intData = atoi(DataChar);
         //Serial.println(intData);
         dimming = intData;
       }
       if(chk == 's'){
         Serial.println(Data);
         Data = Data.replace('s', ' ');
         Data = Data.trim(); 
         char DataChar[4];//char Array to store the char conversion values from DataToInt
         Data.toCharArray(DataChar, sizeof(DataChar));
         intData = atoi(DataChar);
         if (intData == 0) {
             digitalWrite(4, HIGH); 
             Serial.print("here");
         }
         if (intData == 1) {
             digitalWrite(5, HIGH); 
         }
         if (intData == 2) {
             digitalWrite(6, HIGH); 
         }
         if (intData == 3) {
             digitalWrite(7, HIGH); 
         }
         if (intData == 5) {
             digitalWrite(4, LOW); 
         }
         if (intData == 6) {
             digitalWrite(5, LOW); 
         }
         if (intData == 7) {
             digitalWrite(6, LOW); 
         }
         if (intData == 8) {
             digitalWrite(7, LOW); 
         }
       }
   
}


void clearing() {
        //Serial.print("Clearing ALL");
        for(int i=0;i<21;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();    
}

I was thinking to implement Pin Change Interrupt Request 2 (pins D0 to D7) (PCINT2_vect) but its priority is also very high as compared to the USART_RX_vect so it will always put my serial.reads back, what to do in such a case?

     dimmer == false;

That doesn't do anything useful.

That doesn't do anything useful.

changed it wrongfully wrote that, but what about the priority?

volatile int AC_LOAD = 3;    // Output to Opto Triac pin

Why "volatile"? Why not "const"? (and, as a side-order, why not "byte"?)

Why "volatile"? Why not "const"? (and, as a side-order, why not "byte"?)

I will use that variable in interrupt

So? If it never changes it can be const.

I see where you are going with interrupt priority, but the rate of interrupts for 9600 baud is quite slow. I suspect something else is wrong, for example, your debugging prints.

9600 baud is quite slow

I thought of the same case but even I then I don’t think that I will get my Serial.reads read correctly as it will still keep on creating interrupts

Show us your debugging output.