for what its worth I tried your code cleaned up a bit and slightly changed to suit a 16f1825
and it works perfectly with nice steady results , could you have a noisy pot or power supply ?
Code:
'****************************************************************
'* Name : Nacelle_Steady-On_Lights_12F1840_16Mhz_Int.pbp *
'* Author : Ross A. Waddell *
'* Notice : Copyright (c) 2016 *
'* : All Rights Reserved *
'* Date : 02/02/2016 *
'* Version : 1.0 *
'* Notes : Steady-on nacelle engine lights (TOS Enterprise) *
'* : *
'****************************************************************
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _CP_OFF & _WDTE_ON & _PWRTE_ON & _MCLRE_ON & _CLKOUTEN_OFF
__config _CONFIG2, _PLLEN_ON & _LVP_OFF
#ENDCONFIG
OSCCON=$70
DEFINE OSC 32
ANSELA = %00001000 ; Analog on PORTA.4(AN3) only
ADCON1 = %10100000 ; Right-justified results in 10-bits; Fosc/32 as timer;
; VREF is connected to VDD
TRISA = %111110 ' Make all pins input except for RA0 (ser out)
trisc= %11011111 ' ccp1
ADCInVal VAR WORD ; stores ADCIN result read from trim pot
LEDBrVal VAR WORD
FadeInPause CON 25 ; Pause during LED fade in
i VAR WORD
' ***************************************************************
' Set up PWM on CCP1
' ***************************************************************
CCP1CON = %00001100 ; Use CCP1 in PWM mode
' Set duty cycle registers initially to 0
CCP1CON.4 = 0
CCP1CON.5 = 0
CCPR1L = 0
lata.0=1
pause 2000
' ************************
' CCP1 uses TMR2
' ************************
T2CON = %00000110 ; Timer2 on with 1:16 prescaler
PR2 = 255 ; For 16Mhz OSC the desired output freq of 976.563Hz
; is achieved with this PR2 value (10-bit resolution
; with 1:16 prescaler)
MaxDuty VAR WORD ; According to Darrel:
; MaxDuty = (PR2 + 1) * 4
MaxDuty = (PR2 + 1) * 4 ; 1024
MinDuty CON 0 ; Minimum brightness for this application
MaxADCVal CON 1023 ; 255 for 8-bit; 1023 for 10-bit
compVal VAR WORD ; stores last-changed ADC value
CounterA var BYTE ' Just a BYTE Temporary working variable
DataW var WORD ' Just a WORD Temporary working variable
RawData var WORD [16] ' Array holding ADC Result
serout2 PORTA.0,84, ["ready",13,10 ]
GOSUB DO_ADCIN_Chk
LEDBrVal = ADCInVal
compVal = LEDBrVal
FOR i = 0 to LEDBrVal
CCP1CON.4 = i.0
CCP1CON.5 = i.1
CCPR1L = i >> 2
pause FadeInPause
NEXT i
Main:
GOSUB DO_ADCIN_Chk
PAUSE 100
IF ADCInVal <> compVal THEN
LEDBrVal = ADCInVal
compVal = LEDBrVal
gosub ChngLEDBrightness
ENDIF
GOTO Main
'*********** Read ADC or USART Rc inputs *******************************
Do_ADCIN_Chk:
' Stuff 16 Element WORD Array full of ADC values
' ----------------------------------------------
For CounterA=0 to 15
ADCON0 = %00001101 ' Select Channe3, Turn-On A/D
Pauseus 50 ' Wait for channel to setup
ADCON0.1 = 1 ' Start conversion
While ADCON0.1=1:Wend ' Wait for conversion
DataW.HighByte=ADRESH ' Read variable from ADC and save
DataW.LowByte=ADRESL
RawData(CounterA)=DataW
Next CounterA
' Sort ADC Input
' --------------
CounterA=0
GetADCSortLoop:
If RawData(CounterA+1) < RawData(CounterA) then
DataW=RawData(CounterA)
RawData(CounterA)=RawData(CounterA+1)
RawData(CounterA+1)=DataW
If CounterA>0 then CounterA=CounterA-2
endif
CounterA=CounterA+1
If CounterA < 15 then goto GetADCSortLoop
' Quanticise, discarding top and bottom FOUR elements
' ----------------------------------------------------
DataW=0
For CounterA=4 to 11
DataW=DataW+RawData(CounterA)
Next CounterA
ADCInVal=DataW>>3 ' Divide Result by EIGHT
serout2 PORTA.0,84, ["adc ",#ADCInVal,13,10 ]
return
'*********** Set duty registers ****************************************
ChngLEDBrightness:
CCP1CON.4 = LEDBrVal.0
CCP1CON.5 = LEDBrVal.1
CCPR1L = LEDBrVal >> 2
RETURN
Bookmarks