try this
Code:
<font color="#000000"> <font color="#008000">'
' Pic Configuration
' =================
</font>@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BOD_ON
<font color="#008000">'
' Hardware configuration
' ======================
'
' I/Os
' ----
</font>TRISIO = %00010010
<font color="#008000">'
' ADC's
' -----
</font>ANSEL = %00000010
<font color="#000080">DEFINE </font>ADC_BITS 10 <font color="#008000">' ADCIN resolution (Bits)
</font><font color="#000080">DEFINE </font>ADC_CLOCK 1 <font color="#008000">' ADC clock source (Fosc/8)
</font><font color="#000080">DEFINE </font>ADC_SAMPLEUS 11 <font color="#008000">' ADC sampling time (uSec)
</font>ADCON0.7 = 1 <font color="#008000">' Right justified results
'
' Comparator
' ----------
</font>CMCON0 = 7 <font color="#008000">' Disable comparator
</font>VRCON = 0 <font color="#008000">' CVreff = OFF
'
' CCP
' ---
' PWM Freq=4KHz
' Initial duty cycle set to 50%
' PICMultiCalc says
' 10 Bit resolution when prescaler = 1:1
' MAX duty value 1000
' PR2 = 249
</font>CCP1CON = %00001100 <font color="#008000">' PWM mode
</font>PR2 = 249 <font color="#008000">'
</font>T2CON = %00000100 <font color="#008000">' Start Timer2, Prescaller 1:1
'
' Software variables
' ==================
</font>Duty <font color="#000080">VAR WORD
</font><font color="#008000">'
' Software constants
' ==================
</font>PWM_ON <font color="#000080">CON </font>%00001100
PWM_OFF <font color="#000080">CON </font>0
<font color="#008000">'
' Software/Hardware initialisation
' ================================
</font>GPIO = 0
<font color="#000080">PAUSE </font>50 <font color="#008000">' OSC settle delay
</font>Duty = 0
<font color="#000080">GOSUB </font>SetPWMDuty
<font color="#008000">'------------------------------< Main program >-----------------------------------
'
</font>Start:
<font color="#000080">ADCIN </font>1,Duty <font color="#008000">' read pot
</font><font color="#000080">IF </font>Duty>1000 <font color="#000080">THEN </font>Duty =1000 <font color="#008000">' avoid results >1000
</font><font color="#000080">GOSUB </font>SetPWMDuty <font color="#008000">' Set PWM duty cycles
</font><font color="#000080">PAUSE </font>1 <font color="#008000">' allow few PWM cycles
</font><font color="#000080">GOTO </font>Start
<font color="#008000">'
'---------------------------------------------------------------------------------
'-------------------------------< Subroutines >-----------------------------------
'
</font>SetPWMDuty:
<font color="#008000">' Datasheet recommend to stop PWM, load duty cycles vale
' to avoid possible glitches.
'
'
' stop PWM
' --------
</font>CCP1CON = PWM_OFF
<font color="#008000">'
' load Duty values
' ----------------
</font>CCPR1L = Duty >>2
CCP1CON.5 = Duty.1
CCP1CON.4 = Duty.0
<font color="#008000">'
' Start PWM
' ---------
</font>CCP1CON = CCP1CON | PWM_ON
<font color="#000080">RETURN
</font><font color="#008000">'
'---------------------------------------------------------------------------------
</font>
things to remember, disable the PWM mode, update the duty cycle, start PWM mode and allow few PWM freq cycle.. unless results could be weird.
Last edited by mister_e; - 6th May 2007 at 02:14.
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Bookmarks