PIC16F887 controlled switch

Discussion in 'Embedded Systems and Microcontrollers' started by Alho, Apr 14, 2012.

  1. Alho

    Thread Starter New Member

    Nov 3, 2011
    12
    0
    hi, i am trying to write C code for a switch controlled by PIC16F887 microcontroller. i am using the internal 4MHz oscillator and i am using micrC for PRO.

    the code i have written is pasted below:

    void main()
    {
    ANSEL = 0;
    ANSELH = 0;
    C1ON_bit = 0;
    C2ON_bit = 0;

    TRISD3_bit = 1;
    TRISD2_bit = 1;

    TRISD.F0 = 0;
    TRISD.F1 = 0;
    TRISD.F4 = 0;

    PORTD.F0 = 0;
    PORTD.F4 = 0;
    PORTD.F1 = 1;

    while(1)
    {
    if (Button(&PORTD, 2, 1, 1))
    {
    PORTD.F0 = 1;
    PORTD.F1 = 0;
    }

    if (Button(&PORTD, 3, 1, 1))
    {
    PORTD.F0 = 0;
    PORTD.F1 = 1;
    PORTD.F4 = 0;
    }
    }
    }


    so far its working, pressing button 1 turns on RED led and turns off GREEN LED. Pressing button 2, turns off RED led and turns on GREEN led.

    the thing is i need to include a buzzer that beeps every 30 seconds as long as the RED led is on. and the same "button 1" should be used to turn off the buzzer without turning off the RED led. if i pressed button 2 the buzzer should turn off as with the RED led.

    any help greatly appreciated....
    10Q
     
  2. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    the_RB on here has this : http://www.romanblack.com/one_sec.htm
    check it out, very useful and will be applicable to you for the 30second delay between buzzer beeps..
    in respect to making the buzzer make noise..
    dont forget a reverse bias diode to stop it destroying your IO pin.
    in addition... you can use the pwm module on your pic.. see the datasheet chapter 11
     
Loading...