![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Einly Guest
|
Compose music using PIC? |
Posted: Sun Nov 10, 2002 3:13 am |
|
|
Dear all,
I read some articles say we can use PIC to compose music by just adjusting the output digital frequency and delay period. However my PCM compiler is the older version 2.7.1. Is that possible to compose music also?
I have tried using a transducer as the output of the sound and I compose the followig lines:
for i=1 to 100
{
delay_ms(1);
PORTB=0;
delay_ms(0);
}
By varying the delay period I can hear different sounds but it is quite terrible sound. Can you tell me how to design a good music? Erm... maybe you can give me an example for c,d,e,f,g,a,b,c+. Thanks a lot...
Yours sincerly,
Einly
___________________________
This message was ported from CCS's old forum
Original Post ID: 8728 |
|
![](templates/subSilver/images/spacer.gif) |
No_Fear Guest
|
Re: Compose music using PIC? |
Posted: Sun Nov 10, 2002 5:34 am |
|
|
Dear Einly,
Most dos programmers know that producing sound by pc speaker is easy.With little effort you can convert them to pic.
First of all, you need to know how to produce sound.To produce a signal beep, you should vibrate speaker.If you know how to vibrate, then no problem to hear it.To vibrate a speaker you should give a signal (e.g. square wave signal or triangular signal).Let me to give you an example :
while(1)
{
output_high(PIN_A0);
DELAY_MS(100);
output_low(PIN_A0);
}
This code makes a square wave signal on pin_a0.When you connect it to a speaker you should hear a signal.So, you see, it is very easy to produce a sound.If you add some analog devices (for example wave shapers, filter) you can hear different tones.You asked for some notes.When you change the delay time in code you'll hear different sounds.If you look at the web for notes you'll see every note has a special frequency.For example (the number is not real i'm inventing) note c has 447 hz freq.If you rewrite code under these freqs you'll have notes and you can play every notes if you want.
If you have questions don't hasitate to ask.
Rgrds, No_Fear
:=Dear all,
:=
:= I read some articles say we can use PIC to compose music by just adjusting the output digital frequency and delay period. However my PCM compiler is the older version 2.7.1. Is that possible to compose music also?
:=
:= I have tried using a transducer as the output of the sound and I compose the followig lines:
:=
:=for i=1 to 100
:={
:= delay_ms(1);
:= PORTB=0;
:= delay_ms(0);
:=}
:=
:= By varying the delay period I can hear different sounds but it is quite terrible sound. Can you tell me how to design a good music? Erm... maybe you can give me an example for c,d,e,f,g,a,b,c+. Thanks a lot...
:=
:=
:=Yours sincerly,
:=Einly
:=
:=
:=
:=
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 8731 |
|
![](templates/subSilver/images/spacer.gif) |
R.J.Hamlett Guest
|
Re: Compose music using PIC? |
Posted: Sun Nov 10, 2002 5:55 am |
|
|
:=Dear all,
:=
:= I read some articles say we can use PIC to compose music by just adjusting the output digital frequency and delay period. However my PCM compiler is the older version 2.7.1. Is that possible to compose music also?
:=
:= I have tried using a transducer as the output of the sound and I compose the followig lines:
:=
:=for i=1 to 100
:={
:= delay_ms(1);
:= PORTB=0;
:= delay_ms(0);
:=}
:=
:= By varying the delay period I can hear different sounds but it is quite terrible sound. Can you tell me how to design a good music? Erm... maybe you can give me an example for c,d,e,f,g,a,b,c+. Thanks a lot...
:=
:=
:=Yours sincerly,
:=Einly
You will not get very far, using this type of approach. The problem here, is that the loop itself, adds a (hopefully fixed) constant to the delay time involved, making the timings pretty foul, and the delay_ms counter, does not give the sort of resolution needed for audio really.
You could get closer, by measuring the error, and using delay_us, with a value from a 'look up' table, for each note, to give reasonable frequencies.
A much better approach is to use a chip with a CTC included (most of the 'larger' chips have this), and program this to give accurate frequencies. The beep on a normal PC, uses this ability (with the CTC on the PC), and can be programmed to give quite reasonable tones. You should also consider adding a significant amount of audio filtration (a square wave is not the nicest of audio tones).
You may also need to chose a non standard crystal frequency to give better values for the tones. However (for instance), using a 16F family chip, and a 20Mhz crystal, you could program Timer2, to use the *4 prescaler, and interrupt every 40 counts (arbitrary values, but chosen so that the interrupt handler, should be shorter than the interrupt interval), to give a 32uSec interrupt. Then in the interrupt, a look-up table and counter can be used to generate an output 'tone' on a pin, with something like:
int tone_reqd;
#bit OPBIT=PORTB.0
const int tones[] = { 70,74,76,83,88,94,99,105,112,118,126,133,141 };
#INT_TIMER2
void tick(void) {
static int tick;
static int toggle;
if (tick) --tick;
else {
tick=tones[tone_reqd];
toggle^=1;
OPBIT=toggle;
}
}
main() {
setup_timer_2(T2_DIV_BY_4,40,1);
enable_interrupts(TIMER2);
enable_interrupts(global);
tone_reqd=1;
while (1) ;
}
Changing the value of 'tone_reqd' (in main), would select a different output rate. The actual 'tuning', would depend on the values stored in the table 'tones', and the numbers I have put, are some guesses, for possibly suitable values, however these are 'off my head', so will probably need changeing to get reasonable notes. This version outputs the tone on pin B0.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 8732 |
|
![](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
|