Getting the pwm in pic

Discussion in 'Programmer's Corner' started by adriano08, Sep 23, 2010.

  1. adriano08

    Thread Starter Member

    Apr 25, 2010
    33
    1
    How do we write a c code for generating pwm in pic 18f4520?

    And perhaps some explanation?

    #include<pic.h>
    #include<delays.h>

    void main()
    {
    ...
    ...
    }

    I'm still learning. Thanks.:)
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
  3. PGP_Protector

    New Member

    Sep 24, 2010
    2
    0
    This is the Code I used on a PIC24FJ128GA010
    It's about the most striped down version I can find.
    Code ( (Unknown Language)):
    1.  
    2. #include <p24FJ128GA010.h>
    3. _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & FWDTEN_OFF & ICS_PGx2)
    4. _CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)
    5. void __attribute__((interrupt, no_auto_psv)) _T3Interrupt(void);
    6. void _ISRFAST _T3Interrupt( void){_T3IF = 0;}
    7. int main()
    8. {
    9.     T3CON = 0x8010;     // This is about 1.25KHz with the Debugger board running at 8MHz
    10.     PR3 = 400-1;
    11.     _T3IF = 0;
    12.     _T3IE = 1;
    13.     OC1R =OC1RS=200;
    14.     OC1CON = 0x000E;
    15.     while(1);
    16. }
    17.  
    This would produce (on that chip with an 8MHZ Crystal) a 1.25Khz Square Wave (50% modulation)
     
  4. Eng_Bandar

    Active Member

    Feb 27, 2010
    50
    1
  5. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Here is an english translated version of the link.

    It is a very good tutorial.

    http://translate.google.com/transla...&tl=en&u=http://www.dbaasco.com/vb/t5028.html

    The only thing that confuses me, is the part where you talk about the two men born to you.. I have yet to figure that out. ;)

     
  6. Eng_Bandar

    Active Member

    Feb 27, 2010
    50
    1
    Hi retched,​

    hahahaha
    I think translation of google not perfect. And also Arabic langauge not like English big difference between them. problem where here ? In Arabic langauge find rules to pronounce any word let's take example​

    رَجُل

    This word mean man

    رِجْل


    This word mean leg

    both words have same letters not same in its shapes

    if I wrote this word for you
    رجل
    and I ask you what is mean this word?
    realy you can't know it without its shapes
    same problem with google
    google don't distingishes shapes of the word ​

    I hope every thing clear for you ​
     
  7. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Yes.. I thought the translation was rather funny myself.
     
  8. Eng_Bandar

    Active Member

    Feb 27, 2010
    50
    1
    very fun translation.
    legs here mean pins of microcontroller

    and man word there mean legs in electronic mean pins of microcontroller

    Be aware.

    :D

    Check also my youtube channel
    http://www.youtube.com/user/EngBandar1

    I hope that will help you

    In future I will put English tutorials in my channel
    In Matlab, MikroC Pro, Orcad, Proteus and others

    :D
     
  9. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Good. I think you have a lot to offer. And you explain things in a very easy to understand way, with examples and theory.

    Ill check out your work.
     
Loading...