PWM on 18F pics

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,302
I am trying to get a pwm routine running on a pic18f24k22, and 18f2321
with no clues as how to get the routine working, neither of the datasheets have a working example asm file to start me off.:confused:

anybody got a link to a working pwm routine for these series of pics?
 
I am not into assembler much. I ran into a problem with GCBasic and PWM on the 18f26k22. So here is the massaged assembler output to turn on all the CCPx PWM's.

Rich (BB code):
 ;Set up the assembler options (Chip type, clock source, other bits and pieces)
 LIST p=18F26K22, r=DEC
#include <P18F26K22.inc>
 CONFIG LVP = OFF, MCLRE = INTMCLR, PBADEN = OFF, CCP2MX = PORTB3, WDTEN = OFF, BOREN = OFF, FOSC = INTIO67
;********************************************************************************
;Set aside memory locations for variables
ABCED EQU 256
ZEROX EQU 257
;********************************************************************************
;Vectors
 ORG 0
 goto BASPROGRAMSTART
 ORG 8
 retfie
;********************************************************************************
;Start of program memory page 0
 ORG 12
BASPROGRAMSTART
;Call initialisation routines
 rcall INITSYS
;Start of the main program
 bcf TRISA,5,ACCESS
 bcf TRISC,1,ACCESS
 bcf TRISC,2,ACCESS  ;CCP1 default
 bcf TRISB,3,ACCESS  ;CCP2 with mux config PortB,3
 bcf TRISB,5,ACCESS  ;CCP3 default
 bcf TRISC,6,ACCESS
 movlw 112    ;16MHz
 movwf OSCCON,ACCESS
 bsf OSCTUNE,PLLEN,ACCESS ;enable 4xPLL for 16mips
;******** CCP1 *****************
 movlw 12  ;std PWM mode
 movwf CCP1CON,ACCESS
 movlw 0  ;16 MHz (PRx + 1)*1/16000000*TMRx prescale
 movwf PR2,ACCESS
 movlw 0  ;D.C. 50% (CCPRxL:CCPxCON<5:4>)/4(PRx+1)
 movwf CCPR1L,ACCESS
 bsf  CCP1CON,DC1B1,ACCESS
 bcf  CCP1CON,DC1B0,ACCESS
 bsf  T2CON,TMR2ON,ACCESS  ;no prescale
;******** CCP2 *****************
 movlw 12  ;std PWM mode
 movwf CCP2CON,ACCESS
 movlw 8  ;set CCP2 PWM timer as TMR4
 banksel CCPTMRS0
 iorwf CCPTMRS0
 movlw 50  ;50 instr cycles * 16 prescale * 1/16000000 = 50 us or freq. of 20kHz
 banksel PR4
 movwf PR4
 movlw 20  ;40% D.C. with PR4=50: 20 (instr cycles) * 16 (prescale) * 1/16000000 = 20 us
 movwf CCPR2L,ACCESS
 movlw 7  ;Prescale is 16, TMR4ON=1
 banksel T4CON
 movwf T4CON
;******** CCP3 *****************
 movlw 12  ;std PWM mode
 banksel CCP3CON
 movwf CCP3CON
 movlw 128  ;set CCP3 PWM timer as TMR6
 banksel CCPTMRS0
 iorwf CCPTMRS0
 movlw 100  ;100 instr cycles * 16 prescale * 1/16000000 = 100 us or freq. of 10kHz
 banksel PR6
 movwf PR6
 movlw 50  ;50% D.C. with PR4=100: 50 (instr cycles) * 16 (prescale) * 1/16000000 = 50 us
 banksel CCPR3L
 movwf CCPR3L
 movlw 7  ;Prescale is 16, TMR6ON=1
 banksel T6CON
 movwf T6CON
;*******************************
 clrf TRISA,ACCESS ;just keeping some leds off
 movlw 223
 movwf LATA,ACCESS
START
 movlw 255  ;nonsense stuff
 banksel ABCED
 movwf ABCED,BANKED
 movlw 123
 movwf ZEROX,BANKED
 bra START
BASPROGRAMEND
 sleep
 bra BASPROGRAMEND
;********************************************************************************
INITSYS
 clrf BSR,ACCESS
 clrf TBLPTRU,ACCESS
 bcf ADCON0,ADON,ACCESS
 bcf ADCON2,ADFM,ACCESS
 movlw 0
 banksel ANSELA
 movwf ANSELA
 movwf ANSELB
 bcf CM2CON0,C2ON,ACCESS
 bcf CM1CON0,C1ON,ACCESS
 clrf PORTA,ACCESS
 clrf PORTB,ACCESS
 clrf PORTC,ACCESS
 clrf PORTE,ACCESS
 return
;********************************************************************************
 
 END
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,302
I copied and pasted the #2 post but it just dose not do anything,
I need to get an asm file already in use to see how its setup.
 
Last edited:
My copy paste from my previous post, project build, perfectly fine, here is the output from MPLAB ISE v8.66:
Rich (BB code):
Release build of project `C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\ElectroTech DodgyDave.mcp' started.
Language tool versions: MPASMWIN.exe v5.40, mplink.exe v4.38, mplib.exe v4.38
Fri Sep 27 09:30:28 2013
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.o".
Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.err".
Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.hex".
Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.lst".
Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.cof".
Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\ElectroTech DodgyDave.mcs".
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F26K22 "18f26k22 PWM.asm" /l"18f26k22 PWM.lst" /e"18f26k22 PWM.err"
Executing: "C:\Program Files\Microchip\MPASM Suite\mplink.exe" /p18F26K22 "18f26k22 PWM.o" /z__MPLAB_BUILD=1 /o"18f26k22 PWM.cof" /M"18f26k22 PWM.map" /W /x
MPLINK 4.38, Linker
Copyright (c) 1998-2010 Microchip Technology Inc.
Errors    : 0

Loaded C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.cof.
----------------------------------------------------------------------
Release build of project `C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\ElectroTech DodgyDave.mcp' succeeded.
Language tool versions: MPASMWIN.exe v5.40, mplink.exe v4.38, mplib.exe v4.38
Fri Sep 27 09:30:32 2013
----------------------------------------------------------------------
BUILD SUCCEEDED
Saying it doesn't work is not helpful, what are the errors from MPLAB project output window? You did change the asm file lines "p= ....." , and "#include<...." to your 18f24k22chip, correct?
 
Last edited:

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,302
OK so i got it to work but, portc 3&5 don't alter in intensity,just on /off, i can alter the intensty of portb3 ok, but portb5 don't do much by way of dimming just on or off.
 
With CCP1 (PortC,2), I was just fooling around to see what the max clock rate of the PWM is. The example asm file says 16 MHZ. Only problem here is that at 16 MHZ there is only 2 bits of resolution, not good for any sort of duty cycle range. Change the PR4 setting to 100 to get a slower clock of 10kHz, and should be O.K.

CCP2 is PortB,3 with the CONIFG of CCP2MX = PORTB3, and CCP3 is PortB,5 in my example code, just to avoid any confusion.

The 18f2xk22, 18f4xk22 series have more than 256 SFR's, so be sure you use banksel in your MAIN code for those registers outside this range. My example shows exactly which registers need the banksel's.
 
Top