Hey guys, will this work?


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Hey guys, will this work?

    Creating a buck converter and need to monitor a current sence voltage and change a pwm duty cycle based on the current sence ADC result. The target output current is user adjustable by a pot and a max current is set by a pot that is internal to the product and set at the factory. These pots are in series so only 1 input is needed. This is protection for a short at the output or the wrong load.

    So there are 2 inputs to the pic, the feedback from the current sence resistor and the voltage accross a potential divider for current set. I can set the hardware so that the current sence count and the current limit count are the same value when a given current target is met.

    Also, looking at the pbp pro manual I see a max HPWM of 32.767kHz yet the datasheet lists over 200kHz. Will the HPWM command actually allow a frequency of 200kHz? This is on a 18F1320.

    Here is what I think will work:

    Code:
    OSCCON = $70
    define osc 8
    Define  ADC_BITS        10     	' Set number of bits in result
    Define  ADC_CLOCK       3     	' Set clock source (3=rc)
    Define  ADC_SAMPLEUS    10    	' Set sampling time in uS
    
    CSET   VAR WORD 'CURRENT SET
    CSEN   VAR WORD 'CURRENT SENCE  
    DUTY   VAR wORD 'HPWM DUTY CYCLE
    
    ADCON0 = %00011001
    ADCON1 = %00001111
    ADCON2 = %10111111
    TRISA = %00000000
    TRISB = %00110011
    
    DUTY = 800 'SET INITIAL OUTPUT CURRENT HIGH TO ENABLE FAST STARTUP 
    CSET = 0
    CSEN = 0
    
    LOOP:
    ADCIN 6, CSET 'READ VALUE OF CURRENT SET POT
    ADCIN 4, CSEN   'READ VALUE OF CURRENT SENCE
    IF CSEN > CSET Then LET DUTY = DUTY - 1
    IF CSEN < CSET THEN LET DUTY = DUTY + 1
    HPwm 1,DUTY,32767 'OUTPUTS THE DUTYCYCLE AT 32.767KhZ
    GOTO LOOP

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    HPWM limit is as stated in your manual. To reach higher frequency, you need to write the PWM register manually. Use PICMultiCalc to find PR2 and Timer Prescaller value, then plug the DutyCycle values in CCP1CON and CCPR1L registers... double check your datasheet to confirm.

    For Buck converter, usually the higher the better for reducing coil and capacitors size.

    Make sure your ADCONx setting don't interfer with PBP ADCINs defines. You may want to place your define after those ADCONx lines, or get rid of ADCIN. Once again PICMultiCalc may give you some hints about min acquisition time and faster clock conversion source.... which is probably a must for buck converter. Don't forget to check the Resolution for the frequency you choosed. @8Mhz & 200KHz, you should have something like 7 bit of resolution.. 40 dutycycle steps... not bad, not good, depending how fancy you want it.

    In some case it's not a so good idea to use a PIC for that, just for the reaction time and what your PIC will also need to do apart the voltage regulation.
    Last edited by mister_e; - 11th May 2009 at 20:32.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    Hmm, eventually I have to get this running over 100kHz, so I will need to learn how to manually write to the registers and preload timers etc. I have never done this.
    All the links I've come across for the multicalc are dead. I had it once but lost it at some point.
    There is not much else going on with the pic really. If a pin goes high, it turns on a FET and runs the converter. Pin the goes low, it switches off. Thats it really.

    I've swapped the location of the DEFINE's.
    Other than that, at 32.767kHz, will that code do what I think it should do?

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    In theory it should work. Theory is always theory

    PicMultiCalc download
    http://www.picbasic.co.uk/forum/atta...7&d=1225550328

    Probably you can get some inspiration of the following
    http://www.picbasic.co.uk/forum/show...0&postcount=10
    Last edited by mister_e; - 11th May 2009 at 20:42.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    Ok, so using the calc I get PR2 = 9, duty register = 20 and with a 1:1 prescaler.

    Is this about right?:

    Code:
    OSCCON = $70
    define osc 8
    
    CSET   VAR WORD 'CURRENT SET
    CSEN   VAR WORD 'CURRENT SENCE  
    DUTY   VAR wORD 'HPWM DUTY CYCLE
    
    ADCON0 = %00011001
    ADCON1 = %00001111
    ADCON2 = %10111111
    TRISA = %00000000
    TRISB = %00110011
    Define  ADC_BITS        10     	' Set number of bits in result
    Define  ADC_CLOCK       3     	' Set clock source (3=rc)
    Define  ADC_SAMPLEUS    10    	' Set sampling time in uS
    
    
    'DUTY = 30 'SET INITIAL OUTPUT CURRENT HIGH TO ENABLE FAST STARTUP 
    CSET = 0
    CSEN = 0
    
    duty = 20                           ' duty value for 50% duty cycle
    PR2 = 9                             '
    T2CON = %00000100                   ' timer2 on, prescale 1:1
    CCPR1L = duty>>2                    ' MSB of duty cycle value
    CCP1CON=%00001100 | (dUTY<<5)       ' set PWM mode and store the 
                                        '   2 LSB of duty
    
    
    
    
    LOOP:
    ADCIN 6, CSET 'READ VALUE OF CURRENT SET POT
    ADCIN 4, CSEN   'READ VALUE OF CURRENT SENCE
    IF CSEN > CSET THEN LET DUTY = DUTY - 1
    IF CSEN < CSET THEN LET DUTY = DUTY + 1
    CCPR1L = duty>>2                    ' MSB of duty cycle value
    CCP1CON=%00001100 | (dUTY<<5)       ' set PWM mode and store the 
                                        '   2 LSB of duty
    
    'HPWM 1,DUTY,32767 'OUTPUTS THE DUTYCYCLE AT 32.767KhZ
    GOTO LOOP

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Almost... but no cigar. Check CCP1CON for YOUR particular PIC. CCP1CON is a bit different on this one (and I think my example was false anyway ... dooh) as usual, MicroChip can't be consistent between all their PIC model. Good idea to release a datasheet for each and every different model though

    Code:
    CCP1CON=%00001100 | (Duty<<4)
    Last edited by mister_e; - 11th May 2009 at 21:38.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    Hehehe well it helps to actually turn on the PWM module!!
    Glitchy though. I've delayed the update by 1mS to smooth it out but still a big problem. Going to try disabling the PWM then updating the duty cycle and turning it back on.

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    To get Glitch-Free, you have to synchronize the updates with Timer2.

    The HPWM10 module does that with 10-bit resolution, and works just like the HPWM command.
    http://www.picbasic.co.uk/forum/show...7805#post37805

    But at your higher frequency, you'll still need to do it manually.
    It does show how to though.

    hth,
    DT

  9. #9
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel, I was just reading through that thread thinking it all looks familiar. I hadnt taken a look at your attachement yet.

    I'll have to digest in the morning. Is there a tutorial in your attachement, and will it work for 18F devices?

    I hate having one of those brains that learns by examples and questions rather than independant learning, feel so bad asking all the time. At least it usually goes in.

    I should drink more. Alcohol kills brain cells, therefore one could deduce that the slower brains cells are in contact with the alcohol longer, which would lead to them being killed off. This would leave you with only faster brain cells and thus more intelligence as the slower ones are no longer holding up your thinking. So on that thought, I'm going to the fridge to open a can of knowledge.

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    There's not really a tutorial.
    But there is a test program in the .zip that shows how to use it.

    The max frequency is still 32.767khz.
    And it will work with 12F's, 16F's or 18F's. As long as they have CCP modules.

    However, while you grab me one of those cans of knowledge ...

    The whole thing boils down to synchronizing the update with TMR2.
    And HPWM10 has extra code to handle different frequencies/prescaler/CCP modules, which you won't need with a constant frequency on a single CCP.

    So all you need to do with your existing code, is wait for the TMR2IF before changing the dutycycle ...
    Code:
    TMR2IF       VAR PIR1.1
    
    TMR2IF = 0
    while !TMR2IF : wend       ; let current cycle complete
    CCPR1L = duty>>2           ; MSB of duty cycle value
    CCP1CON.4=Duty.0           ; set PWM mode and store the 
    CCP1CON.5=Duty.1           ; 2 LSB of duty
    No digesting required.
    Do I get that Can of knowledge now?
    <br>
    DT

  11. #11
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    Sure do.

    I'll report in the morning, its about 2am and my 18 month old is coughing himself awake as I type. I've got a feeling its going to be a long night. Poor guy, always so sick. Yay for ear tubes on Thursday.

Similar Threads

  1. PortA Doesn't Work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 11
    Last Post: - 8th September 2015, 18:41
  2. pls help me verify my code not work, why???
    By chai98a in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th January 2010, 09:19
  3. Can't get POT work on P12f675 - Newbie
    By berslan in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 26th March 2008, 21:22
  4. How to set ICD-2 clone to work with PBP?
    By G-R-C in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th October 2006, 02:50
  5. Pin RA4 doesn't work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 15th July 2004, 12:03

Members who have read this thread : 0

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