PDA

View Full Version : 12F1822 HPWM Duty Not Changing



retepsnikrep
- 11th April 2013, 09:11
Can someone cast an eye over this code please.

8mhz pic. Trying to generate 200hz HPWM.

Duty seems stuck at about 60% whatever I try even when it's supposed to be off and idling high with

Duty set at 255. Scope shows pwm is working at 200hz but I can't vary the duty.

Does the Duty variable have to be a WORD?

Confused.




#config

__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOREN_OFF & _CLKOUTEN_OFF &

_FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF

#ENDCONFIG

DEFINE OSC 8 'Set PicBasic Pro processor speed to 8 Mhz (Must match oscillator

value)
OSCCON = %01110010 'Set osc 8mhz and stable

TRISA = %00101001 'PortA Output on Pin A1,A2,A4 & Input on A0,A3,A5
ANSELA = %00000000 'All Digital I/O
APFCON = %10000100 'Set Tx/Rx function to RA4 & RA5
ADCON0 = %00000000 'ADC Disabled

OPTION_REG = %01111111 'Global Enable Weak Pull Ups
WPUA = %00011011 'Weak Pull Up enabled on Ports A0 A1 A3 A4

DEFINE HSER_BAUD 10400 'Set Baud rate to 10400bps
DEFINE HSER_BITS 9 'Set to 9 bit mode
DEFINE HSER_EVEN 1 'Set Even Parity
DEFINE HSER_CLROERR 1 'Clear overflow error automatically

DEFINE CCP1_REG PORTA 'HPWM channel 1 pin Port A Fan PWM Driver
DEFINE CCP1_BIT 2 'HPWM channel 1 pin Bit 2

'************************************************* ******************************

'Variables Constants and I/O definitions

'------------------------ Variables 16bit --------------------------------------



FanSpeed var byte 'Ipu Fan Speed Pwm Setting


Start:


FanSpeed = 255

HPWM 1, FanSpeed, 200 'Change/Start 200hz HPWM 'Set Duty 0 = Off 255 = FullOn

end

Charlie
- 11th April 2013, 10:47
I haven't worked through your code in detail, but the HWPWM is 10 bits wide so 50% dutycycle would be 512. I also don't see a lot of configuration code, but I'm not familiar with PBP3. PBP 2.6 doesn't have a HWPWM command.

Acetronics2
- 11th April 2013, 14:08
Did you verify the minimum available PWM freq in your manual ???

just an idea ...

Alain

retepsnikrep
- 11th April 2013, 16:07
I agree I thought lowest frequency would have to be higher, but when set at 200hz with 8mhz pic scope shows output at 200hz but changing the duty variable does not change the duty :?

wdmagic
- 11th April 2013, 16:33
charlie, i'm using 2.6 and I can use HPWM, I dont see why you have to define ccp1, heres an example code for a 683 chip, reading a ADC and outputting pwm based on reading.


DEFINE ADC_BITS 10 ' A/D number of bits
DEFINE ADC_CLOCK 1 ' Use A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us

RES1 Var Word ' A/D converter result

TRISIO.0 = 1 ' AN0 is input
TRISIO.2 = 0 ' PWM1 is output

AGAIN:
ADCIN 0, Res1 ' Read Channel 0 data
res1 = res1 / 256 ' 0-255

if res1 < 10 then res1 = 0 ' Low Auto Shutoff

HPWM 1, res1, 18000 ' PWM Pin, Setting, Frequency (20k Max)
pause 10
goto again

end

So with no defines it works fine, when HPWM command is used it will turn on the correct port and adjust the settings. If you dont use the hpwm it fuctions as a normal i/o port.

you shouldnt have to define CCP1, its hardcoded into that chip to begin with, CCP1 will always be on PORTA.2 to begin with, unless you force a change if allowed to move to a different pin (which yours does, but only if you tell it too) NOTE that I set my PWM output pin to a 0 as output, you dont have to do this either I dont believe, unless you want to use it as a I/O before engaging the PWM. It works so I havent changed it. Unless you decide to change output pins, or enable the other 4 hpwm modules on this chip, you shouldnt have to use anything but base code to set pwm. Oh and about the duty cycle, I believe if you dont set it to 10 bit it defaults to 8bit, the documentation says it can handle up to 10 bit and theses a section on how to adjust registers to allow for it. but if you dont change them... they should work as default from 0-255.

Charlie
- 11th April 2013, 18:08
You were right - for some reason I typed HWPWN instead on HPWM, so I couldn't find it in the 2.6 manual. I have always configured by hand, and have never used this command. I also always use 10 bits.
I still recommend checking that all the configuration is correct. I have also seen issues with low frequencies on this device. Why not set it to 2K to test that's not the issue here, just to eliminate that possibility?