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,118
    Likes:
    89
    Location:
    Seattle, WA
    ONLINE
    As a general suggestion MrDEB, look at your messages after you post them to see if they're readable. Ain't nobody going to take the trouble to read your PSTRCON resister settings above. Yes, it takes a tiny bit of effort to post a capture of the table, but don't expect people to dig up the data sheet because you couldn't go to the trouble of posting something readable.


    image.jpg
     
  2. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I agree with you, GO WITH WHAT I GOT
    Just curious as to how to get the outputs to use pwm and if I am interpreting the data sheet correctly that the outputs CAN be 180 degrees out of phase. I may just be grabbing straws.
     
  3. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    YES I should have posted the data sheet showing the PSTRCON register. THANKS
    Question is in reality can the outputs be 180 degrees out of phase. I looked at the timing diagrams but ??
    IMO to save headaches and a train wreck am just going with what I have but feel that maybe IF the outputs can be out of phase that it may be a learning example just as the low voltage detection was.
    This simple project opened up some interesting project ideas for me and maybe a learning experience for other members??
     
  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,145
    Likes:
    907
    Location:
    Rochdale UK

    Using the CCP module in half bridge mode will give you a PWM signal on P1a and the inverse signal on P1b.

    LG mentioned this a while back... P1c and P1d are the same as the other two when in full bridge... You may be able to increase the dead time to alter it slightly more..
     
  6. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15
    You can use the Pulse-Steering Mode to output a PWM signal onto multiple output pins using the PSTRCON register, as opposed to any of the half/full bridge modes. The output polarity of the bits is set by the CCP1M<1:0> bits of the CCP1CON register.

    See section 13.4.7 in the datasheet
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    Yeah.. I've been looking at that too... It seems like you can vary the output's quite a bit...
     
  8. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,792
    Likes:
    134
    Location:
    morristown,tn
    Yep like I said you can invert them, can't change there duty just invert them I don't think that will make his candle look better.

    Want better use software PWM and that could be easy or as hard as you want it to be.
     
  9. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    m going withhat I have. Adding the blue didn't help much except add to the current draw. This is a battery powered project.
    As for the different PWM outputs, I hate to say it but this will be a first--kinda giving up on anymore changes. Its an interesting subject to get this chip to output different outputs (PWM) but feel I am not making any progress. I looked at the data sheet and made several code changes but see no real difference.
    THANKS for all the input, it has been an interesting ride on this simple project. One aspect I am looking at is to use the PWM from this project on an LED matrix display. but maybe use a better pic w/ more PWM outputs that are not multiplexed together.
     
  10. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    89
    Location:
    Seattle, WA
    ONLINE
    After reading through the data sheet, I'm back to the belief that you have exactly one active output, the pin connected to the yellow LED. Watching your video closely, I do not see any indication of any red color. I think it looks decent – I'm just pointing this out in the hope of understanding what you're actually doing.

    The 18F13K22 has one ECCP module and therefore one PWM output available. It's true that this single output may be made available on four pins, but there is only one PWM channel. The four output channels can be used in various configurations to control an H-bridge for motor control, but all four outputs change in lockstep with each other with options for polarity of each output and a (configurable) delay before switching some of the outputs. From the data sheet:

    13.4.7 PULSE STEERING MODE

    In Single Output mode, pulse steering allows any of the PWM pins to be the modulated signal. Additionally, the same PWM signal can be simultaneously available on multiple pins.

    Once the Single Output mode is selected (CCP1M<3:2> = 11 and P1M<1:0> = 00 of the CCP1CON register), the user firmware can bring out the same PWM signal to one, two, three or four output pins by setting the appropriate STR<D:A> bits of the PSTRCON register, as shown in Table 13-2.

    While the PWM Steering mode is active, CCP1M<1:0> bits of the CCP1CON register select the PWM output polarity for the P1<D:A> pins
    .

    Also consider your code. The only command that can change any of the outputs is:

    SetDutyPercent1()

    So the following possibilities exist:

    1. The yellow LED is the only active LED.

    2. The yellow LED and red LED are both active, and both on at the same time

    3. The yellow LED and red LED are both active, with inverted operation (yellow on, red off and yellow off, red on).

    If you've managed to active more than one output, either case 2 or case 3, you've hit on the perfect ratio of current limiting resistors to create a nice look. I think it more likely you have just got the yellow LED active.

    Your result looks decent. Go with it. But I think you'll be just fine if you leave out the red LED.
     
  11. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    message from LG with GM typing (for all the anal's out there).
    The light looks good, but you need more flicker thats slower, not less thats quicker! you need to throw in small bits of darkness (totaly off) for upto 500ms.
    MR Deb, put your hands between your legs and squeeze, if it hurts then you have the bits needed, stop playing in your comfort zone of swordfish. Its time to delete that crap compiler and start on C. its easier, its far more powerful, and it will do the job properly!
    Basic is for kids, not for people that feel pain when squeezing the stones :D
     
  12. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    They are his words not mine, i may or may not agree!

    Actually I dont have a view one way or other!
     
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,792
    Likes:
    134
    Location:
    morristown,tn
    The only problem with swordfish is knowing how to use it. It's not really a baby basic. But with C you could use any chip you want. I did like swordfish because of the 18f chips and the ease of using USB but i have to use PBP for 10f and 12f and the 16F and there no way the same.

    So i'm now using C cause it works on any thing i use not just pic but atmel arm and the 16 bit Texas chips that are cheap as sin.
     
    • Informative Informative x 1
  14. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    YES I am looking at either C or assembly. I have two books but neither is about C. I think (need to locate) basic pro and ??
    When I purchased I was under the impression it had a disc but no disc. Any suggestions on book title for learning C
    On the red LED, its there just not real bright. and yes I want to slow down the flicker more. Maybe add some more flicker loops with lower DUTY amounts.
    Am in the process of creating a holder for this.
     
  15. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I would suggest you learn the basics from tutorials online. Just google. Or DuckDuck. Then get some good books.. don't waste your money on beginner C books.
    Some really good books:
    OReilly - 21st Century C
    OReilly - Understanding and Using C Pointers
    Expert C Programming - Deep C Secrets
     
    • Informative Informative x 1
  16. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15
    1) Why is it crap? because you don't know it?
    2) easier for whom?
    3) far more powerful based on what?
    4) asm will do the job properly, too, but I wouldn't suggest a beginner use it.

    A tool is a tool. There's nothing MrDeb wants to do that requires switching to a different language.
     
    • Agree Agree x 1
    • Disagree Disagree x 1
  17. ghostman11

    ghostman11 Well-Known Member

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

    I will answer this for him, mainly because when i started i started with swordfish.
    Well i will list what i dont like then you decided if its crap.
    1). the free version has some serious limits if you want to do anything serious code size for a start
    2) mr deb would have been better with software pwm, in sword fish running 4 arrays isnt easy if indeed possiable
    3) for around 18 months the developer vanished and stopped work on it, leaving bugs. he is back but working on a new baby.
    4) support for a long time was via the community only.

    Thats just for starters. With C you have way more control, and the ability to use inline assembly, some compilers are free with no code size limits. There comes a point when like it or not Basic just dosnt cut it. And there has to be a reason its call BEGINNERS All Symbolic Instruction Code.
    name 3 things you can do with smaller faster more efficient code in swordfish, that you cant do in swordfish.
    that will answer your question. If you just want really easy then go for flow code!
    Mr deb dont get hung up on books, any C book will give you the basics, but best way is to start small and play around with it, the junebug was great for this
     
    • Agree Agree x 1
  18. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    Mr Deb, the following is just an observation, please dont be offended. I would stay away from ASM for now, it is far more cryptic, and to get the most from it you need a deep understanding of the hardware.
    C is alot more like Basic, there are 'real' words in it. Well written C is pretty easy to follow once you understand it a little. A good grasp of C should put you in a position to then go deeper and look at asm, With say C18 you get a ASM print out of the compiled code, this can help you with learning ASM once your ready to go into that.
    But for now forget ASM, its a language that relies on menomics not difficult once you get there, but for now go with C and use that as a chance to get better practice at understanding datasheets. Out of everything an understanding of what a datasheet tells you, is what really matters, and if i am honest i think its your lack of datasheet knowledge and how to extract that information that is holding you back.
    You will get there but you will need to formal a more formal path, you tend to jump about a bit and i think that is holding you back
    very best reagrds Mr Deb
     
    • Agree Agree x 1
  19. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,792
    Likes:
    134
    Location:
    morristown,tn
    Mr T is right simple beginner books are a waste of money and you can learn a lot with Google and DuckDuck is great it's on the pi I love it.
     
  20. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    89
    Location:
    Seattle, WA
    ONLINE
    I'm really hesitant to jump into this one. Yes, I am a brain-damaged Basic programmer. That said, there is nothing I have set out to do using Swordfish that I haven't accomplished.

    Sure, the free DEMONSTRATION version of Swordfish has some extremely generous restrictions on code size. So generous in fact that many users never come up against the barrier and don't even know that it's there. Good for them, not so good for the developer who is trying to make a living from selling it.

    I will not get into a debate about C vs. Basic. C is definitely more portable; in my case at least Basic lets me rapidly accomplish the things I desire to do.

    But let's step back to the issue at hand. The discussion is wether C would overcome the limitations of Swordfish Basic for MrDEB. I'm going to be brutally blunt here – apologies to any hurt feelings along the way. MrDEB's problems don't stem from any limitations of the language he's using. The limitations start from a lack of basic understanding of the language syntax, a lack of logical understanding of what commands do and a lack of a logical understanding of program flow. Changing from Basic to C, Forth or any other language doesn't change the need to understand these things. 5000 posts between here and Digital-DIY will lend some credence to these thoughts.
     
    • Agree Agree x 1
  21. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    Use the language that you feel good about. If you feel good about swordfish, and you do not see any restrictions, use swordfish.

    If at some point you feel that swordfish has too many restrictions and you really need something else to break away from them then switch to something else. But if you got to that point, you don't feel good about swordfish anymore, do you? If you still feel good, don't change. If it's not broken, don't fix it.
     
    • Agree Agree x 1

Share This Page