This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Calling function in the same file

When I paste my Buzzer code in the main(), it works. But when I made a buzzer() to have this function execute in the main(), it didn't work.
However,I want to use the buzzer function to execute in the main().
How? please help me!!

#pragma DEBUG OBJECTEXTEND CODE
#pragma PL  (60)
#pragma PW  (120)

#define FOREVER 1
#include <89c51rd2.h>
#include <stdio.h>

void Delay(void);
void Buzzer(void);

sbit Buzer = P0^3;

void Buzzer()
{

	int i=0;
	P2=0x00;
	Buzer=0;
 	while(i<6000)
 	{
		TMOD=0x01;
		TH0=0x0D8;		//if it is not flashing change it to 00 for TH0 and Tl0
		TL0=0x0F0;
		TR0=1;
		while(TF0==0){}
		TR0=0;
		TF0=0;
		Buzer=~Buzer;
		i++;
	}


}
void main()
{
  while (FOREVER)
  {
  	Buzzer();

	}
}

Parents
  • hi,

    so the buzzer should go on and off. But it didn't. It go beep for a long time

    Sure, it is exactly what you have programmed. Look at main() and see that you call function Buzzer() in infinite loop (called FOREVER). As this loop does never end so you hear "beep for a long time"
    Try next:

    void main()
    {
       Buzzer();
    
       while (FOREVER)
       {
       }
    }
    
    I think it gives you a hint.

    Regards,
    Oleg

Reply
  • hi,

    so the buzzer should go on and off. But it didn't. It go beep for a long time

    Sure, it is exactly what you have programmed. Look at main() and see that you call function Buzzer() in infinite loop (called FOREVER). As this loop does never end so you hear "beep for a long time"
    Try next:

    void main()
    {
       Buzzer();
    
       while (FOREVER)
       {
       }
    }
    
    I think it gives you a hint.

    Regards,
    Oleg

Children
No data