Steps

  1. Configure the PORT D0 as output .
  2. Configure the PORT D2 as input.
  3. Read the switch status.
  4. ON/OFF the Buzzer as per switch status.

Code

Demo

Pic16f877a buzzer.gif