Searching \ for 'using the PWM module' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/io/pwm/index.htm?key=pwm
Search entire site for: 'using the PWM module'.

Truncated match.
PICList Thread
'using the PWM module'
1998\10\29@060106 by Bart Stofferis

flavicon
face
Hi there ...

Can anyone help me with a source code of how to program the PWM module
of the PIC16C73A.

I would like to generator a 100 Hz PWM signal using this CCP module.

Software PWM is no problem but asks to much processor time

thanks in advance ...

Bart

1998\10\29@103632 by Matt Bonner

flavicon
face
Bart Stofferis wrote:

> Can anyone help me with a source code of how to program the PWM module
> of the PIC16C73A.
>
> I would like to generator a 100 Hz PWM signal using this CCP module.
>
1.  Set CCP1CON for PWM mode
     eg: CCP1CON = 0b00101111
   You might need to vary bits 5 and 4 (these are part of
   the duty cycle calculation)

2.  Set your prescaler using the timer2 control register (T2CON).
   You can set it to prescale by 1, 4, or 16 by setting/clearing
   bits 1 and 0. The value you choose will depend on your oscillator
   frequency.  Choose "1" for now - you can change it if the next
   step doesn't give you a reasonable calculated frequency.

3.  Calculate the value to load into the PR2 register for your
   desired frequency.  Example 10-2 in the PIC16C7X data book shows
   this fairly clearly.  You may have to adjust your prescale value
   (step 2).  For example, to generate approximately 2kHz on a '74A
   with a 3.6864MHz oscillator and using a prescale of 1, I use:
     PR2 = 0x1B

4.  Calculate your "centre" duty cycle.  Note the way MChip defines
   the PWM duty cycle, though: it is the ON *time* of the PWM
   waveform (as opposed to a duty cycle expressed as a ratio of
   ON time to total period).  Load the 2 lowest bits of this value
   into bits 5 and 4 of CCP1CON.  The MSbits go into CCPR1L.  Vary
   your duty cycle by changing this value.  Again, example 10-2
   shows a calculation to determine the resolution of your duty
   cycle variation.

5.  Start/stop the PWM by setting/clearing T2CON.2

I've probably left something out and I'm sure I've made some typos -
others will fill in the blanks.

HTH, Matt

1998\10\29@104408 by Bart Stofferis

flavicon
face
Hi Matt ...

Thanks a lot for your response !!

In the mean time i found already a solution ... The duty cycle in the
manual is
indeed specified as the on-time ... that was a bit confusing for me as a
beginner
PIC-programmer.

I know that 100 Hz is not possible with a Xtal frequency of 18.432 MHz,
the lowest
frequency i can achieve is 1125 Hz. That's no problem because the
switching devices
can handle up to 5 kHz.

If lower frequencies are desired an external synchronisation signal must
be used.

So ! After it works it is always a piece of cake ...


Thanks again and see you again !

Bart


> {Original Message removed}

1998\10\29@155728 by paulb

flavicon
face
Bart Stofferis wrote:

> I know that 100 Hz is not possible with a Xtal frequency of 18.432
> MHz, the lowest frequency i can achieve is 1125 Hz.  That's no problem
> because the switching devices can handle up to 5 kHz.

 Uh-oh!  Sounds like you *do* have a problem there!  Are you saying the
lowest pulse frequency (PWM cycle) is 1125Hz or the lowest PWM clock
(resolution) is 1125Hz?

 Your FET switching frequency must equal or better the smallest
increment by which the PWM pulse will be adjusted.
--
 Cheers,
       Paul B.

1998\10\29@162528 by andre

flavicon
face
part 0 2330 bytes content-type:multipart/alternative; boundary="------------697063B6C227587EE55A3" (decoded 7bit)


--------------697063B6C227587EE55A382E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit



Bart Stofferis wrote:

> Hi there ...
>
> Can anyone help me with a source code of how to program the PWM module
> of the PIC16C73A.
>
> I would like to generator a 100 Hz PWM signal using this CCP module.
>

Hi Bart,

I was working on 10 khz hardware PWM for  pic16c74a  4 Mhz clock
I just got it done. I had my macros saved in separate include file
what ever macro I used in this code I copped in  to make it 1 asm file.
this code will work on pic demo 2 and RA0 pot will change the  DUTY
cycle. for testing purpose I outputted ADC result to portb just to see it
and at the same time DUTY willl effect . you can easily modify it to lower

the frequency. To make it work for 73a you need to change processor type,
include file , disable portE and portD.
attached is the code

Andre Abelian








--------------697063B6C227587EE55A382E
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<HTML>
<TT>&nbsp;</TT>

<P>Bart Stofferis wrote:
<BLOCKQUOTE TYPE=CITE>Hi there ...

<P>Can anyone help me with a source code of how to program the PWM module
<BR>of the PIC16C73A.

<P>I would like to generator a 100 Hz PWM signal using this CCP module.
<BR>&nbsp;</BLOCKQUOTE>
Hi Bart,

<P>I was working on 10 khz hardware PWM for&nbsp; pic16c74a&nbsp; 4 Mhz
clock
<BR>I just got it done. I had my macros saved in separate include file
<BR>what ever macro I used in this code I copped in&nbsp; to make it 1
asm file.
<BR>this code will work on pic demo 2 and RA0 pot will change the&nbsp;
DUTY
<BR>cycle. for testing purpose I outputted ADC result to portb just to
see it
<BR>and at the same time DUTY willl effect . you can easily modify it to
lower
<BR>the frequency. To make it work for 73a you need to change processor
type,
<BR>include file , disable portE and portD.
<BR>attached is the code

<P>Andre Abelian
<BR>&nbsp;
<BR>&nbsp;

<P><TT>&nbsp;</TT>
<BR>&nbsp;

<P>&nbsp;</HTML>

--------------697063B6C227587EE55A382E--

part 1 5143 bytes content-type:application/x-unknown-content-type-asm_auto_file; name="cd.asm" (decoded base64)

;******************************************************************************
;                        ANDRE  ABELIAN
;                    DATA IMAGE TECHNOLOGY    
;                           10.29.98
;                CAPACITOR DISCHARGE IGNITION MODULE
;                        NOT DONE YET ???
;******************************************************************************

       list      p=16c74a            
       #include        
;        #include
       errorlevel -302        

       __CONFIG   _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC

GO                EQU        2                ; ADC START AND STOP BIT
DC_HI                EQU        0x55
DC_LO                EQU        0x56
T2_PERIOD        EQU        0xA0
COUNT20                EQU        0xA1
SWEEP                EQU        0xA2

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

               CBLOCK        0x20
               COUNT                
               TEMP                
               ADC_RESULT        
               ENDC


; ***** MACROS *****
;
; LOAD VALUE AND SAVE IT TO DESTINATION

MOVLF                MACRO        VALUE,DEST
               MOVLW        VALUE
               MOVWF        DEST
               ENDM
; example
; MOVLF        0x05,TEMP        ; LOAD 5 SAVE IT IN TEMP
; *****************************************************************
;
; SELECT ADC CHANNEL
; SCHRA0 MEANS - SELECT CHANNEL RA0

SCHRA0                MACRO                        ; SELECT RA0 ANALOG INPUT
               BCF        ADCON0,5
               BCF        ADCON0,4
               BCF        ADCON0,3
               ENDM

SCHRA1                MACRO                        ; SELECT RA1 ANALOG INPUT
               BCF        ADCON0,5
               BCF        ADCON0,4
               BSF        ADCON0,3
               ENDM

SCHRA2                MACRO                        ; SELECT RA2
               BCF        ADCON0,5
               BSF        ADCON0,4
               BCF        ADCON0,3
               ENDM

SCHRA3                MACRO                        ; SELECT RA3
               BCF        ADCON0,5
               BSF        ADCON0,4
               BSF        ADCON0,3
               ENDM

SCHRA4                MACRO                        ; SELECT RA4
               BSF        ADCON0,5
               BCF        ADCON0,4
               BCF        ADCON0,3
               ENDM

SCHRE0                MACRO                        ; SELECT RE0
               BSF        ADCON0,5
               BCF        ADCON0,4
               BSF        ADCON0,3
               ENDM

SCHRE1                MACRO                        ; SELECT RE1
               BSF        ADCON0,5
               BSF        ADCON0,4
               BCF        ADCON0,3
               ENDM

SCHRE2                MACRO                        ; SELECT RE2
               BSF        ADCON0,5
               BSF        ADCON0,4
               BSF        ADCON0,3
               ENDM


;*****  DEFINE *****

#DEFINE                BANK1                BSF        STATUS,RP0
#DEFINE                BANK0                BCF        STATUS,RP0                

;******************************************************************************

; ***** CLEAR RAM LOCATIONS INDIRECLY *****

               MOVLF        0x05,FSR        ; INITIALIZE POINTER AND TO RAM
               CLRF        INDF                ; CLEAR INDF REGISTER
               INCF        FSR,F                ; INC POINTER
               BTFSS        FSR,4                ; ALL DONE ?
               GOTO        $ - 3                ; NO CLEAR NEXT

;******************************************************************************

;***** SETUP  PORTS,ADC,OPTIION,INTERRUPT *****

               BANK1
               MOVLF        B'11111111',PR2                ; DEFAULT PWM PERIOD
               MOVLF        B'11111111',TRISA
               MOVLF        B'00000000',TRISB
               MOVLF        B'00000000',TRISC
               MOVLF        B'00000000',TRISD
               MOVLF        B'11111111',TRISE
               BSF        PIE1,TMR1IE                ; ENABLE TMR1 INTERRUPT
               MOVLF        B'10000111',OPTION_REG        ; TIMER 256 PULLUP DISABLE
               BANK0
;
;***** PWM SETUP *****
;
INITPWM                MOVLF        4H,T2CON                ; SETUP T2CON WITH PRESCALER = 0 AND TIMER2 ON
               MOVLF        0FH,CCP2CON                ; SETUP CAPTURE/COMPARE TO PWM MODE STANDARD RESOLUTION
               MOVLF        .125,CCPR2L                ; SET COMPARE REGISTER TO HALF FOR NOW
               BANK1
               MOVLF        .99,PR2                        ; THIS IS COMPARE REGISTER IF YOU CHANGE THE
                                               ; VALUE IT WILL CHANGE THE FREQUENCY OF PWM
;******************************************************************************
;
; ***** ADC *****
;
               BANK1
               CLRF        ADCON1
               MOVLF        B'00000000',ADCON1        ; SETUP PORTA,E ANALOG Vref VDD
               BANK0
               MOVLF        B'11000001',ADCON0        ; CLOCK FROM RC. ADCON IS ON

;******************************************************************************
; INFORMATIONS ABOUT ADC CHANNELS
;
; SCRA0  SELECT ADC CHANNEL 1                                
; SCRA1  SELECT ADC CHANNEL 2
; SCRA2  SELECT ADC CHANNEL 3
; SCRA3  SELECT ADC CHANNEL 4
; SCRA4  SELECT ADC CHANNEL 5
; SCRE0  SELECT ADC CHANNEL 6
; SCRE1  SELECT ADC CHANNEL 7
; SCRE2  SELECT ADC CHANNEL 8
;
;******************************************************************************

; ANOTHER WAY OF MONITORING ADC BY USING INTERRUPT
;
;                BANK1
;                BSF        PIE1,ADIE        ; ENABLE A/D INTERRUPTS
;                BANK0
;                MOVLF        0xC1,ADCON0        ; RC CLOCK IS ON , CH ONE SELECTED
;                BCF        PIR1,ADIF        ; CLEAR A/D INTERRUPT FLAG BIT
;                BSF        INTCON,PEIE        ; ENABLE ALL INTERRUPTS
;
;                BSF        ADCON0,GO        ; START A/D
;                                        ; ADIF BIT WILL BE SET AND THE
                                       ; GO/DONE BIT IS CLEARED UPON
                                       ; COMPLETION OF THE A/D CONVERSION
;******************************************************************************                        
;
; ***** MAIN PROGRAM *****
;
START                SCHRA0                        ; SELECT CHANNEL RA0
               BSF     ADCON0,GO            ; START CONVERSION
               MOVLF   .20,TEMP        ; THIS LINE IS 20
               DECFSZ  TEMP,F                ; WAIT A WHILE
               GOTO    $-1
               BTFSC   ADCON0,GO            ; TEST FOR END OF CONVERSION
               GOTO    $-1
               MOVF    ADRES,W            ; LOAD A/D RESULT INTO W
               COMF        ADRES,W                ; COMPLIMENT IT
               MOVWF   ADC_RESULT        ; STORE IT
               MOVWF        PORTB
;
;***** CCPR2L IS PWM DUTY REGISTAER *****

;
;PWM_DUTY
;        
               MOVWF        CCPR2L                ; LOAD A/D RESULT IN PWM REGISTER
               GOTO        START

;
;***** THIS IS FOR TEST
;
;                BTFSS        PIR1,ADIF        ; TEST FOR A/D FLAG BIT TO GO HIGH
;                BCF        PIR1,ADIF        ; CLEAR A/D FLAG FOR NEXT GO


;***** DELAY *****

DELAY        
       CLRF        TMR0                ; START AT 0
       MOVF        TMR0,W                ; LOAD TIMER IN W PREPARE TO COMPARE
       XORLW        .125                ; IS IT  125
       BTFSS        STATUS,Z        ; CHECK IT
       GOTO        $ - 3                ; NO TRY AGAIN
       RETURN                        ; YES KEEP W CLEAN


               END      

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...