![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
FunkyLabRat
Joined: 13 Feb 2012 Posts: 10
|
Help with making a 16F628A (or other chip) metronome! |
Posted: Thu Mar 21, 2013 12:42 pm |
|
|
Hi all,
I'm trying to make a metronome using the internal oscillator of a 16F628A. I have the option of using an external crystal if I should need one, but to keep things simple I'm trying it this way first.
After experimenting a lot with the timers on the 16f628a and their overflow rates and pre-scalers, I've had no success. I've succeeded in getting a steady 'second' (60 BPM) though, but found no working way to divide it to get other BPM rates.
I'm sure there's a working way, but after months of experimenting I really hope someone can push me in the right direction. I won't give up easily, but this is starting to drive me crazy...
So, here's my latest try which should work but doesn't:
Instead of using the setup_timer_1 and timer interrupts, I've wrote the program below, which is using #use timer instead.
I've discarded of formulas to check the ticks for the BPM, and now just try to count every 500ms (= 120 BPM). When I try it, the flickering LED is not even close to 120 BPM; it's way too slow.
Code: | #include <16F628A.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(int=4000000)
#use timer(timer=1, tick=1ms, bits=16, ISR) // TICK Time is: 1,024 ms. Precise enough?
int16 ticks;
void main()
{
enable_interrupts(GLOBAL);
while(TRUE)
{
ticks = get_ticks();
if (ticks >= 500) { // 120bpm = 500ms per beat count
output_toggle(PIN_A1); // toggle LED on pin A1
set_ticks(0); // reset ticks counter
// set_timer1(0); //necessary?
}
}
}
|
Any suggestions, or a working metronome code? Thanks in advance! I'm really trying. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 21, 2013 1:26 pm |
|
|
Google ? As the first resource in code searches ?
Search Google for:
Quote: | metronome "#use delay" "#fuses" |
|
|
![](templates/subSilver/images/spacer.gif) |
FunkyLabRat
Joined: 13 Feb 2012 Posts: 10
|
|
Posted: Thu Mar 21, 2013 2:11 pm |
|
|
Thank you, but I've tried for many months to accomplish this, and of course I've used Google for searching and reading documents for a lot of hours on the subject.
There was one search result I haven't read with your search terms. I'll check that one out.
I want to make a steady, very accurate metronome. Most of the code I've found is not useful.
Edit: That specific search result seems to be useful. I'm going to study it. Thanks ;) |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Thu Mar 21, 2013 3:08 pm |
|
|
You'll have to use an external crystal or oscillator if you want accuracy. The internal oscillator may not be good enough for you, though a metronome shouldn't need .001% timing !!
I did Google using PCM programmer's keywords and found several articles, including one with CCS C code, in a PDF. Seemed reasonably useful....
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Mar 22, 2013 3:49 am |
|
|
I can't start to answer your question, it's too open ended.
I think you need to sit down and work out a specification. :-
1) School/hobby/commercial project / professional music studio?
2) Accuracy, stability.
3) Range.
4) Resolution.
5) User input.
6) Rate indication.
I can make a guesses at the above, but will waste my time and yours offering solutions.
Code: |
1) Should be self-explanatory.
2) It's your project. Look up the internal R/C time & temperature stabilities in the data sheet.
Then decide if you need to go to a ceramic resonator or crystal.
3) What are minimum and maximum BPM required?
4) Are you wanting to go in steps of 0.5, 1, 2BPM, 1% steps, dunno?
5) How will the user change the BPM?
(keypad, single/multi-turn pot, rotary switches, up/down buttons.
6) How will user know what BPM setting is?
(depends to some extent on 5, big dial, LCD, don't care)
|
Or, as has been suggested, try google.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Fri Mar 22, 2013 4:41 am |
|
|
It's worth perhaps saying, that _modern_ metronomes generally will all use crystals, and be many times more accurate than is really needed.
Historical 'mechanical' metronomes were considered 'good', if they offered +/-1% accuracy, and the internal oscillator of the 628A, can easily achieve this, _if the supply rail is good_. The quoted accuracy is just on +/-1%, if the supply is regulated at 3.5v. However if the supply is (say) 5v, the warranted accuracy is only +/-2%, which is in the 'fair' level for a traditional metronome.
Most chips will be well inside this range, but you are bound to get some that are on the limits.
Then separately, re-consider the approach a little. The standard beat rates normally wanted from a metronome, are:
40,42,44,46,48,50,52,54,56,58,60,63,66,69,72,76,80,84,88,92,96,
100,104,108,112,116,120,126,132,138,144,152,160,168,176,184,200,208
Now if going electronic, you might also want offer options to accent on the second, third, and fourth tick. Now, these are beats/minute, so you can count with a high frequency tick, and get very precise levels easily, but take advantage of an interrupt to do this, and use a look-up table to give the counts wanted. Look at the example RTC:
<http://www.ccsinfo.com/forum/viewtopic.php?t=26177&start=0>
Now, note how this works by subtracting a 'count', and triggering the 'event' (seconds here), when the ticker is less than the count. The same exact approach can be used with a look up table of count values (load these into a variable in the main when the new beat rate is selected, don't access the array in the interrupt), Then have the interrupt just fire the output 'beep' (ideally setup hardware so the noise is something like a NE555, triggered by a mono-stable multivibrator, so a single pulse on an output wire generates the 'tick' sound - or something like a 12F675 doing this).
The key point is that this algorithm gives precise long term accuracy, even if the individual 'ticks' can drift by up to one interrupt interval, and the tick rate can be constant, yet give you the variability needed in the output. Have a second output triggering a different duration on another monostable, and you can trigger this instead of the first monostable on every 'n' beats to give the accenting.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Mar 22, 2013 5:29 am |
|
|
That's two of us asking you to produce a detailed spec.
And you've been given a good steer.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|