sábado, 25 de dezembro de 2010

Comunicação entre 2 Arduinos por Rádio (Final)

Software de Recepção para Comunicação entre 2 Arduinos por Rádio

Este é o software de recepção, que utiliza em parte as rotinas desenvolvidas por Maurice Ribble (30/08/2009), disponíveis em http://www.glacialwanderer.com/hobbyrobotics.

/*
Projeto:
   Transmissão de dados por RF entre duas placas Arduino

Objetivos:
   Experimentar a transmissão de dados por RF entre duas placas Arduino
   Experimentar os módulos de transmissão e recepção de RF
   Estabelecer os fundamentos para futuras experiências envolvendo controle remoto
  
Componentes:
   2 Placas Arduino Duemilanove com ATmega328
   1 módulo transmissor de RF de 315 MHz
   1 módulo receptor de RF de 315 MHz

Autor: Karl Heinz Benz
Data: 23/12/2010
Correções, sugestões e nova documentação devem ser enviadas a karlbenz@terra.com.br
Licenciamento: Atribuição-Uso não-comercial-Compartilhamento pela mesma licença 3.0 Brasil
http://creativecommons.org/licenses/by-nc-sa/3.0/br/

Utilização do pacote de software desenvolvido por:
   Maurice Ribble
   30/08/2009
   http://www.glacialwanderer.com/hobbyrobotics
*/

// Módulo de RECEPÇÃO

#define LED_PIN     13

void setup()
{
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(1200);  // Hardware suporta até 2400, mas 1200 fornece maior alcance.
}

void loop()
{
  Serial.println(readUInt(true));
  digitalWrite(LED_PIN, HIGH);
  delay(50);
  digitalWrite(LED_PIN, LOW);
}

// Maurice Ribble
// 30/08/2009
// http://www.glacialwanderer.com/hobbyrobotics
// This does some error checking to try to make sure the receiver on this one way RF
//  serial link doesn't repond to garbage

#define NETWORK_SIG_SIZE 3

#define VAL_SIZE         2
#define CHECKSUM_SIZE    1
#define PACKET_SIZE      (NETWORK_SIG_SIZE + VAL_SIZE + CHECKSUM_SIZE)

// The network address byte and can be change if you want to run different devices in proximity to each other without interfearance
#define NET_ADDR 5

const byte g_network_sig[NETWORK_SIG_SIZE] = {0x8F, 0xAA, NET_ADDR};  // Few bytes used to initiate a transfer

// Receives an word over the RF network
word readUInt(bool wait)
{
  int pos = 0;          // Position in the network signature
  word val;             // Value of the unsigned int
  byte c = 0;           // Current byte
 
  if((Serial.available() < PACKET_SIZE) && (wait == false))
  {
    return 0xFFFF;
  }
 
  while(pos < NETWORK_SIG_SIZE)
  {
    while(Serial.available() == 0); // Wait until something is avalible
    c = Serial.read();

    if (c == g_network_sig[pos])
    {
      if (pos == NETWORK_SIG_SIZE-1)
      {
        byte checksum;

        while(Serial.available() < VAL_SIZE + CHECKSUM_SIZE); // Wait until something is avalible
        val      =  Serial.read();
        val      += ((unsigned int)Serial.read())*256;
        checksum =  Serial.read();
       
        if (checksum != ((val/256) ^ (val&0xFF)))
        {
          // Checksum failed
          pos = -1;
        }
      }
      ++pos;
    }
    else if (c == g_network_sig[0])
    {
      pos = 1;
    }
    else
    {
      pos = 0;
      if (!wait)
      {
        return 0xFFFF;
      }
    }
  }
  return val;
}

Nenhum comentário:

Postar um comentário