Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with school project

Status
Not open for further replies.

mngeow

New Member
This is the current problem I have with my microcontroller.I have connected a buzzer to RC1 and a push button to RC2,LCD module is connected to PORTD.However when I run the program and test it out,the LCD module displays what I want it to when the push button is pressed but the buzzer doesn't produce any sound.I've attached the file to this post,maybe you guys could take a look at my code and tell me whats wrong.Thanks :D

ps:I'm using a PIC184550 microcontroller.I also want to include a 3sec interrupt such that the buzzer will only remain on for 4s when the push button is pressed,but I wanna sort this problem out first,would be helpful if you could show me how to do it though.
 

Attachments

  • Project.c
    5.2 KB · Views: 258
How does it get past the red line?

Code:
        lcd_write_data('E');		// write "2" to LCD
        lcd_write_data('N'); 		// write "3" to LCD
        lcd_write_data('I');
        lcd_write_data('E');
        lcd_write_data('D');
        [COLOR="red"]while(1);	//stop here for now[/COLOR]
    }
    if(PORTD==0xFF)
    {
        PORTCbits.RC1=1;
        while(1)
        {
             if(PORTCbits.RC2==1)
             {
                  PORTCbits.RC0=1;
                  onetone();		//sound ON then OFF

Mike.
 
Your code is stuck in an infinite loop, as Pommie pointed out. You need to remove the ';' after the while(1) in the line Pommie highlighted.
 
Doesn't seem to work when i take away the ";",I get an error when building,I tried taking away the whole while loop but the buzzer doesn't make any noise.By the way is there a way to make the lcd clear the screen when RC2=0 so that when RC2=1 again it will display the text again
 
Last edited:
I am sorry, I was looking at the unformatted version that was in the OP and it looked like there was an open curly brace after the while(1);. But it is still an infinite loop that you are getting stuck in there.
You can clear the LCD by sending 0x01 as a command.
 
Code:
while(1)
{

    	lcd_write_cmd(0x80);

 	lcd_write_data('E');
		
	lcd_write_data('N'); 		// write "A" to LCD
	
	lcd_write_data('T');		// write "B" to LCD

	lcd_write_data('R'); 		// write "C" to LCD

	lcd_write_data('Y');

	lcd_write_cmd(0xC0);		// Move cursor to line 2 position 1
	
	lcd_write_data('D'); 		// write "1" to LCD

	lcd_write_data('E');		// write "2" to LCD

	lcd_write_data('N'); 		// write "3" to LCD
	
    	lcd_write_data('I');
	
    	lcd_write_data('E');

	lcd_write_data('D');

}

How are you exiting this loop?

EDIT: I am surprised this code works, what happens if RC1 != 1 at the beginning? Does the PIC reset?
 
Last edited:
Code:
while(1)
{

    	lcd_write_cmd(0x80);

 	lcd_write_data('E');
		
	lcd_write_data('N'); 		// write "A" to LCD
	
	lcd_write_data('T');		// write "B" to LCD

	lcd_write_data('R'); 		// write "C" to LCD

	lcd_write_data('Y');

	lcd_write_cmd(0xC0);		// Move cursor to line 2 position 1
	
	lcd_write_data('D'); 		// write "1" to LCD

	lcd_write_data('E');		// write "2" to LCD

	lcd_write_data('N'); 		// write "3" to LCD
	
    	lcd_write_data('I');
	
    	lcd_write_data('E');

	lcd_write_data('D');

}

How are you exiting this loop?

EDIT: I am surprised this code works, what happens if RC1 != 1 at the beginning? Does the PIC reset?


Oh I think I get it now.I modified the program,now the LCD and buzzer work when I press the push button,but the lcd doesn't clear and the buzzer doesn't go off when i release the push button.Here's the code:
 

Attachments

  • ProjectModified.c
    5.1 KB · Views: 219
Code:
while(1)
{
    onetone();		//sound ON then OFF

    Delay1KTCYx(250);
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    Delay1KTCYx(250);

    onetone();		//sound ON then OFF

    Delay1KTCYx(250);
    Delay1KTCYx(250);
    Delay1KTCYx(250);
    Delay1KTCYx(250);
 }

You are never exiting this loop. while(1) does not mean loop once, it means while(true).
If you want three beeps use a for loop.
Code:
int i = 0;
for(;i<3;i++)
{
    //beep
}
 
Last edited:
Will it still be able to work after I use interrupt programming?Cause I wanna modify the program again later to make it such that the buzzer will only be on for 3s then go off.But I wanted to get the buzzer and lcd working first.
 
Was thinking of using it to make the buzzer turn off after it has been on for 4s?Thats the only thing I can use which has been taught by my school.
 
That shouldn't bee too hard, just a matter of setting up a timer and using the interrupt to turn the buzzer on and off.
 
I don't the specifics off hand, but the basic flow is to set up a timer, enable it's interrupt flag, then enable the global interrupt. When the timer interrupts, it will go to the interrupt vector according to its priority, and then to your service routine. What compiler are you using?
Check out the datasheet for the timers section and also the interrupt section.
 
I'm using MPLAB IDE,not sure what software that is,my school gave it to me,its under microchip.K thanks for the info :D
 
I encountered another problem again :(
I changed the while(1) loop for the buzzer to a for loop,but the buzzer doesn't go off,it only goes off after I release the push button.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top