Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

READING A PUSHBUTTON & display on LCD

Status
Not open for further replies.

fantabulous68

Junior Member level 2
Joined
Aug 15, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,591
I recently got a pikit 2, its the 1st time that im using push button switches.
im using 4 pushbuttons.

All i want for now is:
when i power the circuit The LCD displays "infrared liquid level detector", so the lcd code and initialising is working. BUT
when i press ENTER/Settings button....i want "enter/settings" to display on the LCD. And that is not happening when i press the button. Please help me solve this problem....i attached a picture of how i connect the switch to the pic.


Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>


char input_sw;
#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4    	//ENTERSETTINGS switch

void init(void)
{   

    OSCCON|=0x60; //set fosc to 4Mhz
	TRISB=0x00; 
	TRISC=0xFC;
        TRISA = 0b10000010; 	// RA7 high imp, RA3 is serial out,
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;
	lcd_init(); //call LCD initialisation
}

 void read_input_sw(void)
{
 char input_sw2; 
 input_sw = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1)); //create a binary combination of the input switches
 if (input_sw != 0) //check if any of the switches are pressed
	{
	 DelayMs(20); //switch debouncing
	 input_sw2 = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1));
	 while (input_sw2 != 0) //if a switch is pressed wait for release
			{
			 input_sw2 = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1));
			 DelayMs(20); //switch debouncing
  			}
	 }
}
 
   
[COLOR="Red"]void home_screen(void)[/COLOR]
{
	lcd_clear();
	lcd_goto_L1();
	[COLOR="Red"]lcd_puts("INFRARED LIQUID"); //home screen message (line 1)[/COLOR]
	lcd_goto_L2();
[COLOR="Red"]	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)[/COLOR]

	read_input_sw();
	while(input_sw == 0)

		read_input_sw();
		switch(input_sw)
	{
		case 8: break;	
		//	ENTER/SETTINGS	button pressed	
		case 1: {	lcd_clear();
		            lcd_goto_L1();
		[COLOR="Red"]            lcd_puts(" ENTER/SETTINGS ");[/COLOR]
		            DelayS(1);
		            lcd_clear();
 					
					break;
				}
                   default : break; //should any abnormalties occur
    } 
	return;
}


void main(void)
{
	init();	// initialise I/O ports, LCD
	while (1)
	{
	home_screen();
   
	} 
	 
}
 

I will suggest you to try to read one switch first and when you have done this successfully add another and so on.

When I read several switches from a port I usually:

- Of course set the switches connection as inputs.
- Read the complete port
- Mask the port to get rid of non wanted bits
- Check if a button has been pressed.

As you can see I read the complete port and then mask.

Again I strongly suggest you to change your code to read one switch first.

Added after 2 minutes:

Also what you need to do is to put a volltmeter on the switch connection to the microcontroller and make sure it will go to zero when pressing the button, if the voltage does not change, then check on the port configuration.
 

Thanks for your suggestions........It is working now....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top