Arduino programos kodas

#include <SPI.h>
const int markeris=8;
const int klaida=4;
const int VejoPin=3;
const int Power1=5;
const int Power2=6;
const int VGROUT=2;//sukeiciau 2 ir 7 vietomis, kad tur42iau pertraukima, kurie galimi 2 ir 3 pin'uose UNO versijai
const int TempCL=7;
byte buf[256];
byte BETZ[256]={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,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x06,0x06,0x06,0x06,0x07,0x07,0x07,0x08,0x08,0x08,0x08,0x09,0x09,0x09,0x0A,0x0A,0x0A,0x0A,0x0B,0x0B,0x0B,0x0C,0x0C,0x0C,0x0D,0x0D,0x0D,0x0E,0x0E,0x0E,0x0F,0x10,0x11,0x11,0x12,0x12,0x13,0x14,0x14,0x15,0x15,0x16,0x17,0x17,0x18,0x18,0x19,0x1A,0x1A,0x1B,0x1B,0x1C,0x1C,0x1D,0x1E,0x1F,0x1F,0x20,0x20,0x21,0x22,0x23,0x24,0x25,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x48,0x49,0x4B,0x4D,0x4E,0x4F,0x52,0x53,0x55,0x57,0x58,0x5A,0x5C,0x5D,0x5F,0x60,0x62,0x64,0x65,0x66,0x67,0x69,0x6A,0x6C,0x6E,0x6F,0x71,0x72,0x73,0x74,0x76,0x78,0x7B,0x7D,0x80,0x83,0x85,0x88,0x8A,0x8D,0x8F,0x92,0x94,0x97,0x9A,0x9C,0x9D,0x9F,0xA1,0xA4,0xA6,0xA9,0xAC,0xAE,0xB1,0xB4,0xB6,0xB9,0xBB,0xBF,0xC1,0xC2,0xC3,0xC5,0xC6,0xC8,0xCA,0xCC,0xCE,0xD0,0xD2,0xD4,0xD6,0xD8,0xDA,0xDC,0xDE,0xE0,0xE1,0xE3,0xE5,0xE7,0xE9,0xEB,0xED,0xEF,0xF1,0xF3,0xF4,0xF7,0xF9,0xFB,0xFD,0xFF};
byte dat[256];//={0x82,0xA9,0x0f,0x12,0x55,0x66,0x77,0x88,0x99,0x0A,0x19,0x28,0x37,0x46,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
byte checksumreal,komanda,clr;
int komilgis;
boolean beginas=false;
boolean process_error=false;
byte checksumsend=0;
int ctklaida=0;
volatile boolean doprint=false;
volatile byte pos;
signed long GenDaznis_us, prev_time_GenD, VejoGreitis_us, prev_time_us;
volatile boolean process_it;
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))    //Speedup the analogRead() function
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))      //Speedup the analogRead() function
void setup (void){
  Serial.begin (115200);         // debugging
  pinMode(markeris, OUTPUT);
  pinMode(klaida, OUTPUT);
  pinMode(VGROUT, INPUT);
  pinMode(Power1, OUTPUT);
  pinMode(Power2, OUTPUT);
  pinMode(MISO, OUTPUT);         // have to send on master in, *slave out*
  SPCR |= _BV(SPE);              // turn on SPI in slave mode, get ready for an interrupt 
  clr=SPSR;
  clr=SPDR;
  delay(10);
  ctklaida=0;
  pos = 0;                       // buffer empty
  checksumsend=0;
  process_it = false;
  SPI.attachInterrupt();         // now turn on interrupts
  sbi(ADCSRA, ADPS2);    //Speedup the analogRead() functionSS
  cbi(ADCSRA, ADPS1);
  cbi(ADCSRA, ADPS0);
  digitalWrite(klaida, LOW);
  attachInterrupt(digitalPinToInterrupt(VejoPin), rising, RISING);
  attachInterrupt(digitalPinToInterrupt(VGROUT), rising1, RISING);
  //pinMode(TempCL, OUTPUT);
  //setPwmFrequency(TempCL,1);
  //analogWrite(TempCL, 127);
} 
ISR (SPI_STC_vect){              // SPI interrupt routine
  digitalWrite(markeris, HIGH);
  byte c = SPDR;                 // grab byte from SPI Data Register
  byte z = 0;
  if (c==0x81 and beginas==false) {//0X81 hex'E - PAKETO PIRMAS BAITAS
    beginas=true;checksumreal=0;komilgis=0;pos=0;checksumsend=0; 
  }  
  if (beginas==true) {
    buf[pos]=c;
    checksumreal+=c; 
    switch (pos) {
      case 0:     z=dat[pos];
                  checksumsend+=dat[pos]; 
                  break;
      case 1:     z=dat[pos];
                  checksumsend+=dat[pos]; 
                  komanda=c;
                  break;
      case 2:     z=dat[pos];
                  checksumsend+=dat[pos]; 
                  komilgis=c; 
                  break;
      default: 
                if ((komanda & 0xF8)==0x30) {
                  if (pos<(komilgis+3)) {
                    BETZ[(komanda & 0x07)* 32 + pos-3]=c;    //Komandos A8-AF apraso, kuris is astuoniu paketas bus irasytas i BETZ masyva. Viename pakete 32 baitai. A8: 0-32 masyvo baitai AF - 224-256 baitai
                  }
                } else if ((komanda & 0xF8)==0x40) {
                   z=BETZ[(komanda & 0x07)* 32 + pos-3];
                } else {
                  //Serial.println("Komanda kita ");
                   z=dat[pos];
                } 
                if (pos==(komilgis+3)) {
                   dat[pos]=255-checksumsend;
                   z=dat[pos];
                  } else if (pos==(komilgis+4)) {
                   z=0x55;
                   process_it=true;
                   checksumsend=0;
                   pos=0;
                   beginas=false;
                   if (checksumreal==0xFF) {
                       process_error=false;
                      } else {
                       process_error=true;
                      digitalWrite(klaida, HIGH);
                   }
                } else {
                   checksumsend+=z; 
                }  
    }
    SPDR=z;  
    if (pos < sizeof(buf)) pos++; else { pos=0;beginas=false; } } digitalWrite(markeris, LOW); } void loop (void){ byte apkr; unsigned long TPM; unsigned int VGRmpers, x, CL, CH, GL, GH, TPL, TPH;//unsigned int - 0...256, signed int, -127...+127 reiksmes if (process_it) {process_it = false;} x=analogRead(A0); dat[3]=lowByte(x); dat[4]=highByte(x); x=analogRead(A1); dat[5]=lowByte(x); dat[6]=highByte(x); x=analogRead(A2); dat[7]=lowByte(x); dat[8]=highByte(x); x=analogRead(A3); dat[9]=lowByte(x); dat[10]=highByte(x); x=analogRead(A4); dat[11]=lowByte(x); dat[12]=highByte(x); x=analogRead(A5); dat[13]=lowByte(x); dat[14]=highByte(x); CH=VejoGreitis_us>>16; CL=VejoGreitis_us;
  dat[15]=lowByte(CL); dat[16]=highByte(CL); dat[17]=lowByte(CH); dat[18]=highByte(CH);//vejo greicio perdavimas 4 baitai
  GH=GenDaznis_us>>16; GL=GenDaznis_us;
  dat[19]=lowByte(GL); dat[20]=highByte(GL); dat[21]=lowByte(GH); dat[22]=highByte(GH);//generatoriaus daznio perdavimas 4 baitai
  TPM=millis(); TPH=TPM>>16; TPL=TPM;
  dat[23]=lowByte(TPL); dat[24]=highByte(TPL); dat[25]=lowByte(TPH); dat[26]=highByte(TPH);//TPL //TPH
  //paruosiame apkrovai vejo greitį padaugintą iš 16 (užpildysime 0-16m/s kiekvieną iš 16 vėjo greicio 16 tarpinių reiksmiu - viso 256 reiksmes)
  VGRmpers=1000000*16/12/VejoGreitis_us;//VGRmpers reiškia vėjo greitį padaugintą is 16, 12 - vėjo daviklio charakteristika tiek mimpulsų per sekundę esant 1m/sek vėjo greičiui
  analogWrite(Power1, BETZ[VGRmpers]);
  analogWrite(Power2, BETZ[VGRmpers]);
}
void SePrHe(int x) {
if (x<16) {
    Serial.print("0");
    Serial.print(x, HEX);
  } else {
    Serial.print(x, HEX);
  }
  Serial.print(" ");
  return;
}
void rising() {
  attachInterrupt(digitalPinToInterrupt(VejoPin), falling, FALLING);
  GenDaznis_us=micros()-prev_time_GenD;
  prev_time_GenD = micros();
}
void falling() {
  attachInterrupt(digitalPinToInterrupt(VejoPin), rising, RISING);
}
void rising1() {
  attachInterrupt(digitalPinToInterrupt(VGROUT), falling1, FALLING);
  VejoGreitis_us=micros()-prev_time_us;
  prev_time_us = micros();
}
void falling1() {
  attachInterrupt(digitalPinToInterrupt(VGROUT), rising1, RISING);
}
void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
  }
  TCCR2B = TCCR2B & 0b11111000 | mode;
}
}

Parašykite komentarą

El. pašto adresas nebus skelbiamas. Būtini laukeliai pažymėti *