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

BoostC Charlieplexed PWM 32

Discussion in 'Microcontrollers' started by Mike - K8LH, Jun 25, 2008.

  1. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Would anyone care to study or critique my very first BoostC program?

    It's a novelty 12F683 program that does 32 PWM brightness levels per LED in a Charlieplexed matrix of 20 LEDs. The refresh rate is 62.5 Hz and each LED duty cycle can be varied from 0% to 20% in thirty two 0.625% (100 usec) steps.

    The picture doesn't seem to show the smooth transitions I'm seeing. In fact you can hardly tell those are green LEDs.

    Mike

    [​IMG]

    [​IMG]
     

    Attached Files:

    Last edited: Jun 25, 2008
    • Like Like x 2
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    Very nice indeed. Managing 32 PWM levels on a 4*5 matrix completely in software is impressive. And a good refresh rate as well.

    Code wise, I like the way you lay it out, especially the comments. I hate the recent trend of putting comments within a procedure and in the code space ( and on every other line). Your full width comments before and right side comments during a procedure (and initialisation) should be mandatory.

    I also like the neatness of the end_of_period and end_of_cycle checks. I'm assuming that you couldn't find a way to avoid the asm - the shift at end_of_period looks like it should be C able.

    The only thing I can see that isn't required is the clearing of PIE1.

    Overall an excellent example. A++.:D

    Mike.
     
  3. BeeBop

    BeeBop Active Member

    Joined:
    Dec 22, 2005
    Messages:
    1,281
    Likes:
    23
    Location:
    Vancouver Canada
    WOW!!
    Yes, I must agree with Mike(Pommie); this is very well done, and your source is so well commented and very easy to follow.

    Mike(Mike, K8LH,) I think you should have an award for getting the most punch out of the least resources!! :D :D :D
     
  4. dave

    Dave New Member

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


     
  5. edeca

    edeca Active Member

    Joined:
    Apr 8, 2008
    Messages:
    589
    Likes:
    38
    Location:
    England

    That's really impressive. I was considering doing something similar with the 8x8 square LED blocks from Futurelec to display patterns, I hadn't considered PWM to dim the lights. I might have to try on a much smaller scale with the Junebug LEDs tonight ;)

    What's the board you are using, it looks like a nice size for prototyping or small projects.
     
  6. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Mike: I like it :)
     
    Last edited: Jun 25, 2008
  7. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Gentlemen,

    Thank you all for the nice comments.

    Pommie,

    I'm still trying to come up with optimized C code to replace those remaining few assembler sections. The C code equivalent for those rlf instructions uses two more words and cycles. I guess I'm being a "tightwad" (grin).

    edeca,

    That's a small Radio Shack proto' board made of phenolic or similar material.

    Regards, Mike
     
  8. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    What is that second "jack" thing - lower left? Is that your programming connector? Better lighting and a second 3/4 angle shot would be good.

    Cool little doohickey! :D You should do a YouTube movie so we can see it in action.
     
    Last edited: Jun 25, 2008
  9. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    I use 3.5mm sterio jacks on proto' boards for TTL232 connections.

    Sorry about the picture. I tried several different shots, lighting, shutter speeds, etc., trying to show off the smooth fade from about 10% to 100% brightness but never did come up with a good shot. I even tried it with red LEDs (below) but "no joy" as the color is all washed out.

    I don't have any video equipment for YouTube. Sorry.

    BTW, thanks for turning me onto BoostC. Writing C code for 12F' and 16F' devices for the first time is a blast.

    Regards, Mike
     

    Attached Files:

  10. mneary

    mneary New Member

    Joined:
    Feb 27, 2007
    Messages:
    4,502
    Likes:
    67
    Location:
    California USA
    The green LEDs look good on my monitor. :) The brighter ones are a little washed out but the effect is perfect.

    Photography is a lot better than I would normally do. :eek:
     
  11. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    You have a digital camera!?!? Never seen one yet that won't do video. That's how I do mine.

    Kewl, huh? And C code is very very portable from PIC to PIC. I wrote code for 16F88 and with only minor changes ran it on 18F248.
     
    Last edited: Jun 25, 2008
  12. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    I'll grab the camera docs' tomorrow and see what it says about a video mode.
     
  13. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,564
    Likes:
    2
    Location:
    South Mald Isld
    Hi Mike, K8LH nice work.

    I have a problem.Can you tell me whats the meaning of "Refresh rate is 62.5 Hz"?

    I did a small matrix 5X7 recently.I can light up any LED on that.But I don't know what is Refresh rate?
     
  14. edeca

    edeca Active Member

    Joined:
    Apr 8, 2008
    Messages:
    589
    Likes:
    38
    Location:
    England
    The lights are multiplexed (charlieplexed actually) meaning that the code needs to quickly light each segment lots of times per second to give the impression that they are on. In addition the fading means that some need to be on for longer than others, a longer 'duty cycle'.
     
  15. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Hi Siraj143,

    In multiplexed displays the refresh rate is the number of times per second that an LED is lighted or refreshed. A refresh rate of less than 60 Hz may be perceived as flickering.

    Another important factor in multiplexed displays is duty cycle. That's the total amount of time an LED is lighted during a given period expressed as a percentage or a fraction. Duty cycle affects peak and average current requirements to light a display to full brightness. In this case for dimming or fading an LED, we use PWM to change the duty cycle without changing the peak current supplied to the LEDs and this has the effect of changing the "average" LED current and brightness level.

    Mike
     
    Last edited: Jun 26, 2008
  16. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,564
    Likes:
    2
    Location:
    South Mald Isld
    Hi thanks for the explanation now I understood.If I flash an LED 50 times per second it will show us a flickering.But if I flash it 60 times per second it will stay like a charm without noticing any flicker.

    OK for one bulb is ok I understood.

    Let say I have multiplexed 5 columns so I have called 5 delays in between them.

    In this situation the refresh rate is given to the whole multiplex routine (5 columns) or to a single column?
     
  17. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Refresh Rate = 1 / (column1period + column2period + column3period + column4period + column5period)
     
    Last edited: Jun 26, 2008
  18. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Slightly off topic but...

    I've been following your progress on the Clock/Calendar/Thermometer project (with Predko 2-pin 74LS174 LCD interface) and I suspected that you didn't have to change much code when porting from the 16F88 to the 18F248. Very nice project and writeup btw (as always)...

    Would you consider testing my DayOfWeek routine now that you have calendar capability?

    Mike

    Code (text):
    unsigned char Month = 6;         // 1..12
    unsigned char Day = 18;          // 1..31
    unsigned char Year = 08;         // 0..99 (2000-2099)

    //  71 instructions on 12F683 (BootsC 16)

    unsigned char DayOfWeek()        // Mike McLaren, K8LH, Aug '07
    {                                // returns 0..6 (Sun..Sat)
      unsigned char Leap = 0;
      const char Base [] = { 5, 1, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3 };

      Leap = (Year & 3) || (Month > 2);
      return (Base[Month-1] + Day + Leap + Year + Year/4) % 7;
    }
     
     
    Last edited: Jun 26, 2008
  19. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    Mike,

    I can confirm your algorithm works correctly until 1st March 2100.

    I wrote a little excel sheet to confirm it.:D

    Mike.
     
  20. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    I breadboarded Mike's creation, going by the schematic he provided. Holy hell!!! That's a lot of wires to squeeze onto a little breadboard! As you can see, I gave up keeping it neat after a while. Just too many...
    mike_led002_800x600.jpg
    The switches switch GP0 and GP1 between programming mode and run mode.

    But it doesn't work!! :confused: Some LEDs light, but no action.

    Mike! How accurate is that schematic? It doesn't say the resistor values, so I did some educated guessing.
     
  21. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Sorry you're having problems.

    Transistors are 2N3904 NPN. Most people see that they're sourcing drivers and assume they're PNP (because the drawing is so fuzzy).

    I originally used 510 ohm base resistors and 20 ohm current limiting resistors on the rows but the board in the picture has been taken apart so many times that there are NO resistors at all at the moment. Bad practice I know. It just shows how forgiving this stuff is...

    Let me know how it goes?

    Mike
     
    Last edited: Jun 26, 2008

Share This Page