Pages: [1]   Go Down
Author Topic: Timer1 interrupit problem  (Read 389 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
« Last Edit: May 03, 2013, 11:29:41 am by paulolinhares » Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 for(;x==0;y=0)
 
Logged

Pages: [1]   Go Up
Jump to: