Learning AVR-C Episode 7: PWM

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ม.ค. 2025

ความคิดเห็น • 91

  • @JeroenJK
    @JeroenJK 8 ปีที่แล้ว +4

    PWM is weird / hard to understand when you see it for the first time, especially implementing it yourself.
    Your videos (this one and the one about Timers) really helped me. Thanks man!

  • @RobGirdler
    @RobGirdler 3 ปีที่แล้ว +6

    I know this is many years later, but just came across this series and it has been a great help.
    However, I came across an issue in this episode where the LED was staying at a constant voltage. Fix for it was to make the dutyCycle variable a volatile. Just thought I'd add it for anyone else who may come across the same issue in the future.

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว +2

    The ISR function is called when the specified vector occurs. In this case, the ISR is run when Timer0 overflows. In the function you put code that is necessary to be run when the interrupt occurs. It's really applications specific, but you generally want to keep is small and concise. When the interrupt occurs, the code address where the program just was is stored, so after the ISR, the program just jumps back to that point.

  • @GingerNingerGames
    @GingerNingerGames ปีที่แล้ว +1

    These videos are still brilliant. I'm still finding them helpful in learning to get this stuff running outside the arduino IDE, it's a bit of fun and helping me find ways to make my code WAY faster

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว +2

    Not using hardware PWM. You have to use the two timer compare registers to produce the ON time then the OFF time. Only that way you can control the frequency of the PWM.

  • @MarcinRdest
    @MarcinRdest 10 ปีที่แล้ว +10

    Handling interrupts makes _delay_ms longer than expected - that's why you have used so high prescaler (1024) what eliminates so high delay in _delay_ms :). The result is low freq of wave. There are two solutions:
    1. Do not use interrupts :)
    2. Set TCCR0B = (1

    • @cristianlugano
      @cristianlugano 7 ปีที่แล้ว

      Thanks for that note. I couldn't find where the error was

    • @sfndude1768
      @sfndude1768 6 ปีที่แล้ว

      thank you so much!

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว

    How do you mean? When the duty cycle is 0, when the timer resets, the LED comes on, then immediately turns off. So it still turns on. If you want to avoid that you can add an exception to have it held low.

  • @user21XXL
    @user21XXL 6 ปีที่แล้ว

    thx for a great episode.
    if you put a cap parallel to the led, you can filter out that spike which turns
    the led on at low OCR0A values (low pass filter)

  • @allthatremains1609
    @allthatremains1609 11 ปีที่แล้ว

    Great!!! but its possible to fix a frequency for the PWM??? that´s why i need to create a PWM with a certain Frequency!!! Thanks

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว

    In timer video, you had to manually change the state of the pin. With PWM, all of the pin changing is handled by the hardware of the chip.

  • @PatrickHansen101
    @PatrickHansen101 9 ปีที่แล้ว

    Marcin Rdest Thank you :) i was wondering why it was taking ages to get to the full 100% and reset, disabling interrupts before delay and enabling them again after, made it work as it's supposed to.

  • @MrAshwindersingh
    @MrAshwindersingh 4 ปีที่แล้ว +1

    nice explanation

  • @doudi463
    @doudi463 3 ปีที่แล้ว

    Thanks for the help

  • @funguy2767
    @funguy2767 3 ปีที่แล้ว

    Thank you!

  • @shvideo1
    @shvideo1 3 ปีที่แล้ว

    Some videos on this topic said that all delay functions will be affected if you change the behavior of the Timer/Counter 0! If that's true, then the behavior of _delay_ms() may be unpredictable. I love these detailed videos on MCU programming. Thank you very much for all the details!

    • @Cafedex
      @Cafedex 7 หลายเดือนก่อน

      Arduino uses timer 0for mill is so yes playing around with timer 0 will affect Arduino. Timer 2, 3 no problem unless Arduino functions use them. Check the documentation of functions being used.

  • @williamrichards753
    @williamrichards753 11 ปีที่แล้ว +1

    I don't currently have the code with me, it is at the lab at my school, but i can email you it tomorrow. Thanks

  • @grave8digger8
    @grave8digger8 2 ปีที่แล้ว

    Hi, i need PWM for my Trainspeed Regulation. I didn't find anything.
    Can you help me ?

  • @ainstaink8312
    @ainstaink8312 8 ปีที่แล้ว

    Great video! I have been working mostly with AtMega324...looks like it slightly different in that chip

  • @euanwells6968
    @euanwells6968 4 ปีที่แล้ว +1

    i know this video went up 7 years ago.....but if i want t change the value of the DutyCycle based on the push of a button, how might i go about that?
    push1 = 25%
    Push2 = 62%
    Push3 = 87.5%
    push4 = 100%

    • @RobGirdler
      @RobGirdler 3 ปีที่แล้ว

      Not sure if you ever got an answer, but saw the question and here is what I would do.
      Make an array of values to store the various duty cycle values in the code. Then add a button to an additional pin (eg digital pin13, which is not used for this PWN example), using that button to trigger an interrupt (see a previous video in this series on using a button if needed). When the button is pressed, it triggers a separate interrupt to the timer, so in that code block, you simply set the duty cycle to the value pointed to the current entry in the array, then increment the array index to the next position. Then next time the button is pressed, it sets to the next value and so on.
      Couple of things to watch for, firstly you will need to debounce the switch, either in hardware or code. If not, then a single press may actually make the interrupt fire multiple times. Secondly, just keep an eye on the array index and ensure it can never exceed the bounds and resets to the first value correctly after the last one is read.

  • @yoopata
    @yoopata 7 ปีที่แล้ว

    Thanks for good video! Its been a big help!

  • @williamrichards753
    @williamrichards753 11 ปีที่แล้ว

    How would i go about setting up two pwm's on the same chip. i am trying and i keep getting bazaar results. my end goal would be to wire both pwms to a motor driver to program a motor?

  • @bt97gml
    @bt97gml 11 ปีที่แล้ว

    can the mosi/miso/usck pins used for programming be set as input/outputs without bricking the chip?

  • @adarshkrishnan3699
    @adarshkrishnan3699 7 ปีที่แล้ว

    Using 2 PWM Channels on ATmega32.
    - I want 10 pulses from OC0 & turn off PWM on OC0
    - Then wait for x us.
    - Then I want 10 Pulses from OC2 & turn off PWM on OC2.
    - Then wait for x us.
    - Repeat this step in while(1) loop.
    I'hve tried many ways to obtain this.....but some times, OC0 & OC2 produces output at the same time.....which is not acceptable.
    When OC0 is producing 10 pulses..... OC2 should wait till 10 pulses on OC0 pin + x us.
    Quite a challenging thing....I think.
    Pls do u have any idea how to do this kind of task....Pls help

  • @lhizalorenzo448
    @lhizalorenzo448 5 ปีที่แล้ว

    Hello sir can I use this for spwm for inverter

  • @albertom6239
    @albertom6239 4 ปีที่แล้ว

    why use fast PWM number 3 and not number 7?

  • @ThiagoMeloDMA
    @ThiagoMeloDMA 2 ปีที่แล้ว

    Hi friend, thanks for lesson! Do you can help me about how to create a three PWM signals in ATtiny461A? Thank very much! :D

  • @mechatronicsninja3806
    @mechatronicsninja3806 6 ปีที่แล้ว

    brilliant , thank you !!!

  • @VaLeEBP
    @VaLeEBP 8 ปีที่แล้ว

    Nice tutorial :)
    I have a question, if I want a different frecuency on my PWM, I have to modify TCNT0 register? If is that how I can do that I know theres a ecuation to calculate tthe value for the register and have that frequency, but I don't know how I have to programm the timer?

  • @nosherbet
    @nosherbet 11 ปีที่แล้ว

    Very good thanks!

  • @pianoman112233
    @pianoman112233 11 ปีที่แล้ว

    is there a way to use pwm if you have interupts and they use the same timer

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว

    Yes they can.

  • @jwbonnett
    @jwbonnett 6 ปีที่แล้ว

    How would I set separate duty cycles on separate pins?

  • @delwyndavid4834
    @delwyndavid4834 5 ปีที่แล้ว

    The brightness is not changing can you help please

  • @dewianamf7475
    @dewianamf7475 5 ปีที่แล้ว

    Please make e video about SPWM in AVR

  • @stratorenzo
    @stratorenzo 11 ปีที่แล้ว

    thanks for the tutorial. i have a problem with the PWM ATtiyn85, i want to use the "dead time" Do you have any idea for this? Thanks!!

  • @chethiyawijewickrama6425
    @chethiyawijewickrama6425 5 ปีที่แล้ว

    How can we apply this method to control servo motor.

  • @amirahussein3806
    @amirahussein3806 8 ปีที่แล้ว

    how can I use something like that to implement a simple DAC using Mega128 please ??

  • @KabooM1067
    @KabooM1067 7 ปีที่แล้ว

    Immensely helpful video. Thank you so much.

  • @amikhimji
    @amikhimji 11 ปีที่แล้ว

    Great video thanks

  • @MiNombreEsGabo
    @MiNombreEsGabo 11 ปีที่แล้ว

    If I want to code in avr assemby language, how can I select the clock source for my uC? Do I have to do it from the code, or at the moment of the programming the hardware using the avr dude? Thanks by the way

  • @justinberdell7517
    @justinberdell7517 2 ปีที่แล้ว

    Could someone explain to me what all these different lines mean in detail? I am an EE, I don't often program MCUs, but I need to right now. Cutting right to the meat and potatoes, my application requires me to set the PWM frequency of Arduino Micro digital pins 9, 10, 11 to ~32kHz, I believe it works out to 31,250 Hz specifically. I can find examples on doing this on a ATmega328p all over the interned, but I am doing it on a Micro which uses an ATmega32u4. I would really like to use timer4 since my application is BLDC motor control and from what I see online the ATmega32u4 timer4 is perfect for this application, but I think that looks a little complex for my sill level. I've seen other solutions with TCCR0,1,2 in the ATmega328p that look much simpler, and I think I can pull this off with similar solutions only on the ATmega32u4. Can anyone explain to me how I might do this???

  • @kampkrieger
    @kampkrieger 6 ปีที่แล้ว

    why do you use timers and interrupts, why not just set OCR0A to another value instead of the duty cycle variable?

  • @javeshbhardwaj
    @javeshbhardwaj 8 ปีที่แล้ว

    how can we change the frequency of the PWM wave i am using AVR atmega2560?
    Can we do the same kind of overflow and ISR thing u did with a 8 bit register, with a 16 bit register?
    an example will be much appreciated. I am new to hardware programming.
    Thank you

  • @pindiprince7671
    @pindiprince7671 8 ปีที่แล้ว

    Hi HHD,
    I need to know how to directly program Atmega328P direction from computer using Atmel Studio 7. Without any external programmer.

  • @TheZhazz
    @TheZhazz 2 ปีที่แล้ว

    I need to use two pins, so PD6 and PD5 for changing the direction of a DC motor, so i did your code and then i added this to use PD5, can you check that this is correct?
    int main(void)
    {

    DDRD = (1

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว

    Just Google Atmel Studio 5. You'll have to enter some information, but other than that, it's free.

  • @javeshbhardwaj
    @javeshbhardwaj 8 ปีที่แล้ว

    is the frequency of this generated pwm is (F_CPU)/(2 * 256)?

  • @Saywoohuu
    @Saywoohuu 11 ปีที่แล้ว

    Thank you so much!!

  • @YellowBrailleMeditation
    @YellowBrailleMeditation 11 ปีที่แล้ว

    Hello! I'm missing something. At 3:47, you don't need to flip the pin for PWM (note 1). I don't understand. Can you explain me and point me where to look in the past video? Thanks and good jobs for your series!

  • @satvikkethireddy6264
    @satvikkethireddy6264 8 ปีที่แล้ว

    How would one go and apply this to servo's?

  • @shubhisharma2515
    @shubhisharma2515 6 ปีที่แล้ว

    sir please make a video for colour sensor connection and programing by atmel studio for arduino mega.

  • @hungdt09
    @hungdt09 9 ปีที่แล้ว

    Let me try with my AVR board. Thanks you.

  • @Ophis1984
    @Ophis1984 9 ปีที่แล้ว

    what bits need to be changed to do this on another pin, am trying to use pb3 so changed to DDRB = (1

    • @DavidM1156
      @DavidM1156 8 ปีที่แล้ว

      you need to check out the WGM registers for the Oscilator you are using. If they are not established, the PWM mode won't be applied

  • @michaelalexanderko6937
    @michaelalexanderko6937 9 ปีที่แล้ว

    You earn a new subscriber sir ! But im using arduino 328p I used the same code but it doesn't work in the arduino I guess it only work on the atmel328 chip only ?

    • @thegreatxyz
      @thegreatxyz 8 ปีที่แล้ว +1

      if you are using an arduino you have to set F_CPU to 16000000 for proper timing.I tested the code on the 328p and it works.

  • @dgamma1
    @dgamma1 10 ปีที่แล้ว

    thansk dude!

  • @CrOwbaRVids
    @CrOwbaRVids 9 ปีที่แล้ว

    Can you explain how to stop pwm? i am trying to write a pwm library for atmega8. I have sorted the pwm chanel setup but i cant turn each chanel off on demand.

    • @barefooter2222
      @barefooter2222 9 ปีที่แล้ว +1

      CrOwbaRVids just write a 0 into the duty cycle. That should make the PWM 0.

  • @Prajwal_Rao
    @Prajwal_Rao 6 ปีที่แล้ว

    can we have 8 pwm pins on this chip itself?? which chips support 8 pwm pins?

    • @Prajwal_Rao
      @Prajwal_Rao 6 ปีที่แล้ว

      can multiplexing help?

  • @mariacrisan5975
    @mariacrisan5975 11 ปีที่แล้ว

    thank you :)

  • @MilanKarakas
    @MilanKarakas 8 ปีที่แล้ว

    Anyone have tutorial on Attiny85 with timers and ADCs? I am little bit stuck with timer1 on Attiny working on internal PLL at 16 MHz, and internal PLL for timer at 64 MHz. Any help will be appreciated. Thanks.

  • @FreClimbs
    @FreClimbs 11 ปีที่แล้ว

    Thanks for this very helpfull tutorial.
    isn't it better to insert the delay (and =+10 duty) cycle in a while loop in the interupt like this: ?
    ...
    ISR(TIMER0_OVF_vect)
    {
    OCR0A = (dutyCycle/100)*255;
    _delay_ms(100);
    dutyCycle += 10;
    if(dutyCycle > 100)
    {
    dutyCycle = 0;
    }
    }
    i've recognized that like this my "timing" matches, while if i put the delay loop in the while loop of the main program it isn't. (why?) Additionaly if i delete the delay, then this method gives a feable light on my led (as you would expect), while if i put the =+ dutycycle in the mainloop it doesnt give any light...

  • @patrykkorycki262
    @patrykkorycki262 8 ปีที่แล้ว

    What is maximal duty cycle?

    • @humanHardDrive
      @humanHardDrive  8 ปีที่แล้ว

      The max duty cycle would be 100%, or always on.

  • @minhly650
    @minhly650 9 ปีที่แล้ว

    What is exactly F_CPU ? What is the range of the frequency that F_CPU can be set ?
    Could anyone help me out ?

    • @WhoWantsToKnow81
      @WhoWantsToKnow81 9 ปีที่แล้ว +1

      Anh Minh Ly F_CPU is a #define which allows you to determine at which frequency the chip you are programmer should be running with. Which values are supported is determined by the datasheet for the chip you are trying to program. A value of 20 million (= 20 mega) indicates a value of 20 MHz. Chips like the ATMega328 and ATTiny45, for example, have internal oscillators which can be set to a maximum of 8MHz I believe. Anything higher than that (such as 16 MHz, 20 MHz) will require an external oscillator.

  • @demetriusdemarcusbartholom8063
    @demetriusdemarcusbartholom8063 2 ปีที่แล้ว

    ECE 312, UofA.

  • @koggism
    @koggism 7 ปีที่แล้ว

    Good videos, but boy do you tinker with the code off camera!!

  • @mikhailnikitin3900
    @mikhailnikitin3900 10 ปีที่แล้ว

    I'm trying to set PWM for motor with 2 timers. one it should be timer0 on than timer0 off
    after it should be timer2 on than timer2 off and etc...
    I got buzzer sound instead of a good results....
    how can I switch off timer...?
    thanks!
    here is my code:
    #define F_CPU 20000000
    #include
    #include
    #include
    double dutyCycle = 0;
    double Motor2 = 0;
    int main(void)
    {
    DDRD = (1

  • @humanHardDrive
    @humanHardDrive  11 ปีที่แล้ว

    Send me an email at humanHardDrive@gmail.com. I'm interested to see your code.

  • @Wael123-g3c
    @Wael123-g3c 2 ปีที่แล้ว

    thank you 💚