PWM on 18F pics

Discussion in 'Embedded Systems and Microcontrollers' started by Dodgydave, Sep 25, 2013.

  1. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,983
    744
    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?
     
  2. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    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.

    Code ( (Unknown Language)):
    1.  ;Set up the assembler options (Chip type, clock source, other bits and pieces)
    2.  LIST p=18F26K22, r=DEC
    3. #include <P18F26K22.inc>
    4.  CONFIG LVP = OFF, MCLRE = INTMCLR, PBADEN = OFF, CCP2MX = PORTB3, WDTEN = OFF, BOREN = OFF, FOSC = INTIO67
    5. ;********************************************************************************
    6. ;Set aside memory locations for variables
    7. ABCED EQU 256
    8. ZEROX EQU 257
    9. ;********************************************************************************
    10. ;Vectors
    11.  ORG 0
    12.  goto BASPROGRAMSTART
    13.  ORG 8
    14.  retfie
    15. ;********************************************************************************
    16. ;Start of program memory page 0
    17.  ORG 12
    18. BASPROGRAMSTART
    19. ;Call initialisation routines
    20.  rcall INITSYS
    21. ;Start of the main program
    22.  bcf TRISA,5,ACCESS
    23.  bcf TRISC,1,ACCESS
    24.  bcf TRISC,2,ACCESS  ;CCP1 default
    25.  bcf TRISB,3,ACCESS  ;CCP2 with mux config PortB,3
    26.  bcf TRISB,5,ACCESS  ;CCP3 default
    27.  bcf TRISC,6,ACCESS
    28.  movlw 112    ;16MHz
    29.  movwf OSCCON,ACCESS
    30.  bsf OSCTUNE,PLLEN,ACCESS ;enable 4xPLL for 16mips
    31. ;******** CCP1 *****************
    32.  movlw 12  ;std PWM mode
    33.  movwf CCP1CON,ACCESS
    34.  movlw 0  ;16 MHz (PRx + 1)*1/16000000*TMRx prescale
    35.  movwf PR2,ACCESS
    36.  movlw 0  ;D.C. 50% (CCPRxL:CCPxCON<5:4>)/4(PRx+1)
    37.  movwf CCPR1L,ACCESS
    38.  bsf  CCP1CON,DC1B1,ACCESS
    39.  bcf  CCP1CON,DC1B0,ACCESS
    40.  bsf  T2CON,TMR2ON,ACCESS  ;no prescale
    41. ;******** CCP2 *****************
    42.  movlw 12  ;std PWM mode
    43.  movwf CCP2CON,ACCESS
    44.  movlw 8  ;set CCP2 PWM timer as TMR4
    45.  banksel CCPTMRS0
    46.  iorwf CCPTMRS0
    47.  movlw 50  ;50 instr cycles * 16 prescale * 1/16000000 = 50 us or freq. of 20kHz
    48.  banksel PR4
    49.  movwf PR4
    50.  movlw 20  ;40% D.C. with PR4=50: 20 (instr cycles) * 16 (prescale) * 1/16000000 = 20 us
    51.  movwf CCPR2L,ACCESS
    52.  movlw 7  ;Prescale is 16, TMR4ON=1
    53.  banksel T4CON
    54.  movwf T4CON
    55. ;******** CCP3 *****************
    56.  movlw 12  ;std PWM mode
    57.  banksel CCP3CON
    58.  movwf CCP3CON
    59.  movlw 128  ;set CCP3 PWM timer as TMR6
    60.  banksel CCPTMRS0
    61.  iorwf CCPTMRS0
    62.  movlw 100  ;100 instr cycles * 16 prescale * 1/16000000 = 100 us or freq. of 10kHz
    63.  banksel PR6
    64.  movwf PR6
    65.  movlw 50  ;50% D.C. with PR4=100: 50 (instr cycles) * 16 (prescale) * 1/16000000 = 50 us
    66.  banksel CCPR3L
    67.  movwf CCPR3L
    68.  movlw 7  ;Prescale is 16, TMR6ON=1
    69.  banksel T6CON
    70.  movwf T6CON
    71. ;*******************************
    72.  clrf TRISA,ACCESS ;just keeping some leds off
    73.  movlw 223
    74.  movwf LATA,ACCESS
    75. START
    76.  movlw 255  ;nonsense stuff
    77.  banksel ABCED
    78.  movwf ABCED,BANKED
    79.  movlw 123
    80.  movwf ZEROX,BANKED
    81.  bra START
    82. BASPROGRAMEND
    83.  sleep
    84.  bra BASPROGRAMEND
    85. ;********************************************************************************
    86. INITSYS
    87.  clrf BSR,ACCESS
    88.  clrf TBLPTRU,ACCESS
    89.  bcf ADCON0,ADON,ACCESS
    90.  bcf ADCON2,ADFM,ACCESS
    91.  movlw 0
    92.  banksel ANSELA
    93.  movwf ANSELA
    94.  movwf ANSELB
    95.  bcf CM2CON0,C2ON,ACCESS
    96.  bcf CM1CON0,C1ON,ACCESS
    97.  clrf PORTA,ACCESS
    98.  clrf PORTB,ACCESS
    99.  clrf PORTC,ACCESS
    100.  clrf PORTE,ACCESS
    101.  return
    102. ;********************************************************************************
    103.  
    104.  END
    105.  
    106.  
     
    Dodgydave likes this.
  3. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,983
    744
    Thanks, will try it.
     
  4. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,983
    744
    Tried it but still not working, i am using Asm files not basic.
     
  5. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,730
    Post what you got, and tell us what "not working" means.
     
    Eric007 likes this.
  6. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,983
    744
    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: Sep 27, 2013
  7. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    My copy paste from my previous post, project build, perfectly fine, here is the output from MPLAB ISE v8.66:
    Code ( (Unknown Language)):
    1. Release build of project `C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\ElectroTech DodgyDave.mcp' started.
    2. Language tool versions: MPASMWIN.exe v5.40, mplink.exe v4.38, mplib.exe v4.38
    3. Fri Sep 27 09:30:28 2013
    4. ----------------------------------------------------------------------
    5. Clean: Deleting intermediary and output files.
    6. Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.o".
    7. Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.err".
    8. Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.hex".
    9. Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.lst".
    10. Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.cof".
    11. Clean: Deleted file "C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\ElectroTech DodgyDave.mcs".
    12. Clean: Done.
    13. Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F26K22 "18f26k22 PWM.asm" /l"18f26k22 PWM.lst" /e"18f26k22 PWM.err"
    14. 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
    15. MPLINK 4.38, Linker
    16. Copyright (c) 1998-2010 Microchip Technology Inc.
    17. Errors    : 0
    18.  
    19. Loaded C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\18f26k22 PWM.cof.
    20. ----------------------------------------------------------------------
    21. Release build of project `C:\MPLAB_projects\MPASM_Projects\ElectroTech DodgyDave\ElectroTech DodgyDave.mcp' succeeded.
    22. Language tool versions: MPASMWIN.exe v5.40, mplink.exe v4.38, mplib.exe v4.38
    23. Fri Sep 27 09:30:32 2013
    24. ----------------------------------------------------------------------
    25. BUILD SUCCEEDED
    26.  
    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: Sep 27, 2013
  8. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,983
    744
    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.
     
  9. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    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.
     
    Dodgydave likes this.
Loading...