Play a tone PIC 16F628

Discussion in 'Embedded Systems and Microcontrollers' started by nerdegutta, Nov 24, 2010.

  1. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Hi,

    I have a pic 16F628 circuit, and when a button is pressed, it triggers a relay.

    Now I want to add a new feature; when the time is out, I want a piezo to buzz for 2 seconds. I have a 555-timer based piezo buzzer, and when I connect it to the pic and reprogram it, it's making a buzz. Just as I want.

    But isn't it possible to connect the piezo directly to the pic? I think I've read somewhere, and seen some source code about that, but now I cannot find it.

    Anyone?
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What language are you using?

    make a pin high, wait 1ms, turn pin low, wait 1ms, repeat 250 times for a 1/2 second tone. Vary the times for different frequencies.
     
  3. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Yeah, just as I thought, some info missing. :)

    I'm using MPLAB and Hi tech C.

    So by turning a pin "on and off" really fast makes a tone?

    Code ( (Unknown Language)):
    1.  
    2. for(i=0;i<250;i++)
    3. {
    4. pin high
    5. delay_ms(1);
    6. pin low
    7. delay_ms(1);
    8. }
    9.  
    Something like this?
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    That's a low tone, for a speaker. You may want to make a low pass filter on the output to smooth it out a bit. like a 100 ohm in series with output pin and maybe a 0.1uF cap from that resistor to ground. This makes the sound a tad "cleaner" on a dynamic speaker. If you are using a piezo, you may need to tweak the components to cut off under 20kHz.
     
  5. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Thanks.

    I think this will do. I just want to hear when the time period of the relay end.
     
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Let me know if it works. I haven't done it in so long that I forgot the exact values, as well as what I used for a lowpass. :eek:
     
  7. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    OK. I will let you know, by posting the result in this thread. I will do it in the evening on thursday the 25.11.10.:)
     
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    I'll be here. Include source code, schematic and board photos for completed projects area. This would be a good example for others getting going in PIC programming.

    P.S. Try out BoostC! They have a ton of examples, and it is what I am most familiar with.
     
  9. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I'd recommend a resistor as the capacitance of a piezo is a lot higher than the 50pF recomendation for pin loading. It might cause overcurrent and possibly resetting. I can't figure out the maths to work out how big the resistor should be.
     
  10. Kermit2

    AAC Fanatic!

    Feb 5, 2010
    3,789
    945
  11. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Hi.

    This circuit I'm planning to use in my new UV-exposure box. The software is just made as an example. I will use Port A as input, and Port B as output. RA6 is now set to light the LED for just 2 seconds. This will be the test button. RA7, RA5-RA0 will have different values, so with a switch, I'm able to change the trigger-port, and still just have two buttons. Test and on.

    This is the software, so far:

    Code ( (Unknown Language)):
    1.  
    2. /*
    3.  
    4. Program:        main.c
    5. Description:    Timer program for UV-box
    6. PIC:            16F628
    7. IDE:            MPLAB
    8. Compiler:        Hi - Tech C
    9. Date:            Nov 2010
    10. Author:            Jens Christoffersen
    11. web:            www.nerdegutta.org
    12.  
    13. */
    14.  
    15. #include <htc.h>
    16. #define _XTAL_FREQ 4000000
    17.  
    18. /* Configuration */
    19.  
    20. __CONFIG    (WDTDIS &
    21.             PWRTEN &
    22.             MCLREN &
    23.             BOREN &
    24.             LVPDIS &
    25.             DATUNPROT &
    26.             UNPROTECT &
    27.             INTIO);
    28.  
    29. /* Prototyping the functions */
    30. void lightLED();
    31. void beep();
    32.  
    33. /* Global variables */
    34. unsigned char i;
    35.  
    36. /* Functions */
    37. void lightLED()
    38. {
    39.     PORTB = 0b00000001; // Setting bit 0 to HIGH
    40.     __delay_ms(1000);
    41.     PORTB = 0b00000000; // Setting all bits to LOW
    42. } // end lightLED
    43.  
    44.  
    45. void beep()
    46. {
    47.     for (i=0;i<250;i++)
    48.     {
    49.         PORTB = 0b00000010; // Setting bit 1 to HIGH
    50.         __delay_ms(1);
    51.         PORTB = 0b00000000; // Setting bit 1 to LOW
    52.         __delay_ms(1);
    53.     } // end for-loop
    54. } // end beep
    55.  
    56. /* Main program */
    57. void main()
    58. {
    59. TRISA = 0b11111111;    // Setting all bits on port a to input
    60. TRISB = 0b00000000;    // Setting all bits on port b to output
    61.  
    62. PORTA = 0b00000000; // Setting all bits on port a to LOW
    63. PORTB = 0b00000000;    // Setting all bits on port b to LOW
    64.  
    65. CMCON = 0x07;    // Disabling the analogue comparators
    66.  
    67. while (1)
    68. {
    69.     if (RA6)
    70.         {
    71.         for(i=0;i<2;i++) // Create a delay for 2 minutes
    72.         {
    73.             lightLED();
    74.         } // end for-loop
    75.         beep();
    76.         break;
    77.         } // end if
    78.  
    79. } // end while
    80.  
    81. }  //end main
    82.  
    I have not yet implemented the other time procedures.

    As you can see, I'm using the internal oscillator.

    I've made a schematic, and taken a picture, see attachment. I'm not quite sure about the connections on the schematic. I made the breadboard first, then I drew the schematic. Yes, I know - A little backwards... :)

    The piezo is connected to pin 7, and there is no resistor. I tried with some resistors, with different values, and the result was the volume of the beep. Haven't tried a speaker.

    The powersource is for the moment a "jump-started" PC power supply. When I make the PCB I will include a .01uF and a 100uF capacitor over the LM7805.

    Comments are welcome. :)
     
    Last edited: Nov 25, 2010
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    The piezo will likely be the louder solution for this.
     
  13. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Yes, and I guess that it is more space saving.

    I don't want a big box. I want it as small as possible. :)
     
Loading...