Google-Apps
Hauptmenü

Post a Comment On: Ken Shirriff's blog

"Deep dive into how the Teensy microcontroller interacts with the Arduino library"

4 Comments -

1 – 4 of 4
Blogger josh said...

Great article as always!

I believe that the input range for the `value` parameter to `AnalogWrite()` is 0-255 rather than 0-256.

The Arduino API documentation supports this...


```
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.
```

Also, looking at source code for the canonical implementation (the one used by the original AVR-based Arduino boards), we see that passing a 255 as value turns the pin on completely...

```
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
else
{
switch(digitalPinToTimer(pin))
[....PWM code here....]
```

I'll try to find a TEENSY to test with, but my guess is that `analogWrite` with a value of `255` will result in a 100% duty cycle (no PWM) waveform.

Thanks again!

-josh

June 18, 2021 at 9:59 AM

Blogger gotj said...

The teensy 4 is a beast man, can toggle a pin @150MHz via software/bit banging, proven:

#define PIN 13

void setup () { pinMode(PIN, OUTPUT); }

void loop () {
while (1) {
CORE_PIN13_PORTSET = CORE_PIN13_BITMASK;
CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK;
}
}

June 18, 2021 at 1:52 PM

Blogger Ken Shirriff said...

josh: I verified that Teensy turns the pin on completely for an analogWrite value of 256; with a value of 255, it is on 255/256 of the time. While the Arduino divides the PWM cycle into 255 intervals of 8 microseconds, the Teensy has 256 intervals of 8 microseconds, so it goes one higher.

June 21, 2021 at 7:01 PM

Anonymous Anonymous said...

DO I understand this fully, no, will this be helpful for my rocket club to control an EDF, also probably no, was it cool, ooooohhhhh yeaaaaaaa.

September 15, 2022 at 2:08 PM

You can use some HTML tags, such as <b>, <i>, <a>

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot