Which PIC TIMER is best for PWM

Discussion in 'Embedded Systems and Microcontrollers' started by camerart, Feb 13, 2018.

  1. camerart

    Thread Starter Well-Known Member

    Feb 25, 2013
    717
    31
    Hi,
    I want to convert a digital signal to PWM on an 18LF4520 PIC.
    There are 4x TMR, TIMERS, which is best for PWM?
    How I understand it is, there can be up to 10 1-2ms channels within a 20ms cycle.
    Why are the CCP PINS used instead of simple timers?
    Camerart
     
  2. Dodgydave

    AAC Fanatic!

    Jun 22, 2012
    7,235
    1,194
    You use the CCP timer on the pics it's a deadicated timer for pwm, you set the on/off times and frequency with one timer

    Problem is the datasheet is useless at explaining it or giving a working Asm file for you to copy and modify, which is why nobody uses them.
     
  3. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    5,944
    1,390
    If you use the PWM function of the CCP module then the work of producing the PWM signal is done by the hardware and your code can get on with other stuff.
     
  4. LesJones

    Well-Known Member

    Jan 8, 2017
    1,298
    303
    I found the PWM module on the PIC18F2431 easy to use. This it the code to initialise the PWM module ad the code to change it's duty cycle.
    Code (Text):
    1.  
    2. ;   ******   PIC18F2431 PWM initialization ******
    3.  
    4. ;PTCON0 Mode bits 0-1 00 free run, Prescale bits 3-2  11 1/64, Postscale bits 7 - 4 0000  1:1
    5.    MOVLW   b'00000000'
    6.    MOVWF   PTCON0
    7.  
    8.    CLRF   PTMRL
    9.    CLRF   PTMRH
    10.  
    11.    MOVLW   0xFF
    12.    MOVWF   PTPERL
    13.  
    14.    MOVLW   0x0F  
    15.    MOVWF   PTPERH
    16.  
    17.    MOVLW   b'00100000'
    18.    MOVWF   PWMCON0
    19.  
    20.    MOVLW   b'00000001'  
    21.    MOVWF   PWMCON1
    22.  
    23.    MOVLW   b'00000000'
    24.    MOVWF   DTCON
    25.  
    26.    MOVLW   0xFF
    27.    MOVWF   OVDCOND
    28.  
    29.    MOVLW   0x00
    30.    MOVWF   OVDCONS
    31.  
    32. ;   movlw   b'10110011'   ;Faults on
    33.    movlw   b'10000000'   ;Faults off
    34.    MOVWF   FLTCONFIG
    35.  
    36.    MOVLW   b'10000000'   ;Timebase on, count up
    37.    MOVWF   PTCON1
    38.  
    39. ;   MOVLW   b'00000000'
    40. ;   MOVWF   PDC0  
    41.  
    42. ; End of initialisation
    43.  
    44. ;This is the code to change the duty cycle of the PWM waveform.
    45. PWM_Write
    46.    bsf     PWMCON1,UDIS   ;Disable the PWM buffer update  Max value 3F FC (1F FE) (0F FF)
    47.    MOVFF   Temp_L, PDC0L       ;bits 2 - 7 used
    48.    MOVFF   Temp_H, PDC0H       ;bits 0 - 5 used
    49.    bcf     PWMCON1,UDIS   ;Enable the PWM buffer update
    50.  
    51.  
    The PWM module just runs without affecting the code that is running. You only need to run 4 lines of code to change it's duty cycle.

    This code is from a PID motor speed controller for driving the X axis table on my milling machine. I don't remember much about it as it was about 10 years ago when I wrote it.
    I dont think it will be much different on you PIC.

    Les.
     
    Last edited: Feb 13, 2018
  5. camerart

    Thread Starter Well-Known Member

    Feb 25, 2013
    717
    31
    Hi D and A,
    I found a fair explanation of CCPs and I'm now being reminded of past programs I've used, without actually putting two and two together.

    So I can understand:
    'Say' there are 10 channels within the 20ms cycle, 1 used and 9 not used. Am I correct that for CH1, there is a start trigger then the rest of CH1 is a %HIGH and a %LOW for up to the 2ms, and the rest of the channels each have a trigger, but stay low for the rest of the 2ms?
    C
     
  6. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    5,944
    1,390
    If you are only using 1 channel then the signal is 2mS high and 18mS low. You can set the PWM duty cycle to match that and the PWM period to 20mS.
     
  7. camerart

    Thread Starter Well-Known Member

    Feb 25, 2013
    717
    31
    Hi L,
    I'm afraid I'm stuck with only being able to read BASIC, but thanks. [although
    I can almost see what your code is doing]
    C
     
  8. camerart

    Thread Starter Well-Known Member

    Feb 25, 2013
    717
    31
    Hi A,
    I'll be using more than 1CH, but only 1 to start with, so I would like to have all of the channels available, in case.

    Can we clarify the terms please. Words like duty cycle etc.
    Is there a trigger, if so what is that term called?
    What is the term for each of the channels?
    What is the term for the full 20ms total of all of the channels.
    C
     
  9. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    5,944
    1,390
    The PWM is free running - there is no trigger.
    If you want to generate more than one 1-2mS pulse in the 20mS frame then it gets a good bit more complicated. Using the hardware PWM you would need to set up an interrupt for each cycle and change the duty cycle as desired for each channel.
     
  10. camerart

    Thread Starter Well-Known Member

    Feb 25, 2013
    717
    31
    Hi A,
    If there is no trigger, what starts the CCP count?
    I will need to find out about the Terms used as in the questions at #8, so I can follow.
    C
     
  11. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    5,944
    1,390
  12. camerart

    Thread Starter Well-Known Member

    Feb 25, 2013
    717
    31
Loading...