Which PIC TIMER is best for PWM

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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
 

Dodgydave

Joined Jun 22, 2012
11,284
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.
 

AlbertHall

Joined Jun 4, 2014
12,345
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.
 

LesJones

Joined Jan 8, 2017
4,174
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:
;   ******   PIC18F2431 PWM initialization ******

;PTCON0 Mode bits 0-1 00 free run, Prescale bits 3-2  11 1/64, Postscale bits 7 - 4 0000  1:1
   MOVLW   b'00000000'
   MOVWF   PTCON0

   CLRF   PTMRL
   CLRF   PTMRH

   MOVLW   0xFF 
   MOVWF   PTPERL

   MOVLW   0x0F   
   MOVWF   PTPERH

   MOVLW   b'00100000' 
   MOVWF   PWMCON0

   MOVLW   b'00000001'   
   MOVWF   PWMCON1

   MOVLW   b'00000000' 
   MOVWF   DTCON

   MOVLW   0xFF
   MOVWF   OVDCOND 

   MOVLW   0x00
   MOVWF   OVDCONS

;   movlw   b'10110011'   ;Faults on
   movlw   b'10000000'   ;Faults off
   MOVWF   FLTCONFIG 

   MOVLW   b'10000000'   ;Timebase on, count up
   MOVWF   PTCON1

;   MOVLW   b'00000000'
;   MOVWF   PDC0  

; End of initialisation

;This is the code to change the duty cycle of the PWM waveform.
PWM_Write
   bsf     PWMCON1,UDIS   ;Disable the PWM buffer update  Max value 3F FC (1F FE) (0F FF)
   MOVFF   Temp_L, PDC0L       ;bits 2 - 7 used
   MOVFF   Temp_H, PDC0H       ;bits 0 - 5 used
   bcf     PWMCON1,UDIS   ;Enable the PWM buffer update
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:

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
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
 

AlbertHall

Joined Jun 4, 2014
12,345
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.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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:
;   ******   PIC18F2431 PWM initialization ******

;PTCON0 Mode bits 0-1 00 free run, Prescale bits 3-2  11 1/64, Postscale bits 7 - 4 0000  1:1
   MOVLW   b'00000000'
   MOVWF   PTCON0

   CLRF   PTMRL
   CLRF   PTMRH

   MOVLW   0xFF
   MOVWF   PTPERL

   MOVLW   0x0F 
   MOVWF   PTPERH

   MOVLW   b'00100000'
   MOVWF   PWMCON0

   MOVLW   b'00000001' 
   MOVWF   PWMCON1

   MOVLW   b'00000000'
   MOVWF   DTCON

   MOVLW   0xFF
   MOVWF   OVDCOND

   MOVLW   0x00
   MOVWF   OVDCONS

;   movlw   b'10110011'   ;Faults on
   movlw   b'10000000'   ;Faults off
   MOVWF   FLTCONFIG

   MOVLW   b'10000000'   ;Timebase on, count up
   MOVWF   PTCON1

;   MOVLW   b'00000000'
;   MOVWF   PDC0

; End of initialisation

;This is the code to change the duty cycle of the PWM waveform.
PWM_Write
   bsf     PWMCON1,UDIS   ;Disable the PWM buffer update  Max value 3F FC (1F FE) (0F FF)
   MOVFF   Temp_L, PDC0L       ;bits 2 - 7 used
   MOVFF   Temp_H, PDC0H       ;bits 0 - 5 used
   bcf     PWMCON1,UDIS   ;Enable the PWM buffer update
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.
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
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
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
 

AlbertHall

Joined Jun 4, 2014
12,345
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.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
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.
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
 
Top