MikroBasic to PBP Pro: Boost mode SMPS


Closed Thread
Results 1 to 29 of 29

Hybrid View

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

    Default MikroBasic to PBP Pro: Boost mode SMPS

    Hi group,

    I found an instructables article on creating a boost mode power supply based on a PIC. Its programmed in MikroBasic so I thought Id try and convert it to PBP PRO.

    There are a couple of questions I have and a few issues. I'd also like to add the ability to have pushbutton up and down dimming and soft start/soft stop too.

    Anyway, here is the original program:

    Code:
    dim temp as word
    dim final_period, final_duty, high_duty, low_duty, last_adjust as byte
    dim  supply_multiplier, period_multiplier as float
    dim v_feedback, v_supply, t_rise, SMPS_duty, t_period, SMPS_period as float
    dim EE_pointer, EE_data as byte
    
    
    'set values
    const v_ref as float=3.8               'float  5.1 in the example
    const supply_ratio as float=11.35      'float
    const osc_freq as float=8              'integer or float
    const L_Ipeak as float=67             'float
    const fb_value as word=443             'word   225 in the example
    
    
    sub procedure PRE_CALC
    'precalculations
      supply_multiplier=v_ref*supply_ratio
      period_multiplier=(4/osc_freq)
    end sub
    
    
    
    
    sub procedure UPDATE_SMPS
    'take Vss reading
          temp=0
          temp=ADC_read(ANS0) ' ADC conversion
    'convert to voltage
          v_feedback=temp                 'put ADC in float
          v_supply=v_feedback/1024        'find supply % of vref
          v_supply=v_supply*supply_multiplier  'find supply volts
    'calculate rise time
          t_rise=L_Ipeak/v_supply                'find rise time @ supply volts for L/Ipeak
    'calculate CCPR1L:CCP1CON<5:4>
          SMPS_duty=t_rise*osc_freq        'find duty cycle value
          final_duty=SMPS_duty             'convert to byte
    'calculate period
          t_period=(t_rise*1.33)           'dutycycle = .75period
    'calculate PR2
          SMPS_period=(t_period/period_multiplier)  'find period value
          final_period=SMPS_period-1.0              'put in byte
    
    
    'put in register masks
          high_duty=(final_duty>>2)        'high 6 bits in CCPR1L
          low_duty=(final_duty<<6)         'low two bits for CCP1CON
          low_duty=(low_duty>>2)           'shift back to CCP1CON<5:4>
          low_duty.3=1                     'PWM configuration bit
          low_duty.2=1                     'PWM configuration bit
    
    
    'LOG TO EEPROM
       'only write if we measure somthing,
       'prevents false writes durring programming b/c MCLR is disabled
       if temp > 0 then
          EE_data=word(temp>>8) 'high 8 bits first address
          Eeprom_Write(EE_pointer, EE_data)
          EE_pointer=EE_pointer+1
          EE_data=temp      'low 8 bit second address
          Eeprom_Write(EE_pointer, EE_data)
          EE_pointer=EE_pointer+1
          'put final_duty in eeprom
          Eeprom_Write(EE_pointer, final_duty)
          EE_pointer=EE_pointer+1
          'put final_period in eeprom
          Eeprom_Write(EE_pointer, final_period)
          EE_pointer=EE_pointer+1
          'reset write pointer if needed
          if EE_pointer > 197 then EE_pointer=1 end if
         'update the write pointer
          Eeprom_write(0, EE_pointer)
       end if
    
    
    end sub
    
    
    
    
    main:
    
    
    'set ADC
      GPIO = 0
      CMCON0 = 7
      TRISIO = 0              ' designate gpio as output
      TRISIO.ANS0 = 1         ' pin ANS0 as input  (Supply feedback)
      TRISIO.ANS1 = 1         ' pin ANS1 as input (HV-feedback)
      ANSEL.ANS1=1
      ANSEL.ANS0=1
      ADCON0.VCFG = 0         ' Vdd as Vref
    
    
    'Low Voltage Indicator Light
      GPIO.4=1 'indicator light on
    
    
    'delay and take Vss reading
      EE_pointer=Eeprom_read(0)
      delay_ms(1000)
      PRE_CALC
      UPDATE_SMPS
    
    
    'setup PWM
      PR2=final_period 'sets PWM frequency
      CCPR1L=0    '8 most sig. duty cycle bits =0
      CCP1CON.5=0 'least significant bits = 0
      CCP1CON.4=0 'least significant bits = 0
      CCP1CON=12  'turns on PWM (set CCP1CON=0 at any time to turn off PWM)
      T2CON=4     'turns on TIMER2 module (required for PWM timing)
      last_adjust=0
    
    
    while true
          temp=0
          temp=ADC_read(ANS1) ' ADC conversion
          if temp > fb_value then   'feed back OVER set voltage
             if last_adjust=1 then 'if 0 then already off
                CCPR1L=0
                CCP1CON.5=0
                CCP1CON.4=0
                GPIO.5=1
                GPIO.4=0
                last_adjust=0
             end if
          else
             if last_adjust=0 then
                CCPR1L=high_duty
                CCP1CON=low_duty
                GPIO.5=0
                GPIO.4=1
                last_adjust=1
             end if
    
    
          end if
    wend
    
    
    end.
    And here is my PBP PRO conversion so far:

    Code:
    define OSC 48temp VAR word
    final_period var byte
    final_duty var byte
    high_duty var byte
    low_duty var byte
    last_adjust var byte
    supply_multiplier  var word 'as float
    period_multiplier var word 'as float
    v_feedback var word
    v_supply var word
    t_rise var word
    SMPS_duty var word
    t_period var word
    SMPS_period var word
    EE_pointer var byte
    EE_data var byte
    v_ref var byte
    supply_ratio var byte
    osc_freq var byte
    L_Ipeak var byte
    fb_value var word
    
    
    '-------------------------------------------------------------------------------
    
    
    'set values
    v_ref = 38/10              'float  5.1 in the example, 3.8 here
    supply_ratio = 1135/100    'float, 11.35 here
    osc_freq = 8               'integer or float
    L_Ipeak = 67               'float
    fb_value = 443             'word   225 in the example
    
    
    '-------------------------------------------------------------------------------
    
    
    ADCON1 = %00001011
    ADCON2 = %10000111
    
    
    TRISA = %00011111
    TRISB = %00000000
    TRISC = %00000111
    CMCON = 7
    
    
    '-------------------------------------------------------------------------------
    
    
    PRE_CALC:                  'precalculations
      supply_multiplier=v_ref*supply_ratio
      period_multiplier = 4/osc_freq
    return
    
    
    '-------------------------------------------------------------------------------
    
    
    UPDATE_SMPS:
    'take Vss reading
          temp=0
          ADCIN 0, temp
    'convert to voltage
          v_feedback=temp                 'put ADC in float
          v_supply=v_feedback/1024        'find supply % of vref
          v_supply=v_supply*supply_multiplier  'find supply volts
    'calculate rise time
          t_rise=L_Ipeak / v_supply       'find rise time @ supply volts for L/Ipeak
    'calculate CCPR1L:CCP1CON<5:4>
          SMPS_duty=t_rise*osc_freq        'find duty cycle value
          final_duty=SMPS_duty             'convert to byte
    'calculate period
          t_period=t_rise*133/10           'dutycycle = .75period
    'calculate PR2
          SMPS_period=(t_period/period_multiplier)  'find period value
          final_period=SMPS_period-1              'put in byte
    
    
    'put in register masks
          high_duty=(final_duty>>2)        'high 6 bits in CCPR1L
          low_duty=(final_duty<<6)         'low two bits for CCP1CON
          low_duty=(low_duty>>2)           'shift back to CCP1CON<5:4>
          low_duty.3=1                     'PWM configuration bit
          low_duty.2=1                     'PWM configuration bit
    
    
    'LOG TO EEPROM
       'only write if we measure somthing,
       'prevents false writes durring programming b/c MCLR is disabled
       if temp > 0 then
          EE_data=(temp>>8) 'high 8 bits first address
          Write 0,EE_pointer, 10,EE_data
          EE_pointer=EE_pointer+1
          EE_data=temp      'low 8 bit second address
          Write 0,EE_pointer, 10,EE_data
          EE_pointer=EE_pointer+1
          'put final_duty in eeprom
          Write 0,EE_pointer, 20,final_duty
          EE_pointer=EE_pointer+1
          'put final_period in eeprom
          Write 0,EE_pointer, 30,final_period
          EE_pointer=EE_pointer+1
          'reset write pointer if needed
          if EE_pointer > 197 then 
          EE_pointer=1 
          endif
         'update the write pointer
          write 0, EE_pointer
       endif
    
    
    return
    
    
    '-------------------------------------------------------------------------------
    
    
    main:
    
    
    'set ADC
      'GPIO = 0
      'CMCON = 7
      'TRISa = 0              ' designate gpio as output
      'TRISa.1 = 1         ' pin ANS0 as input  (Supply feedback)
      'TRISa.2 = 1         ' pin ANS1 as input (HV-feedback)
      'ANSEL.ANS1=1
      'ANSEL.ANS0=1
      'ADCON0.VCFG = 0         ' Vdd as Vref
    
    
    'Low Voltage Indicator Light
      porta.4=1 'indicator light on
    
    
    'delay and take Vss reading
      Read 0, EE_pointer
      'EE_pointer=Eeprom_read(0)
      pause 1000
      gosub PRE_CALC
      gosub UPDATE_SMPS
    
    
    'setup PWM
      PR2=final_period 'sets PWM frequency
      CCPR1L=0    '8 most sig. duty cycle bits =0
      CCP1CON.5=0 'least significant bits = 0
      CCP1CON.4=0 'least significant bits = 0
      CCP1CON=12  'turns on PWM (set CCP1CON=0 at any time to turn off PWM)
      T2CON=4     'turns on TIMER2 module (required for PWM timing)
      last_adjust=0
    
    
    while 
          temp=0
          ADCIN 0, temp ' ADC conversion
          if temp > fb_value then   'feed back OVER set voltage
             if last_adjust=1 then 'if 0 then already off
                CCPR1L=0
                CCP1CON.5=0
                CCP1CON.4=0
                porta.5=1
                porta.4=0
                last_adjust=0
             endif
          else
             if last_adjust=0 then
                CCPR1L=high_duty
                CCP1CON=low_duty
                porta.5=0
                porta.4=1
                last_adjust=1
             endif
    
    
          endif
    wend
    
    
    end]
    Ok, first thing, I have not set up the ADC or inputs/outputs yet, this isnt an issue at the moment. i have also not finished with the EEprom yet.

    My first question is regarding the While - Wend loop.

    If the program is stuck doing stuff in this loop it cant respond to any push button inputs to turn it on or off, or dim the brightness etc. This part of the program will have to be done at the same time as the buttons are monitored. So, do I have the buttons on an interrupt or does this loop run in the back ground somehow?

  2. #2
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    I'll give this a go... even though there are definately better programmers around here than me

    With microcontrollers there is very few instances where things can "go on in the background". Acutally NONE as far as the main processor is concerned, it can only do one thing at a time. Most all PIC's include onboard peripherals like PWM channels or USART serial links, timers and counters, etc. (see the datasheet for your PIC of choice and it will list all the different functions that each pin can be set up for) these peripherals can be set up to do something in the background, like the PWM channel, where you define a given pulse width and it will continue to output that pulse at a given duration and frequency while your program continues on.

    Now, you could set up the pushbutton to be tied to an I/O pin that had "Interrupt On Change" capability, either rising edge or falling edge. Then when you pushed the button an INT would be generated and the Pic could jump to the INT handler(check out Darrell Taylors "instant interrupts" for the "best" way to impliment interrupts.

    OR you could just include a check for when the button pin changed state within the while/wend loop.

    Remember... "can only do one thing at a time" is a relative statement... ie. if you push a button and the contact is closed for 200 msec's then that gives the processor a LOT of time the do something else and still get around to seeing that you are pushing a button.

    good luck!
    Last edited by Heckler; - 10th October 2011 at 21:06.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    Hi Heckler,

    Yup, I am aware things are done one at a time. I was wondering if timer based pwm could run whilst an interrupt on change was being checked for.
    I am looking at DT's instant interrupts right now actually

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


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    The code below will flash the LED but not create a pwm output. I'm guessing its because there are fractions and not integers to work with thus creating 0's and other odd results.
    The convert to voltage portion works fine - I changed it, but the rest needs work still.


    Code:
           temp=0      ADCIN 0, temp
          'write 40, temp
          
    'convert to voltage
          v_feedback=temp                                                 'put ADC in float
                                                                                   'v_supply=v_feedback/1024
          v_supply = 1024/v_feedback * 10                            'find supply % of vref
                                                                                   'v_supply=v_supply*supply_multiplier  'find supply volts
          v_supply = v_supply * supply_multiplier / 10
          write 60, v_supply
          
    'calculate rise time
          t_rise=L_Ipeak / v_supply                                       'find rise time @ supply volts for L/Ipeak
                                                                                   ' = 68/24 = 2.833uS (example = 24vin)
    'calculate CCPR1L:CCP1CON<5:4>
          SMPS_duty=t_rise*osc_freq                                    'find duty cycle value
                                           '2.833*48 = 136
          final_duty=SMPS_duty                                            'convert to byte
          
    'calculate period
          t_period=t_rise*133/100                                         'dutycycle = .75period
                                                                                    '2.833*133/100 =  3.767uS
    'calculate PR2
          SMPS_period=(t_period/period_multiplier)                   'find period value
                                                                                   '3.767/(4/48) = 45.2
          final_period=SMPS_period-1                                    'put in byte
                                                                             ' = 44.2
    'put in register masks
          high_duty=(final_duty>>2)        'high 6 bits in CCPR1L
          low_duty=(final_duty<<6)         'low two bits for CCP1CON
          low_duty=(low_duty>>2)           'shift back to CCP1CON<5:4>
          low_duty.3=1                     'PWM configuration bit
          low_duty.2=1                     'PWM configuration bit
    
    
    
    
    main:
    CCPR1L=high_duty       'flash LED to show alive
    CCP1CON=low_duty
    portc.0 = 1
    pause 100
    portc.0 = 0
    pause 100
    GOTO main

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    I see an error on your PBP program

    Code:
    define OSC 48temp VAR word
    should be

    Code:
    DEFINE OSC 48
    temp VAR word
    though I had no time to check all of your code.

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    Oops - thats a copy/paste error!!

    Basically, I need help setting up a CCP for PWM operation - I have never done it.

    Searching for examples...

  7. #7
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    I like you project but im new user icanot able to post prease can you give instruction how can i communicate with different people in this chart

  8. #8
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    Can I ask for the link to the original project on Instructables?

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


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    Sorry, I downloaded the zip with all the files in it, so I dont have a link. I'm sure you could search for it on their site though.

    Incidentally, I got this boost controller to work and have been adding features to it. It works quite well but I still need to clean up some noise on the output.

  10. #10
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    A search revealed a number of them. I was just trying to narrow down which circuit you were dealing with.

    Bo

  11. #11
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    Im a new use in this forum my project concerned about generation of 3 phase SPWM I have not know how to comunicate with different user in this link

  12. #12
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: MikroBasic to PBP Pro: Boost mode SMPS

    Quote Originally Posted by sangaboy View Post
    Im a new use in this forum my project concerned about generation of 3 phase SPWM I have not know how to communicate with different user in this link
    If you have questions, ask them in open forum, create your own thread, and people most likely will help. PM messages from strangers are usually unwelcome, and the same person who might help you in your thread may be UNWILLING if you PM them. Additionally your questions and the answers posted in a thread may well help someone else who is having a similar problem. I myself have learned many things working on someone else's problems.
    Last edited by Archangel; - 25th November 2011 at 02:30.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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