PWM pins in pic18f4431 in MPLAB

Thread Starter


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


; 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


Joined Jan 18, 2008
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.



Joined Jul 18, 2013
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.


Joined Feb 24, 2006
i have read datasheet of pic18f4431 but i don't understand how can i do it
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


Joined Sep 13, 2015
i want to activate PWM pins in pic18f4431 in MPLAB please help me!
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:

    pwm_init();                                //reset the pwm
    pwm_setpr(PTPS_4x, 1000);                //set period
    pwm_setdc0(pwm_getpr()/2);                //set duty cycle
    pwm_setdc1(pwm_getpr()/3);                //set duty cycle
as shown below.



Joined Feb 24, 2006
But that work may not apply to his goal of utilizing the pwm functions of this particular PIC -
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.