16F1828 HPWM and ADC


Closed Thread
Results 1 to 20 of 20
  1. #1
    Join Date
    Jan 2014
    Posts
    84

    Default 16F1828 HPWM and ADC

    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 !


    Code:
    #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

  2. #2
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    ..or a simple way to see where is the problem (PORTC.2 is not set ADC) !

    Datasheet 16F1828
    http://ww1.microchip.com/downloads/e.../40001419F.pdf

    Code:
    #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
    '=============================================================

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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 ?

  4. #4
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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.

  5. #5
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    This code work perfectly :

    Code:
    main:
    adcin 6 , adval 
    pause 10
    
    duty1 = adval
    
    hpwm 2 , duty1, 2000 
    
    high inh1
    high inh2
    
    goto main
    end

  6. #6
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    If I add HPWM1 line, I have not correct ADC input

    Here AN6 pin is not ADC input:
    Code:
    main:
    adcin 6 , adval 
    pause 10
    
    duty1 = adval
    
    hpwm 2 , duty1, 2000 
    hpwm 1 , 128, 2000
    
    high inh1
    high inh2
    
    goto main
    end

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    try setting the pwm manually (the frequency will be way off ) but its worth a try


    Code:
     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

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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

  10. #10
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    Thank you very much for your help ,Richard !
    I made the changes, but now CCPR1L = 0

    Code:
    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
    '=============================================================

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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
    Last edited by richard; - 17th November 2015 at 10:13. Reason: wrong info

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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
    Last edited by richard; - 17th November 2015 at 09:52. Reason: wrong line

  13. #13
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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)

    Code:
    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
    '=============================================================

  14. #14
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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






  15. #15
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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!!

  16. #16
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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 :

    Code:
    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:
    Code:
    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):
    Code:
    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 ??):
    Code:
    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.

    Code:
    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.

  17. #17
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC


  18. #18
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    Hi, Midali

    I do not see much config lines for your CCP Modules ...

    Code:
    '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
    Last edited by Acetronics2; - 21st November 2015 at 09:20.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  19. #19
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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...

  20. #20
    Join Date
    Jan 2014
    Posts
    84


    Did you find this post helpful? Yes | No

    Default Re: 16F1828 HPWM and ADC

    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!

Similar Threads

  1. ADC values above 4095 for 12 bit ADC
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st February 2015, 22:24
  2. ADC problem on 18F46K22 device using all 28 ADC pins
    By JimAvanti in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd February 2012, 20:22
  3. Replies: 3
    Last Post: - 23rd October 2011, 12:53
  4. Newbie HPWM / ADC question
    By Johan in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 25th June 2007, 12:52
  5. HPWM of the port of two HPWM
    By ja2rqk in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th April 2007, 15:05

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts