View previous topic :: View next topic |
Author |
Message |
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
Question on tone generation among interrupts |
Posted: Thu Jan 16, 2020 1:06 pm |
|
|
Hi all,
I am working in integrating code for a PIC18F45K20 with another developer where I am using his radio library object files so basically I have a set of defined functions from that library that I have to use for my application.
One of the things that his library has is a periodic interrupt function that gets called every 6 mS and it is using timer2 of the PIC the only thing I do during that interrupt is to call some of his radio synchronization routines. I am developing part of an interface that does all the non-radio things and one of the things that I have to do is generate audible tones ~2KHz and take care of a bunch of other visual indicators. Normally this would be a very simple thing to do as far as generating any audio tones I would tend to just use the PWM module which can run freely on its own without being bothered by other interrupts. Unfortunately, on this PIC the PWM module requires the timer2 module which is not available to me. So I am generating tones in one of two ways either programmatically in a for or while loop and using a interrupt from another timer where the tone is generated within the interrupt of that timer in my case I am using timer1 with an interrupt of 250uS to generate a 2KHz tone. The problem I am having is that whenever I generate a tone with a duration between 100 to 200 mS is that the tone sounds garbled because the periodic high priority 6mS interrupt is taking place, so the 2KHz audio is getting chopped at a 166.7Hz (6mS) so no one likes the way this tone sounds. Unfortunately I cannot shut down the timer2 interrupt nor can I use an external device to generate a tone and I cannot change the selected PIC for this design. I was thinking of perhaps using timer2 that is running the 6mS interrupt and whenever I am generating a tone that I could do it inside a while loop where I read the timer2 counter to a value, toggle the IO pin that drives the speaker ON then when the timer2 counter equals the desired period value of my tone I then toggle the IO pin back to OFF. As far as I know there is no latency influence on the timer2 interrupt when you read the counter registers. I have tried this and it appears not to work too well.
FYI this is the way the timer2 is been setup:
The PIC is running from an external oscillator at a frequency of 9.0000MHz
setup_timer_2(4, 224, 15); // 6mS interrupt
I am wondering if anyone in this forum full of some very smart people have some kind of creative idea or solution |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Jan 16, 2020 2:02 pm |
|
|
Use a second, 50 cent PIC as a 'gated 2KHz tone generator'. An I/O pin from the 'master' PIC tells it to 'beep or not'.
OK, so it costs a wee bit of money BUT far less than the R&D coding time !
Sure it's 'fun' to get one PIC to do everything IF you're retired and have nothing else to do but for a real project, the above is a real solution. |
|
|
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
|
Posted: Thu Jan 16, 2020 2:19 pm |
|
|
Thanks for the hint, actually that was my first solution, using a SOT23-5 pin PIC but unfortunately they don't want to add any more parts into the design and I am running out of ideas. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Jan 16, 2020 2:45 pm |
|
|
So they'd rather pay you hundreds if not thousands of dolllars to TRY to get a possible one PIC solution ?
If so they're not looking at the 'big picture' or 'bottom line'.
I see this with my 'proof of concept' projects, clients wanting a low cost product not understanding the high cost of R&D. I added a 'daughter' board to solve a nasty timing problem,needed xtal,caps,3 chips. Yes, they complained BUT it was a cost effective solution. 1 day R&D AFTER I'd spent a WEEK trying to do it 'their' way.
Perhaps there's another PIC, more powerful, more money. That probably will involve several days of R&D, rework PCB, testing,coding, $$$$$
Maybe, there's some clever coding you can do, but again , time IS money, and who says it WILL work ?
If, IF, this product is say 10,000 units then I can see it might be better for a 1 PIC solution. At ,100 units, it's $50 for the 2nd PIC, so 1/2 hr of my R&D time.Done....
I'll wager you've spent a LOT more time chasing this ....
and then there's the pots of coffee to consider !
Jay |
|
|
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
|
Posted: Thu Jan 16, 2020 2:52 pm |
|
|
This is going into a high volume consumer product that is quite small and they already have a molded enclosure, so they don't want extra parts at all. This is what I call a simple complicated product, sounds like an oxymoron!
You right I have had quite a few cups of coffee so far. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Jan 17, 2020 1:58 am |
|
|
As a question, why is Timer 2 'not available'?.
OK it's being used here for the 6mSec, but assuming there is another
timer available, it should be pretty simple to just use one of these for
the 6KHz event, then giving you Timer 2 for the PWM.
So the interrupt handler would just need to be moved to this other
timer, and it's routine would have to increment it's counter (since
timer 2 is the only timer that 'self resets' at a count). But a total of
perhaps three lines of code, and then you have the PWM available to
generate the tone.... |
|
|
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
|
Posted: Fri Jan 17, 2020 7:53 am |
|
|
Unfortunately for me, life is not so simple. I am working with another developer (something I will avoid in the future) and he is "hell-bent" in only using timer2 for his library application. I had originally suggested he use any other timer available for his library interrupt so I could use timer2 for PWM....but NO GO.
So timer2 is not existent for me. I am beginning to believe that there is no real way to fix this other than adding additional circuitry external to this PIC. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Jan 17, 2020 9:31 am |
|
|
sigh, I feel for you, caught between rock and hard case......
I hate to think what the REAL cost of this is......$$$$ |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Jan 17, 2020 9:31 am |
|
|
Aaargh!....
Another option, change PIC.
May of the slightly later chips have more than one timer selectable for
the PWM. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Jan 17, 2020 9:59 am |
|
|
yeah, that's not an option, I rechecked the OP....sigh. PCBs probably made, no coins to R+R the PIC.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Jan 17, 2020 10:01 am |
|
|
A real pity, since the PIC18F45K22, is pin compatible, and gives three
timers that can be used for the PWM. Only change needed in the code
would be that #PIN SELECT is needed to route the peripherals. |
|
|
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
|
Posted: Fri Jan 17, 2020 11:27 am |
|
|
Unfortunately, I was brought into this project when the hardware design was already developed by someone else. Normally I like to take the project and design both the hardware and firmware which makes my life easier. As the old proverb goes; "too many cooks spoil the broth"
I thank you all for your kind advice, over the years I have had great advice and gained some wisdom from the many talented people in this forum. |
|
|
|