Go Down

Topic: Timer1 interrupit problem (Read 431 times) previous topic - next topic

May 03, 2013, 05:16 pm Last Edit: May 03, 2013, 06:29 pm by paulolinhares Reason: 1
the code has compiled, but don't work.

the code should call the function ping every 5 seconds if it does not receive "the command" via serial port, but it hangs on the call interruption and not run but nothing.


Code: [Select]
#include <TimerOne.h>
byte MEUEND=B00001111;
byte OUTEND=B00000000;
byte MEND=B00001111;
byte MEND2=B11110000;
byte MTIPO=B00000011;
void setup()
{
 Serial.begin(9600);
 Serial.flush();
 
}

void envia(Mensagem Msg)
{
 Serial.println(Msg.Fcontrole);
 Serial.println(Msg.msg.id);
 Serial.println(Msg.msg.dados);
 Serial.println(Msg.msg.payload);
 Serial.println(Msg.verifica);
 Serial.println(Msg.numseq);
 Serial.flush();
}
Mensagem recebe()
{
 Mensagem Msg;
 Msg.Fcontrole=Serial.read();  
 Msg.msg.id=Serial.read();
 Msg.msg.dados=Serial.read();
 Msg.msg.payload=Serial.read();
 Msg.verifica=Serial.read();
 Msg.numseq=Serial.read();  
 Serial.flush();
 return(Msg);
}
void trata(Mensagem Msg, int* y)    //função que recebe a mensagem
{
 byte endereco;
 endereco=Msg.Fcontrole>>4;
 endereco= endereco&MEND;
 if (endereco == MEUEND)  //vê se foi eu quem mandou
 return;
 if (endereco == OUTEND)
 {
   byte tipo;
   tipo = Msg.Fcontrole&MTIPO;
   if(tipo==0)
   if(Msg.msg.id=='p') //testa pra ver se é ping
   {
     pong();
   }
   else
   {
     *y=1;
     return;
   }
 }
}
void ping ()
{
 Mensagem Msg;
 Msg.Fcontrole = MEUEND;
 Msg.Fcontrole=Msg.Fcontrole<<4;
 Msg.Fcontrole=Msg.Fcontrole&MEND2;
 Msg.msg.id='p';
 envia(Msg);
}
void pong()
{
 Mensagem Msg;
 Msg.Fcontrole = OUTEND;       //pra testar com 1 arduino só usar OUTEN
 Msg.Fcontrole=Msg.Fcontrole<<4;
 Msg.Fcontrole=Msg.Fcontrole&MEND2;
 Msg.msg.id='q';
 envia(Msg);
}

void loop()
{
 Serial.flush();
 Mensagem Msg;
 int x=0;
 int y=0;
 for(;x==0;y=0)
 {
   ping();
   Timer1.initialize(5000000);
   Timer1.attachInterrupt(ping);
   while(y==0)
   {
     
       if(Serial.available()>5)
       {
         trata(recebe(),&y);
       }
   }
   Timer1.detachInterrupt();      
 }
}


PS.: the struck Mensagem and Req is declared in TimeOne.h

Arrch


but don't work.


That's a piss poor way of describing an issue. Do you drop your car off at the mechanic with a note that says "it doesn't work, fix it please"?

The code does something. You want it to do something else. The difference is a mystery to us. Also, use CODE tags when posting code.



but don't work.


That's a piss poor way of describing an issue. Do you drop your car off at the mechanic with a note that says "it doesn't work, fix it please"?

The code does something. You want it to do something else. The difference is a mystery to us. Also, use CODE tags when posting code.



I fix.

Henry_Best


the code has compiled, but don't work.

the code should call the function ping every 5 seconds if it does not receive "the command" via serial port, but it hangs on the call interruption and not run but nothing.


What is this line supposed to do?
Code: [Select]

 for(;x==0;y=0)


Go Up