No announcement yet.

PIC 16F628A and HPWM implementation

  • Filter
  • Time
  • Show
Clear All
new posts

  • PIC 16F628A and HPWM implementation

    I am prototyping a controller based on a PIC16F628A to use as a programmable throttle for a brushed DC motor. The PIC will provide controlled rpm changes using the HPWM command. The PWM output will be sent to a Pololu 18v17 motor controller to handle the current requirements. The theory is to provide the 18v17 with a constant DC voltage and control the motor speed with varying pulses.

    I have looked at the sample program on the MEL website and it appears to accomplish the task without the use of the HPWM command. I also studyed the 16F628A part document and see the computations and all the registers that have to be manipulated. Frankly it does look intimidating and I'm not a newbie when it comes to data sheets.

    I am really interested in using the HPWM command if it takes care of a lot of the computations in the background. I will probably use a 4Mhz or 8Mhz oscillator for chip clock. So what I really need to know initially is what register settings I will need to set in the code to make HPWM work. I was hoping to find an example on the forum for a PIC with similar architecture. I didn't see a "search" function though on this new interface so I have to post this message.

    In the PBP manual I did see a paragraph which leaves me a bit confused. The text I have highlighted in yellow is was has me wondering. Is this text meaning to just deliberately refer to the PIC17C7xxx devices or is it something meant to include other devices such as mine?

    Click image for larger version

Name:	Phrase Question.jpg
Views:	33
Size:	66.4 KB
ID:	7463
    In addition if someone could point me to a posting or example which successfully makes use of HPWM on the PIC 16 devices it would help with my learning curve on this project.
    Attached Files

  • #2
    Sorry about all the redundant images, I thought I was successfully removing the ones that were too large.


    • #3
      ask yourself these questions

      1 how many ccp/pwm modules does your chip have
      2 what timer resources can your ccp/pwm module/s employ
      3 what pin choices/modes can be made for ccp outputs

      when these have been established the choice or even the necessity become self evident


      • #4
        If anyone could address the underlined issues in the following but the most important one is the last one before the image:

        The data sheet indicates that there is only one PWM module on this device and that it is on (Pin 9) CCP1/RB3. I have planned for this on my circuit design.
        The data sheet indicates that CCPR1L and CCPR1H are LSB and MSB of the register and I don't expect I will need to access this but the HPWM command may do so.
        The data sheet indicates that CCP1IF is the Interrupt flag and is not used in PWM mode.

        The data sheet indicates that CCP1CON- bits 0-3 will set Pin 9 into PWM condition but I don't know if this is done by the PBP compiler when it encounters the HPWM command.

        The data sheet indicates that CCP1X:CCP1Y- bits 5-4 are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPRxL but I don't know iif they are set by PBP.

        The data sheet Table 9-1 indicates that Timer2 is associated with the operation of the PWM part of the CCP1/PWM Module. I expect that the Timer2 control register values have to be set but what I don't know is whether PBP establishes these at compile time (knowing OSC value, the Frequency and Channel) so that when I invoke the HPWM command with the correct arguments everything is set.

        A data sheet Table lists the registers required setting BUT which of these do I need to set and which are set by PBP at compile time?

        Click image for larger version

Name:	PWM Registers.jpg
Views:	30
Size:	69.2 KB
ID:	7467


        • #5
          If anyone could address the underlined issues in the following but the most important one is the last one before the image:
          there is only one possible timer for that chip .,one pwm mode , 1 o/p pin .there are no choices .
          hpwm 1,duty,frequency is all thats required


          • #6
            Thanks for clarifying.

            In that case I will conclude that all the various register settings are taken care of by PBP.


            • #7
              I am now looking at alternative PBP output commands to send to my motor control board like "Pulsout."

              Since my control pulses should be spaced a relatively long duration apart (100->300 milli-seconds) by design and the high state also fairly long (70->150 milli-seconds) the minimum frequencies provided by "PWM" or "HPWM" might be too fast for this application.

              In looking at "Pulsout" it seems as though I could simply use Pulsout with Pause and vary the two as needed during the course of motor running.

              I do have a question though about how clean the pulse is on the output pin. This question arises from reading the PBP3 manual which adds a caution for the "PWM" output which I highlighted in the following image:

              Click image for larger version

Name:	From PBP Manal.jpg
Views:	27
Size:	70.0 KB
ID:	7473

              My question is this: Does the same precaution apply for the "Pulsout" Output? That is do I have to provide a similar filter on my pcb design to clean up the output? I ask this because I don't have a very good (mine is a very old Tektronix) scope to look at the output.


              • #8
                The Pololu G2 18V17 output PWM signal (average voltage applied to the motor) is a direct mirror of the logic level PWM signal you feed it. If the goal is to control the motor speed then why on earth are you aiming for 3-10Hz PWM frequency?

                Why should your control pulses be spaced 100-300ms apart?

                But to answer your question, Yes, PULSOUT provides an accurately timed pulse but it won't be able to generate such long pulses.



                • #9
                  I personally would NOT use the PULSOUT command as the processor will NOT be able to do anything else during the PULSOUT command duration.
                  Dave Purola,


                  • #10
                    I'm concerned that I've been over thinking my design scheme and how HPWM works.

                    Let's say I'm using a 4 Mhz oscillator and that the HPWM page in the PBP manual states that the minimum frequency is 245 Hz and my duty cycle is 127.
                    For a simple example I might place this line in my code: HPWM 1,127,245 If I understand correctly, the real time effect is that every 4 milliseconds the dedicated PWM pin will go high for 50% of the time and low the next 50% of the time. In other words a theoretical complete square wave with a period of .004 sec? Someone please confirm that I understand what is going on electrically or correct me if I'm wrong.

                    My objective may be more clear if I summarize the task at hand:

                    Project: Controller for conventional analog DC (not DCC) model railroad to automatically run trolly/LRV to end of line and reverse.
                    Objective: Starting, cruising, and stopping vehicle by varying the motor speed through three different conditions-
                    Overcome inertia upon vehicle start, one duty cycle setting
                    Reduce speed/rpm at cruise condition, possibly another duty cycle setting
                    Slowing down gradually to a stop, yet a third duty cycle setting
                    I expect timing the system to be by trial and error. I anticipate the steady input voltage to be ~10vdc.

                    I just can't get over thinking that a 245Hz pulse stream is way too fast for this to work even though the PWM output is well within the spec of the Pololu controller. That is why I previously mentioned the numerical values that Henrik commented on. It just seems to me that a pulse train with a period of 100-300 milliseconds would be more than adequate. Please correct my assumptions.


                    • #11
                      Well Abecker, for the first question, you are correct. At 245 Hz. you will get a square wave at a period of .00408 seconds.. As far as what the Pololu controller can tolerate I have no idea as I have never worked with one before. Back in my train day's I built my own controller and it was based on 120 Hz. period pwm control. It had an accel and brake button as well as memory so the hand held control could be unplugged and plugged in another location of the layout. 102 Hz. worked quite well for HO sized engines and there was enough Vibration in the waveform to break the motors loos at extremely low speeds.
                      Dave Purola,


                      • #12

                        Thanks for offering your experience with the model railroad controller. At least I now know that the 102Hz. worked for you. I might get a minimum frequency more in the ballpark of your design if I use a 2Mhz oscillator rather than the 4.

                        You identified the problem exactly. When I run the trolleys with my present automated trip control design, unless the constant DC input is relatively high the trolley doesn't want to budge from its stopped condition. In this case the constant dc input is high enough to allow the trolley to overcome inertia which it does but and it then scoots down the track at unrealistic speeds.

                        My present version of the automated trip controller (using the PIC16F722's extra pins for driving an LED display and programming buttons) relies on three sensors in the track some inches back from the end to tell the controller when to change to cruise speed at the beginning and at the end of the line when to stop. The third one is at the end of a turnout spur.

                        I will continue running bench tests and may convince myself to upgrade my clunky old scope so I don't have to speculate on what will happen.


                        • #13
                          Tests with a real motor and the Pololu motor controller under control of the 16F628A have shown great results today. I have run a test motor (not yet a model trolley motor) at duty cycles of 100%, 50% and 25% all with the HPWM command line. The frequency was 250. Each setting visually appeared to be what I had intended. Short of connecting the output to a scope to see exactly what is happening I could say the tests were a success. I intend to formulate the correct pulse settings experimentally for 4 different model vehicles.

                          If I gain enough skill programming with interrupts, I may attempt to use interrupts to detect the track sensors and report the vehicle position back to the main program line.

                          Thanks Dave and Henrik for your feedback.


                          • #14
                            I am including my next inquiry with the HPWM because it is a sub-process in the program which makes use of HPWM and the design objective is explained previously.

                            The quick project summary is: Change motor speed or stop trolly/LRV when a track sensor goes high thus allowing for automated trips along the length of track.

                            I have successfully used HPWM with a test motor and now need to respond to the track sensors. There are three sensors and I have studied a circuit to multiplex the three such that the External Interrupt pin "B0" will see a high state when any one is tripped. I also have the code to identify which of the three PortB pins has gone high. When the interrupt returns the high pin value is represented in a variable.

                            Here is my problem: The main line of code is executing an HPWM process continuously as the vehicle is traveling along the length of track. Then when it encounters a sensor it needs an interrupt to either change to a different rate of speed or stop depending on which sensor is encountered. In the first case the HPWM rate must change in the second case the signal should stop altogether..

                            I'm not sure if there is a way react to the interrupt in the main program line without stopping the HPWM command momentarily to check the interrupt status bit. Since this needs to be a continuous process to keep the vehicle moving i9n the first case above, I wonder if using the HPWM command will even work? I might just as well forget about the HPWM command and put a sequence of "High-Pause-Low-Pause-Check Interrupt" sequences in a loop to deliver the pulses and check the interrupt activity, am I right?

                            If anyone knows of a different strategy I am open to ideas. But if you would address my musings concepts that would even be useful.


                            • #15
                              abecker, I don't know what you mean by "I'm not sure if there is a way react to the interrupt in the main program line without stopping the HPWM command momentarily to check the interrupt status bit." The hpwm command sets the pwm output to a specified value and runs continuously with out interruption.You can then go other things in the program with out having to maintain any thing to do with the generation of the pwm signal except change the pwm value when needed.
                              All of the other time can be spent looking at the switch inputs and when necessary change the pwm value.
                              With a program so simple there is no need to use interrupts. Just sit in a loop and look at the switch inputs and make your decisions based on them.
                              Dave Purola,