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

    ghostman11 Well-Known Member

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

    Where i disagree with you is regarding Mr deb, As LG first pointed out, for mr Deb the best way forward would have been 5 independent software pwm arrays, or a multi dimensional array (i cant remember what he said), apparently as i understand it you advised that swordfish didnt have that particular capability.
    Now you have to remember there is a huge difference between yourself and your capabilities, and those of both LG and mr Deb. I am sure in your capable hands you can get swordfish to do pretty much anything, in the same way Nigel and code a couple of dozen lines with his eyes shut, and get the same thing.
    Look on the swordfish forum, for a long time there was chaos because the developer just gave up and went off to do other things for well over a year! my personal view is for a beginner your better off learning a language with good support and back up, because if the dev guy goes missing or gives up, you have learn t a language that will eventually no longer support any new chips! that one fact alone would lead me to discourage beginners to go anywhere near swordfish.
    my suggestion for C was based on C18 or Keil C, both are good compilers, both have large company support, in the case of C18 or its variants, you can be sure new chips will be quickly supported. and the free versions only lack some code optimizations, for most that will never mater, if it does then you can get a full version free for 90 days!
    add it up, it makes no sense to go the swordfish route if you are new to this stuff.
     
  2. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    A while back Eric and others were, posting about how good oshonsoft basic was, it was heavily suggested to beginners to go that path, as i understand it, that product is no longer supported?? i may be wrong?. The point is for beginners its better to stay with a large company untill you know your stuff. no point being left high and dry! Ok not so much of a problem being left High!!:D but not nice being left dry
     
  3. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Jonsea is kinda right, yes I at first had no clue of what I was doing. Using swordfish and simple routines I have been able to do most of my code using what I have learned. It gets complicated as I have never had any schooling in programing to begin with. My time at DDIY and ELECTRO TECH have helped tremendously. YES lots of train wrecks along the way. I look back and ask myself why did I ask such stupid questions but then how else than to learn from people more experienced than me.
    For an example, the CANDLE PROJECT I wrote the entire code myself using examples for guidance. In fact I come up with a way to randomize the duty of the LEDs PWM (thought about this during mass this morning) using CONST arrays and random generator. This might produce a better flicker? As for the RED Led(c.4), yes it is using PWM as well but different from the yellow C.5 port? Looking at it it is almost if not impossible to see any difference without maybe a scope. Hoping maybe the logic analyzer in MPLAB would do the deed but haven't got that far.
    As for jumping around, yes that is one of my hangups but I get too many different ideas. In fact am going to reconfigure the Beer Cooler project code. At present it is a pretty hacky code. I need to get a better handle on the 6 hour and 20 minute timers using an interrupt instead of just a counter and a bunch of IF THEN statements.
    As for programing, I am going to take a closer look at C. The candle project could have been accomplished using a 10F pic maybe (am sure a 16F would do the deed. SWORDFISH is great but limited to 18F only.
     
  4. dave

    Dave New Member

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


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,808
    Likes:
    135
    Location:
    morristown,tn

    I'm with Jon on this Swordfish is great for the 18f chips and the limit only came up one time and it was when doing some pov with LEDs but the guy that came up with it
    I figure wanted to sell Swordfish so why not buy it I did. And I like the USB and SD support. That comes with the full copy.
    It's like this there more to this then copying code and hoping it works you have to learn how to use it and that's that.
    He could use plan old loop and delays to get PWM it don't take hardware PWM write it good and you could use more pins for three colors but that means you have to know how long each pass takes.
    Or get it done with a timer there a sample that shows how to toggle three LEDs change the timing and guess what you have three pins doing PWM it's that easy.
    Then cut the power randomly and it will look like the real deal.
     
  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,808
    Likes:
    135
    Location:
    morristown,tn
    One of the great programmer of all times or I think he is said if you can write code good code in a loop you'll never write good code using timers or interrupts I see his point.
     
  7. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,119
    Likes:
    90
    Location:
    Seattle, WA
    Like I said, I don't want to debate Swordfish in general but there is an important point to make.

    The developer of Swordfish has made available tools to create device files for any new PIC18F-series parts – they use the device files Microchip provides to create the needed Swordfish files. Some of the generated files do require some editing in well-understood areas, but so as long as Microchip doesn't change things too radically, new parts will be supported.

    The modules used by Swordfish to support various functions (UART, PWM, I2C, SPI to name a few examples) are written in Swordfish Basic. This is a radical departure from what other Basic implementations do. This means that existing modules can be updated to support new chips and features (the 18F-K-series require many changes to accommodate different pin assignments and features for example) and modules for new functions can be developed using only Swordfish Basic. Users aren't locked into support from the vendor to provide these updates.
     
  8. ghostman11

    ghostman11 Well-Known Member

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

    3V0 did some code for me for my daughter, it used a rgb led. It was ALL done with loops! it works beautifully, but again you need to distinguish between the every day joe, and someone like 3V0 who has a complete mastery of programming. The rest of us are stuck with timers and interrupts :D.

    MR DEB

    you have a june bug, using the logic analyzer is very easy with that, if you use a pk2 or pk3, then using the junebug as a logic analyzer makes alot of sense.
    I guess with basic you like it or you dont! years ago i got a commodore vic 20 the day it was launched, i used basic with that and loved it. i am just not a fan of basic on micro's, anyway i feel i answered the questions i was asked, so i am out of here!
    Mr deb its no good looking at C, you either go for it or stick with swordfish, the worse thing for you at the moment is try and have a foot in both camps. Whatever you choose, your first job is earn the basic's and earn to understand data sheets, it can be difficult but its we worth doing
     
    • Agree Agree x 1
  9. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    When you need to babysit couple dozens pins attached to time sensitive hardware, interrupts certainly help.
     
  10. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I love SWORDFISH but I really want to venture into using chips other than 18F.
    I tried using just DELAY loops but the PWM module looks so much nicer.
    As for interrupts I have been questioning which is better as far as efficiency, Using an interrupt or a loop with a counter. that loops while measuring temps.
    I may have phrased this wrong but looking at other SF codes using timers and interrupts, the interrupts look better but in actual use which is better. My code for the Beer cooler looks really clunky compared to people that KNOW exactly what their doing.
    Me, I have a brief understanding of the basics but have a need to experiment by asking the WHAT IF question. But that's how I ended up with my present Candle code.
    As for the logic analyzer, I would like to give it a go but need to investigate how to using the PK2 and Junebug.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    There is nothing wrong with basic.... I use oshonsoft in a number of applications... The only reason I use C is for when optimisation is an issue ( granted of late it's always an issue )... Compiled basic can run as fast as assembled code.. Swordfish is quite a good basic....

    MrDeB.. Unless you need better bitwise manipulation or string manipulation, keep with basic...
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Vladimir has just released a massive update to his software... I think he will be around for a bit!!
     
  13. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    Ok I give in! I hate basic you all think its great! Mr Deb you will be confused! the world is spinning normally again! for newcomers i would still recommend a larger company, but hey advise as you see fit and i will do likewise.
    Funny how programming languages are very much like marmite!
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    I think basic is a great way to get into micro programming.... Of course, sooner or later, you are going to need something more... But unless you can lay out 500+ on a decent optimising C compiler, basic should do just as well as a free C compiler that doesn't optimise jack s**t..

    I find C far easier than anything else... but some basic's can be good RAD tools...
     
  15. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15
    If you hate BASIC, that's fine w/me. I'm not going to try and change your mind.

    Not to pile on here (and you DO make some good points about support, etc), but I've used a number of different languages on 8-bit micros, not just C and BASIC, and they all have their up and downsides. Heck, depending on the circle in which you travel some might call C a seriously broken (or crap) language!

    After 16 pages of forum posts I cringe at the thought of trying to explain to MrDEB the world of C with its make files, linker scripts, memory models, and object code libraries. (MrDEB- No insult intended here, either)

    Imagine explaining how to declare an interrupt routine, why your byte data suddenly becomes ints, or what volatile is, or how to get an array larger than 256 bytes with C18. Or why that "universal C code" written for one compiler won't compile when using another one from the same vendor (ie C18 and XC8).

    Some folks don't need the (questionable) "power of C", they'll never be professional programmers, and they just want to get something done. The guy's trying to make an LED blink. If that requires the use of multi-dimensional arrays (and you can't figure out how to represent that data another way), you might need to look at the problem differently. The answer isn't always "you need a different tool".

    Or a different chip for that matter. If you're not willing to read (and understand) the 300+ page datasheets of most chips these days, I'd suggest picking a chip family and staying with it. You might be tempted to think that switching from a PIC18 to a PIC10 isn't that big a deal, but they ARE different. Sometimes you have no choice, but realize that decision may cause you to switch tool sets, programmers, etc. Is it really worth it to save a buck a chip? (remember, we're talking about a hobbyist here)

    Oh, and in my experience Swordfish (even the free version) can produce better code than many of the C compilers I've used.

    In the end, I think we all agree... use whatever works for you.
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    The ONLY thing I have against Swordfish is the lack of support for anything else... I would have thought it would cater for more than just pic18...
     
  17. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15
    That is a pretty serious limitation if you're concerned about that sort of thing, but I think it emphasizes my point about switching chips. The PIC families are different enough that switching from one to the other isn't always the easiest of things.

    How many different chips does C18 support?
     
    Last edited: Jan 20, 2014
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Its now XC8.... pic10, pic12, pic16, pic18...

    However! I see what you mean... Oshonsoft is either 16, 10 or 18 ... Whereas Sourceboost is all 8 bit pics as well...
     
  19. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Boy this thread has drifted away from a candle flicker code to the age old debate of which programming language is best.
    I looked at the free C tutorials and am asking myself do I really want to get involved in another language? At 62 years young I am really questioning this proposed move. At present Swordfish satisfies all my requirements except being able to use chips other than 18F.
    Going to ponder this but first I have dug up the Junebug and going to investigate weather the red led(C.4) is using PWM out of phase from the yerllow led (C.5).
    The red led(C.4) at present is enabled and has a flickering appearance but is it 180 degrees or ? from the C.5 output.
    That is my quest this morning.
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    The answer is No!!

    Quite right!!! Lets get back to the topic..
     
  21. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    Yes sorry mr Deb, lets not forget you have invested alot of time with swordfish basic, on balance it's probably what you should stick with.
    You do however need more practice at datasheets, it is the one thing i think everyone will agree on, if you dont understand what the datasheet is telling you, then your dead in the water.
    Keep plugging away Mr Deb you will get there! dont worry too much about the extra grey hair you give some people ;) afterall hair dye is cheap :D
     
    • Agree Agree x 1

Share This Page