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. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    Just FYI.... in case LittleGhostman is still working on this.


    image.jpg
     
  2. 4pyros

    4pyros Well-Known Member Most Helpful Member

    Joined:
    Oct 22, 2010
    Messages:
    4,368
    Likes:
    282
    Location:
    Pennsylvania, USA
    I dont think we will be seeing Little Ghostman anytime soon.
    But its interesting the OP is not updating this thread.
     
  3. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I pm littleghostman about a week ago just to see if he had anything to add or suggest.
    I received some 1000mcd leds that I encircled with some paper and changed the code around. The candle effect is great but considering adding some blue highlights.
    The paper tended to mix the red and yellow together for a very realistic candle effect and not a simple flashing led like was suggested and what I started with.
    After some experimenting with adding a slight ting of blue I plan to drizzle some hot melt glue.
     
  4. dave

    Dave New Member

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


     
  5. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland

    Sorry to say LG has left the forum, He did finish the candle thing, using RGB leds, it look really good, he had trouble moving it to sf basic, not sure what he did with the code, it isnt where i thought it would be, i will ask him in the morning. But i am afraid its unlikely he will be back on this forum
    jason
     
  6. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    So to sum this thread up, should anybody ever try to make sense of it...

    In 265 messages, in a thread that was largely about creating an LED candle, the answer is pretty much "I went a different way" and there's nothing to be learned here.

    Thank you everyone for playing.
     
  7. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I guess someone hasn't read all the posts and deciphered some that were instructive and some that were destructive as in off topic. I never posted my final code so how can you determine if nothing was learned. I myself learned alot about how a simple project can entail a massive amount of data to achieve results. By experimenting with different codes and tweek the codes to achieve desired results.
    There is a word called inquisitive and imagination. If it doesn't work as desired then ask for suggestions and experiment with the suggestions.
    This is what I have done from day one.
     
  8. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I have not followed the thread, but I agree with you. In a large project you concentrate just to "get things done". A simple hobby project really gives you the opportunity to dig deep into specific details.
     
  9. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    True statement that.

    I guess my outlook on the forums is a little different than MrDEB's (and, by that I mean completely different down to the core).

    As I see it, someone posts about a question they have or a problem they're having under a descriptive and accurate title. This forms a request for help from people who presumably know something about the topic.

    People who do have some knowledge expend effort and time explaining the topic at hand or making suggestions for improvements if there's not a clear cut answer. There may be discussion among many people, including the original poster to work towards a good solution.

    If the subject is about improving a way of doing something, rather than simple clarification about how something works, the social contract formed when the request for help was made, requires that the original poster report back on how the solution was actually accomplished, so that everyone gains from the time and effort of those willing to help. Without this final step, only the original poster gains anything from those efforts, and anyone searching the forum in the future to solve a similar problem is left in the dark about anything learned in the thread.

    To make forums useful, a few things are helpful:

    1. A clear, descriptive accurate title.

    2. The entire topic should be discussed in the same thread and particularly in the same forum so that all of the related information is in the same place.

    3. The final solution should be documented and summarized; if no solution was found, this should be so stated.

    If these steps are followed, many people gain from the knowledge and efforts of those willing to help. Conversely, only the original poster benefits, and anyone later attempting to follow the discussion will find it impossible with bits and pieces spread across the Internet.

    This is only my opinion of course, but I think people offering to help have similar expectations.
     
    • Agree Agree x 2
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    MrDEB !! I tend to agree with JonSea on this! I was genuinely interested, as I think were several others... It would be nice for a little feedback. Even it it was to say "Sorry guy's it didn't work"
     
    • Like Like x 1
    • Agree Agree x 1
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,808
    Likes:
    135
    Location:
    morristown,tn
    Yes it would be nice to see if it worked Ian and Jon both have points cause some day every one is going to stop helping Mrdeb show us something even if you think it's not good it's still nice to see a outcome.
     
    • Agree Agree x 2
  12. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Had to locate my webcam (wife borrowed it) but here is a video of present but thinking of slowing down the flicker rate and adding a blue tinge. And need to buy some more hot melt glue sticks to drizzle onto leds.

    here is the code in Swordfish that needs cleaning up and slowing down the flicker rate.
    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
    Dim red As PORTC.4  ' red
    Dim chC As PORTC.3
    Dim chD As PORTC.2
    // main program...
    pwm.SetFreq(5000)
    pwm.Start1
    PSTRCON = $07          // sets all outputs

    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

     
     
    • Thanks Thanks x 1
  13. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    The video actually looks pretty decent.

    It's not possible to figure out what you're actually doing from the video and code you've posted. I think you mentioned using a yellow and a red LED but the only active port pin in your code is PWM1.

    *shrug*
     
  14. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Are you sure?? I have the yellow connected to C.5 and the red to C.4 then I have PSTRCON = $07 which enables the other 3 PWM outputs.
    C.5 and C.3 are the same and C.4 and C.2 are 180 degrees different if I read the data sheet right. I could be wrong. Pretty sure this issue was addressed in this thread somewhere and ? ? suggested. I will look back over the thread.
    I found placing the paper ring around the LEDs improved the appearance. When I get back to the shop I plan to add the blue. The LEDs are 1000mcd piranha LEDs I got off Ebay.
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    I think it works pretty well... Thanks for the video MrDeB...
     
  16. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Thanks guys
    now am curious if I in reality have only one active output port?
    Will investigate. Looking at weather I can use the logic analyser or just slow down the frequiency> Tried from 50 to 5000. Nothing appears different??
    Tried changing the PSTRCON register
     
  17. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Here is the posted code with a better?? explanation of what I found makes for a smoother transition with the flicker. I tried using DEC(DUTY) but the transition looks better by just adjusting the amount of DUTY in what will call flicker loops. I tried lots of different codes and methods to smooth out the flicker transitions. The CCP1CON register is revised in the PWM2.bas module. If you want I will post the INCLUDE routine revisions. Hopefully this is where the output error? that Jonsea suggests that I only have 1 output. Maybe the PSTRCON register is wrong? This register was addressed in post 106 and 112. I tried changing from $07 to $0F but can't see much difference but the red led seems? to flicker different?? That POV crops up. On to inserting a blue led into the mix.
    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"
    // local duty variable...
    Dim Duty As Byte
    Dim x As Word

    Dim yellow As PORTC.5 ' yellow
    Dim red As PORTC.4  ' red
    Dim chC As PORTC.3
    Dim chD As PORTC.2

    // main program...
    pwm.SetFreq(5000)   '5000
    pwm.Start1
    PSTRCON = $0f        
    // sets all outputs

    Output (yellow)
    Output (red)
    Output (chC)
    Output (chD)

    While true
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 1
    //xxxxxxxxxxxxxxxxxx
    Duty = 30
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc(Duty)                   ' increase DUTY amount by one
        DelayMS(5)
    Until Duty > 100             ' when DUTY reaches 101 code continues to next " flicker"loop


    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 2
    //xxxxxxxxxxxxxxxxxx
    Duty = 70                      ' When the Duty reaches 101 then the DUTY is changed to 70
    Repeat                         ' I found doing it this way avoided the BLINKING effect and made a SMOOTH
        pwm.SetDuty1Percent(Duty)    ' transition to the next flicker
    Inc (Duty)
    DelayMS(5)
    Until Duty > 100
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 3
    //xxxxxxxxxxxxxxxxxx
    Duty = 60
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(15)
    Until Duty > 100
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 4
    //xxxxxxxxxxxxxxxxxx
    Duty = 50
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(25)
    Until Duty > 70
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 5
    //xxxxxxxxxxxxxxxxxx
    Duty = 30
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc(Duty)
        DelayMS(5)
    Until Duty > 100
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 6
    //xxxxxxxxxxxxxxxxxx
    Duty = 70
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(5)
    Until Duty > 90
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 7
    //xxxxxxxxxxxxxxxxxx
    Duty = 60
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(5)
    Until Duty > 80
    //xxxxxxxxxxxxxxxxxxxxxxxx
    ' flicker 8
    //xxxxxxxxxxxxxxxxxx
    Duty = 50
    Repeat
        pwm.SetDuty1Percent(Duty)
    Inc (Duty)
    DelayMS(15)
    Until Duty > 70
    Wend

     
     
  18. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,808
    Likes:
    135
    Location:
    morristown,tn
    Mrdeb It looked good in the video. I think what Jon is telling you your code is only outputting pwm on one pin. It don't matter if its four pins its still putting out the same signal. That chip sends out a pwm signal out four pins all four alike or two inverted but still the same signal so if its 50% there all at that.
    But it works that's what you was after and it's not bad.

    Just not what Jon and Little ghostman where trying to help you do.
     
  19. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    The PWM2.bas module as written can control 2 PWM modules....

    SetDuty1() controls the first and SetDuty2() controls the second. All of your commands are directed to module 1.

    Of course if you've changed the standard module, there's no telling what it does any more. The best way of "experimenting" on modules is to leave the original intact, save it under a new name and make changes to the new file only.

    PMW_with_who_knows_what_random_changes_got_made.bas for example.

    If you don't do this, it will lead to confusion later on. Swordfish first looks for modules in the same folder as the program file. Next it looks in the user library folder and finally in the systems library folder. If you've changed a copy of the original file and put it in the same directory as the program file under the original name, it will work until you move the program file for some reason. Then, when the modified file is no longer in the same folder as the main program file, the compiler will grab the unmodified version from one of the other default locations and your program won't work anymore.
     
  20. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I saved the original in another SAMPLES folder. Here is the first section of the PWM2 module that sets up the additional outputs then I added the PSTFCON = $07 in the main code which sets up the output steering which if I understand it correctly, two outputs are 180 degrees out of phase from the two other outputs. Pretty sure I have the register configuration wrong but experimenting. Wonder if I put the PSTFCON register setup in the PWM2 .bas module. The INCLUDES are setup first so before the code gets to the present register setting it would already be loaded. Am curious how to achieve the 180 degree output difference.
    Code (text):
    Module pwm
    Dim
       FMaxDuty As Word,
       FTMR2ON As T2CON.2
    #if _device in (18F1220, 18F1320)
      Dim FPWM1Pin As PORTB.3  // RB3 connected to CCP1 module
    #elseif _device in (18F13K22)
      Dim FPWM1Pin As PORTC.5   // RC5 connected to CCP1 module
      Dim FPWM2Pin As PORTC.4
      Dim FPWM3Pin As PORTC.3
    #else
      Dim FPWM1Pin As PORTC.2  // RC2 connected to CCP1 module
      Dim FPWM2Pin As PORTC.1  // RC1 connected to CCP2 module
    #endif
    {
    ****************************************************************************
    * Name    : Start1                                                         *
    * Purpose : Start PWM Channel 1                                            *
    ****************************************************************************
    }
    Public Sub Start1()
        CCP1CON = $0c    ' $0c  PWM mode P1A, P1C active high :P1B, P1D active high
        Output(FPWM1Pin)
        Output(FPWM2Pin)
        Output(FPWM3Pin)
        FTMR2ON = 1  
    End Sub
    {

    Here is the PSTRCON register settings

    REGISTER 13-4: PSTRCON: PULSE STEERING CONTROL REGISTER(1)
    U-0 U-0 U-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-1
    — — — STRSYNC STRD STRC STRB STRA
    bit 7 bit 0
    Legend:
    R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
    -n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
    bit 7-5 Unimplemented: Read as ‘0’
    bit 4 STRSYNC: Steering Sync bit
    1 = Output steering update occurs on next PWM period
    0 = Output steering update occurs at the beginning of the instruction cycle boundary
    bit 3 STRD: Steering Enable bit D
    1 = P1D pin has the PWM waveform with polarity control from CCP1M<1:0>
    0 = P1D pin is assigned to port pin
    bit 2 STRC: Steering Enable bit C
    1 = P1C pin has the PWM waveform with polarity control from CCP1M<1:0>
    0 = P1C pin is assigned to port pin
    bit 1 STRB: Steering Enable bit B
    1 = P1B pin has the PWM waveform with polarity control from CCP1M<1:0>
    0 = P1B pin is assigned to port pin
    bit 0 STRA: Steering Enable bit A
    1 = P1A pin has the PWM waveform with polarity control from CCP1M<1:0>
    0 = P1A pin is assigned to port pin
    Note 1: The PWM Steering mode is available only when the CCP1CON register bits CCP1M<3:2> = 11 and
     
  21. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    Sorry I asked. You've got something that works ok for you. Just go with it.

    I don't think there is much to be gained at this point by trying to figure out what is actually going on.
     

Share This Page