Combine AVR code with arduino code

Hey guys
I was trying to get some relay boards connected to ports A/B/C/L of my Arduino mega2560
and turning them on/off according to a message I will be receiving from the PC
and then returning some analog/digital reads to that PC

so i wrote this basic program to do that for me:

#include <SoftwareSerial.h>
#include <avr/io.h>

const byte AnalogInputs[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
int led = 13; //indication LED
int val;
word wordVal;
byte buffer[11];

void setup()
{
  Serial.begin(115200);
  pinMode (led, OUTPUT);

  DDRC = 0xFF; // Relay Board 1
  DDRA = 0xFF; // Relay Board 2
  DDRB = 0xFF; // Relay Board 3
  DDRL = 0xFF; // Relay Board 4
//inputs
  DDRG = 0x00;
  DDRE = 0x00;
  DDRH = 0x00;
  DDRD = 0x00;
}

void loop() 
{
  Serial.readBytes(buffer, sizeof(buffer));
  ParseAllRelays(buffer[]);
  writeAllAnalog();
  delay (1000);
}


void writeAllAnalog(void)
{
  word checksum;
  byte message[40] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  message[0] = 0xA5;
  message[1] = 0x5A;
  checksum = message[1] + message[0];
  
  for(int i = 0; i<16 ; i++)
  { 
    val = analogRead(AnalogInputs[i]);  
    wordVal = word(val);
    message[(i+1)*2] = lowByte(wordVal);
    message[(i+1)*2+1] = highByte(wordVal);
    //checksum = checksum + wordVal;
    checksum = checksum + message[(i+1)*2]+message[(i+1)*2+1];
  }
  message[34] = PORTG;
  message[35] = PORTE;
  message[36] = PORTH;
  message[37] = PORTD;
  checksum = checksum + message[34] + message[35] + message[36] + message[37];
  message[38] = lowByte(checksum);
  message[39] = highByte(checksum);
  
  Serial.write(message, sizeof(message));
}



void ParseAllRelays(byte buffer[])
{
  //checksum checker:
  word checksum = 0x0000;
  for (int i=0 ; i<sizeof(buffer)-2; i++)
    checksum = checksum + buffer[i];
  
  if ((buffer[0] == A5) && (buffer[1] == 5A) && (lowByte(checksum) == buffer[9]) && (highByte(checksum) == buffer[10]))
  {
    PORTC = buffer[2]; // Relay Board 1
    PORTA = buffer[3]; // Relay Board 2
    PORTB = buffer[4]; // Relay Board 3
    PORTL = buffer[5]; // Relay Board 4
  }
}

although Im receiving error messages like "note: suggested alternative: 'PORT0'
PORTA = buffer[3]; // Relay Board 2
^~~~~
PORT0
"
and something about A8 not being declared?

how can I combine arduino with AVR code?

How much pins your Relay board used?
Each of this lines setup as OUTPUT the whole PORT - 8 pins at once.
Is it what you try to do?

yes I have 4 relay boards, 8 relay each
all relays are opto-coupler decoupled so i hope there will be no problem to sink all the power needed to operate them

there's no point in reading from the serial interface if there's nothing the and presumably nothing to process, as well

    if (Serial.available())  {
         read something
    }

even if you read a specific # of bytes, what happens of you get out of sync. looks like your message has a fixed prefix, assuming hex value: 0xA5 and 0x5A.

it may make sense to read one byte and if it's 0xA5, read another and if it's 0x5A read the remainder of the message.

if either is not correct, go back to looking for 0xA5. there's nothing to prevent a relay value in your message having those values.

after reading a complete msg, if the checksums bad, got back to looking for 0xA5

if you have a correct checksum, call a routing to process the message. it's been validated both with the prefix and checksum (i.e. no need to parse anything) just set the relays

doesn't need to be explicitly set to zero. doesn't need to be initialized to anything. the incoming message simply overwrites what's there

but you should verify each step. you should verify a bad (wrong checksum) or out of sync (not starting with 0xA5) msgs

You mean

 if ((buffer[0] == 0xA5) && (buffer[1] == 0x5A)…

no doubt. Et cetera.

a7

You should read the input pins from PING, PINE, PINH and PIND. Reading the PORTx registers reads output bits.

WARNING! Serial uses PORTE bit 0 and PORTE bit 1. You can't use those pins for input at the same time you use them for Serial.

WARNING: I don't think all of the PORTG signals are connected to Arduino MEGA pins. Are you designing your own boards with an ATmega2560 chip?

Similar with PORTD. Only pins 0, 1, 2, 3, and 7 are connected to Arduino MEGA i/O pins.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.