Need programming help!

Hello guys!

I have two WiFly shields on the Ardunio Uno Board. Both operate in the AdHoc mode. On one board I placed two buttons and two LED. When a button is pressed, the corresponding LED. This is sent via UDP to the other module. The program code of the transmitter:

#include <string.h>

// SC16IS750 Register definitions
#define THR        0x00 << 3
#define RHR        0x00 << 3
#define IER        0x01 << 3
#define FCR        0x02 << 3
#define IIR        0x02 << 3
#define LCR        0x03 << 3
#define MCR        0x04 << 3
#define LSR        0x05 << 3
#define MSR        0x06 << 3
#define SPR        0x07 << 3
#define TXFIFO     0x08 << 3
#define RXFIFO     0x09 << 3
#define DLAB       0x80 << 3
#define IODIR      0x0A << 3
#define IOSTATE    0x0B << 3
#define IOINTMSK   0x0C << 3
#define IOCTRL     0x0E << 3
#define EFCR       0x0F << 3

#define DLL        0x00 << 3
#define DLM        0x01 << 3
#define EFR        0x02 << 3
#define XON1       0x04 << 3  
#define XON2       0x05 << 3
#define XOFF1      0x06 << 3
#define XOFF2      0x07 << 3

// SPI Pin definitions
#define CS         10
#define MOSI       11
#define MISO       12
#define SCK        13

#define ASSOCIATE_TIMEOUT 5000

// Global variables
char incoming_data; 
char TX_Fifo_Address = THR; 

int i = 0;
int j = 0;
int k = 0;
char clr = 0;
char polling = 0;

// SC16IS750 register values
struct SPI_UART_cfg
{
  char DivL,DivM,DataFormat,Flow;
};

struct SPI_UART_cfg SPI_Uart_config = 
{ 0x60,0x00,0x03,0x10};


// PIN Belegung
int SW1 = 2;         // grüner Taster auf PIN 2 - AUF
int SW2 = 3;         // roter Taster auf PIN 3  - ZU
int LED_gruen = 4;   // grüne LED auf PIN 4     - AUF
int LED_rot = 5;     // rote LED auf PIN 5      - ZU

// Ausgangszustand der Taste
int SW1prev = 0;
int SW2prev = 0;

// Zustand der Taster nach einer Aktion
int SW1value;
int SW2value;

void setup() 
{
 
  // Initialize SPI pins
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  pinMode(SCK,OUTPUT);
  pinMode(CS,OUTPUT);
  digitalWrite(CS,HIGH); //disable device 

  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
  clr=SPSR;
  clr=SPDR;
  delay(10); 
  
  
   // LED`s als Output
  pinMode(LED_gruen, OUTPUT); 
  pinMode(LED_rot, OUTPUT);
  
  // grüner Taster als Input und auf High gesetzt
  pinMode(SW1, INPUT); 
  digitalWrite(SW1, HIGH); // SW1 = 1
  
  // roter Taster als Input und auf High gesetzt
  pinMode(SW2, INPUT);
  digitalWrite(SW2, HIGH); // SW2 = 1

      // grüne LED Test - ein mal blinken
  digitalWrite(LED_gruen, HIGH);
  delay(1000);
  digitalWrite(LED_gruen, LOW);
  
  // rote LED Test - ein mal blinken
  digitalWrite(LED_rot, HIGH);  
  delay(1000);
  digitalWrite(LED_rot, LOW);
  
  Serial.begin(9600);  // Baudrate
  
  Serial.println("                WiFly 1 - UDP Uebertragung         ");
  Serial.println("--------------------------------------------------");
  Serial.println();
  
  Serial.println("Baudrate: 9600");
  Serial.println();
  
  // Initalisierung und testen von SC16IS750
  if(SPI_Uart_Init()) //Methodenaufruf
  { 
    Serial.println("Bridge erfolgreich initialisiert!"); 
  }
  else
  { 
    Serial.println("Bridge konnte nicht initialisert werden, blockieren.\n\r"); 
    while(1); 
  }

  //Methodenaufruf
  autoconnect(); // AdHoc Netzaufbau
   
  SW1value = digitalRead(SW1);
  Serial.println(SW1value);
  
  //SW2value = digitalRead(SW2);
  Serial.println(digitalRead(SW2));
} // setup Ende


void loop()
{
  //SW1prev = 0
  //SW1value = 0
  
 SW1value = digitalRead(SW1);
  
 // SW1prev = SW1value;
  while(SW1prev = SW1value)
  {
    if(SW1value == 1)
    {
      select();
      spi_transfer(0x00); // Transmit command
      spi_transfer('A');
      deselect();
  //  SPI_Uart_println("AAAA");
      Serial.println("A");
      digitalWrite(LED_gruen, HIGH);
      delay(2000);           
      digitalWrite(LED_gruen, LOW);
      SW1value = 0;
    }
 
  SW2value = digitalRead(SW2);
  
 // SW2prev = SW2value;
  while(SW2prev = SW2value)
  {
    if(SW2value == 1)
    {
      select();
      spi_transfer(0x00); // Transmit command
      spi_transfer('Z');
      deselect();
//      SPI_Uart_println("ZZZZ");
      Serial.println("Z");
      digitalWrite(LED_rot, HIGH);
      delay(2000);           
      digitalWrite(LED_rot, LOW);
      SW2value = 0;
    }
  } 
    
  // Terminal routine
  if(SPI_Uart_ReadByte(LSR) & 0x01) // Incoming data
  { 
    polling = 1;
    i = 0;   
    while(polling)
    {     
      if((SPI_Uart_ReadByte(LSR) & 0x01))
      {
        incoming_data = SPI_Uart_ReadByte(RHR);
        Serial.print(incoming_data,BYTE);
        //Serial.print(incoming_data, HEX);
        //Serial.print(" ");
      }  
      else
      {
        polling = 0;
      }
    }
  }
  else if(Serial.available()) // Outgoing data
  {
    incoming_data = Serial.read();
    select();
    spi_transfer(0x00); // Transmit command
    spi_transfer(incoming_data);
    deselect();
  }
  
} // loop Ende

void select(void) //Methode
{
  digitalWrite(CS,LOW);
}


void deselect(void) //Methode
{
  digitalWrite(CS,HIGH);
}


char SPI_Uart_Init(void) // Initialize and test SC16IS750  //Methode
{
  char data = 0;

  SPI_Uart_WriteByte(LCR,0x80); // 0x80 to program baudrate
  SPI_Uart_WriteByte(DLL,SPI_Uart_config.DivL); //0x50 = 9600 with Xtal = 12.288MHz
  SPI_Uart_WriteByte(DLM,SPI_Uart_config.DivM); 

  SPI_Uart_WriteByte(LCR, 0xBF); // access EFR register
  SPI_Uart_WriteByte(EFR, SPI_Uart_config.Flow); // enable enhanced registers
  SPI_Uart_WriteByte(LCR, SPI_Uart_config.DataFormat); // 8 data bit, 1 stop bit, no parity
  SPI_Uart_WriteByte(FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
  SPI_Uart_WriteByte(FCR, 0x01); // enable FIFO mode

  // Perform read/write test to check if UART is working
  SPI_Uart_WriteByte(SPR,'H');
  data = SPI_Uart_ReadByte(SPR);

  if(data == 'H')
  { 
    return 1; 
  }
  else
  { 
    return 0; 
  }

}


void SPI_Uart_WriteByte(char address, char data) //Methode
// Write <data> byte to SC16IS750 register <address>
{
  long int length;
  char senddata[2];
  senddata[0] = address;
  senddata[1] = data;

  select();
  length = SPI_Write(senddata, 2);
  deselect();
}


long int SPI_Write(char* srcptr, long int length) //Methode
// Write string to SC16IS750
{
  for(long int i = 0; i < length; i++)
  {
    spi_transfer(srcptr[i]);
  }
  return length; 
}


void SPI_Uart_WriteArray(char *data, long int NumBytes) //Methode
// Write string to SC16IS750 THR
{
  long int length;
  select();
  length = SPI_Write(&TX_Fifo_Address,1);

  while(NumBytes > 16)
  {
    length = SPI_Write(data,16);
    NumBytes -= 16;
    data += 16;
  }
  length = SPI_Write(data,NumBytes);

  deselect();
}


char SPI_Uart_ReadByte(char address) //Methode
// Read byte from SC16IS750 register at <address>
{
  char data;

  address = (address | 0x80);

  select();
  spi_transfer(address);
  data = spi_transfer(0xFF);
  deselect();
  return data;  
}

char autoconnect(void) //Methode
{
  // Exit command mode if we haven't already
  SPI_Uart_println("");  
  SPI_Uart_println("exit");
  delay(500);

  // Enter command mode 
  SPI_Uart_print("$$");
  delay(500);

  // Reboot to get device into known state
  Serial.println();
  Serial.println("Rebooting");
  SPI_Uart_println("reboot");
  delay(3000);
  
  Serial.println("ready");

}


void Flush_RX(void) //Methode
// Flush characters from SC16IS750
{
  int j = 0;
  while(j < 1000)
  {
    if((SPI_Uart_ReadByte(LSR) & 0x01))
    {
      incoming_data = SPI_Uart_ReadByte(RHR);
    }  
    else
    {
      j++;
    }
  }
}

 
char Wait_On_Response_Char(char num) //Methode
// Wait for incoming char number <num> and return it
{
  i = 1;
  while(1)
  {
    if((SPI_Uart_ReadByte(LSR) & 0x01))
    {
      incoming_data = SPI_Uart_ReadByte(RHR);
      //Serial.print(incoming_data, BYTE);
      if(i == num){ 
        return incoming_data; 
      }
      else{ 
        i++; 
      }
    }  
  }
}


void SPI_Uart_println(char *data) //Methode
// Write array to SC16IS750 followed by a carriage return
{
  SPI_Uart_WriteArray(data,strlen(data));
  SPI_Uart_WriteByte(THR, 0x0d);
}


void SPI_Uart_print(char *data) //Methode
// Routine to write array to SC16IS750 using strlen instead of hardcoded length
{
  SPI_Uart_WriteArray(data,strlen(data));
}

 
char spi_transfer(volatile char data) //Methode
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

Sending work. I can look in the data mode, the receiver also that arrive “A” or “Z”. I would like to read this incoming data and process and thereby make the respective LED lights. The program that I wrote, it does not, unfortunately:

#include <string.h>

// SC16IS750 Register definitions
#define THR        0x00 << 3
#define RHR        0x00 << 3
#define IER        0x01 << 3
#define FCR        0x02 << 3
#define IIR        0x02 << 3
#define LCR        0x03 << 3
#define MCR        0x04 << 3
#define LSR        0x05 << 3
#define MSR        0x06 << 3
#define SPR        0x07 << 3
#define TXFIFO     0x08 << 3
#define RXFIFO     0x09 << 3
#define DLAB       0x80 << 3
#define IODIR      0x0A << 3
#define IOSTATE    0x0B << 3
#define IOINTMSK   0x0C << 3
#define IOCTRL     0x0E << 3
#define EFCR       0x0F << 3

#define DLL        0x00 << 3
#define DLM        0x01 << 3
#define EFR        0x02 << 3
#define XON1       0x04 << 3  
#define XON2       0x05 << 3
#define XOFF1      0x06 << 3
#define XOFF2      0x07 << 3

// SPI Pin definitions
#define CS         10
#define MOSI       11
#define MISO       12
#define SCK        13

#define ASSOCIATE_TIMEOUT 5000

// Global variables
char incoming_data; 
char TX_Fifo_Address = THR; 

int i = 0;
int j = 0;
int k = 0;
char clr = 0;
char polling = 0;

// SC16IS750 register values
struct SPI_UART_cfg
{
  char DivL,DivM,DataFormat,Flow;
};

struct SPI_UART_cfg SPI_Uart_config = 
{ 0x60,0x00,0x03,0x10};


// PIN Belegung
int LED_gruen = 4;   // grüne LED auf PIN 4     - AUF
int LED_rot = 5;     // rote LED auf PIN 5      - ZU


void setup() 
{
 
  // Initialize SPI pins
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  pinMode(SCK,OUTPUT);
  pinMode(CS,OUTPUT);
  digitalWrite(CS,HIGH); //disable device 

  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
  clr=SPSR;
  clr=SPDR;
  delay(10); 
  
  
   // LED`s als Output
  pinMode(LED_gruen, OUTPUT); 
  pinMode(LED_rot, OUTPUT);

  // grüne LED Test - ein mal blinken
  digitalWrite(LED_gruen, HIGH);
  delay(1000);
  digitalWrite(LED_gruen, LOW);
  
  // rote LED Test - ein mal blinken
  digitalWrite(LED_rot, HIGH);  
  delay(1000);
  digitalWrite(LED_rot, LOW);
  

  Serial.begin(9600);  // Baudrate
  
  Serial.println("                WiFly 2 - UDP Uebertragung         ");
  Serial.println("--------------------------------------------------");
  Serial.println();
  
  Serial.println("Baudrate: 9600");
  Serial.println();
  
  // Initalisierung und testen von SC16IS750
  if(SPI_Uart_Init()) //Methodenaufruf
  { 
    Serial.println("Bridge erfolgreich initialisiert!"); 
  }
  else
  { 
    Serial.println("Bridge konnte nicht initialisert werden, blockieren.\n\r"); 
    while(1); 
  }

  
  autoconnect(); //Methodenaufruf
 
} // setup Ende


void loop()
{
  char cmd;

   if(SPI_Uart_ReadByte(LSR) & 0x01) // Incoming data
   {
      
    cmd = SPI_Uart_ReadByte(RHR);
    Serial.println( cmd);
    Serial.println();
   
    if(cmd == 'A')
    {
       Serial.println("A");
       digitalWrite(LED_gruen, HIGH);
       delay(2000);                    
       digitalWrite(LED_gruen, LOW);
    }          
  
    if(cmd == 'Z')
    {
      Serial.println("Z");
      digitalWrite(LED_rot, HIGH);   
      delay(2000);                  
      digitalWrite(LED_rot, LOW); 
    }
    
    
    // Terminal routine
    polling = 1;
    i = 0;
    while(polling)
    {
      if((SPI_Uart_ReadByte(LSR) & 0x01))
      {
        incoming_data = SPI_Uart_ReadByte(RHR);
        Serial.print(incoming_data,BYTE);
        //Serial.print(incoming_data, HEX);
        //Serial.print(" ");
      }  
      else
      {
        polling = 0;
      }
    }

  }
  else if(Serial.available()) // Outgoing data
  {
    incoming_data = Serial.read();
    select();
    spi_transfer(0x00); // Transmit command
    spi_transfer(incoming_data);
    deselect();
  }
 
} // loop Ende

void select(void) //Methode
{
  digitalWrite(CS,LOW);
}


void deselect(void) //Methode
{
  digitalWrite(CS,HIGH);
}


char SPI_Uart_Init(void) // Initialize and test SC16IS750  //Methode
{
  char data = 0;

  SPI_Uart_WriteByte(LCR,0x80); // 0x80 to program baudrate
  SPI_Uart_WriteByte(DLL,SPI_Uart_config.DivL); //0x50 = 9600 with Xtal = 12.288MHz
  SPI_Uart_WriteByte(DLM,SPI_Uart_config.DivM); 

  SPI_Uart_WriteByte(LCR, 0xBF); // access EFR register
  SPI_Uart_WriteByte(EFR, SPI_Uart_config.Flow); // enable enhanced registers
  SPI_Uart_WriteByte(LCR, SPI_Uart_config.DataFormat); // 8 data bit, 1 stop bit, no parity
  SPI_Uart_WriteByte(FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
  SPI_Uart_WriteByte(FCR, 0x01); // enable FIFO mode

  // Perform read/write test to check if UART is working
  SPI_Uart_WriteByte(SPR,'H');
  data = SPI_Uart_ReadByte(SPR);

  if(data == 'H')
  { 
    return 1; 
  }
  else
  { 
    return 0; 
  }

}


void SPI_Uart_WriteByte(char address, char data) //Methode
// Write <data> byte to SC16IS750 register <address>
{
  long int length;
  char senddata[2];
  senddata[0] = address;
  senddata[1] = data;

  select();
  length = SPI_Write(senddata, 2);
  deselect();
}


long int SPI_Write(char* srcptr, long int length) //Methode
// Write string to SC16IS750
{
  for(long int i = 0; i < length; i++)
  {
    spi_transfer(srcptr[i]);
  }
  return length; 
}


void SPI_Uart_WriteArray(char *data, long int NumBytes) //Methode
// Write string to SC16IS750 THR
{
  long int length;
  select();
  length = SPI_Write(&TX_Fifo_Address,1);

  while(NumBytes > 16)
  {
    length = SPI_Write(data,16);
    NumBytes -= 16;
    data += 16;
  }
  length = SPI_Write(data,NumBytes);

  deselect();
}


char SPI_Uart_ReadByte(char address) //Methode
// Read byte from SC16IS750 register at <address>
{
  char data;

  address = (address | 0x80);

  select();
  spi_transfer(address);
  data = spi_transfer(0xFF);
  deselect();
  return data;  
}

char autoconnect(void) //Methode
{
  // Exit command mode if we haven't already
  SPI_Uart_println("");  
  SPI_Uart_println("exit");
  delay(500);

  // Enter command mode 
  SPI_Uart_print("$$");
  delay(500);

  // Reboot to get device into known state
  Serial.println();
  Serial.println("Rebooting");
  SPI_Uart_println("reboot");
  delay(3000);
  
  Serial.println();
}


void Flush_RX(void) //Methode
// Flush characters from SC16IS750
{
  int j = 0;
  while(j < 1000)
  {
    if((SPI_Uart_ReadByte(LSR) & 0x01))
    {
      incoming_data = SPI_Uart_ReadByte(RHR);
    }  
    else
    {
      j++;
    }
  }
}

 
char Wait_On_Response_Char(char num) //Methode
// Wait for incoming char number <num> and return it
{
  i = 1;
  while(1)
  {
    if((SPI_Uart_ReadByte(LSR) & 0x01))
    {
      incoming_data = SPI_Uart_ReadByte(RHR);
      //Serial.print(incoming_data, BYTE);
      if(i == num){ 
        return incoming_data; 
      }
      else{ 
        i++; 
      }
    }  
  }
}


void SPI_Uart_println(char *data) //Methode
// Write array to SC16IS750 followed by a carriage return
{
  SPI_Uart_WriteArray(data,strlen(data));
  SPI_Uart_WriteByte(THR, 0x0d);
}


void SPI_Uart_print(char *data) //Methode
// Routine to write array to SC16IS750 using strlen instead of hardcoded length
{
  SPI_Uart_WriteArray(data,strlen(data));
}

 
char spi_transfer(volatile char data) //Methode
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

I’m stumped, maybe I can have someone help you by!

Thank you!

what is not working correctly?

before we pore over this code.

the read of the register and the data processing does not work

I was reading over your post and am trying to get WiFly to connect over an adhoc network using UDP which is similar to your project. Did you have success communicating over an adhoc network?

~McK