Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 29, 2024, 12:52:56 00:52


Login with username, password and session length


Pages: [1]
Print
Author Topic: question about sound and pic  (Read 4857 times)
0 Members and 1 Guest are viewing this topic.
jeanninemtv
Senior Member
****
Offline Offline

Posts: 311

Thank You
-Given: 57
-Receive: 25


« on: July 18, 2012, 07:31:54 19:31 »

hi,

i'm playing a little bit with the pics this days and i tried do do a 12F675 greeting card with a melody (starting with a proton code using freqout, but then i migrate to XC8 using TMR1 to fix the delays of note duration (dummy FLAG polling without interruption) and TMR0 to kick an interrupt using a prestablished code (i reduced a lot my code with this, but i need to put a __delay_ms() when i'm approaching the bank limits (i don't know why it can happen in XC8 because all my work is in C and usually it can handle with the job of bank jumping,anyway is another story for another post ) .



the dummy questions are not about the frequency algorithm (because it s working) but they are about the output:

1: i'm planning to use a simple buzzer, @3.3V.  but i wanna know if i send a triangle wave sound would be improved...

2:  if my las question was rubbish and the best way to use a non square wave is by using a speaker, i have a mini mini speaker, so ... can i filter with a pi RC network then a buffer and finally the speaker? (i dont want to put lot of power, in fact i would like to have a guitar-like sound in the output ) 

thanks

Logged
solutions
Hero Member
*****
Offline Offline

Posts: 1826

Thank You
-Given: 656
-Receive: 905



« Reply #1 on: July 18, 2012, 10:04:00 22:04 »

I don't know what you mean by "sound improved". If you put a triangle into a buzzer, all you're going to do, I suspect, is severely reduce the amplitude.

You might try a piezo for your app instead of a speaker...they're cheap and high impedance....above all, FLAT
Logged
jeanninemtv
Senior Member
****
Offline Offline

Posts: 311

Thank You
-Given: 57
-Receive: 25


« Reply #2 on: July 18, 2012, 10:32:51 22:32 »

yes, the simple solution is to feed directly a tiny coin size piezo buzzer, i was just wondering if was possible to emulate the waveform of a guitar using extra stages ^^ ...

anyway, i still have a doubth about XC8 with a 12F675 ... i'll discuse this later...
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #3 on: July 18, 2012, 11:37:24 23:37 »

XC8 with PIC12F675 is OK, I used it without troubles.
It is simply PICC from Hi-Tech.
Logged
Parmin
Hero Member
*****
Offline Offline

Posts: 582

Thank You
-Given: 496
-Receive: 133


Very Wise (and grouchy) Old Man


« Reply #4 on: July 19, 2012, 12:14:56 00:14 »

Piezo transducers have limited frequency range, they would not sound well on lower frequency, thus guitar sound may not be able to be replicated well.
Logged

If I have said something that offends you, please let me know, so I can say it again later.
jeanninemtv
Senior Member
****
Offline Offline

Posts: 311

Thank You
-Given: 57
-Receive: 25


« Reply #5 on: July 19, 2012, 02:52:59 02:52 »

Piezo transducers have limited frequency range, they would not sound well on lower frequency, thus guitar sound may not be able to be replicated well.

Ok, then i'll use the buzzer for the 'greeting card mode '  ^^

but the idea that was jumping in my head was to reuse the same melody generator (with a  rock tune) to try to emulate a guitar to put it inside a teddy bear ^^ :p  then i was thinking of a little speaker and some shaping circuitry to emulate fuzzy or any guitar like sound.

It's possible to do this with any additive harmonic synthesis using as a base the squarewave and a little ban of filters or any gimmick?  or it's too complicated and i must use something like "roman black's 1bit sound" instead???

Posted on: July 19, 2012, 03:49:10 03:49 - Automerged

XC8 with PIC12F675 is OK, I used it without troubles.
It is simply PICC from Hi-Tech.

ok, in fact it s my first time with c compilers (i 'm using mikroC too but i'm testing XC because of the announced very shrink optimal space after compilation )


so the plan was:

setting up the variables associated to the musical note (value to compare each T0 interrupt and number of times the loop is performed)

enable the TO interrupt

perform a fixed delay ...  by polling the TMR1IF in the main loop. (no Wreg involved in this task, so as i understood it's not deprecated when the TMR0 interrupt arrive)
(so i enable the TMR1ON then i poll and finally when the delay is completed i disable the TMR1ON)

and so on after recharging my variable registers to play  a  new note

first thing i did was to put a progressive scale list, it worked ok, but for an unexpected reason it auto looped without reaching the last notes ...  i solved it forcing the system to do a while(1) {;} at the end ...

then i changed the list to play a song, then it played just one note ...after saying 'grrr'  i added a built in delay ( __delay_ms(500)) at the start of the list of notes) and it worked...
b
then the song grew and when i was reaching the 256B first block no note was played ... another 'grr', then i added another __delay_ms(200)  and it played the rest of the notes, thats why i'm a little bit surprised ...



Code:
void interrupt isr(void)
{
  INTCONbits.T0IF = 0;
        ++count;
        if (count==vcount)
        {
            LED1= !LED1;
            count=0;
            TMR0=vTMR0;
        }
        T0IF=0;
}

to cut paste in macro way
Code:
#define  C4() vTMR0=91;vcount=4

and the polling ...
Code:
delay200(void)
{        TMR1ON=1;
        while(!TMR1IF)
        {;}
        LED2=! LED2;
        TMR1IF=0;
        TMR1ON=0;
}


then i use in the code as :

Code:
....

 ei();                           // enable global interrupts

    while (1)
    {
         __delay_ms(500);//__delay_ms(500);
//         __delay_ms(500);__delay_ms(500);__delay_ms(200);

         INTCONbits.T0IE=1;
        B3();delay200(); // and the rest of the notes ...
...
best regards.
« Last Edit: July 19, 2012, 03:10:26 03:10 by jeanninemtv » Logged
solutions
Hero Member
*****
Offline Offline

Posts: 1826

Thank You
-Given: 656
-Receive: 905



« Reply #6 on: July 19, 2012, 09:37:00 09:37 »

Piezo transducers have limited frequency range, they would not sound well on lower frequency, thus guitar sound may not be able to be replicated well.

True...best to use a pair of studio monitors....
 Tongue

However, the problem was framed as

"i'm playing a little bit with the pics this days and i tried do do a 12F675 greeting card with a melody:"
Logged
jeanninemtv
Senior Member
****
Offline Offline

Posts: 311

Thank You
-Given: 57
-Receive: 25


« Reply #7 on: July 19, 2012, 10:42:38 10:42 »

oups ^^  my mind went too off-topic (or outside??) .  sorry for that !

don't redlist me for having an overlapping idea ...

so going back to the card, i think i will put some leds to improve that.  But as it will be battery operated, what strategy would be low budget to drive from 3.3V a diode array? Low Vth Leds?  or a ic driver? 

Regarding the code modifications, i will add a timeup wdt to put sleeping the pic and then wake up it with a button ( open/close the card or shaking the object ).

But i still have the doubt  of why i need to put a _delay_ms() each certain number of notes to ensure that my interrupt + dummy polling driven sound plays all the desired notes ... that's so confusing to me !!!

and last of all,  do you know any pcb material to work with at home that is thinner than the standard we use ?? (i mean thin as a paper sheet or a cardboard )
« Last Edit: July 19, 2012, 10:52:08 10:52 by jeanninemtv » Logged
solutions
Hero Member
*****
Offline Offline

Posts: 1826

Thank You
-Given: 656
-Receive: 905



« Reply #8 on: July 20, 2012, 02:14:26 02:14 »

You can write conductive ink onto paper....it only has to work for a few seconds before it gets thrown away
Logged
Parmin
Hero Member
*****
Offline Offline

Posts: 582

Thank You
-Given: 496
-Receive: 133


Very Wise (and grouchy) Old Man


« Reply #9 on: July 21, 2012, 01:16:22 01:16 »

 0.4mm or 0.8mm FR4 PCB are common for the RC flights hobbyist.
I believe now an then on eBay you could buy some.

Otherwise, send me a PM of your address, and I'll post you some 0.4mm and 0.8mm stuff.
Logged

If I have said something that offends you, please let me know, so I can say it again later.
PaulC
Active Member
***
Offline Offline

Posts: 170

Thank You
-Given: 3935
-Receive: 133


information is free and should be shared for free


« Reply #10 on: July 22, 2012, 10:17:39 10:17 »

Not sure if this would help:
been messing with sound myself recently & using mikrobasic for pic..
Sound Library
The mikroBasic PRO for PIC provides a Sound Library to supply users with routines necessary for sound signalization in their applications. Sound generation needs additional hardware, such as piezo-speaker (example of piezo-speaker interface is given on the schematic at the bottom of this page).

Library Routines
Sound_Init
Sound_Play
Sound_Init
Prototype sub procedure Sound_Init(dim byref snd_port as byte, dim snd_pin as byte)
 
Returns Nothing.
 
Description Configures the appropriate MCU pin for sound generation.

Parameters :

snd_port: sound output port address
snd_pin: sound output pin

 
Requires Nothing.
 
Example Sound_Init(PORTC, 3)  ' Initialize sound at RD3

 

Sound_Play
Prototype sub procedure Sound_Play(dim freq_in_Hz as word, dim duration_ms as word)
 
Returns Nothing.
 
Description Generates the square wave signal on the appropriate pin.

Parameters :

freq_in_Hz: signal frequency in Hertz (Hz)
duration_ms: signal duration in miliseconds (ms)

  Note : Frequency range is limited by Delay_Cyc parameter. Maximum frequency that can be produced by this function is Freq_max = Fosc/(80*3). Minimum frequency is Freq_min = Fosc/(80*255). Generated frequency may differ from the freq_in_hz parameter due to integer arithmetics. 
Requires In order to hear the sound, you need a piezo speaker (or other hardware) on designated port. Also, you must call Sound_Init to prepare hardware for output before using this function.
 
Example ' Play sound of 1KHz in duration of 100ms
Sound_Play(1000, 100)

 

Library Example
The example is a simple demonstration of how to use the Sound Library for playing tones on a piezo speaker.

 Copy Code To Clipboard
program Sound

sub procedure Tone1()
    Sound_Play(659, 250)                ' Frequency = 659Hz, duration = 250ms
end sub

sub procedure Tone2()
    Sound_Play(698, 250)                ' Frequency = 698Hz, duration = 250ms
end sub

sub procedure Tone3()
    Sound_Play(784, 250)                ' Frequency = 784Hz, duration = 250ms
end sub

sub procedure Melody()                  ' Plays the melody "Yellow house"
    Tone1() Tone2() Tone3() Tone3()
    Tone1() Tone2() Tone3() Tone3()
    Tone1() Tone2() Tone3()
    Tone1() Tone2() Tone3() Tone3()
    Tone1() Tone2() Tone3()
    Tone3() Tone3() Tone2() Tone2() Tone1()
end sub

sub procedure ToneA()                   ' Tones used in Melody2 function
    Sound_Play( 880, 50)
end sub

sub procedure ToneC()
    Sound_Play(1046, 50)
end sub

sub procedure ToneE()
    Sound_Play(1318, 50)
end sub

sub procedure Melody2()                 ' Plays Melody2
dim counter as byte
    for counter = 9 to 1 step -1
        ToneA()
        ToneC()
        ToneE()
    next counter
end sub

main:

  ANSEL  = 0                            ' Configure AN pins as digital I/O
  ANSELH = 0
 
  C1ON_bit = 0                          ' Disable comparators
  C2ON_bit = 0
 
  TRISB  = 0xF8                         ' Configure RB7..RB4 as input, RB3 as output

  Sound_Init(PORTC, 3)
  Sound_Play(880, 1000)

  while TRUE                            ' endless loop
      if (Button(PORTB,7,1,1)) then     ' If PORTB.7 is pressed play Tone1
          Tone1()
          while (RB7_bit <> 0)
            nop                         ' Wait for button to be released
          wend
      end if

      if (Button(PORTB,6,1,1)) then     ' If PORTB.6 is pressed play Tone2
          Tone2()
          while (RB6_bit <> 0)
            nop                         ' Wait for button to be released
          wend
      end if

      if (Button(PORTB,5,1,1)) then     ' If PORTB.6 is pressed play Tone3
          Tone3()
          while (RB5_bit <> 0)
            nop                         ' Wait for button to be released
          wend
      end if

      if (Button(PORTB,4,1,1)) then     ' If PORTB.5 is pressed play Melody2
          Melody2()
          while (RB4_bit <> 0)
            nop                         ' Wait for button to be released
          wend
      end if

      if (Button(PORTB,3,1,1)) then     ' If PORTB.4 is pressed play Melody
          Melody()
          while (RB3_bit <> 0)
            nop                         ' Wait for button to be released
          wend
      end if
    wend
end.

HW Connection


Example of Sound Library connection

Logged

find it , read it , share it .
jeanninemtv
Senior Member
****
Offline Offline

Posts: 311

Thank You
-Given: 57
-Receive: 25


« Reply #11 on: July 22, 2012, 01:56:56 13:56 »

off course i know it , but in fact i was trying to learn more to do with XC8, that's why i replaced the delay based routine with timer usage ...

but i havent checked the codesize generated with mikroC or miKroBasic...
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC