PDA

View Full Version : PWM basics problem



pleiny
- 28th August 2015, 07:50
In trying to get a 16887 PWM running I am using the following code. When I run the code the LED's don't change in intensity at all and in fact the PWM does not appear to run. I can't decide if the freq and duty aren't set properly to have the LED's dim and go brighter or if the code has some other problem that the PWM isn't running at all.
Port RC1 is CCP2 at pin 16 and Port RC2 is CCP1 at pin 17
Any thoughts please?


#IF __PROCESSOR__ = "16F887"
#DEFINE MCU_FOUND 1
#CONFIG
cfg1 = _INTRC_OSC_CLKOUT ; INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
cfg1&= _WDT_ON ; WDT enabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLRE_ON ; RE3/MCLR pin function is MCLR
cfg1&= _CP_OFF ; Program memory code protection is disabled
cfg1&= _CPD_OFF ; Data memory code protection is disabled
cfg1&= _BOR_ON ; BOR enabled
cfg1&= _IESO_ON ; Internal/External Switchover mode is enabled
cfg1&= _FCMEN_ON ; Fail-Safe Clock Monitor is enabled
cfg1&= _LVP_OFF ; RB3 pin has digital I/O, HV on MCLR must be used for programming
cfg1&= _DEBUG_OFF ; In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
__CONFIG _CONFIG1, cfg1

cfg2 = _BOR40V ; Brown-out Reset set to 4.0V
cfg2&= _WRT_OFF ; Write protection off
__CONFIG _CONFIG2, cfg2
#ENDCONFIG

#ENDIF

;----[Verify Configs have been specified for Selected Processor]----------------
; Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
#ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF
INCLUDE "modedefs.bas"

TrisA = %00011000 'Port A3,A4 inputs rest outputs (A3 cannot output)
TrisB = %00000110 'Port B all outputs except B1 and B2 input
TrisC = %00000000 'Port C all outputs
TrisD = %00000000 'Port D all outputs
ansel = 0 'and turn off analog
AnselH = 0 'turn of rest of analogs
CM1CON0 = 0 'turn off comparators
CM2CON0 = 0 'turn off comparators
SSPCON.bit5 = 0 ' disable serial port, pins are I/O
OPTION_REG = %10000000 '1 turn off weak pull ups
INTCON = %00000000
CCP1CON = %00001100 ' Mode select = PWM
CCP2CON = %00001100 ' Mode select = PWM

'LED mapping

DEFINE CCP1_REG PORTC 'Channel-1 port
DEFINE CCP1_BIT 2 'Channel-1 bit
DEFINE CCP2_REG PORTC 'Channel-2 port
DEFINE CCP2_BIT 1 'Channel-2 bit
AA var word
duty var byte
freq var word
aa = 0

MainLoop:

high PortD.2
pause 500
low portd.2
pause 500

for aa = 1 to 255
duty=aa '50% duty cycle
freq=4000 'test frequency
hpwm 1,duty,freq 'Start PWM
HPWM 2,duty,1000 ' Send duty cycle PWM signal at 1kHz

pause 100
next


goto mainloop

end

mark_s
- 28th August 2015, 14:14
freq=4000 'test frequency
hpwm 1,duty,freq 'Start PWM
HPWM 2,duty,1000 ' Send duty cycle PWM signal at 1kHz


I know one thing that's not right. You can not have two different frequency on each HPWM channel. They must be set to the same frequency. Duty cycle can be changed independently. You are rapidly setting HPWM1 to freq and then HPWM2 to 1000hz it never gets a chance to start.