Menu

uno mega328p pwm

Help
2017-03-01
2017-03-01
  • stan cartwright

    stan cartwright - 2017-03-01

    I can't seem to find mega328p uno pwm examples and the forum posts suggest problems.
    I get a 2.66 Khz square wave with this code but if I put the pwm outside the do loop I don't.
    Are there any examples please. I googled for hours and tried pic examples.

    #chip mega328p,16
    #option explicit
    #include <UNO_mega328p.h>
    #define PWM_Out1 portb.1 ;uno board socket 9
    dir PWM_Out1 out
    #define PWM_Freq 1
    #define PWM_Duty 127
    #define PWM_Out 1
    do
    PWMOut 1,127,10
    loop
    
     

    Last edit: Anobium 2017-03-01
  • kent_twt4

    kent_twt4 - 2017-03-01

    Code tags?

    Your code here
    

    Because you are only getting software PWM with the Atmel devices currently? Opportunity knocks for a AVR HPWM library.

     
    • Anobium

      Anobium - 2017-03-01

      I put the code tags in.....

       
  • kent_twt4

    kent_twt4 - 2017-03-01

    Let me clarify code tags are three tildes before and after code or Control-ALT-C
    "~~~"
    your code
    "~~~"

    Here is the manual manipulation of PWM/Output Compare registers for hardware PWM on the mega328p UNO.

    #chip mega328p,16
    
    '************pwm************************
    DIM PWM_LED as Word Alias OCR1AH,OCR1AL
    PWM_LED = 0
    dir PortB.1 out   'OC1A
    TCCR1A = B'10000001'    'Clear OCR1A on compare match, 8 bit fast pwm
    TCCR1B = B'00001010'    'PRESCALE 1/8, 8 MHz CK fuse, equals 7.81KHz
    '50% duty cycle
    PWM_LED = 127
    
    do
    
    loop
    
     
  • stan cartwright

    stan cartwright - 2017-03-01

    Thank you kent. I couldn't find that in the demos,manual,this forum,google.
    Having just bought a uno I found most info is c++. Confusing for a beginner to know if pic code can be used with arduino.
    I notice there's no #include <uno_mega328p.h> in your code. When is it necessary?
    Thanks for explaining ~~~. I had used ~~~~ at the begining but not at the end. Where is this explained in the site info? thanks again...I'll be back. </uno_mega328p.h>

     
  • Anobium

    Anobium - 2017-03-01

    Hardware PWM [HWPWM] (as shown in the manaul) support PICs only. Got time to write a library - should be relatively simple.

    ~~~~
    #include <uno_mega328p.h>
    ~~~~
    This is an ease of use library that allows the users to address the ports in a similar way to the Arduino IDE.</uno_mega328p.h>

    Re the ~~~~. See the </> or the ? in every posting. These explain how to insert code and the Help respectively.

     

    Last edit: Anobium 2017-03-01
  • stan cartwright

    stan cartwright - 2017-03-01

    Like others,when I've got a list of stuff I had trouble finding,I'll make a help sheet for stuff like pwm,timer interrupts etc

     
  • kent_twt4

    kent_twt4 - 2017-03-01

    @stan cartwright
    Yea, when you can't find an inbuilt GCB command you have to improvise :). That is the beauty about GCB you can dig right in to the datasheet and set the respective device registers to do exactly what you need. Unfortunately, Hugh/Evan/ and the rest of the developers are doing such a good job they have got most all the bases covered now, and the sense of discovery is waning :(.

    When Hugh started developing the Atmel AVR devices for GCB, it was definetely a bold and exciting move. So now having dual use code for PIC and AVR in most all cases is pretty unique.

    C++, when I see methods and "."'s, my brain goes blank...ughh.

    @Anobium
    I'll have a look at the hpwm.h for the mega and tiny and let you know.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.