Continue to Site

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.

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

My software PWM challenge...

Status
Not open for further replies.
There is no "does not work for me". Either it works for everyone or nobody.

It is the nature of computing.

You have some experts helping you here. I would listen to what they have to say.

I have simple PWM code for multiple LEDs that is just a nested set of loops if you want it. It uses a an 2 dim array to set the brightness for a series of steps. Sort of a simple light show.
 
Last edited:
OK, it wouldn't work for anyone then. ;)

I have, instead, followed your advice and bought some 32MHz PICs.
 
I think I am almost there, to be honest. I have hope anyway...

Barring the TMR0 interrupt experiment, at 4MHz, I have gone through all of the following, for 9 LEDs:

  1. 8-bit PWM, 255 loop counter with an "if then elsif" setting each LED on or off on each iteration = flicker. PWM routine measured as taking 25ms.
  2. 8-bit PWM, 255 loop counter but setting each LED on before the loop and only setting LEDs off inside the loop with a ">=" check. PWM routine measured as taking 20.9ms.
  3. 7-bit PWM, 127 loop counter, still setting each LED on before the loop and only setting LEDs off inside the loop with a ">=" check. PWM routine measured as taking 9.8ms.
  4. 7-bit PWM, 127 loop counter, still setting each LED on before the loop and then only setting a LED off inside the loop with an "=" check - so it only happens once. PWM routine then measured as taking 6.5ms.

I have learned a lot through this, most of it seems to be about code optimisation (which is a great thing to learn too) but the interrupt routine was like a mix of 8-bit PWM and method 4 above - setting on, then turning off when PWM = DesiredBrightness - I can assure you that I saw flicker at 4MHz. Even when setting the TMR0 to 127 at the end of the interrupt. And that was with a single LED, not 9 of them.

I never tried the TMR0 interrupt method with 7-bit PWM... but I'd kinda moved on by then.

I just need to try method 4 in the actual circuit, then I will report back. If that fails, then I hang my hopes on the 16F1823.
 
You have some experts helping you here. I would listen to what they have to say.

P.S. - no disrespect ever intended to the experts, I have taken on-board both main suggestions that were made to me (interrupt and high-speed oscillator) and moved them both forwards - the first by giving it a damn good go, and the second by purchasing some new hardware - from Holland! - as I couldn't find them on eBay (my usual source).
 
Last edited:
I'm not sure. I was saying I've followed the advice given, because I agree that it's high quality... but that empirical testing has eventually caused me to do things differently to that advice and that no shun should be inferred from my choosing to do that. I'm not looking for a forum-fight, I just want a resolution to my issue and I think there's hope in the way that I've now decided on doing things. Again, testing will prove that, or otherwise. I don't think I should be castigated and be accused of "doing things the hard way for some reason" just 'cos I chose a different way to do something.

I will say again - I actually went and tried TMR0 interrupt at 4MHz, as advised, and I observed flickering of the LED. I can't do it much simpler than "LED = on", "PWM = PWM + 1", "if (PWM == DesiredBrightness) then LED = off end if", "TMR0 = 127", "INTCON_TMR0IF = 0" can I? I did that and it flickered. If I've done it wrong, please do correct me, but what I think you're implying is that I refused (and continue to refuse) to take the advice given because of some innate stubbornness. I didn't. I appreciate all the advice and think it is high quality, but I saw a single LED flickering via TMR0 interrupt at 4MHz and 1:1 WDT and TMR0 = 127, so I then concluded that I have no chance with 9 LEDs.
 
...
I don't think I should be castigated and be accused of "doing things the hard way for some reason" just 'cos I chose a different way to do something.
...

Hippogriff- I believe I owe you an explanation of why I said that.

I said that in frustration as I had twice suggested the very simple and downright logical fix of adding a 20MHz xtal to your circuit! But you were continuing to talk about changing to PIC32 or ordering 16F1823.

I was not implying that your code method itself was "the hard way" it was in reference to the point that you wanted to continue struggling at 4MHz when most people would just throw in a fast xtal from their junkbox and have all probelms solved. Not order exotic PICs from other countries!

It was not meant as a personal attack, just my opinion that you were "doing it the hard way" and up to this point you have still not specified any logical, rational reason for not adding a 20MHz xtal or cheap 20MHz resonator and having your existing PIC instantly performing 5 times faster...
 
Mr RB - thank you for your comments. I was likely a little prickly myself when I read that comment, so my response(s) was out of proportion. I appreciate the assists you've provided. I learned something about interrupts - especially the 1:1 WDT and setting TMR0 to be greater than 0 at the end of the routine - so I am happy. I will report back to the forum if I have successfully stopped flicker on 9 LEDs at 4MHz when I finally get around to trying it - it shouldn't be too long, have just got a bit distracted with boring work.

As for crystals... would you believe I do not have any? It's true. I have never used them. That, in itself, would be an interesting learning experience, but one I've avoided up to now.
 
Ah wow you don't have any xtals? That took me by surprise! You can pull them out of old equipment like TVs and VCRs etc but the difficult part can be finding the two 22pF caps.

As a suggestion for "easy use" you can get 3-pin ceramic resonators, these are cheaper than xtals and have everything you need in one component. The two outer legs go to the two PIC osc pins, and the middle leg goes to ground. Nothing could be easier. :)

I bought a bag of 100 resonators; 3pin 20MHz for exactly this type of purpose, when you need the PIC to be at full 20MHz speed but don't need clock accuracy. PM me your mailing address and I'll mail you a few as a measure of forum friendship.
 
I'm all about easy use, for sure. I started PIC programming without a crystal - even though I bought several books and they always had them in, and I saw stuff on the Internet and they always mentioned them. It bugged me, at first, but as completed project after project went by (Mood Lamp, LED Strip, IR Receiver, now my artwork) I just found I'd never needed one. Then, it seems, doing too much with an iddy-biddy PIC (PWMing 9 LEDs) has brought this home to roost, maybe.

Where's "out there"? If it's outside the UK then I'd feel too bad taking you up on your kind offer... the postage would likely be more than the parts, so I should just go get some off eBay if I want to investigate, and I should. I must say, though, the offer is definitely taken in the spirit it was made... gracias!
 
I'm in Australia, don't sweat the postage they are tiny little things and will go in a standard letter size so it's very cheap. Seriously, I have a bag of 100 and I use maybe a few a year. PM me. :)
 
Reporting back to the forum...

I received my PIC 16F1823s and I believe I have got them running at 32MHz. I did this by setting "OSCCON_SCS = 0", "OSCCON_IRCF = 14" and "OSCCON_SPLLEN = 1". I have done a simple test like reported previously:

8-bit PWM, 255 loop counter with an "if then elsif" setting each LED on or off on each iteration. The PWM routine was measured as taking 25ms with a 16F684 running at 4MHz and I experienced flicker on the LEDs when it was all plugged together. For measuring purposes, though, I just ran it on a breadboard without any LEDs connected to the 9 pins.

I have now done the same with the PIC 16F1823... as expected (32 divided by 4 = 8) the PWM routine on the 16F1823 is measured as taking 3.18ms... 25ms divided by 8 = 3.125.

The real test is when I hook it up to the 3 RGB LED strips (not done yet) and when I incorporate all the other coding improvements we have discussed in this thread.

Sadly, though, I have got myself into a puzzling predicament with my PICKit 2 Programmer software.

When I first tried to program a 16F1823 it told me that it was an unsupported part. So I went to Microchip's site and I updated the Programmer - the About box now tells me it is running with:

Application Version 2.61.00
Device File Version 1.62.09
OS Firmware Version 2.32.00

This is fine for programming 16F1823s - but when I put in other PICs, like a 16F684 or a 12F683 it now tells me they are unsupported parts. When I select Manual Device Select from the Programmer menu, these PICs do not appear in the list.

Thankfully, I was paranoid and I had saved a backup of the existing "PK2DeviceFile.dat" and, when I put that file into the folder, and run the Programmer I get the 16F684 and 12F683 auto-detected again fine, but not the 16F1823.

With the old "PK2DeviceFile.dat" I get:

Device File Version 1.61.00

...from the About box.

Am I supposed to merge these .dat files somehow? Or should this have happened during the update of the software? Or have I done something extremely weird for it to get into this state?

Desperate to understand this a bit better.
 
Scratch the cry for help! I started reading Microchip's forums and saw someone advise, without a smirk, that just closing down and re-opening the PICKit 2 Programmer software a number of times might help... and, guess what?, I did that and it now auto-detects 12F683, 16F684 and 16F1823. So I'm happy and will remember for next time.
 
7-bit PWM routine takes 0.95ms at 32MHz, as opposed to 9.8ms on the 4MHz. All just as I'd hoped. Now time to set up the circuit properly again... let's see if flicker occurs now.
 
Congrats! :)

I don't think you'll see any flicker at 3.2mS per loop or 0.95mS per loop, even with some housekeeping delaying the loops they should be well above the eye's flicker frequency.

I still suggest you look at putting the PWM code in the TMR0 interrupt, for rock solid brightness levels. Even with no flicker if you manually do PWM between other code there is chance the other code will cuase brightness changes.
 
Final post to, probably, close this thread off... actually knocked-up another 'circuit' on Veroboard so I could test this properly and I can report that it all works very nicely. Not so much proof, here, but it's supposed to be...

Flickering LEDs (you can't tell, but they are):

**broken link removed**

And non-flickering LEDs (again, you can't tell, but they're not):

**broken link removed**

...and this is with the non-optimised 8-bit PWM code... used as a comparison test here. It can only get better now.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top