c++ 有人知道Arduino上的DDRC寄存器与ESP32上的DDRC寄存器等效吗?

rjee0c15  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(123)

亲爱的StackOverflowers:
我有兴趣做一个声悬浮DIY项目,但我有一个esp 32 qC,我不知道等效的DDRC寄存器。下面的代码:

//made by milespeterson101
//published on 6/17/2022
//heres the code (:

byte TP = 0b10101010; // Every other port receives the inverted signal
void setup() {
  DDRC = 0b11111111; // Set all analog ports to be outputs
  
  // Initialize Timer1
  noInterrupts(); // Disable interrupts
  
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  OCR1A = 200; // Set compare register (16MHz / 200 = 80kHz square wave -> 40kHz full wave)
  
  TCCR1B |= (1 << WGM12); // CTC mode
  TCCR1B |= (1 << CS10); // Set prescaler to 1 ==> no prescaling
  TIMSK1 |= (1 << OCIE1A); // Enable compare timer interrupt
  
  interrupts(); // Enable interrupts
}
ISR(TIMER1_COMPA_vect) {
  PORTC = TP; // Send the value of TP to the outputs
  TP = ~TP; // Invert TP for the next run
}
void loop() {
  // Code ends here (:
}

有关该项目的更多详细信息,请访问:https://create.arduino.cc/projecthub/milespeterson101/ultrasonic-levitation-acoustic-levitation-experiment-8050f8
提前感谢!
我原以为它不需要任何代码修改就能工作,但不幸的是,我在这一点上卡住了。

ncecgwcz

ncecgwcz1#

我不确定是否可以用中断来完成,但是这个代码应该和你的代码做的差不多。40Khz信号的时间周期是12.5uS,所以检查是否已经超过了12uS应该关闭。

#define bit7 15
    #define bit6 2
    #define bit5 4
    #define bit4 16
    #define bit3 17
    #define bit2 5
    #define bit1 18
    #define bit0 19
    
    
    long int t0 = 0;
    long int t1 = 0;
    bool ledStatus = false;
    
    void setup() {
      // put your setup code here, to run once:
      pinMode(bit7, OUTPUT);
      pinMode(bit6, OUTPUT);
      pinMode(bit5, OUTPUT);
      pinMode(bit4,OUTPUT);
      pinMode(bit3,OUTPUT);
      pinMode(bit2, OUTPUT);
      pinMode(bit1,OUTPUT);
      pinMode(bit0, OUTPUT);
    
      t0 = micros();
      
    
    }
    
    void loop() {
      t1 = micros();
      if(t1 > (t0+12)){
        digitalWrite(bit7, ledStatus);
        digitalWrite(bit6, ledStatus);
        digitalWrite(bit5, ledStatus);
        digitalWrite(bit4, ledStatus);
        digitalWrite(bit3, ledStatus);
        digitalWrite(bit2, ledStatus);
        digitalWrite(bit1, ledStatus);
        digitalWrite(bit0, ledStatus);
    
        ledStatus = !ledStatus;
        t0 = micros();
      }
    
    }

相关问题