Menu

Three stage PWM on PIC

ashimotok0
2025-01-15
2025-01-19
  • ashimotok0

    ashimotok0 - 2025-01-15

    Hi, I am designing a 3-stage solenoid valve actuator controller using a PC 12F1571. The idea is to PWM control the valve coil with a low frequency PWM (low frequency to create 'dither') firstly with 95% duty for 0.2s, then 65% duty for 2s and finally 40% duty to remain in that 40% state until the power is cycled. I am running the clock at 8MHz.

    My code is copied below ... the 1st PWM works fine and looking on my 'scope the waveform is pretty near perfect and the frequency bang on 250Hz. However, it never executes the second and third PWM cycles. Any ideas please?

    P.S. I am am programming the PIC using a PICKit2 along with the PicKit2 + GC software, which I purchased recently from GC. I urge anyone using PIC's to buy that software ..it's awesome.


        #chip 12F1571, 8
        #config mclr=on
    
        Dir PORTA Out
    
        #define HPWM16_1_Freq 0.25         'PWM Frequency 250hZ
        #define HPWM16_1_Duty 95           'Duty 98%
        HPWM16On ( 1 )
    

    Wait 200 ms

        #define HPWM16_1_Freq 0.25         'PWM Frequency 250hZ
        #define HPWM16_1_Duty 60           'Duty 65%
        HPWM16On ( 1 )
    

    Wait 2000 ms

        #define HPWM16_1_Freq 0.25         'PWM Frequency 250hZ
        #define HPWM16_1_Duty 40           'Duty 40%
        HPWM16On ( 1 )
    
     
    • jackjames

      jackjames - 2025-01-18

      P.S. I am am programming the PIC using a PICKit2 along with the PicKit2 + GC software, which I purchased recently from GC. I urge anyone using PIC's to buy that software ..it's awesome.<

      What is GC software?

       
      • ashimotok0

        ashimotok0 - 2025-01-19

        @jackjames .... GC = Great Cow Basic

         
        • Chris Roper

          Chris Roper - 2025-01-19

          It is also a suite of Gas Chromatography software.

          Great Cow Basic is usually abbreviated to GCB or GCBasic

           
  • Anobium

    Anobium - 2025-01-15

    You are using Fixed Mode PWM which means that the I get a warning when I compile.

    GCBASIC (2025.01.15 (Windows 64 bit) : Build 1455)
    
    Compiling: first-start-sample.gcb
    Program compiled successfully (Compile time: 1.527 seconds)
    
    Assembling program using GCASM
    Program assembled successfully (Assembly time: 0.011 seconds)
    
    Warnings have been generated:
    
     first-start-sample.gcb (14): Warning: SCRIPT/Constant: HPWM16_1_DUTY equates to 95 cannot reassigned to equate to 60
     first-start-sample.gcb (19): Warning: SCRIPT/Constant: HPWM16_1_DUTY equates to 95 cannot reassigned to equate to 40
    
    Done
    

    Fixed mode means you get to set the frequency and duty once.


    I am thinking that I have done a set of video on this subject using this chip. Why? Because there is a set of demonstrations GCBASIC\Demonstration_Sources.git\PWM_Solutions\PIC_PWM16_Examples\

    Specificially, there is a demo 060_12F1572_PWM_Dynanic_Mode_HPWM16_Channel_1_2_3.gcb that shows the three PWM channels operating.

    So, you would use HPWM16(1, 10, 16384) '10 kHz, 25% duty cycle (16384/65535) and change the frequency and the duty to meet you needs.

    But, please walk through the demos to see if this helps you.

    Evan

     
  • ashimotok0

    ashimotok0 - 2025-01-16

    Thanks for reply Evan.

    Initially, I did watch your YouTube video and used the example code: -
    HPWM16(1, 10, 16384) '10 kHz, 25% duty cycle (16384/65535)

    But I need a frequency of 250Hz and the example code will not take a non-integer value, specifically a value of 0.25 ...so the minimum frequency I could achieve was 1kHz. I am not averse to using another type of PIC if there is another way of achieving what I want to achieve.

     
    • Anobium

      Anobium - 2025-01-17

      OK. You can expose the calcualations made by the compiler and then use the values to set these different dutys.

      Get the compiler to create a CDF file. Looks in the preferences of this settings.

      The CDF will show you the register constants being used, see below for the full name but HPWM16_1_HPWM16F_xxx_Scriptis the constant naming. Below also shows the registers needed to set. Then, do this a each times changing the Fixed Mode constants and again note the values.

      Always look in the FINAL/CONSTANT section of the CDF.

      Then, create sub routine(s) to change to the duty using the values discovered from the CDFto set the correct registers. You MUST set PWM1LD = 1 to make the change.

      Set Duty98()
         PWM1PRL = the value of HPWM16_1_HPWM16F_PRL_Script   
         PWM1PRH = the value of HPWM16_1_HPWM16F_PRH_Script
         PWM1DCL = the value of HPWM16_1_HPWM16F_DCL_Script
         PWM1DCH = the value of  HPWM16_1_HPWM16F_DCH_Script
         PWM1LD = 1
      end Sub
      

      This will resolve.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.