ADCSRA on Esp32

Xelbor
Posts: 2
Joined: Thu Sep 14, 2023 11:35 am

ADCSRA on Esp32

Postby Xelbor » Mon May 27, 2024 9:16 am

Hello! I want to transfer code from Arduino Nano to Esp32. However, I encountered the following problems:
exit status 1

Code: Select all

'ADCSRA' was not declared in this scope
'TCCR2A' was not declared in this scope
error: 'ACBG' was not declared in this scope
'ACME' was not declared in this scope
And with others.
Could you tell me how I can transfer this code (for example, what can I replace it with) from Arduino to Esp 32?

Code: Select all

byte ttAComp(unsigned long timeOut = 7000){  // pulse 0 or 1 or -1 if timeout
  byte AcompState, AcompInitState;
  unsigned long tEnd = micros() + timeOut;
  AcompInitState = (ACSR >> ACO)&1;               // читаем флаг компаратора
  do {
    AcompState = (ACSR >> ACO)&1;                 // читаем флаг компаратора
    if (AcompState != AcompInitState) {
      delayMicroseconds(1000/(rfidBitRate*4));    // 1/4 Period on 2 kBps = 125 mks 
      AcompState = (ACSR >> ACO)&1;               // читаем флаг компаратора      
      delayMicroseconds(1000/(rfidBitRate*2));    // 1/2 Period on 2 kBps = 250 mks 
      return AcompState;  
    }
  } while (micros() < tEnd);
  return 2;                                             //таймаут, компаратор не сменил состояние
}

void rfidACsetOn(){
  //включаем генератор 125кГц
  pinMode(FreqGen, OUTPUT);
  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);  //Вкючаем режим Toggle on Compare Match на COM2A (pin 11) и счет таймера2 до OCR2A
  TCCR2B = _BV(WGM22) | _BV(CS20);                                // Задаем делитель для таймера2 = 1 (16 мГц)
  OCR2A = 63;                                                    // 63 тактов на период. Частота на COM2A (pin 11) 16000/64/2 = 125 кГц, Скважнось COM2A в этом режиме всегда 50% 
  OCR2B = 31;                                                     // Скважность COM2B 32/64 = 50%  Частота на COM2A (pin 3) 16000/64 = 250 кГц
  // включаем компаратор
  ADCSRB &= ~(1<<ACME);           // отключаем мультиплексор AC
  ACSR &= ~(1<<ACBG);             // отключаем от входа Ain0 1.1V
}

bool searchEM_Marine( bool copyKey = true){
  byte gr = digitalRead(G_Led);
  bool rez = false;
  rfidACsetOn();            // включаем генератор 125кГц и компаратор
  delay(6);                //13 мс длятся переходные прцессы детектора 
  if (!readEM_Marie(addr)) {
    if (!copyKey) TCCR2A &=0b00111111;              //Оключить ШИМ COM2A (pin 11)
    digitalWrite(G_Led, gr);
    return rez;
  }
  rez = true;
  keyType = keyEM_Marine;
  for (byte i = 0; i<8; i++){
    if (copyKey) keyID[i] = addr [i];
    Serial.print(addr[i], HEX); Serial.print(":");
  }
  Serial.print(F(" ( id "));
  Serial.print(rfidData[0]); Serial.print(" key ");
  unsigned long keyNum = (unsigned long)rfidData[1]<<24 | (unsigned long)rfidData[2]<<16 | (unsigned long)rfidData[3]<<8 | (unsigned long)rfidData[4];
  Serial.print(keyNum);
  Serial.println(F(") Type: EM-Marie "));
  if (!copyKey) TCCR2A &=0b00111111;              //Оключить ШИМ COM2A (pin 11)
  digitalWrite(G_Led, gr);
  return rez;
}

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: ADCSRA on Esp32

Postby liaifat85 » Mon May 27, 2024 2:50 pm

The ESP32 has different timer configurations compared to the Arduino Nano. Instead of directly manipulating timer registers like TCCR2A and TCCR2B, you'll need to use the ESP32's Timer API to achieve similar functionality.

https://docs.espressif.com/projects/esp ... timer.html

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ADCSRA on Esp32

Postby MicroController » Tue May 28, 2024 7:32 pm

... and you'll have to come up with a way of not using the AVR's analog comparator.

Who is online

Users browsing this forum: Baidu [Spider] and 126 guests