Sound frequency For Basic PRO Compiler

Summary of Sound frequency For Basic PRO Compiler


This article presents a PIC16F877 microcontroller code snippet to control a speaker via a push button. When the button is pressed, a flag toggles, triggering a sound generation subroutine that outputs a 100Hz tone for 120 milliseconds. The system continuously monitors the button state and uses an 8-bit ADC configuration while setting Port B as output and Port A as input.

Parts used in the PIC16F877 Speaker Control:

  • PIC16F877 Microcontroller
  • Speaker connected to Port B bit 0
  • Push Button Switch connected to Port A bit 0
  • 4 MHz Oscillator
@ device pic16f877
define osc 4
define adc_bits 8
main:
    trisb =$00
    trisa =$ff
    adcon1 = 7
    speaker    var portb.0
    sw1        var porta.0
    flg_set    var bit
    speaker =0
    flg_set =0
while(1)
    if(!sw1) then
       pause 150
       toggle flg_set
      while(!sw1):wend       
    endif    
   gosub sounds                    
   pause 10   
wend
end

sounds:
   if(flg_set)then
       pause 10
       sound speaker,[100,10]'100 =(1-127 generate frequency 78.74-10 khz )
                       '10  = constant time 12ms/1 = 120 ms 
   else
       sound speaker,[0,0]  
   endif  
return

Quick Solutions to Questions related to PIC16F877 Speaker Control:

  • How is the oscillator frequency defined?
    The oscillator is defined as 4 MHz using the define osc 4 command.
  • What are the port configurations?
    Port B is set as output (trisb =$00) and Port A is set as input (trisa =$ff).
  • How does the button press trigger the sound?
    Pressing the switch toggles a flag variable which activates the sounds subroutine.
  • What frequency does the sound generate?
    The code generates a frequency of 100 Hz when the flag is set.
  • How long does the sound last?
    The sound duration is calculated as 120 milliseconds based on the time constant.
  • What happens if the switch is not pressed?
    The speaker outputs silence represented by [0,0] in the sound array.
  • How is debouncing handled in the code?
    A 150 millisecond pause is used after detecting the switch press to debounce.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter