Finding the correct PORT on a PIC and implementation of a buzzer
Afternoon all,
I am currently delving into the brand new world (to me anyway) of embedded systems and am struggling to understand something. I've looked around for an answer but either the answer is so staggeringly obvious that it's not implicitly stated or I am asking the wrong question.
The question is this; I have a QL200 development board which has several components I am interfacing with a PIC16F877A (Push buttons, LEDs, LCD screen and buzzer). The last component I am really struggling with is the buzzer.
Through some reading , I am led to believe that to make the buzzer sound I simply have to set the pin it is operating on high. However I am unsure how to do this. Furthermore, I have found (by luck or guidance) the ports that all the other components run on but I have no clue as to actually how they are assigned their ports. Is it by assigning them a port or are they set on a certain port as standard?
For example, my LCD panel is running off PORTD:
#define lcd PORTD
void writeCommand(unsigned char ch)
{
lcd = ch;
RS = 0;
RW =0;
E = 1;
lcdDelay();
E=0;
}
void Init_lcd(void)
{
ADCON1 = 0x07; //required setting of analog to digital
TRISD = 0x00;
TRISA1 = 0;
TRISA2 = 0;
TRISA3 = 0;
writeCommand(0x0f);
writeCommand(0x38); //set to two line mode
clearDisplay();
writeString("MAIN MENU");
}
Is that from a data sheet? I simply got told it was on PORTD so ran with that and it works.
However now it comes the turn of the buzzer and I'm stumped as to even get started. Several examples online use PORTB but I'm already using push buttons as inputs on PORTB:
#define TempButton1 RB1
#define TempButton2 RB2
#define TempButton3 RB3
Last thing, is that once I find the port the buzzer is running on, is it a simple matter of raising it high to get the buzzer to sound? i.e:
//declarations
#define buzzer PORT(x)
//in a function somewhere
buzzer = 1; //hopefully buzzer should sound
A lot of my research indicates that this way should work but some use PMW and frequencies to achieve a sound. Thanks in advance!
Solution 1:
You will have to toggle the output port pin at twice the frequency you desire, since this is also described as an audio and voice feature, and just enabling the output cannot do that. If it shares a port with other outputs, you must preserve the state of the other outputs while you toggle it.
If you don't have an oscilloscpe, get one if you are doing any serious work. Also useful for seeing port levels (in or out) at the chip.