1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Using PWM on a 18F13K22

Discussion in 'Microcontrollers' started by MrDEB, Nov 27, 2013.

  1. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Been studying the mplab instruction manual and get the impression that to use the logic analiser is to have the Vcc and Grd connected and use the PGD and PGC for logic signal. Connect the PK2 pins 4, 5, 6,(icspdat, icspclk, aux) to desired port pins to be monitored.
    Have yet to try in hopes I won't fry something but need to look futher as the mplab manual is kinda brief on how to connect but hopefully my interpretation is correct.Plan to use jumper wires between PK2 and circuit.
     
  2. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Found this on Youtube

    A whole new area I kinda knew was available but never investigated.
    This tool looks like it could be a valuable asset regardless what language is being used to write code.
     
  3. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Well that was painless but C5 and C4 are both outputting the same in phase square wave.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK

    JonSea
    There you are... Salvation...:joyful:
     
  6. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15
    Calling pwm.Start1() sets CCP1CON = $0C, which sets all of the outputs to active high.

    Try this (from the code you posted back in #271 or so), or read what JonSea said.
    Code (text):

    {
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    Candle Code test using PWM
    pwm W freq .bas
    this one works but rev 2 does not have more than 1 port
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    }
    Device = 18F13K22
    Clock = 8

    Include "osc1322.bas"
    Include "RandGen.bas"
    Include "Util1322.bas"

    // import PWM module...
    Include "PWM2.bas"
    Const color(10) As Byte = (100, 70,65, 90,75,95,85, 60, 80, 85)
    // local duty variable...
    Dim Duty As Byte
    Dim x As Word

    Dim yellow As PORTC.5 ' yellow (ECCP P1A)
    Dim red As PORTC.4  ' red (P1B)
    Dim chC As PORTC.3
    Dim chD As PORTC.2

    // main program...
    pwm.SetFreq(5000)
    pwm.Start1

    // this assigns PWM output onto P1A, P1B, and P1C pins
    // Note: The PWM Steering mode is available only when the CCP1CON register bits
    // CCP1M<3:2> = 11 and P1M<1:0> = 00. Also, the TRIS bits must be set to output
    // mode ('0') to enable the pin output driver in order to see the PWM signal on the pin
    PSTRCON = $07  // sets all outputs

    // P1A-P1B polarity
    // CCP1CON bits 3-0 CCP1M<3:0>: Enhanced CCP Mode Select bits
    // 1100 = PWM mode; P1A, P1C active-high; P1B, P1D active-high
    // 1101 = PWM mode; P1A, P1C active-high; P1B, P1D active-low
    // 1110 = PWM mode; P1A, P1C active-low; P1B, P1D active-high
    // 1111 = PWM mode; P1A, P1C active-low; P1B, P1D active-low
    //
    // calling pwm.Start1() sets CCP1CON = $0C, so bits[3:0] = 1100 (setting all active high)
    // to get P1A and P1B opposite, try setting bit1 = 0 and bit0 = 1
    CCP1CON.bits(1) = 0
    CCP1CON.bits(0) = 1

    Output (yellow)
    Output (red)
    Output (chC)
    Output (chD)
    While true
    Duty = 30
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc(Duty)
      DelayMS(5)
    Until Duty > 100
    Duty = 70
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(5)
    Until Duty > 100
    Duty = 60
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(15)
    Until Duty > 100
    Duty = 50
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(25)
    Until Duty > 70
    Duty = 30
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc(Duty)
      DelayMS(5)
    Until Duty > 100
    Duty = 70
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(5)
    Until Duty > 90
    Duty = 60
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(5)
    Until Duty > 80
    Duty = 50
    Repeat
      pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(15)
    Until Duty > 70
    Wend
     
     
  7. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
    I can't really understand MrDEB's fascination with a 180° phase shift between yellow and red LEDs. This just means that when the yellow LED is on, the red LED is off and vice versa. The two LEDs are not independently controllable with the single ECCP module of the 18F13K22, as was pointed out by LittleGhostman many, many pages ago in this epic tale. Alternate yellow/red LEDs don't seem to offer much in the way of making a decent presentation of a candle. If the yellow is dimmer, the red is brighter. In this arrangement, there is no off state, nor even a dim state; changing the duty cycle can only vary the overall color from yellow to orange to red.

    If there is some reason that alternating yellow/red LEDs are useful, it's a simple matter to achieve this with a single port pin.

    alternate action LED.jpg
     
  8. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    At this point in time I just am curious as to how to accomplish getting two of the outputs out of phase. I originally wanted to try this with out of phase outputs but at present the results are fine but for reference it might become in handy somewhere some time.
    Going to try out tumbleweeds suggestion just for reference.
     
  9. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
    It would definitely be handy for driving an H-bridge. Beyond that, probably not much else, particularly in regards to this topic.
     
  10. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Well tumbleweed gets the chocolate cigar today. The code he posted works to have the C5 (yellow led) out of phase with C4(red led.
    Here is a really crappy video of the two leds (kinda bright without the paper shade, the leds are 1000mcd piranha leds. The red is on the left in the video.
    The screen video shows the yellow on channel 1 and the rel led on channel 3. Wouldn't say they are 180 degrees but maybe I need to change the logic analyzer settings. I need to find out how to do better screen shots.
    th_Video4_zps95f56a46.jpg

    SCREEN SHOT

    th_Video5_zps509b501b.jpg

    Jonsea is probally right. The only use of using the out of phase outputs would be an H bridge or a candle simulation. With the red out of phase LOOKS like it has possibilities of a better candle sim. Going to put entire citcuit into holder and see what comes out.
     
  11. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
    MrDEB, I guess you didn't really read my last two posts. I'll stop trying to explain anything since you just ignore anything not in agreement with how you wish something works.

    I definitely DID NOT say an out-of-phase output was useful for a candle simulation.
     
    Last edited: Jan 20, 2014
  12. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    A certain young man said something earlier, now I have no idea if this would work but i will tell you what he said.
    If your hell bent on using the pwm module with one side out of phase, then bring it back into phase and shift it??? apparently this is not what he would do ever but, MrDeb a mad suggestion follows, according to said young man, put a pnp transistor on the pin that looks off when the other is on, that brings it back into phase, then drive a 555 timer off that pin, in repeatable single shot mode. apparently he thinks you could then alter the 555 to create a set delay, so one pin is a slightly different duty cycle but in phase!
    OK its mad as a bag of frogs, but you got to give it to LG, that sure is a creative way!!!! Then he said something unrepeatable, and suggested the following. forget the pwm module. just do 2 loops in the main code, increment a variable to a large number, then in an array have your duty cycle, offset the other pin by something like B=A+1000. Anyway that is the gist of it, he kind of lost me half way through. He demoed it in c for me, and it works, not brilliantly but pretty good, the colour range is a bit limited but your not dealing with many colours anyway!
     
  13. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Someone mistook what I posted.
    I should have stated that the PWM IMO was good for a candle simulation. The in phase looked fine and the only reason why I continued to research the out of phase was out of curiosity but it has the possibility of adding a different dimension to the candle flicker which has some red, lots of yellow and a little bit of blue.

    As for using two loops and incrementing at different rates, I tried this and it worked but no where as well as the PWM method. I even considered using a 556 timer instead of a pic but then the pic solved several issues such as randomness.

    The PWM allows the slow or fast progression of the duty cycle on time or off time. I found using only the on time INC(DUTY) and not the DEC(DUTY) to work better.
    I could have used an NPN and a PNP transistor on one output to achieve basically the same? out of phase output but isn't one of the goals in using a pic to be simplicity and lower parts count.
    I am going to take a closer look at the logic analyzer tomorrow as the wave does not appear exactly out of phase but it could be a setting on the mplab. Curious why nobody even commented on the two completely different wave forms.
     
  14. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
    No, I did not mistake what you said. I explicitly explained why this would not work well for a candle simulation, and in fact its only likely use was for driving an H-bridge.

    Regarding comments on your logic analyzer post...the most likely reason no one has commented on it is because the quality of your picture makes it impossible to see clearly.

    I might suggest two options:

    1. Goggle to see how to do a screen capture

    2. Read the PICkit 2 Logic Analyzer instructions to see how to save the data, which produces a graphic of the entire data set.

    I'd explain, but after all the effort it took to get you to produce a legible schematic, I just don't have the energy.

    Regarding the two outputs not being exactly 180 out-of-phase, the data sheet explains that there is a delay setting between the outputs to accommodate the different switching times of the PNP and NPN transistors used in an H-bridge, which the device is designed for.
     
  15. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
  16. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    Its called dead band control Mr Deb, its main use is when Mosfets are used because of gate capacitance. That is why you have a slight delay between the two. Google a full Hbridge circuit, that will how you exactly what the module is for.
    Just as a side note, as LG said way back at the start, the best pic would have been the humble 18f1330, it is mostly designed for motor control and has 3 or 4 SEPARATE PWM modules, it is used for driving 3 or 4 H bridges, it would have given you the 4 independent pwm outputs.
    So in theory it should have taken you around an hour to complete the project. You then could have used the out of phase pin from each module to drive a different led . This is where the datasheet would have saved you.
    It really is the key to your projects, i bang on about it because i want it to stick ;)
     
  17. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
    I'm not sure what MrDEB's video shows. If the two LEDs are out of phase, they are out of phase at the PWM frequency. If the program listing has anything to do with the video:

    pwm.SetFreq(5000)

    the PWM frequency is 5000 Hz. Anything above ~20 Hz appears solid to the eye. The logic analyzer trace is too blurry to tell anything.
     
  18. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    After reading Jonseas post I got to wondering why I can see the LEDs "blinking"with the pwm.SetFreq(5000) .

    As for the crappy screen shot video, Have yet to figure out why the wave forms are way different but they appear close to being 180 degrees out of phase with the delay issue.

    Tumbleweed posted the code I am presently using for this screen shot and comparing my version to Tumbleweeds revision found I have the
    CCP1CON.bits(1) = 0 in the PWM2 but not CCP1CON.bits(0) = 1
     

    Attached Files:

  19. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,117
    Likes:
    89
    Location:
    Seattle, WA
    Glad to see you figured out how to do a screen capture MrDEB. There's at least some progress.

    Unfortunately, the screen capture you posted isn't actually showing what you think it does. Let's first look at three PWM plots. THE PWM module is running at 5 KHz (the same as you code), and the plots show 50%, 10% and 90% duty cycles. The blue and purple cursors show one PWM cycle, with the frequency at the upper right of each plot. Note that the pattern is continuous and one cycle always takes 200 usec regardless of duty cycle. If the duty cycle had been varying during the sample period, the ratio of high to low would vary, but the period would be the same.

    PWM - Comparison Screen shots.jpg

    If you look at the screen shot MrDEB posted, it doesn't show this regular pattern - even with a varying duty cycle, the pattern should be continuous. So what's wrong with MrDEB's plot? Look at the plots above. The sample rate is 100 kHz. The sample rate on MrDEB's plot is 5 kHz, with the note Signals greater than 2500 Hz will alias. The plot below was taken with a sample rate of 5 kHz on the same 50% duty cycle data in the first plot above. It looks totally different and it's totally wrong.

    Insufficient Sample Rate.jpg

    This can be seen more clearly with the saved data from the logic analyzer, a portion of which is shown below. Much more data is shown than in the window of the logic analyzer screen. The top and bottom traces should show similar data at different time scales but obviously do not.

    Strip Comparison.jpg

    The sample rate must be greater than twice the highest frequency in the data according to Nyquist. If it's not, transitions in the data can be missed. So try again, with a sample rate of more than 10 kHz. Then you'll see what's really happening.
     
  20. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Interesting reading Jon, Thanks for the great presentation. Maybe this will help someone using the logic tool.
    Am going to look at what I have closer
    thanks again
     
  21. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,702
    Likes:
    426
    Location:
    SCOTLAND
    :confused::confused:
    It was posted to help you!! :facepalm:

    It dosnt need a closer look, read what john posted, there is a little red line, alter the settings you have, because you have them too low
     
    • Agree Agree x 1

Share This Page