Serial interrupt

have you guys any idea used this interrupt and how to use it in arduino IDE "isr(usart0_rx_vect)" its an interrupt for the serial.
thanks

It's already used by the interrupt-driven buffered serial driver.
Why do you want to use it?

how do you actually right a code that reads what happens is when im transmitting in the serial port it receives the data but it does not do something about the data it just keep on transmitting what it needs to transmit but if its not transmitting it handles the receive data perfectly. i really dont understand whats wrong.

if (Serial.available() > 0) // it does not execute this when TX is transmitting data
{
go to data processing
}

I've had a board with the same processor as a Mega running all four serial ports at 115200 simultaneously and continuously without problems.
Where's your code?

thanks can you check my code. here is the code.

byte TFVDC3 = 40;
int pcbuffer[5];               // data receive from PC
boolean stopreceive = false;
int i = 0;
int sonarPIN1 = A0;
short sonardata1;
int TxDataBuffer[50];

void setup()
{
    pinMode(TFVDC3, OUTPUT);
    Serial.begin(9600);
    TxDataBuffer[0] = '

;
    interrupts();
    Serial.write("USART initiallize");
}
 
void loop()
{
  int n = 0;
  ultrasonic1();
  while (Serial.available())
      {
        pcreceivedata();              // if data is receive on the serial buffer check it
      }
do
  {
      Serial.write(TxDataBuffer[n]);
      n++;
  }while(n < 3); 
}

void pcreceivedata()
{
  int tempbuffer;
  int startrx = Serial.read();
  if (startrx == '


)                // check for $ in the serial buffer
     {
           do 
             {
                 pcbuffer[0] = '

;
                tempbuffer = Serial.read();
                pcbuffer[i] = tempbuffer;
                i++;
                delay(5);
              }while (tempbuffer != '*');
            stopreceive = true;
            portswitch();
            i = 0; 
      }     
}

void portswitch()
{
  digitalWrite(TFVDC3,!digitalRead(TFVDC3));
}

void ultrasonic1()
{   
  sonardata1 = analogRead(sonarPIN1);
  sonardata1 = map(sonardata1, 0, 1023, 0, 255);
  TxDataBuffer[1] = sonardata1;
}

Did you post something like this earlier?
You're waiting in a single routine for a whole string to arrive.
At 9600, that's over a millisecond a character.

void pcreceivedata()
{
   int tempbuffer;
   int startrx = Serial.read();
   if (startrx == '

This seems like a very bad way to handle serial communication.

Using the below method will allow your micro to do other things between receiving bytes from serial:

void pcreceivedata() {
  static int i; // buffer index

  char inChar = Serial.read();
  if (inChar == '

)                // check for $ in the serial buffer
    {
          do
            {
                pcbuffer[0] = '


This seems like a very bad way to handle serial communication. 

Using the below method will allow your micro to do other things between receiving bytes from serial:

§DISCOURSE_HOISTED_CODE_1§


;
                 tempbuffer = Serial.read();
                 pcbuffer[i] = tempbuffer;
                 i++;
                 delay(5);
              }while (tempbuffer != '*');
            stopreceive = true;
            portswitch();
            i = 0;  
      }       
}

This seems like a very bad way to handle serial communication.

Using the below method will allow your micro to do other things between receiving bytes from serial:

§_DISCOURSE_HOISTED_CODE_1_§

) {
   // Reset index to 1, set 0 element to '


)                // check for $ in the serial buffer
     {
           do 
             {
                 pcbuffer[0] = '

This seems like a very bad way to handle serial communication.

Using the below method will allow your micro to do other things between receiving bytes from serial:

§_DISCOURSE_HOISTED_CODE_1_§

;
                tempbuffer = Serial.read();
                pcbuffer[i] = tempbuffer;
                i++;
                delay(5);
             }while (tempbuffer != '*');
           stopreceive = true;
           portswitch();
           i = 0;  
     }      
}


This seems like a very bad way to handle serial communication. 

Using the below method will allow your micro to do other things between receiving bytes from serial:

§DISCOURSE_HOISTED_CODE_1§



  } else if (inChar == '*') {
    // Set last byte to '*', set stopreceive to true, etc..
  } else {
    // add inChar to buffer, increment index
  }
}

)                // check for $ in the serial buffer
    {
          do
            {
                pcbuffer[0] = '


This seems like a very bad way to handle serial communication. 

Using the below method will allow your micro to do other things between receiving bytes from serial:

§DISCOURSE_HOISTED_CODE_1§


;
                 tempbuffer = Serial.read();
                 pcbuffer[i] = tempbuffer;
                 i++;
                 delay(5);
              }while (tempbuffer != '*');
            stopreceive = true;
            portswitch();
            i = 0;  
      }       
}

This seems like a very bad way to handle serial communication.

Using the below method will allow your micro to do other things between receiving bytes from serial:

§_DISCOURSE_HOISTED_CODE_1_§

@ AWOL: Yeah its just waiting for command comming from PC. its more of a data gathering system and sending it to PC.

@ARRCH: i will test the code that you have provided and see. thanks XD

@ARRCH i tested the code that you sent me. it does not do anything. something is wrong with it. here is what i have done

void pcreceivedata()
{
     static byte i;
     char inchar = Serial.read();
     if (inchar == '

)
        {
          i = 1;
          pcbuffer[0] = '


;
         }
     else if (inchar == '*')
         {
           pcbuffer[2] = '*';
           i = 0;
           portswitch();
         }
     else
         {
           pcbuffer[i] = inchar;
           i++;
         }
}
pcbuffer[2] = '*';

What, exactly, is being sent to the serial buffer? By using the immediate value '2' here, it seems to indicate that every transmission is "$X*" where 'X' is a single character. If that's the case, where every single transmission is 3 characters, you can just wait until Serial.Available is at least 3 and read in the data from there. What are you getting Serial data from?

im getting the data from a PC. '$' is like header or start of data and '*' is the end of data.

the problem now is it does not go to the portswitch() function to turn LED on.

Rolandchok:
im getting the data from a PC. '$' is like header or start of data and '*' is the end of data.

the problem now is it does not go to the portswitch() function to turn LED on.

Open up the serial monitor, set the correct baud rate, and just enter the character '*'. It should run the portswitch() function. If it's not, then it's not a problem with the code I provided.

yeah the serial monitor is echoing the right strings what is should do trying to figure out what is wrong

Rolandchok:
yeah the serial monitor is echoing the right strings what is should do trying to figure out what is wrong

Does portswitch() run? You've been really vague about what you're trying to do, and what exactly is going wrong, so it's really hard to help you troubleshoot.

Post your entire code, as you have it now, what you're expecting it to do, and what it is doing.

here is my whole code. this is killing me. i just migrated from PIC to arduino.

byte TWVDC1 = 35;              // 12VDC 1
byte TWVDC2 = 36;              // 12VDC 2
byte TWVDC3 = 37;              // 12VDC 3 
byte TFVDC1 = 38;              // 24VDC 1
byte TFVDC2 = 39;              // 24VDC 2
byte TFVDC3 = 40;              // 24VDC 3
byte TFVDC4 = 41;              // 24VDC 4
int pcbuffer[2];               // data receive from PC
boolean stopreceive = false;
int i = 0;
int sonarPIN1 = A0;
short sonardata1;
int TxDataBuffer[50];

void setup()
{
    pinMode(TWVDC1, OUTPUT);
    pinMode(TWVDC2, OUTPUT);
    pinMode(TWVDC3, OUTPUT);
    pinMode(TFVDC1, OUTPUT);
    pinMode(TFVDC2, OUTPUT);
    pinMode(TFVDC3, OUTPUT);
    Serial.begin(1200);
    TxDataBuffer[0] = '

;
   interrupts();
   Serial.write("USART initiallize");
   digitalWrite(pc,HIGH);
   delay(500);
   digitalWrite(pc,LOW);
}

void loop()
{
 int n = 0;
 ultrasonic1();
 while (Serial.available())
     {
       pcreceivedata();        // if data is receive on the serial buffer check it
       
     }
 if ( pcbuffer[2] == '*')
   {
      do
         {
           Serial.write(pcbuffer[n]);
           n++;
         }while(n != 3);
      i = 0;
    }
}

void pcreceivedata()
{
    char inchar = Serial.read();
    if (inchar == '


)
         {
           i = 1;
           pcbuffer[0] = '

;
        }
    else if (inchar == '')
        {
          pcbuffer[i] = '
';
         // digitalWrite(pc,!digitalRead(pc));
          portswitch();
        }
    else
        {
          pcbuffer[i] = inchar;
          i++;
        }
}

void portswitch()
{
  switch (pcbuffer[1])
    {      
        case 0xB0:
          digitalWrite(TWVDC1,!digitalRead(TWVDC1));
          break;
       case 0xB1:
          digitalWrite(TWVDC2,!digitalRead(TWVDC2));
          break;        
       case 0xB2:
          digitalWrite(TWVDC3,!digitalRead(TWVDC3));
          break;
       case 0xB6:
          digitalWrite(TFVDC1,!digitalRead(TFVDC1));
          break;
       case 0xB7:
          digitalWrite(TFVDC2,!digitalRead(TFVDC2));
          break;
       case 0xB8:
          digitalWrite(TFVDC3,!digitalRead(TFVDC3));
          break;
       case 0xB9:
          digitalWrite(TFVDC4,!digitalRead(TFVDC4));
          break;
    }
}

void ultrasonic1()
{    
 sonardata1 = analogRead(sonarPIN1);
 sonardata1 = map(sonardata1, 0, 1023, 0, 255);
 TxDataBuffer[1] = sonardata1;
}

Rolandchok:
here is my whole code. this is killing me. i just migrated from PIC to arduino.

You missed this part:

what you're expecting it to do, and what it is doing.

...And never use the phrase "it's not working" on the forums.

i just want to switch a led on and off using a "switch case" statement. but its not switching at all. 1st time to encounter this kind of problem.

in this part when i use the digitalwrite works fine but with switch case statement led not switching state.

else if (inchar == '')
{
pcbuffer _= '
';_

  • // digitalWriteTWVDC1,!digitalRead(TWVDC1));*
  • portswitch();*
  • }*
    ```
    *byte TWVDC1 = 35;              // 12VDC 1
    byte TWVDC2 = 36;              // 12VDC 2
    byte TWVDC3 = 37;              // 12VDC 3
    byte TFVDC1 = 38;              // 24VDC 1
    byte TFVDC2 = 39;              // 24VDC 2
    byte TFVDC3 = 40;              // 24VDC 3
    byte TFVDC4 = 41;              // 24VDC 4
    int pcbuffer[2];               // data receive from PC
    boolean stopreceive = false;
    int i = 0;
    int sonarPIN1 = A0;
    short sonardata1;
    int TxDataBuffer[50];

void setup()
{
   pinMode(TWVDC1, OUTPUT);
   pinMode(TWVDC2, OUTPUT);
   pinMode(TWVDC3, OUTPUT);
   pinMode(TFVDC1, OUTPUT);
   pinMode(TFVDC2, OUTPUT);
   pinMode(TFVDC3, OUTPUT);
   Serial.begin(1200);
   TxDataBuffer[0] = '*
```
;
   interrupts();
   Serial.write("USART initiallize");
   digitalWrite(pc,HIGH);
   delay(500);
   digitalWrite(pc,LOW);
}

void loop()
{
 int n = 0;
 ultrasonic1();
 while (Serial.available())
     {
       pcreceivedata();        // if data is receive on the serial buffer check it
       
     }
 if ( pcbuffer[2] == '*')
   {
      do
         {
           Serial.write(pcbuffer[n]);
           n++;
         }while(n != 3);
      i = 0;
    }
}

void pcreceivedata()
{
    char inchar = Serial.read();
    if (inchar == '*
* )         {           i = 1;           pcbuffer[0] = '* *
;
        }
    else if (inchar == '')
        {
          pcbuffer[i] = '
';
         // digitalWrite(pc,!digitalRead(pc));
          portswitch();
        }
    else
        {
          pcbuffer[i] = inchar;
          i++;
        }
}

void portswitch()
{
  switch (pcbuffer[1])
    {      
        case 0xB0:
          digitalWrite(TWVDC1,!digitalRead(TWVDC1));
          break;
       case 0xB1:
          digitalWrite(TWVDC2,!digitalRead(TWVDC2));
          break;        
       case 0xB2:
          digitalWrite(TWVDC3,!digitalRead(TWVDC3));
          break;
       case 0xB6:
          digitalWrite(TFVDC1,!digitalRead(TFVDC1));
          break;
       case 0xB7:
          digitalWrite(TFVDC2,!digitalRead(TFVDC2));
          break;
       case 0xB8:
          digitalWrite(TFVDC3,!digitalRead(TFVDC3));
          break;
       case 0xB9:
          digitalWrite(TFVDC4,!digitalRead(TFVDC4));
          break;
    }
}

void ultrasonic1()
{    
 sonardata1 = analogRead(sonarPIN1);
 sonardata1 = map(sonardata1, 0, 1023, 0, 255);
 TxDataBuffer[1] = sonardata1;
}*
```

Rolandchok:
i just want to switch a led on and off using a "switch case" statement. but its not switching at all. 1st time to encounter this kind of problem.

Are your sure those case values are correct? Those are some pretty high ASCII values. What are you putting in between your '$' and '*'?

Try adding a serial print in there so you can see the hex value of the character you are checking:

void portswitch()
{
   Serial.print("pcbuffer[1]: "); Serial.println(pcbuffer[1], HEX);
   switch (pcbuffer[1])
   ...
}

its not jumping to the switch case statement. not printing anything.

Rolandchok:
its not jumping to the switch case statement. not printing anything.

Then move the Serial prints to where you have the digitalwrite that was working.