søndag den 15. april 2012

APC220 konfiguration fra Arduino

Som svar på kommentar til tidligere APC220 post er her et lille program fra AOit til at konfigurere APC220 direkte fra en Arduino.

/*Copyleft GNU GPL aoit.dk*/

#include <SoftwareSerial.h>

const int rxPin = 2; //rx pin
const int txPin = 3; //tx pin
const int setPin = 4; //set pin

SoftwareSerial apc220(rxPin, txPin);

void set_para(char hz[6], char rf_rate[2], char pwr[2], char uart_rate[2], char sc[2]) {
analogWrite(setPin, 0);
apc220.print("WR");
apc220.print(" ");
apc220.print(hz);
apc220.print(" ");
apc220.print(rf_rate);
apc220.print(" ");
apc220.print(pwr);
apc220.print(" ");
apc220.print(uart_rate);
apc220.print(" ");
apc220.println(sc);
delay(10);
while(apc220.available()) {
Serial.write(apc220.read());
}
analogWrite(setPin, 1023);

}

void get_para(void) {
int i=0;
char buff[32];
analogWrite(setPin, 0);
apc220.println("RD");
delay(10);
while(apc220.available()) {
Serial.write(apc220.read());
}
analogWrite(setPin, 1023);

}

void get_cmd() {
int i=0;
char buff[50];

if(Serial.available()){
delay(100);
while( Serial.available() && i< 49) { buff[i++] = Serial.read(); } buff[i++]='\0'; } if(i>0) {
char VAR1[4], VAR2[6], VAR3[2], VAR4[2], VAR5[2], VAR6[2];
sscanf(buff,"%s %s %s %s %s %s", VAR1, VAR2, VAR3, VAR4, VAR5, VAR6);
if(strcmp(VAR1,"RD") == 0) {
get_para();
} else if(strcmp(VAR1, "WR") == 0) {
set_para(VAR2, VAR3, VAR4, VAR5, VAR6);
} else {
Serial.println("");
Serial.println("Type 'RD' to ReaD APC220 parameters.");
Serial.println("Type 'WR' to WRite space delimeted APC220 parameters.");
Serial.println("Parameters are as follows:");
Serial.println("Frequency 6 Unit is KHz,for example 434MHz is 434000.");
Serial.println("Rf data rate 1 1,2,3 and 4 refer to 2400,4800,9600,19200bps separetely.");
Serial.println("Output power 1 0 to 9, 9 means 13dBm(20mW).");
Serial.println("UART rate 1 0,1,2,3,4,5 and 6 refers to 1200,2400,4800,9600,19200,38400,57600bps separately.");
Serial.println("Series checkout 1 Series checkout:0 means no check,1 means even parity,2 means odd parity.");
Serial.println("");
Serial.println("WR example: WR 434000 3 9 3 0");

}
}
}

void setup()
{
Serial.begin(9600);
apc220.begin(9600);
}
void loop() {
get_cmd();
}

2 kommentarer:

  1. Hi Andreas - wonderful solution. Thankyou for the help. This solved my issue as RF-magic could not find the COM-port.

    Just for beginners:
    This is how the pins should be set:
    pin_APC220 - pin_arduino
    1 (gnd) - GND
    2 (VCC) - 5V / 3.3V
    3 not connected
    4 (RXD) - D3
    5 (TXD) - D2
    6 not connected
    7 not connected
    8 (SET) - D4
    Note that D0 and D1 will not work as usefull ports on Arduino Uno board, though you should connect RXD to TX (D0) and TXD to DX (D1) when using the APC220 for transmitting purpose.
    Jon

    SvarSlet
    Svar
    1. Oops
      pin 7 (SET) - D4
      (there are no pin 8)

      Slet

Baggrunden for opslag og artikler her på AOit er mine mere end sidste ti års erfaringer i drift og brug af LAMP platformen til at løse et utal af opgaver. En platform der består af Linux, Apache, MySQL og PHP. Alle fire frit og åben software og til at presentere data som som information bruger jeg HTML til formatering og CSS til layout.