• AVR Freaks

AnsweredHot!Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer

Author
Prashant Nayak
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/09/24 00:24:23
  • Location: 0
  • Status: offline
2019/11/11 00:08:10 (permalink)
0

Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer

Hi,
 
   I am an Mtech Student from Allahabad India. I am trying to workout a device on PIC16f526 to play Happy birthday song on PIC-16f526 using XC8 and have connected a PiezoBuzer 3.8khz on RC3 of PIC16f526, also there is one such button that need to be active to turn off the music or turn on the music and a music change button(Change button :not very highly required). Ive tried searching for some code but none worked with PIC 16f526 can anyone help me in writing a code for such, Ill be highly obliged.
#1
katela
Super Member
  • Total Posts : 1460
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 00:22:55 (permalink)
+1 (1)
Which code did you find that worked with a different PIC?
Maybe you should start there, try to modify it to work with your PIC16F526. The forum might assist with that code instead of writing it for you from scratch.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#2
NKurzman
A Guy on the Net
  • Total Posts : 18038
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 01:00:56 (permalink)
+1 (1)
First make a tone with your PWM.
Then make a tone for N milliseconds.

Goggle the notes for the music.
Then Google the frequency to music notes.
Then assemble the pieces.
#3
Ian.M
Super Member
  • Total Posts : 13268
  • Reward points : 0
  • Joined: 2009/07/23 07:02:40
  • Location: UK
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 06:14:22 (permalink) ☼ Best Answerby Prashant Nayak 2019/11/29 01:45:20
+2 (2)
PIC16F526 is baseline so no PWM modules, no Timer 1 and CCP and no NCO.  That means there is *NO* possibility of background or hardware tone generation.  Therefore your tone generator routine must bit-bang the output pin with variable software delays for the high and low times to get the required note, and counting cycles for the note duration as there are no long period hardware timers.
 
If you have *ANY* option to choose a different non-baseline PIC, do so immediately . , . 
Ideally choose one with a 16 bit PWM module or a NCO, but even a 16 bit timer and a 'classic' CCP module would be a big step up!

--
NEW USERS: Posting images, links and code - workaround for restrictions.
I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
#4
du00000001
Just Some Member
  • Total Posts : 3234
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 06:31:03 (permalink)
+1 (1)
The "PiezoBuzzer 3.8 kHz" might not fit as well: 
from this denomination I'd expect a buzzer that's sounding a 3.8 kHz wave when powered. These are usually not applicable for random sound generation.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#5
Link
Junior Member
  • Total Posts : 91
  • Reward points : 0
  • Joined: 2003/11/07 12:48:40
  • Location: Leeds, UK
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 08:39:09 (permalink)
0
You need to implement:
1 x millisecond timer (or division thereof)
1 x frequency based tone generator (1 octave range minimum)
1 x table of frequencies, note-lengths, note delays
 
frequency=264 length=250ms;
delay 500ms;
frequency=264 length=250ms;
delay 250ms;
frequency=297 length=1000ms;
delay 250ms;
frequency=264 length=1000ms;
delay 250ms;
frequency=352 length=1000ms;
delay 250ms;
frequency=330 length=2000ms;
delay 500ms;
frequency=264 length=250ms;
delay 500ms;
frequency=264 length=250ms;
delay 250ms;
frequency=297 length=1000ms;
delay 250ms;
frequency=264 length=1000ms;
delay 250ms;
frequency=396 length=1000ms;
delay 250ms;
frequency=352 length=2000ms;
delay 500ms;
frequency=264 length=250ms;
delay 500ms;
frequency=264 length=250ms;
delay 250ms;
frequency=264 length=1000ms;
delay 250ms;
frequency=440 length=1000ms;
delay 250ms;
frequency=352 length=500ms;
delay 250ms;
frequency=352 length=250ms;
delay 250ms;
frequency=330 length=1000ms;
delay 250ms;
frequency=297 length=2000ms;
delay 500ms;
frequency=466 length=250ms;
delay 500ms;
frequency=466 length=250ms;
delay 250ms;
frequency=440 length=1000ms;
delay 250ms;
frequency=352 length=1000ms;
delay 250ms;
frequency=396 length=1000ms;
delay 250ms;
frequency=352 length=2000ms;
delay 250ms;
#6
NKurzman
A Guy on the Net
  • Total Posts : 18038
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 09:46:04 (permalink)
0
The last time I did this I found is sounded better with a short delay between the notes.
#7
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/11 12:34:27 (permalink)
0
link's code has a 250ms delay between most notes, with an occasional longer delay :)
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
Prashant Nayak
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/09/24 00:24:23
  • Location: 0
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/12 03:03:05 (permalink)
0
So this is the code that is working with PIC16f88A. Ive tried running this same code on 16f526 but guess may be I am out of timers or doesn't have enough space on the controller to fit in the program. can anyone help me extracting out the code for 16f526 from this code :

#include <xc.h>
#include "conf.h"
#define _XTAL_FREQ 4000000
#define TMR2PRESCALE 16
// TONES ==========================================
#define c 261
#define d 293
#define e 329
#define f 349
#define g 392
#define a 440
#define b 493
#define C 523
#define D 587
#define E 659
#define F 698
#define G 784
// Define a special note, 'R', to represent a rest
#define R 0
// ==================================================
int tempo = 2000;
int current_tone = 0; //current playing note
int current_tone_duration = 0; //current note duration
void delay_ms(unsigned int milliseconds) {
while(milliseconds > 0) {
milliseconds--;
__delay_us(990);
}
}
void SetupClock() {
OSCCONbits.IRCF = 0b110; //set internal oscillator to 4 Mhz
OSCCONbits.SCS = 0b00; //use oscillator defined by FOSC
}
void PWM1_Init(long desired_frequency) {
PR2 = (_XTAL_FREQ / (desired_frequency * 4 * TMR2PRESCALE)) - 1;
current_tone = desired_frequency;
}
int PWM_Max_Duty() {
return (_XTAL_FREQ / (current_tone * TMR2PRESCALE));
}
void PWM1_Duty(unsigned int duty) {
if (duty < 1024) {
duty = ((float) duty / 1023) * PWM_Max_Duty();
CCP1X = duty & 2;
CCP1Y = duty & 1;
CCPR1L = duty >> 2;
}
}
void PWM1_Mode_On() {
CCP1M3 = 1;
CCP1M2 = 1;
}
void PWM1_Start() {
PWM1_Mode_On();
#if TMR2PRESCALE == 1
T2CKPS0 = 0;
T2CKPS1 = 0;
#elif TMR2PRESCALE == 4
T2CKPS0 = 1;
T2CKPS1 = 0;
#elif TMR2PRESCALE == 16
T2CKPS0 = 1;
T2CKPS1 = 1;
#endif
TMR2ON = 1;
}
void PWM1_Stop() {
CCP1M3 = 0;
CCP1M2 = 0;
}
void playTone() {
//TODO: The Tone R is not implemented yet.
if (current_tone > 0) {
PWM1_Init(current_tone);
PWM1_Duty(511);

delay_ms(current_tone_duration);
PWM1_Duty(0);
}
}
int main(int argc, char** argv) {
short melody[] = {g, g, a, g, C, b, g, g, a, g, D, C, g, g, G, E, C,
b, a, F, F, E, C, D, C};
char beats[] = {8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4,
4, 4, 8, 8, 4, 4, 4, 2};
char MELODY_LENGTH = sizeof (melody) / sizeof (melody[0]);
SetupClock();
TRISBbits.TRISB3 = 0; //set Pin B3 as output
PORTB = 0x00;
PWM1_Init(500); //exact value doesn't matter at all, just not 0
PWM1_Duty(0);
PWM1_Start();
while (1) {
for (int i = 0; i < MELODY_LENGTH; i++) {
current_tone = melody;
current_tone_duration = tempo / beats;
playTone();
delay_ms(tempo/200);
}
__delay_ms(2000);
}
return 0;
}
#9
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/12 03:18:10 (permalink)
+3 (3)
What are your config settings?
That's one reason I don't recommend putting them in a separate file, then anyone reviewing your code has to ask for them.
 
Have you actually tried compiling this code with the PIC16F526 selected?
What errors did you get?
 
 
As IanM pointed out,  the PIC16F526 does not have a CCP/PWM module, or a TMR2, so you don't have a hope in hell of getting this code running on that PIC.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
Prashant Nayak
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/09/24 00:24:23
  • Location: 0
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/12 03:23:26 (permalink)
0
Yes true when I compile it for PIC16f526 it says a lot of functions not found like CCP,T2CKSP0,T2CKSP1, and some others.
#11
Prashant Nayak
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/09/24 00:24:23
  • Location: 0
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/12 03:25:17 (permalink)
0
So Ive found out that it doesn't have CCP module to it and I don't know any other syntax/module that might work for me.
 
#12
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/12 03:28:45 (permalink)
+1 (1)
Post#6 explained what is needed. No-one is going to write the code for you though.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Playing Happy Birthday to Tune on PIC16f526 using Piezo_buzzer 2019/11/12 03:30:45 (permalink)
+1 (1)
Prashant Nayak
Yes true when I compile it for PIC16f526 it says a lot of functions not found like CCP,T2CKSP0,T2CKSP1, and some others.

They are REGISTERS that are missing, not functions.
That indicates that the hardware you are trying to address is not present.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#14
Jump to:
© 2019 APG vNext Commercial Version 4.5