Nice stuff, James. Good point about the filter. I would argue for using the simplest possible output (unfiltered) first, trying it, then going with the simplest possible filter (an RC filter) trying that, and then go for the complex but better performing filter if needed. I find when I take that approach, I get the project off the ground and working, then have more motivation to get the trickier details done. If I design the whole thing out to be the finest from the start (the way I might do a commercial project) it takes so much time that I am on to some other project before the thing is finished. Hobbies are never done, you know. So far this sound to me like a rainy afternoon construction project. I may even have the hardware I described laying around from something else. -- Lawrence Lile "You can tune a piano, but you cannot tuna fish." James Nick Sears Sent by: pic microcontroller discussion list 01/07/2004 12:40 PM Please respond to pic microcontroller discussion list To: PICLIST@MITVMA.MIT.EDU cc: Subject: Re: [PIC:] Frequency dividing to 440 Hz As an almost lifelong guitar player I think that A4 = 440 is pretty standard these days. One thing you will run into however is that while you can tune just about every instrument you can't tune the piano (not easily and on the spot like a guitar, etc anyway) so often times everyone will tune to whatever it happens to be tuned to. Many commercial tuners offer a variable reference frequency - maybe you should look into this too. It shouldn't be that hard, right? Let's start with a 440Hz square wave. To generate this you need to toggle a bit 880 times per second. Say you run a 20MHz Xtal so 5MHz Inst. Clk.: 5e6 / 880 = 5681.8181.. ~= 5682 cycles (within .0033% plus the tolerance of you oscillator which should be insignificant for this purpose if it is working properly) With no prescaler on TMR1 and the register loaded with zero at the start you get an interrupt every 0x10000 or 65536 cycles. So if you preload the TMR1 register with 65536 - 5682 = 59854 and as the first step in your TMR1 ISR reload the timer with that same value you will get interrupts at 879.972Hz. In reality you also would have to account for the few cycles it takes to get into the ISR and to the point where this happens but this should just mean adding a constant value to the TMR1 preload depending on the specifics of your code (Add the number of cycles it takes to get from interrupt fire to TMR1 reload). The stopwatch feature in MPLAB can be good for getting this sorted out. Then you can make the TMR1 preload value a variable which is changed when the user makes an appropriate input to the device. One cycle difference changes the frequency by .0774 Hz at 440Hz - (this function is of the form y = 1/x and so is not linear). So you could just precalculate a lookup table as in .. "439.91",5683 "439.99",5682 "440.06",5681 .. etc. and use this to feed the LCD and the TMR1 preload based upon a position determined by the user's selected frequency. With this setup it would be very easy to tweak the frequency to say ~442Hz or you could even make an EADGBE guitar tuner or a chromatic tuner if you were so inclined by precalculating the TMR1 values and including them in your lookup table. Combining these two features would be more work though since you can't just offset the A440 referenced TMR values by the same amount across the scale. (You need less adjustment at low frequencies and more at higher frequencies so you'd either need another lookup table or some division routines, etc.) For a variable A4 alone though it should be pretty easy. Once you have your ~440Hz square wave, remember that an ideal square wave only carries energy at multiples of the fundamental. So you need a filter that passes 440 (and a little cushion for frequency adjustment and component tolerances of course) and cuts 880,1320,1760, etc. If you could make a perfect filter that was a brick wall at say 500Hz then you would be left with a perfect 440Hz sine wave. Perfect, perfect, perfect. Yeah right. In reality though I think if you had a good steep filter you could get pretty close. Maxim makes various filter IC's that I have not actually used but I think they just require a few external components and the IC to make 8th order filters and the like. Try: http://para.maxim-ic.com/ss.asp?FAM=Filt&TREE=Filters&HP=Filters.cfm&LN=&SORD=125+122+1052+124&FT_125=202&FT_122=194&FT_1052=12374&FT_124=198&Dest=Search+%26+Update For example look at the MAX7400/MAX7404 (8th order elliptic, switched cap filter - http://pdfserv.maxim-ic.com/en/ds/MAX7400-MAX7407.pdf). In the datasheet it is spec'ed that at 1.5 * Fcorner the output is down at least 75dB (typically 82). If your corner is 500Hz then at 750Hz (before the 2nd harmonic) you are down ~80dB. Should make a pretty decent representation of a sine wave. I would definitely filter the output. It is definitely harder to tune a guitar accurately with distortion on (basically morphs the guitar signal to be more like a square wave) and I wouldn't doubt that the effect would be similar to trying to tune to a square wave. Good luck - sounds like a fun project. Nick ----- Original Message ----- From: "Koen van Leeuwen" To: Sent: Wednesday, January 07, 2004 10:47 AM Subject: Re: [PICLIST] [PIC:] Frequency dividing to 440 Hz > I haven't heard of other "standards" before other than the A4=440 Hz. > I have read somewhere that in Mozart's time a typical A4 was 450 Hz, but it > has been standardized in 1939 as 440 Hz (Source: > http://www.boulder.nist.gov/timefreq/general/glossary.htm > ). > That page also states that the smallest noticable difference is 0.28%, so that > gives at least some headroom :o) > > So I should make my tuner tunable for other scales too :o) > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads