PWM pins in pic18f4431 in MPLAB

Discussion in 'Embedded Systems and Microcontrollers' started by salma12, May 27, 2016.

  1. salma12

    Thread Starter New Member

    May 26, 2016
    3
    0
    i want to activate PWM pins in pic18f4431 in MPLAB please help me!
     
  2. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,692
    901
    Welcome to AAC.

    Please show your code.
    Which MPLAB simulator are you using?

    John
     
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    Have you read through the PWM section 18 of the manual?
    Max.
     
    salma12 likes this.
  4. salma12

    Thread Starter New Member

    May 26, 2016
    3
    0
    thank you
    i'm using mplab 8.91
    that's my code
    ;list p=18f4431
    #include <p18f4431.inc>

    GOTO INIT

    ; initialisation du PWM Contrôle moteur
    Init: movlw 0x00 ; 1:1 prédiviseur INT PWM, 1:1 prédiviseur(Fosc/4)
    movwf PTCON0 ; fonctionnement libre (Free Running)
    ; initialisation de la période
    movlw 0x80 ; PTPERL et PTPERH pour un avoir 78,126KHz de fréquence PWM
    movwf PTPERL ; PTPERH:pTPERL = ((Fosc/4)/(PWMfreq x PTMRps))-1
    movlw 0x00 ; PTPERH:pTPERL = ((40MHz/4)/(78KHz x 1))-1 = 128d = 80h
    movwf PTPERH ; duty-cycle 100% avec 516d = 204h = 1000 0001 00(Q-clocks)
    ; initialisation des sorties PWM
    movlw b'01111110' ; PWM 1,3 et 5 OK pour la mise en service(RB1-3-4)
    movwf PWMCON0 ; fonctionnement indépendant des sorties PWM

    bsf PWMCON1, OSYNC ; OVDCON are synchronous to the PWM timebase

    clrf DTCON ; pas de temps mort

    clrf OVDCOND ; pas d'activité PWM pour le moment

    clrf OVDCONS ; sortie PWM à gnd quand pas actif

    clrf FLTCONFIG ; pas de gestion des défauts

    clrf SEVTCMPL ; effacer car pas de besoin de synchro
    clrf SEVTCMPH ; pour la conversion A/D
    retlw
    END
     
  5. salma12

    Thread Starter New Member

    May 26, 2016
    3
    0
    i have read datasheet of pic18f4431 but i don't understand how can i do it
     
  6. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,692
    901
    I am not conversant with the PIC18F series; however, your code has a "goto init" and then uses a "retlw." A goto doesn't leave a return address on the stack. Use another "goto," or use a "call" to get to the routine.

    I took a brief look at the datasheet, and it doesn't appear that you have analog functions conflicting with digital functions, but it is worth a check. You must turn off the analog functions for the PWM pins.

    John
     
    salma12 likes this.
  7. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    As john says you need to turn off the analog inputs this micro has a ANSEL0 & 1.
    See Ex11-1 to initialize PortA for I/O
    The pwr up is all analogue so usually clrf ANSEL0/1.
    Max.
     
    salma12 likes this.
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,809
    362
    Maybe a better starting point for you is to lay out what you DO understand and go from there.
     
    salma12 likes this.
  9. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    Here is the executive summary:
    1. Every pin has multiple functions which can typically number from two to ten.
    2. Each different function is controlled by the setting of one or more bits scattered over a number of registers.
    3. The datasheet is like a giant reference encyclopedia. You pour over it hour after hour until you suss out the bits you need to set and clear for your particular purpose.
    4. Nobody is going to do it for you; this is one road you must travel alone -- Grasshopper
     
  10. dannyf

    Well-Known Member

    Sep 13, 2015
    1,809
    362
    cann't help you much there but would suggest you take up a more efficient approach to this. The following four lines get pwm done:

    Code (Text):
    1.  
    2.     pwm_init();                                //reset the pwm
    3.     pwm_setpr(PTPS_4x, 1000);                //set period
    4.     pwm_setdc0(pwm_getpr()/2);                //set duty cycle
    5.     pwm_setdc1(pwm_getpr()/3);                //set duty cycle
    6.  
    as shown below.
     
  11. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,941
    383
    salma12 likes this.
  12. dannyf

    Well-Known Member

    Sep 13, 2015
    1,809
    362
    But that work may not apply to his goal of utilizing the pwm functions of this particular PIC -
     
  13. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    And he needs to be smart enough to recognize the difference. Code examples will lead you in the right direction if you can verify that they come from "working" code. Academic examples don't count.
     
Loading...