Alright, welcome to the next microcontroller tutorial with the PIC10F200.
Today we're actually going to be making some music.
So a couple of tutorials ago, we talked about how we didn't really need a whole lot of precision
to blink the LED on and off.
Well, this is a tutorial where we do need more precision.
So I don't know if you know much about music, but you're going to learn more.
Now, music basically is just sound waves.
Well, notes, specific notes are sound waves oscillating at a particular frequency.
And I think looking at a piano is the best way to visualize it.
And you have all the white keys in the black keys.
And you'll notice there's a pattern there.
And on the white keys, there are seven notes, A through G.
And then you have the black notes, which are some of the half notes.
Even though you can have like a half step between E and F.
There is a whole step between C and D, stuff like that.
The interesting thing here is that, between A and A, there is a doubling of the frequency
on so let's say and this is not right at all.
So let's say, this "A" is 100 hertz.
The next "A" up is 200 hertz, the next "A" up is 400.
The next "A" up is 800 hertz.
So with that, you can get a very clear mathematical relationship between the notes and the the
frequency.
And so that is exactly what we're taking advantage of here.
So instead of blinking LED, we're going to be blinking a speaker.
And by causing that speaker to oscillate back and forth at the frequency we want it to,
that is going to make our music.
It may be a challenge, you think, holy cow, how am I going to figure out what the frequency
is the period, the oscillations, all of that is for each of these notes.
Sergey did that work for us, so go to CircuitBread.com, go to the written write-up for basically that
reference material right there where he has all of the notes, what frequency they need
to oscillate at, and depending on what octave they're in as well.
The only thing that I want to point out there is that on the table that he has, there is
one column that says period and that might not make a whole lot of sense.
But when you think about it, if we want to oscillate something 100 times a second.
And then we say, okay, oscillates 100 times a second, that's going to last one second.
If we oscillate something else 200 times a second, if we oscillate that 100 times, that
only takes a half second.
So you need to make sure that to make your notes last the same length as you're creating
your musical masterpiece, that they are oscillating for the same duration of time.
And that's what that period is, in the table that he has.
It says, well, as the frequency goes up, you need to oscillate more to make it so it still
has the same overall length of time.
So if you play an A here and an A there for 100 cycles, this one's not one second, and
this one's not a half second.
So just want to make sure that's nice and clear.
But again, Sergey did all that work for you.
So count yourself lucky.
Thank you, Sergey.
Appreciate it!
Really quick.
I do want to mention this is a magnetic speaker.
So this is only measured at eight ohms.
You cannot drive this off of your 20 milliamp output from your microcontroller.
So, in my case, I just happened to have an NMOS MOSFET lying around that I connected
the gate to the output pin.
And then I put the speaker in series between VDD, the drain, source and ground.
And so as the pin oscillates, it oscillates the gate on the MOSFET, which then basically
makes it so the speaker conducts and doesn't conduct.
So that's how it works.
If you have a piezoelectric buzzer that can do a wide variety of frequencies, then it's
going to be a lot higher impedance and you can go straight to the pin.
But otherwise, if you're going to use basically your typical magnetic speaker like this, you're
going to need some sort of driver circuit in there.
And we have that circuit up on CircuitBread.com, who knows who might even flash it up sometime
while I was talking about this so you can see exactly what you're supposed to do.
But either way, if you try and drive the speaker straight from your pin, it's not going to
work.
Okay, let's get into the program itself.
So as we're looking at the program, you can see this is a pretty long program.
This is by far our longest program.
It is 183 lines.
But a huge amount of it is just defining the notes.
So as we break it down, we have our first 13 lines, which again are just our initialization
getting the microcontroller set up to run the program.
And then from 14 to 39 is the actual program itself, the main routine, which as you can
see, it's just a bunch of calls, but it's called E2 called D#2, call E2, call D#2.
If you are really good at reading music, and you can make a lot of connections right there,
you're going to realize that this is actually "Für Elise" - this is what Sergey wanted
to do.
So you have that E, D#, doo doo doo doo doo doo doo doo doo.
And that is basically all we're able to fit in here.
But we're calling each note individually.
As you also go through you will see that, like lines 23 through 25 calls A1 3 times
and that is because each one of these is an eighth note.
And if you want let's say a quarter note or a dotted quarter note in the case of 23 through
25 you just call the same thing three times, and it plays for three lengths of time.
And that's how you get a longer note.
Now, this is kind of fun, because I actually did something very, very similar to this about
10 years ago, with a much more powerful microcontroller.
It was a PIC16F877A.
And I did it in C, but it's the same concept of - you define the notes, you define the
length, and then you just call those notes as much as you need.
So it's really interesting to see Sergey doing practically the same exact thing in assembly
as I had done in C. But his is obviously a much, much smaller program than mine was because
mine was huge, and that was 10 years ago, and I wasn't really worried about getting
a good size of the program, making sure it fit.
So with that just random, completely useless aside, let's move through this.
I do want to point out in line 38 is a new command and it's pretty straightforward.
SLEEP.
And that is where the microcontroller says I have nothing to do.
And it goes to sleep.
It shuts off the oscillator.
It's stops doing anything.
And by doing that it cuts its power consumption by about 1/500.
So instead of being about one milliamp, it's two micro amps of current consumption.
So it's a huge decrease.
And if you have nothing better for it to be doing, that is a great way to save power,
in general.
This explanation is not going to be very long because there's so much repeated stuff.
So let's just go over the E2 sub routine, because everything else is just the exact
same with different periods and different length of time that it oscillates.
So on line 41, E2, we see that we move the number of periods to play from literal to
our working register, have 255, decimal 255, and then move that into the periods and then
we loop E2 the specified number of periods that we need to.
The only thing here that you have not seen before and might be kind of confusing, is
the XORWF, which, if you haven't done bitwise operations, we've mentioned that in the past,
I highly recommend you look into bitwise operations because they will be useful in any programming
language you do, and they are super, super interesting.
So basically, this is just doing an XOR with the working register and the F register.
So that XOR says that of all of the bits if there is a one in one, but only one of the
two, return one.
If they're both ones, if they're both zeros, then return a zero.
So it's that basic XOR function, but between the working register and the F register, and
so by doing that XOR between the two, that's basically how instead of checking to see Hey,
is it one now?
Change it to zero.
And then having to check Hey, is it zero now?
Change it to a one.
This does all of that in one thing, and basically, hey, whatever it is, flip it back and forth,
back and forth.
So the XORWF is very, very useful in making a single line so you can toggle bit so that's
going to be something we see quite a bit and use all the time for any sort of toggling
requirements.
And, that is very good to know.
But that that's really it.
Once you toggle it.
You load in your delay times.
You do the delay.
You keep on doing this over and over again until you've hit your length of time you've
needed to play this note.
And then you jump back up to the main routine, you do the loop, you go back through and do
it over and over again.
So again, this is a very long and slightly tedious, perhaps, program.
But at the same time, it's really cool to go from a blinking LED to actually making
some music.
So this being so cool, let me load it up right now we'll get it to work so you can see exactly
what it's supposed to sound like.
Load it from the IPE... and... here we go.
And that's it!
The fun thing is, is now you can do basically any song you want!
Within reason.
Because of the limited amount of space, you can't actually create a note for all the notes
in a two octave range, we just run out of space.
So I think Sergey you can only do 16 notes or something like that I don't recall off
the top of my head.
So as always, Sergey has some homework so that you can really, truly learn this and
make your own modifications so that you can get into the nuts and bolts on this.
And that homework is simply - make your own song, choose whatever your favorite song is,
whether it's the Star Wars theme, or some other non-geeky theme song or just something
that is near and dear to your heart, put it in here, change it up and create your own
masterpiece.
As it is, I hope you've enjoyed this video that you've learned a lot.
If you did, like it, subscribe to our channel, all that good stuff and we'll catch you on
the next one.