Cantafford
Member
Hey,
I'm trying to set up the CCP1CON module of the PIC16f870 microcontroller in PWM mode. I wrote this simple code that's supposed to send a 100% duty cycle on the CCP1CON(RC2) pin. I have attached a dc motor interfaced with a L293D driver to test the duty cycle's value. I have calculated the values for the registers using this PWM calculator:
https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html
This is the code:
My CCP1CON(RC2) pin won't initialise. It stays in 0 logic no matter what duty cycle I send. What am I missing? I calculated a pwm frequency of 1.5khz and a duty cycle of 100%
I'm trying to set up the CCP1CON module of the PIC16f870 microcontroller in PWM mode. I wrote this simple code that's supposed to send a 100% duty cycle on the CCP1CON(RC2) pin. I have attached a dc motor interfaced with a L293D driver to test the duty cycle's value. I have calculated the values for the registers using this PWM calculator:
https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html
This is the code:
Code:
#include "p16F870.inc"
; CONFIG
; __config 0xFF3A
__CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _CP_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_ALL
cblock 20h
d1
d2
d3
endc
RES_VECT CODE 0x0000 ; processor reset vector
GOTO START ; go to beginning of program
; TODO ADD INTERRUPTS HERE IF USED
MAIN_PROG CODE ; let linker place main program
START
bsf 03h, 5 ; select bank1
bcf 03h, 6
movlw b'00000000' ; PORTB made as output
movwf 86h
call _pwm_init
movlw h'01' ;direction assigned for the motor
movwf 06h
loop ; loop forever
goto loop
_delay_1s
movlw 0x2D
movwf d1
movlw 0xE7
movwf d2
movlw 0x0B
movwf d3
_delay_1s_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto _delay_1s_0
return
_pwm_init
bcf 87h, 2 ;make RC2 bit an output
movlw b'11001111'
movwf 92h ;PR2
bcf 03h, 5 ; select bank0
bcf 03h, 6
movlw b'00000111'
movwf 12h ;T2CON
movlw b'01100111'
movwf 15h ;CCPR1L
movlw b'00111100'
movwf 17h ;CCP1CON
return
END
My CCP1CON(RC2) pin won't initialise. It stays in 0 logic no matter what duty cycle I send. What am I missing? I calculated a pwm frequency of 1.5khz and a duty cycle of 100%

Last edited: