I trying to control PWM output on a PIC12F683 and I wrote the following code for a quick test:
Code:
@ device pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off
OSCCON=%01110111
DEFINE OSC 8
ADCON0=%00001101
ANSEL = %00111000
TRISIO=%00011000
CMCON0=%00000111
i var byte
pwm_out var GPIO.2
CCP1CON = %00001100
PR2 = 15
CCPR1L = 0
CCP1CON.5 = 0
CCP1CON.4 =0
Pause 200
T2CON.2 = 1
Goto main
Main:
for i=8 to 14
CCPR1L = i
CCP1CON.5=0
CCP1CON.4=0
pause 250
CCPR1L = i
CCP1CON.5=0
CCP1CON.4=1
pause 250
CCPR1L = i
CCP1CON.5=1
CCP1CON.4=0
pause 250
CCPR1L = i
CCP1CON.5=1
CCP1CON.4=1
pause 250
next
for i=14 to 8 step -1
CCPR1L = i
CCP1CON.5=1
CCP1CON.4=1
pause 250
CCPR1L = i
CCP1CON.5=1
CCP1CON.4=0
pause 250
CCPR1L = i
CCP1CON.5=0
CCP1CON.4=1
pause 250
CCPR1L = i
CCP1CON.5=0
CCP1CON.4=0
pause 250
next
CCPR1L=0
Pause 5000
Goto Main
What I’m trying to do is use the finest possible steps in controlling the PWM.
Please ignore the low case spelling on some lines of the code. I did not realized I have the auto capitalize feature enabled and I became lazy. The code works since the Micro Code Studio fixes them for me. I know is a bad and risky habit and I will try to fix it.
The code rumps up and down from 5.5V output to 9.5V in 28 steps and after 5 seconds stop repeats the cycle which should take 14 seconds without the pause. It is just to easily explore all possibilities.
The question is: Can I somehow combine the CCPR1L and CCP1CON<5:4> in one single variable so it is easier to handle in the FOR / NEXT loop and make the code more elegant?
Any input will be greatly appreciated.
Nick
Bookmarks