PDA

View Full Version : PWM using the 12F683



pescador
- 25th April 2016, 02:57
I've read in these forums someone stating that any IO pin can be used for PWM on the 12F683, but in the 683 docs it states only the CCP1 pin can be used for PWM - pin 5. True?

I've already laid out a circuit board for this first try, but of course it can be modified on next rev. I start programming tomorrow with it - any feedback would be appreciated as I do this and post my code.
From what I've read I'll need to setup CCP1 register before I can use the PWM function, and change OSC to 8Mhz..(option?)..

Basically I'm using an input pin (pulled up/ to ground) to "step" the brightness from dim to bright, reach a top brightness, and recycle to dimmest again. Going from 0 to 255 and a repetition rate of I dont know yet (10?).

Continuous loop - the chip will do nothing else..

richard
- 25th April 2016, 03:49
hpwm and pwm are different commands pwm is not continuous and can be on any output pin and is a blocking cmd.
hpwm can only be on a ccpx pin is continuous and is non blocking [ccp1 is gp2 /pin5 on a 12f683]
both work as per the book if your define OSC is correct, hpwm is limited to 0 - 33khz unless the ccpx regs are set manualy

pescador
- 25th April 2016, 04:23
So is this command needed If I'm using any output pin? Does setting the OSC mode to 8Mhz do anything for the PWM as far as resolution or is 4Mhz good enough? (I'm dimming LEDs).

CCP1CON = %00001100 ' CCP1, PWM mode

and

OSCCON = %01110001 ' Internal 8MHz osc.
DEFINE OSC 8

richard
- 25th April 2016, 05:55
easy way


;pic12f683
#CONFIG
cfg = _INTOSCIO
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_ON
cfg&= _CP_OFF
cfg&= _CPD_OFF
cfg&= _BOD_ON
cfg&= _IESO_ON
cfg&= _FCMEN_ON
__CONFIG cfg
#ENDCONFIG


DEFINE OSC 8
OSCCON=$70
CMCON0 = 7
TRISIO = %111011 ;gp2= output

ANSEL =0
; hpwm Channel, Dutycycle, Frequency

hpwm 1,128,600 ; 50% 600 hz

mainloop:

; do stuff

goto mainloop




to do it manually



TRISIO = %111011 ;gp2= output
t2con=5 ; set up timer/pr2 for frequency and pwm bits ; note this is not equivalent to above resulting freq is ? [you work it out]
PR2 = 255;
CCPR1L = pulswidth ; set pulsewidth high bits
ccp1con=12 ; set ccp reg and or pulsewidth low bits

richard
- 25th April 2016, 06:02
Does setting the OSC mode to 8Mhz do anything for the PWM as far as resolution or is 4Mhz good enough? (I'm dimming LEDs).


more freq == more options == finer control == uses more power

whatever floats your boat

pescador
- 25th April 2016, 14:56
here's my first attempt - haven't tried it yet - I will try PWM also as an experiment


'12f638

' ======= config SETUP =================================


#CONFIG
cfg = _INTOSCIO
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_OFF
cfg&= _CP_OFF
cfg&= _CPD_OFF
cfg&= _BOD_ON
cfg&= _IESO_ON
cfg&= _FCMEN_ON
__CONFIG cfg
#ENDCONFIG

' ======= Common Settings =================================

OSCCON = %01110001 ' Internal 8MHz osc.
DEFINE OSC 8
CCP1CON = %00001100 ' CCP1, PWM mode - could be %00001110 for negative
CMCON0 = 7
TRISIO = %101011 ' GP2 and GP4 are outputs
ANSEL = 0

HLEDOUT VAR GPIO.2 ' HPWM output
PLEDOUT var GPIO.4 ' PWM output
BUTTEN var GPIO.3 ' BUTTON IN

HERTZ VAR BYTE
DUTY VAR BYTE

DUTY = 1
HERTZ = 600


; hpwm Channel, Dutycycle, Frequency

' hpwm 1,128,600 ; 50% 600 hz

MAIN:

HPWM 1, DUTY, HERTZ ' continuous output from GP2

IF BUTTEN = 0 THEN ' button pushed
pause 50 ' debounce
DUTY = DUTY +1 ' incrememnt duty cycle
ENDIF
IF DUTY > 254 THEN ' MAX number
DUTY = 1 ' reset duty
ENDIF

GOTO MAIN

end

pescador
- 26th April 2016, 01:13
PWM code and board works... but I had to throw the pause 50 (debounce) out because that caused a blink. HPWM might work better.