View Full Version : 16F1828 HPWM and ADC
midali
- 16th November 2015, 20:11
Hi guys,
I have a strange problem with 16F1828 .
So, I want to use AN6 (PORTC.2) to read a variable from 5k potentiometer . In main program if I use only HPWM2 , all works ok.
If I add HPWM1 , on AN6 pin I have not a proportional voltage, like this pin is not analog input.
Can somebody help me, please ? The last 2 days I spent my time to solve this problem, but no succes !
#CONFIG
__CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
__CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
#ENDCONFIG
DEfine OSC 4
OSCCON = %01101010 'sets the internal oscillator to 4Mhz
ADCON0 = %00000000
ADCON1 = %00000000
FVRCON = %11000010 'voltage reference set to 2.048V
CM1CON0 = 0 ' COMPARATORS OFF
CM1CON1 = 0
CM2CON0 = 0 ' COMPARATORS OFF
CM2CON1 = 0
CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
RCSTA.7 = 0 'SERIAL PORT OFF
'APFCON0 = %10000000
'APFCON1 = %00001010
'APFCON1.3 = 1
TRISA = %00000011
TRISB = %00000000
TRISC = %00000100 ' PORTC.2 input
ANSELA = %00000011
ANSELB = %00000000
ANSELC = %00000100 ' ANALOG INPUT AN6
OPTION_REG.7 = 1 ' disable WPU
WPUA = %00000000
WPUB = %00000000
WPUC = %00000000
porta = 0
portb = 0
portc = 0
inh1 var portc.4
inh2 var portc.0
adval var byte
duty1 var byte
duty2 var byte
duty1 = 0
pause 200
'========================PROGRAM================== ===========
main:
adcin 6 , adval
pause 10
duty2 = adval
hpwm 2 , duty2, 2000
hpwm 1 , duty1, 2000 '- here is problem
high inh1
high inh2
goto main
end
'================================================= ============
1000 x thx
midali
- 17th November 2015, 07:41
..or a simple way to see where is the problem (PORTC.2 is not set ADC) !
Datasheet 16F1828
http://ww1.microchip.com/downloads/en/DeviceDoc/40001419F.pdf
#CONFIG
__CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
__CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
#ENDCONFIG
DEfine OSC 4
OSCCON = %01101010 'sets the internal oscillator to 4Mhz
ADCON0 = %00000000
ADCON1 = %00000000
FVRCON = %11000010 'voltage reference set to 2.048V
CM1CON0 = 0 ' COMPARATORS OFF
CM1CON1 = 0
CM2CON0 = 0 ' COMPARATORS OFF
CM2CON1 = 0
CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
RCSTA.7 = 0 'SERIAL PORT OFF
'APFCON0 = %10000000
'APFCON1 = %00001010
'APFCON1.3 = 1
TRISA = %00000011
TRISB = %00000000
TRISC = %00000100 ' PORTC.2 input
ANSELA = %00000011
ANSELB = %00000000
ANSELC = %00000100 ' ANALOG INPUT AN6
OPTION_REG.7 = 1 ' disable WPU
WPUA = %00000000
WPUB = %00000000
WPUC = %00000000
porta = 0
portb = 0
portc = 0
inh1 var portc.4
inh2 var portc.0
adval var byte
duty1 var byte
duty2 var byte
duty1 = 0
pause 200
'========================PROGRAM================== ===========
main:
adcin 6 , adval ' AN6(PORTC.2) is not ADC ?
pause 10
duty1 = adval
hpwm 1 , duty1, 2000
high inh1
high inh2
goto main
end
'================================================= ============
richard
- 17th November 2015, 08:04
which thing doesn't work
no output on cp1 (portc.5) ?
or
adcin 6 yields an incorrect result ?
can I suggest try
duty1 = 128
hpwm 1 , duty1, 2000
is there an output ?
and
adcin 6, adval
debug #adval
is there a realistic result ?
midali
- 17th November 2015, 08:23
HPWM 1, 128, 1000 - I have a output
ADCIN 6 give me a incorrect result.
If I use ADCIN 6 with any HPWM (2,3,4) , I have correct ADC input, also correct PWM output.
midali
- 17th November 2015, 08:37
This code work perfectly :
main:
adcin 6 , adval
pause 10
duty1 = adval
hpwm 2 , duty1, 2000
high inh1
high inh2
goto main
end
midali
- 17th November 2015, 08:53
If I add HPWM1 line, I have not correct ADC input
Here AN6 pin is not ADC input:
main:
adcin 6 , adval
pause 10
duty1 = adval
hpwm 2 , duty1, 2000
hpwm 1 , 128, 2000
high inh1
high inh2
goto main
end
richard
- 17th November 2015, 08:54
try setting the pwm manually (the frequency will be way off ) but its worth a try
t2con=4
PR2 = 255
CCPR1L = adval ;set pwm
ccp1con=12
I don't have a 16f1828 to test with , I do not think your code is incorrect but at this stage that's all I can offer
richard
- 17th November 2015, 09:01
just noticed you have no adc defines
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
could help, they should be included anyway
richard
- 17th November 2015, 09:18
ADCON1 = %00000000
FVRCON = %11000010 'voltage reference set to 2.048V
you are setting the frv (ps bit 6 of fvrcon is read only) but never connecting it to the adc module .
to use the fvr as ref you need to set adcon1 bits 0 & 1 to 1
you have also selected a conversion clock of fosc/2 which @4mhz is out of specs
you need fosc/4 or rc clock for adc
midali
- 17th November 2015, 09:40
Thank you very much for your help ,Richard !
I made the changes, but now CCPR1L = 0
Define OSC 4
OSCCON = %01101010 'sets the internal oscillator to 4Mhz
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
ADCON0 = %00000000
ADCON1 = %00000011
FVRCON = %10000010 'voltage reference set to 2.048V
CM1CON0 = 0 ' COMPARATORS OFF
CM1CON1 = 0
CM2CON0 = 0 ' COMPARATORS OFF
CM2CON1 = 0
CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
RCSTA.7 = 0 'SERIAL PORT OFF
'APFCON0 = %10000000
'APFCON1 = %00001010
'APFCON1.3 = 1
TRISA = %00000011
TRISB = %00000000
TRISC = %00000100 ' PORTC.2 input
ANSELA = %00000011
ANSELB = %00000000
ANSELC = %00000100 ' ANALOG INPUT AN6
OPTION_REG.7 = 1 ' disable WPU
WPUA = %00000000
WPUB = %00000000
WPUC = %00000000
porta = 0
portb = 0
portc = 0
inh1 var portc.4
inh2 var portc.0
adval var byte
duty1 var byte
duty2 var byte
duty1 = 0
t2con=4
PR2 = 255
ccp1con=16
pause 200
'========================PROGRAM================== ===========
main:
adcin 6 , adval
pause 10
CCPR1L = adval ;set pwm
duty1 = adval
hpwm 2 , duty1, 2000
high inh1
high inh2
goto main
end
'================================================= ============
richard
- 17th November 2015, 09:47
ccp1con=16 is incorrect, that is just nothing
ccp1con=12 ; pwm mode
I made the changes, but now CCPR1L = 0 ?
CCPR1L = adval ;set pwm
if adval= 0 then yes it would
richard
- 17th November 2015, 09:51
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
ADCON0 = %00000000
ADCON1 = %00000011
ADCON1 = %00000011 ; this line just destroyed all your defines
ADCON1 = ADCON1 | %00000011 ; will set bits 0 and 1 without wrecking the others
midali
- 17th November 2015, 10:32
Thx Richard, now I understand.
Now, AN6 work corectly. The value of CCPR1L is 2 x adval. If I divide adval to 2 , exist a little diference betwen PWM1 and PWM2 on outputs (~ 0,8 units)
t2con=4
PR2 = 255
ccp1con=12
pause 200
'========================PROGRAM================== ===========
main:
adcin 6 , adval
pause 10
CCPR1L = adval/2 ;set pwm
duty1 = adval
hpwm 2 , duty1, 2000
high inh1
high inh2
goto main
end
'================================================= ============
richard
- 17th November 2015, 20:52
some more things to consider
1.
Many devices allow a selection of Timer resource for some or all of the PWM
channels. The HPWM command will automatically determine the preset Timer
selection upon execution. Note that PWM channels that share a single timer may
not operate at independent output frequencies. While duty-cycle settings are
always independent from one channel to the next, independent frequency settings
require independent timer resources.
you have not set any individual timer resource options [ CCPTMRS0 ]
so
t2con=4
PR2 = 255
settings get overwritten by use of
hpwm 2 , duty1, 2000
2.
you have not specified how your "pot" is connected , if its connected between vdd an ground then use of a fvr of 2.048 v is totally inappropriate and
may even damage your chip. analogue in voltage should never exceed vref
3.
all your trouble was probably caused by incorrect adc settings , hpwm1 should now work
midali
- 18th November 2015, 08:41
My potentiometer is connect between +5v and gnd.
I can not understood why I had some troubles because :
1. I used AN0 or AN1 and HPWM(1-4) worked perfectly
2. I used AN6 , so HPWM2 worked perfectly(AN6 analog input ok) ! , but when I added HPWM1 , the HPWM2 not worked because I had not a analog input. I suposed that exist a conflict between AN6 and HPWM1.
Now, HPWM1 work . AN6 work but here is some trouble, I have analog input but after some time toggle itself on digital input !
Now I read carefully in datasheet and I'll post the code.
Its clear that I'm begginer in pbp, all my projects are for hobby. Richard, you explained me step by step and I start to understand some "secrets" about settings. Thank you very much for patience!!
midali
- 18th November 2015, 20:41
I'm very confused.
I think that settings are ok, but some its very strange. I changed the PIC with another one, but the same results.
PIC config & settings :
OSCCON = %01101010 'sets the internal oscillator to 4Mhz
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
ADCON0 = %00000000
ADCON1 = %00000000
FVRCON = %00000000
CM1CON0 = 0 ' COMPARATORS OFF
CM1CON1 = 0
CM2CON0 = 0 ' COMPARATORS OFF
CM2CON1 = 0
CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
RCSTA.7 = 0 'SERIAL PORT OFF
TRISA = %00000011 'PORTA.0 and PORTA.1 set Input
TRISB = %00000000
TRISC = %10000100 ' PORTC.7 and PORTC.2 input
ANSELA = %00000011 ' AN0 and AN1 set analog
ANSELB = %00000000
ANSELC = %00000100 ' AN6 set analog
OPTION_REG.7 = %00000001 'WPU enable ; Fosc/4; Prescaler 1/4
WPUA = %00000000
WPUB = %00000000
WPUC = %10000000 ' enable WPUC.7
porta = 0
portb = 0
portc = 0
inh1 var portc.4
inh2 var portc.0
adval var byte
duty1 var byte
Here work all 4 HPWM:
main:
adcin 0 , adval '(or adcin 1 , adval)
pause 10
duty1 = adval
hpwm 1 , duty1, 2000
hpwm 2 , duty1, 2000
hpwm 3 , duty1, 2000
hpwm 4 , duty1, 2000
goto main
end
Here works fine HPWM(2,3,4):
main:
adcin 6 , adval
pause 10
duty1 = adval
hpwm 2 , duty1, 2000
hpwm 3 , duty1, 2000
hpwm 4 , duty1, 2000
goto main
end
Here AN6 toggled to digital input (why ??):
main:
adcin 6 , adval
pause 10
duty1 = adval
hpwm 1 , duty1, 2000
hpwm 2 , duty1, 2000
hpwm 3 , duty1, 2000
hpwm 4 , duty1, 2000
goto main
end
I tried to set manually all 4 CCP , frequency doesn't matter (2-4khz is enough for me) , maxim duty cycle measured with a osciloscope is 99,7 % . What is very strange , after some time, the AN6 toggle from analog to digital input. I hope that manual settings are ok.
T2CON = %00000100 ' Timer2 ON
PR2 = 255
CCP1CON = %00001100
CCP2CON = %00001100
CCP3CON = %00001100
CCP4CON = %00001100
pause 200
'========================PROGRAM================== ===========
main:
adcin 6 , adval
pause 10
CCPR1L = adval
CCPR2L = adval
CCPR3L = adval
CCPR4L = adval
goto main
end
So, If I add HPWM1 in main program , AN6 toggle instantly from analog input to digital input. If I set manual CCP , AN6 toggle from analog to digital after some time.
I tried with 16F1938 , the same program, with different analog inputs and all work ok.
richard
- 18th November 2015, 21:06
this might be the issue
http://ww1.microchip.com/downloads/en/DeviceDoc/80510C.pdf
Acetronics2
- 21st November 2015, 09:10
Hi, Midali
I do not see much config lines for your CCP Modules ...
'APFCON0 = %10000000
'APFCON1 = %00001010
'APFCON1.3 = 1
being commented out ... ( I did not check if correct )
as AN6 is shared by the CCP outputs and your ADC input ...
unfortunately, I do not have those 16F18xx chips here to verify.
did you try with another ADC input ( not CCPx related ), AN10 or 11 i.e. ???
Alain
midali
- 21st November 2015, 11:41
First time when I used HPWM ,I configured the APFCON to "APFCON1.3 = 1" but no succes.
Now I set manually the CCP and APFCON1.3 = 1, all its ok! AN6 work perfectly ! I want to play with it more time because I want to be sure that all its ok.
Also, I tried again with HPWM and some APFCON configs, but the same problems : AN6 is digital input or AN6 toggle from analog to digital input.
I'll try to test with AN10 or An11 , but will be a little hard because my pcb is make ready .
Thank you very much Alain...
midali
- 21st November 2015, 14:50
Conclusions :
1. I tested with AN10 and ADC work perfectly with HPWM. Also, works well with AN0 and AN1.(another ADC I not tested)
2. ADC on AN6 pin work perfectly if I config APFCON1.3 = 1 and manually set for CCP.
3. I think that all troubles was from PIC revision...may be I'm wrong
4. I'll modify my PCB and I'll use AN10 ( this pin is near AN6)
5. In my opinion , is the best if avoid to use PORTC.2 (AN6) .
6. I prefer to work with HPWM because I can set very easy the freq and duty cycle .
Thank you again to Richard and Alain for your help!
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.