1
\$\begingroup\$

I'm attempting get a very basic PWM code to work. However all PWM outputs seem stuck on +5V with no proper PWM output. It works perfectly in the simulator but not on the chip. Any ideas welcome?

    include <P18f4431.INC>

CONFIG     OSC = IRC
CONFIG     FCMEN = OFF
CONFIG     IESO = OFF
CONFIG     PWRTEN = OFF
CONFIG     BOREN = ON
CONFIG     BORV = 20
CONFIG     WDTEN = OFF
CONFIG     WDPS = 1
CONFIG     WINEN = OFF
CONFIG     PWMPIN = OFF
CONFIG     LPOL = LOW
CONFIG     HPOL = LOW
CONFIG     T1OSCMX = OFF
CONFIG     FLTAMX = RC1
CONFIG     SSPMX = RD1
CONFIG     PWM4MX = RB5
CONFIG     EXCLKMX = RC3
CONFIG     MCLRE = ON
CONFIG     CP0 = OFF
CONFIG     CP1 = OFF
CONFIG     CP2 = OFF
CONFIG     CP3 = OFF
CONFIG     CPD = OFF
CONFIG     WRT1 = OFF
CONFIG     WRT2 = OFF
CONFIG     WRT3 = OFF
CONFIG     WRTC = OFF
CONFIG     WRTB = OFF
CONFIG     WRTD = OFF
CONFIG     EBTR0 = OFF
CONFIG     EBTR1 = OFF
CONFIG     EBTR2 = OFF
CONFIG     EBTR3 = OFF
CONFIG     EBTRB = OFF
CONFIG     STVREN = OFF
CONFIG     DEBUG = OFF
CONFIG     CPB = OFF
CONFIG     WRT0 = OFF
CONFIG     LVP = OFF


;***********VARIABLE DEFINITIONS******************

Timer   res 1
Timer2  res 1


;*******************************************************************************
; Reset Vector
;*******************************************************************************

STARTUP  CODE    0x00           ; processor reset vector
  GOTO    START                   ; go to beginning of program


 ;*******************************************************************************
 ; MAIN PROGRAM
 ;******************************************************************************

 PROG1 code

START:

;; Initialise PORTB
;  CLRF  PORTB
;  CLRF LATB
;  movlw 0x00
;  movwf TRISB
;movlw 0xff
;movwf PORTB

movlw    b'10111110'
movwf    TRISC
;  bsf  PORTC, 0

movlw    b'11010000'
movwf    TRISD
bcf  PORTD, 0

 ;***********SET UP OSCILLATOR**************

bsf   OSCCON, IRCF0
bsf   OSCCON, IRCF1
bsf   OSCCON, IRCF2

call INIT_PCPWM


;***********MAIN ROUTINE **********************************

;turn on lamp to show power
bsf PORTD, 1

movlw   d'88'
movwf   PDC0L  ;'set the PWM duty cycle
movlw   d'00'
movwf   PDC0H
bcf INTCON, RBIF
bcf INTCON, RBPU

Loop:
  call Delay
  GOTO Loop                         ; loop forever

 ;*********DELAY ROUTINES**********************

Delay

movlw   d'5'
movwf   Timer
movwf   Timer2

Loop2:
  nop
Loop1:
nop
decfsz Timer, f
goto Loop1
decfsz Timer2, f
goto Loop2

return

;***********************************************
 ;***********SET UP PWM***********************

INIT_PCPWM

movlw   b'01000000' ; deadtime
movwf   DTCON

movlw   b'00000000'
movwf   PTCON0  ; Postcale 1:1, PWM time base input is Fosc/4
                ; PWM time base mode is free-running for edge-aligned operation
movlw   b'10000000'
movwf   PTCON1  ; Count up, timer on

movlw   d'99'    ;PTPERL and PTPERH are set up for a XX PWM frequency
movwf   PTPERL
movlw   d'00'
movwf   PTPERH

movlw   b'01000000' ;configure PWMCON0
movwf   PWMCON0     ; enable PWM 0 to 5, all PWM I/O pairs set to complimentary

movlw   b'00000000' ;configure PWMCON0
movwf   PWMCON1     ; Special event postscaler set to 1
                ; Special event trigger occurs when time-base is counting upward
                ; Updates from duty cycle and period buffer registers are enabled
                ; Ouput overides via OVDCON are synchronous to the PWM timebase

movlw   b'11111111' ; no output override
movwf   OVDCOND

movlw   b'00000000' ;OVDCONS is configured such that all PWM outputs are 0 on power up
movwf   OVDCONS

movlw   b'10010001' ;FAULT A and B enabled in catastrophic mode
movwf   FLTCONFIG

movlw   0x00    ;SECTCMPL and SEVTCMPH are cleared
movwf   SEVTCMPL
movlw   0x00
movwf   SEVTCMPH

bcf FLTCONFIG, FLTAEN   ; disable fault condition
bcf FLTCONFIG, FLTBEN   ; disable fault condition

bsf PTCON1, PTEN    ; enable the PWM time base

return

;***********************************************
  END
\$\endgroup\$
1
\$\begingroup\$

not sure but it might have something to do with your ptper value. for example when i calculate your resolution which is ln(Fosc/Fpwm)/ln2 i get 8.64 bits. I believe you want this to be an integer, such as 8 or 10 bits. So if you backsolve to get the correct pwm frequency, you can then get a proper ptper value. once again, not sure if this is the issue, but it might be..

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I've changed PTPER to 255 to give an 8 bit resolution but still no joy I'm afraid. \$\endgroup\$ – tommo2000 Jul 22 '13 at 12:19
  • \$\begingroup\$ I have found that the FLTCONFIG register was set up incorrectly and a fault had been enabled. All working fine now, thankyou. \$\endgroup\$ – tommo2000 Jul 24 '13 at 22:35
0
\$\begingroup\$

Probably harmless, but this doesn't look right:

bcf INTCON, RBPU

I think it should be:

bsf INTCON2, RBPU

...or let it default to 1 to disable weak pull ups on PORTB (PWM0/1/2/3/5/4). As it is coded, it may just be turning off the GIE bit (disabling General Interrupts).

The only other thing I see is that you enable the PWM time base (PTEN) by:

movlw   b'10000000'
movwf   PTCON1  ; Count up, timer on

...and then later enable it again with:

bsf PTCON1, PTEN    ; enable the PWM time base

I would have disabled it while setting things up and then enabled it afterwards. But after reading the data sheet I can't see why that would make a difference anyway.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks I just tried these suggestions but they don't seem to help. Although I will implement them as best practice. Interestingly all ports are high so they are not acting in complementary mode. Also if I change the polarity of the configuration bit all ports go low. Some possible clues perhaps? \$\endgroup\$ – tommo2000 Jul 20 '13 at 18:42
  • \$\begingroup\$ It might be a good idea to post a circuit diagram. Have you tied MCLR to +5V (via a 10-40k resistor)? \$\endgroup\$ – aja Jul 21 '13 at 0:27
  • \$\begingroup\$ Unfortunately I don't have enough reputation points to post an image but I am basically driving it using a Pickit 2 device with MCLR tied to 5V via a 10k resistor. I'm using the internal oscillator so I might try using an external one to see if that will make a difference. Although the clock seems to be working as I can get the USART working successfully. \$\endgroup\$ – tommo2000 Jul 22 '13 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.