0

I have been trying to get an answer my question for a 2 days already. It's good for knowledge collecting and playing with different things, but there haven't been a solution so far.

I'm trying to leverage the HXT500 Servo with the Fast PWM with a TOP ICR1

I'm using the DFRobot DFRduino UNO v3.0 (R3) analog of the Arduino UNO. Based on the Atmega 328P-PU

I have an Oscilloscope till 5 MHz as additional tool

Question

  1. Why the Fast PWM, TOP ISC1 always has the HIGHT only on a oscilloscope
  2. Why the Clock Prescaler doesn't work

My investigations & observations

I activated the PWM with TOP ICR1 but on my oscilloscope showed only a 5V straight line, I checked different diapasons (horizontal bases) (1a). Connected to pins 10 or 9. I assumed that my UNO works on 1 MHz (9.2.1 Default Clock Source on Page 37) and calculated ICR1, OCR1A:B for 0 and 180 degree.

     void setup() {
      // all as Ports (for outputs)
        DDRB=0xFF;
        DDRC=0xFF;
        DDRD=0xFF;
    
      // Table 16-4 on Page 141
      // Fast PWM TOP is ICR1
      TCCR1A|=(0<<WGM10)|(1<<WGM11);
      TCCR1B|=(1<<WGM13)|(1<<WGM12);
      ICR1=2499;
      
      // Table 16-2 on Page 140
      // Clear OC1A/OC1B on Compare Match, set OC1A/OC1B at BOTTOM (non-inverting mode)
      TCCR1A|=(1<<COM1A1)|(1<<COM1B1);
    
      // Table 16-5 on Page 143
      // clkI/O/8
      TCCR1B|=(0<<CS12)|(1<<CS11)|(0<<CS10); // (2a)
    }
    
    void loop() {
      digitalWrite(LED_BUILTIN, HIGH);  
        // 90 degree
        OCR1A=299;
        OCR1B=299;  
      delay(1000);                      
      digitalWrite(LED_BUILTIN, LOW);   
        // 0 degree
        OCR1A=49;  
        OCR1B=49;  
      delay(1000);                      
    }

There I noticed the following issues:

  • I tried different Prescales - there weren't any differences (2a)
  • I tried 1 MHz, 8 MHz, 16 MHz - straight HIGHT line

What I have tried next:

  • To use an Example for Servo leveraging from Arduino IDE. It worked but as I understood it worked via Interrupts, not a Hardware solution
  • To use Fast PWM, 9 & 10-bit. (2a) Prescales didn't work - I had movement
  • To use Fast PWM, 8-bit (with code adjustment and wrong angles). (2a) Prescalers didn't work - I had movement
2
  • A 5MHz oscilloscope likely can not resolve a 5MHz square wave. What you may see is a distorted wave closer to a sin wave than a square wave. You may not see anything but the DC component of higher frequency signals. I don't know why you only see 5 volts when you are expecting a wave form or voltage less than 5 volts. There maybe something wrong with the code, hardware, connections or you may have your oscilloscope AC coupled.
    – st2000
    Dec 26 '20 at 15:03
  • @st2000, thanks for your attention and comment The wave explanation - yes, it makes sense, I didn't think in this way, thanks Considering possible issues: connections - it's fine, because in another modes it works w/o changing these connections hardware - possible, but I don't know how to check it, but with the Fast AWM 9-bit it works Interestingly: there is the same/similar issue with Normal, CTC modes Code - it would be easiest reason )
    – Ivan
    Dec 26 '20 at 15:28
0

You might want to set the TCCR1A and TCCR1B registers to 0 before your code as the Arduino library sets the WGM10 bit. Or change the first |= to a =. By leaving the WGM10 bit set, you get fast PWM mode, with TOP set to OCR1A. Combined with your other code, this would result in both pins having a 100% duty cycle.

So use

TCCR1A=(0<<WGM10)|(1<<WGM11);
TCCR1B=(1<<WGM13)|(1<<WGM12);

or

TCCR1A=0;
TCCR1B=0;
TCCR1A|=(0<<WGM10)|(1<<WGM11);
TCCR1B|=(1<<WGM13)|(1<<WGM12);

Other than that, you code looks fine, and should give you a 800Hz signal.

PS "I assumed that my UNO works on 1 MHz"; no, the UNO runs at 16Mhz.

1
  • 1
    thanks. I very appreciate the time that you spent here. I have tried and it started working, but with some shift, because there were that wrong assumption, that you mentioned, about the System Clock Speed Currently, the fast PWM TOP ICR1, Prescaler work and the math is matched
    – Ivan
    Dec 26 '20 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.