# PIC16F877A -

#### AleMeirinho

Joined Sep 29, 2021
4
Hello

So I need the code in C language, to make a piano toy reproduces the seven melody notes.

The microcontroller used is a PIC16F877A, 4Mhz crystal oscillator, and the following frequences:

Do - 32,7032 Hz
Re - 36,7081 Hz
Mi - 41,2035 Hz
Fa - 43,6535 Hz
Sol - 48,9993 Hz
La - 55,0000 Hz
Ti - 61,3750 Hz

I really appreciate any help!

#### BobTPH

Joined Jun 5, 2013
4,049
Those frequencies are the low end of the piano keyboard. Are you planning on playing this through a subwoofer?

Bob

#### AleMeirinho

Joined Sep 29, 2021
4
Those frequencies are the low end of the piano keyboard. Are you planning on playing this through a subwoofer?

Bob

Actually, this is only an exercise of my "Microcontroller Course", thats not a project.

I just have to solve the exercise by elaborating the C code.

I already found one, but it's for Arduino, not PIC16F877A

The exercise only asks for a simple code, wich plays each note when the key is pressed, according to each buzzer frequency

#### dl324

Joined Mar 30, 2015
13,532
I already found one, but it's for Arduino, not PIC16F877A
this is only an exercise of my "Microcontroller Course", thats not a project.

#### AleMeirinho

Joined Sep 29, 2021
4

Yes, this exercise is part of the grade.

About adapt to the pic, I just have some doubts about it... because the Arduino input ports are A0, A1, A2, A3, A4, A5 and 3. The output port is 8.

Here is the Arduino code:

void setup ( )
{
pinMode (A0, input);
pinMode (A1, input);
pinMode (A2, input);
pinMode (A3, input);
pinMode (A4, input);
pinMode (A5, input);
pinMode (3, input);
pinMode (8, output);
}

void loop ( )
{
if (digitalRead (A0) == HIGH) { // If button connected to A0 is pressed
tone (8, 264, 100); // Note Do (264 Hz)
if (digitalRead (A1) == HIGH) { // If button connected to A1 is pressed
tone (8, 297, 100); // Note Re (297 Hz)
if (digitalRead (A2) == HIGH) { // If button connected to A2 is pressed
tone (8, 330, 100); // Note Mi (330 Hz)
if (digitalRead (A3) == HIGH) { // If button connected to A3 is pressed
tone (8, 352, 100); // Note Fa (352 Hz)
if (digitalRead (A4) == HIGH) { // If button connected to A4 is pressed
tone (8, 396, 100); // Note Sol (396 Hz)
if (digitalRead (A5) == HIGH) { // If button connected to A5 is pressed
tone (8, 440, 100); // Note La (440 Hz)
if (digitalRead (3) == HIGH) { // If button connected to 3 is pressed
tone (8, 495, 100); // Note Ti (495 Hz)
}

So, according to the adapting, it's just replace the values in Hz and the input and output ports?

#### dl324

Joined Mar 30, 2015
13,532
So, according to the adapting, it's just replace the values in Hz and the input and output ports?
Assuming your PIC libraries have the tone function.

The code you pasted seems to be missing closing curly braces for the if statements.

#### Ian Rogers

Joined Dec 12, 2012
952
Those frequencies are the low end of the piano keyboard. Are you planning on playing this through a subwoofer?

Bob
Are you sure!! Those frequencies are far too high... middle C is 262 hertz.... 32,7032 is past dog hearing..

#### Ian Rogers

Joined Dec 12, 2012
952
Tone is available on Arduino but you'll need to write it for C on pic...
the easiest way is to set up an array that holds the frequency of the note and the timer setting to play that note.

A simple sound is a square wave out of a port pin into a speaker...

#### AleMeirinho

Joined Sep 29, 2021
4
Tone is available on Arduino but you'll need to write it for C on pic...
the easiest way is to set up an array that holds the frequency of the note and the timer setting to play that note.

A simple sound is a square wave out of a port pin into a speaker...
Ignoring the fact of the frequencies are far too high... can you help me with the C code?

#### dl324

Joined Mar 30, 2015
13,532
Are you sure!! Those frequencies are far too high... middle C is 262 hertz.... 32,7032 is past dog hearing..
I assumed the OP was using comma instead of decimal points. Otherwise, the comma is in the wrong place.

#### dl324

Joined Mar 30, 2015
13,532
Ignoring the fact of the frequencies are far too high... can you help me with the C code?
Your question has been moved to Homework Help. For homework, we can only offer guidance. You need to do most of the work.