READING A PUSHBUTTON & display on LCD

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
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 1st thing that happens is The LCD displays "infrared liquid level detector" and it stays that way always, so the lcd code and initialising is working.

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.


Rich (BB 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
  			}
	 }
}
 
   
void home_screen(void)
{
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)

	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();
		            lcd_puts(" ENTER/SETTINGS ");
		            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();
   
	} 
	 
}
 

Attachments

Last edited:

rjenkins

Joined Nov 6, 2005
1,013
You appear to be writing the 'Home Screen' display to the LCD on EVERY pass through the loop, rather than just when it should be on that specific screen?

Try using a variable to set a screen number, then display the appropriate screen text to match the variable. Clear it to Zero for a home or startup screen during the init stage.

The button presses then change the screen number or do other functions, depending on the present screen number (ie. softkeys, the functions match the displayed button titles).
 
Top