Simple switch/led program with PIC16F886

Thread Starter

beeson76

Joined Apr 19, 2010
211
I would like to make a simple embedded program that will Display what is happening on and LCD screen. When Switch One is pressed, it displays "Switch One". When Switch 2 is pressed it displays Switch 2. When both switches are pressed is displays "Both Switch". For the life of my I cannot get it to display "Both Switches" on the LCD when both switches are pressed. I have Switch 1 attached to RB0 and Switch 2 attached to RB1. Both switches display properly until I press both switches and what usually displays is the first one pressed. Please help!!:):) Here is my code:
C:
if (switch1 == 0)                      

            {

                __delay_ms(10);                      

                while (switch1 == 0)        

                    {                              

                       for (a = b; a < 1; a++)      

                            {                      

                                lcd_clear();      

                            }                              

                        lcd_goto(0);              

                        lcd_puts("Switch 1");      

                        b = 1;                      

                          }                              

                  }                                      

        if (switch2 == 0)                          

            {                                      

                __delay_ms(10);                      

                while (switch2 == 0)

                        {                              

                            for (a = b; a < 1; a++)      

                                {                      

                                    lcd_clear();      

                                }                      

                            lcd_goto(0);              

                            lcd_puts("Switch 2");          

                            b = 1;                      

                        }

            }                              

                                               

        if (PORTB == 0xFC)                      

            {                                      

                __delay_ms(10);                      

                while (PORTB == 0xFC)

                        {                              

                                for (a = b; a < 1; a++)      

                            {                      

                                lcd_clear();      

                            }                      

                        lcd_goto(0);              

                        lcd_puts("Both Switches");  

                        b = 1;                      

                        }

            }
 
Last edited:

djsfantasi

Joined Apr 11, 2010
8,078
I would like to make a simple embedded program that will Display what is happening on and LCD screen. When Switch One is pressed, it displays "Switch One". When Switch 2 is pressed it displays Switch 2. When both switches are pressed is displays "Both Switch". For the life of my I cannot get it to display "Both Switches" on the LCD when both switches are pressed. I have Switch 1 attached to RB0 and Switch 2 attached to RB1. Both switches display properly until I press both switches and what usually displays is the first one pressed. Please help!!:):) Here is my code:

Code:
    if (switch1 == 0)                          
            {
                __delay_ms(10);                          
                while (switch1 == 0)            
                    {                                   
                       for (a = b; a < 1; a++)           
                            {                           
                                lcd_clear();           
                            }                                   
                        lcd_goto(0);                   
                        lcd_puts("Switch 1");           
                        b = 1;                           
                          }                                  
                  }                                           
        if (switch2 == 0)                               
            {                                           
                __delay_ms(10);                           
                while (switch2 == 0)
                        {                                   
                            for (a = b; a < 1; a++)           
                                {                          
                                    lcd_clear();           
                                }                          
                            lcd_goto(0);                   
                            lcd_puts("Switch 2");               
                            b = 1;                          
                        }
            }                                  
                                                   
        if (PORTB == 0xFC)                           
            {                                           
                __delay_ms(10);                           
                while (PORTB == 0x21)
                        {                                   
                                for (a = b; a < 1; a++)          
                            {                           
                                lcd_clear();           
                            }                           
                        lcd_goto(0);                   
                        lcd_puts("Both Switches");       
                        b = 1;                           
                        }
            }
Umm, your code tags are incorrect and thus the code doesn’t appear. After the word “code”, replace the = sign with a close brace, “]”.

When both switches are closed, your code whichever switch is closed, then that message will be displayed until the switch is released.

You test for 0xFC to determine if both switches are pressed, but test for 0x21 to keep the message displayed until one of the switches are released. I think (I didn’t check) that one or both of those values are wrong. If so, what do you think will happen.

Just curious… What’s with the for loop around the lcd_clear()?
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
Umm, your code tags are incorrect and thus the code doesn’t appear. After the word “code”, replace the = sign with a close brace, “]”.

When both switches are closed, your code whichever switch is closed, then that message will be displayed until the switch is released.

You test for 0xFC to determine if both switches are pressed, but test for 0x21 to keep the message displayed until one of the switches are released. I think (I didn’t check) that one or both of those values are wrong. If so, what do you think will happen.

Just curious… What’s with the for loop around the lcd_clear()?
Thanks djsfantasi for the quick reply:):) I fixed he "FC" "21" mistake. I was copy and pasting between my programs and somehow got it messed up. Sorry about that. I think I also fixed the coding problems.

Yeah that I think is ONE of my problems is that it latches on to the first switch pressed. So I went with the PORTB == 0xFC for it to try to recognized both switches being pressed. I tried to use "PORTB ==" throughout the whole program, but to program it for that if PORTB == 0x01)if PORTB == 0x02, I cannot get it to recognize a switch press. I am completely confounded on what is going on here:):)
 
Last edited:

djsfantasi

Joined Apr 11, 2010
8,078
You didn’t post the entire program. So I don’t know what switch_1 and switch_2 are?

What pins are the two switches connected to? And what bits in PORTB do they correspond to?

I’m general, what I’m trying to get to is perhaps you’re mapping the two switches case to the wrong value.

Also, it might be easier if you code the third case as
((switch_1 == 0) && (switch_2 == 0))

Then, you might see the mistake.

Your logic has overlapping conditions. Switch_1 pressed is true in both cases where switch_2 is pressed or not pressed. The logic for your first case should be switch_1 is pressed AND switch_2 is not pressed.
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
Thank you djsfantasi for the replies:):) I really appreciate it. Here is the full the code for the whole program. I am working line by line thru the program now. Here is the code:
Code:
#define _LEGACY_HEADERS
#include <htc.h>
#include "lcd.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////DEFINE STATEMENTS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define    SWITCH_1    RB0
#define    SWITCH_2    RB1

#define DelayS(T)    {unsigned char i; for (i = 0; i < T * 10; i++) __delay_ms(100);}    //Delay Macro
#define _XTAL_FREQ    4000000                                    //Needs to be set for __delay_ms

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////CONFIGURATION FUSES (BITS)
////Master Clear Reset enabled & Internal RC No Clock & Watchdog Timer Disable & Power Up Timer On & Brown Out Reset Disabled &
////Low Voltage Porgramming Disabled & Code Unprotect
__CONFIG (MCLRDIS & INTIO & WDTDIS & PWRTEN & BORDIS & LVPDIS & UNPROTECT);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

main()         
{
PORTA = 0x00;                //PORTA is cleared 
PORTA = 0xE9;                //(ob11101001 binary) (0xE9 hex)
                    //PORTA7:A0 is set to high,high,high,low,high,low,low,high
                                                  
PORTB = 0x00;                //PORTB is cleared
PORTB = 0xFF;                //(0b11111111 binary) (0xFF hex)
                    //PORTB7:B0 is set to high,high,high,high,high,high,high,high
                                                  
PORTC = 0x00;                //PORTC is cleared
PORTC = 0xF0;                //PORTC is set to high, high, high, high, low, low, low, low


TRISA = 0xE9;                //(0b11101001 binary)    (0xE9 hex)
                    //LCD--RA1, RA2, RA4 are Control lines for LCD
                    //RA0, RA3, RA5, RA6 are set for switches
                    //PORTA7:A0 is set to input,input,input,output,input,output,output,input

TRISB = 0xFF;                //(0b11111111 binary) (0xFF hex)
                    //All of PORTB are set for switches
                    //PORTB7:B0 is set to input,input,input,input,input,input,input,input
                                              

TRISC = 0x00;                //(0b00000000 binary) (0x00 hex)
                    //All of PORTC is set for LCD
                    //PORTC7:C0 is set to output,output,output,output,output,output,output,output
                                                  
ANSEL = 0;                //Initialize A/D Ports off
ANSELH = 0;                //Initialize ........
ADCON0 = 0;
ADCON1 = 0;
CM1CON0 = 0;                //Initialize Comparator 1 off
CM2CON0 = 0;                //Initialize Comparator 2 off
                                  

OPTION = 0b01010101;            //OPTION REG
                    //xbxxxxx101  1:64
                    //xbxxxx0xxx  Prescaler set to Timer0
                    //xbxxx1xxxx  (T0SE) set to Increment on high-to-low transition on T0CKI pin
                    //xbxx0xxxxx  (T0CS) Internal instruction cycle clock
                    //xbx1xxxxxx  (INTEDG) Interrupt on rising edge of INT pin
                    //xb0xxxxxxx  (RBPU) PORTB pull-ups are enabled by individual PORT latch values
RBPU = 0;                //Don't think I need this, but to be safe...PORTB Weak Internal Pullups enabled
WPUB0 = 1;                //COL_1 Weak pullup is individually ENABLED--INPUT
WPUB1 = 1;                //COL_2 Weak Pullup is inidivdually ENABLED--INPUT
WPUB2 = 1;                //Not being used...Weak Pullup is individually ENABLED
WPUB3 = 1;                //ROW_1 Weak Pullup is individually ENABLED--INPUT
WPUB4 = 1;                //ROW_2 Weak Pullup is individually ENABLED--INPUT
WPUB5 =    1;                 //ROW_3 Weak Pullup is individually ENABLED--INPUT
WPUB6 = 1;                //ROW_4 Weak Pullup is individually ENABLED--INPUT
WPUB7 = 1;                //COL_3 Weak Pullup is individually ENABLED--INPUT

lcd_init();                //LCD Display is Initialized...See LCD.C file and LCD.h
lcd_clear();

char a;                    //
char b;                    //

//The following Code will Display the name of the tester and the Rev. only once for a total of
//2 seconds.  This happens right when you turn it on.  I had to keep it out of the while statement
//because it keeps cycling over and over again.  Therefore it had to be before that.  After the
//Name and Rev. is displayed for 2 seconds, we have to clear the LCD of the Name and Rev..  Therefore
//the use of lcd_clear is required to clear the screen.

lcd_goto(0);                //LCD First Line First Position
lcd_puts("Button Tester");        //Display "Button Tester"
lcd_goto(40);                //LCD Second Row First Position
lcd_puts("Rev. 1.0");            //Display Rev Number
__delay_ms(2000);            //Display Name and Rev. for 2000 ms (2 seconds)
lcd_clear();                //The Screen is cleared of Name and Rev.

while (1)

    {
      
      
        lcd_goto(0);                //Go to Line 1 Position 1
        lcd_puts("Button Tester");        //Display ""
        lcd_goto(40);                //Go to Line 2 Position 1
        lcd_puts("Press Button");        //Display "Press Button"
        b = 0;
      
        while ((SWITCH_1 == 0) && (SWITCH_2 == 1))            //While SW1 is 0(Low)(Pressed)...
            {
                __delay_ms(10);        //Delay 10 ms (Switch Debounce)
                while ((SWITCH_1 == 0) && (SWITCH_2 == 1))    //Check to see if SW1 is low after Debounce
                    {
                        for (a = b; a < 1; a++)            //For Statement to cycle lcd_clear only once
                            {
                                lcd_clear();        //lcd_clear should only cycle once because a = (b = 0)
                            }
              
                        lcd_goto(0);                //On LCD go to Position 1 (Line 1, Position 0)
                        lcd_puts("SWITCH 1");            //Display ""
                        b = 1;                    //Char B is now 1 so For Statement cycles once
                    }
            }
        while ((SWITCH_2 == 0) && (SWITCH_1 == 1))                         
            {                                         
                __delay_ms(10);                     
                while ((SWITCH_2 == 0) && (SWITCH_1 == 1))                 
                    {                     
                        for (a = b; a < 1; a++)         
                            {                         
                                lcd_clear();
                            }                                                                     
                        lcd_goto(0);             
                        lcd_puts("SWITCH 2");         
                        b = 1;                         
                    }
                                                      
            }                                         
        while ((SWITCH_1 == 0) && (SWITCH_2 == 0))                //While SW1 and SW2 are 0(Low)(Pressed)...
            {                                         
                __delay_ms(10);                         
                while ((SWITCH_1 == 0) && (SWITCH_2 == 0))                     
                    {                                 
                        for (a = b; a < 1; a++)         
                            {                             
                                lcd_clear();         
                            }                         
                        lcd_goto(0);                 
                        lcd_puts("BOTH SWITCHES");         
                        b = 1;                         
                    }                                 
            }                                         
      
    }
}
 
Last edited:

djsfantasi

Joined Apr 11, 2010
8,078
Thank you djsfantasi for the replies:):) I really appreciate it. Here is the full the code for the whole program. I am working line by line thru the program now. Here is the code:
Code:
#define _LEGACY_HEADERS
#include <htc.h>
#include "lcd.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////DEFINE STATEMENTS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define    SWITCH_1    RB0
#define    SWITCH_2    RB1

#define DelayS(T)    {unsigned char i; for (i = 0; i < T * 10; i++) __delay_ms(100);}    //Delay Macro
#define _XTAL_FREQ    4000000                                    //Needs to be set for __delay_ms

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////CONFIGURATION FUSES (BITS)
////Master Clear Reset enabled & Internal RC No Clock & Watchdog Timer Disable & Power Up Timer On & Brown Out Reset Disabled &
////Low Voltage Porgramming Disabled & Code Unprotect
__CONFIG (MCLRDIS & INTIO & WDTDIS & PWRTEN & BORDIS & LVPDIS & UNPROTECT);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

main()          
{
PORTA = 0x00;                //PORTA is cleared  
PORTA = 0xE9;                //(ob11101001 binary) (0xE9 hex)
                    //PORTA7:A0 is set to high,high,high,low,high,low,low,high
                                                   
PORTB = 0x00;                //PORTB is cleared
PORTB = 0xFF;                //(0b11111111 binary) (0xFF hex)
                    //PORTB7:B0 is set to high,high,high,high,high,high,high,high
                                                   
PORTC = 0x00;                //PORTC is cleared
PORTC = 0xF0;                //PORTC is set to high, high, high, high, low, low, low, low


TRISA = 0xE9;                //(0b11101001 binary)    (0xE9 hex)
                    //LCD--RA1, RA2, RA4 are Control lines for LCD
                    //RA0, RA3, RA5, RA6 are set for switches
                    //PORTA7:A0 is set to input,input,input,output,input,output,output,input

TRISB = 0xFF;                //(0b11111111 binary) (0xFF hex)
                    //All of PORTB are set for switches
                    //PORTB7:B0 is set to input,input,input,input,input,input,input,input
                                               

TRISC = 0x00;                //(0b00000000 binary) (0x00 hex)
                    //All of PORTC is set for LCD
                    //PORTC7:C0 is set to output,output,output,output,output,output,output,output
                                                   
ANSEL = 0;                //Initialize A/D Ports off
ANSELH = 0;                //Initialize ........
ADCON0 = 0;
ADCON1 = 0;
CM1CON0 = 0;                //Initialize Comparator 1 off
CM2CON0 = 0;                //Initialize Comparator 2 off
                                   

OPTION = 0b01010101;            //OPTION REG
                    //xbxxxxx101  1:64
                    //xbxxxx0xxx  Prescaler set to Timer0
                    //xbxxx1xxxx  (T0SE) set to Increment on high-to-low transition on T0CKI pin
                    //xbxx0xxxxx  (T0CS) Internal instruction cycle clock
                    //xbx1xxxxxx  (INTEDG) Interrupt on rising edge of INT pin
                    //xb0xxxxxxx  (RBPU) PORTB pull-ups are enabled by individual PORT latch values
RBPU = 1;                //Don't think I need this, but to be safe...PORTB Weak Internal Pullups enabled
WPUB0 = 1;                //COL_1 Weak pullup is individually ENABLED--INPUT
WPUB1 = 1;                //COL_2 Weak Pullup is inidivdually ENABLED--INPUT
WPUB2 = 1;                //Not being used...Weak Pullup is individually ENABLED
WPUB3 = 1;                //ROW_1 Weak Pullup is individually ENABLED--INPUT
WPUB4 = 1;                //ROW_2 Weak Pullup is individually ENABLED--INPUT
WPUB5 =    1;                 //ROW_3 Weak Pullup is individually ENABLED--INPUT
WPUB6 = 1;                //ROW_4 Weak Pullup is individually ENABLED--INPUT
WPUB7 = 1;                //COL_3 Weak Pullup is individually ENABLED--INPUT

lcd_init();                //LCD Display is Initialized...See LCD.C file and LCD.h
lcd_clear();

char a;                    //
char b;                    //

//The following Code will Display the name of the tester and the Rev. only once for a total of
//2 seconds.  This happens right when you turn it on.  I had to keep it out of the while statement
//because it keeps cycling over and over again.  Therefore it had to be before that.  After the
//Name and Rev. is displayed for 2 seconds, we have to clear the LCD of the Name and Rev..  Therefore
//the use of lcd_clear is required to clear the screen.

lcd_goto(0);                //LCD First Line First Position
lcd_puts("Button Tester");        //Display "Button Tester"
lcd_goto(40);                //LCD Second Row First Position
lcd_puts("Rev. 1.0");            //Display Rev Number
__delay_ms(2000);            //Display Name and Rev. for 2000 ms (2 seconds)
lcd_clear();                //The Screen is cleared of Name and Rev.

while (1)

    {
       
       
        lcd_goto(0);                //Go to Line 1 Position 1
        lcd_puts("Button Tester");        //Display ""
        lcd_goto(40);                //Go to Line 2 Position 1
        lcd_puts("Press Button");        //Display "Press Button"
        b = 0;
       
        while (SWITCH_1 == 0)            //While SW1 is 0(Low)(Pressed)...
            {
                __delay_ms(10);        //Delay 10 ms (Switch Debounce)
                while (HEADUP == 0)    //Check to see if SW1 is low after Debounce
                    {
                        for (a = b; a < 1; a++)            //For Statement to cycle lcd_clear only once
                            {
                                lcd_clear();        //lcd_clear should only cycle once because a = (b = 0)
                            }
               
                        lcd_goto(0);                //On LCD go to Position 1 (Line 1, Position 0)
                        lcd_puts("SWITCH 1");            //Display ""
                        b = 1;                    //Char B is now 1 so For Statement cycles once
                    }
            }
        while (SWITCH_2 == 0)                          
            {                                          
                __delay_ms(10);                      
                while (HEADDOWN == 0)                  
                    {                      
                        for (a = b; a < 1; a++)          
                            {                          
                                lcd_clear();
                            }                                                                      
                        lcd_goto(0);              
                        lcd_puts("Head Down");          
                        b = 1;                          
                    }
                                                       
            }                                          
        while ((SWITCH_1 == 0) && (SWITCH_2 == 0)                //While SW1 and SW2 are 0(Low)(Pressed)...
            {                                          
                __delay_ms(10);                          
                while ((SWITCH_1 == 0) && (SWITCH_2 == 0))                      
                    {                                  
                        for (a = b; a < 1; a++)          
                            {                              
                                lcd_clear();          
                            }                          
                        lcd_goto(0);                  
                        lcd_puts("BOTH SWITCHES");          
                        b = 1;                          
                    }                                  
            }                                          
       
    }
}
Line 137 has a missing paren. Did you try to compile this? You should have before posting.

And you didn’t correct the overlapping logic. If either switch 1 or 2 is pressed, you will never see the condition for both switches pressed.
 

Pushkar1

Joined Apr 5, 2021
416
I would like to make a simple embedded program that will Display what is happening on and LCD screen. When Switch One is pressed, it displays "Switch One". When Switch 2 is pressed it displays Switch 2. When both switches are pressed is displays "Both Switch". For the life of my I cannot get it to display "Both Switches" on the LCD when both switches are pressed. I have Switch 1 attached to RB0 and Switch 2 attached to RB1. Both switches display properly until I press both switches and what usually displays is the first one pressed. Please help!!
My advice to you is that when you ask the question for embedded project, the first thing you should do is make a block diagram that shows how the input & output device is connected to the microcontroller.

Have you tested the LED with the switch? Initially the LED is off and when you press the switch the LED should be turn ON. If you have successfully done this then your next step should be display message on LCD. If you have done it, after that you have to write a program for LCD with switches
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
Thank you djsfantasi and Pushkar1 for the replies. Happy to report that I got it working. Thanks you guys for the help. I appreciate it very much. The corrected code is in the code blocks above. I did your suggestion djsfantasi and I corrected the overlapping logic by correcting Lines 107 and 122. The above code now lets me hold both SWITCH_1 and SWITCH_2 at the same time and it Display "Both Switches"!!

Now I would like to learn a little bit from this simple circuit. If I were to add a Switch 3 but put a diode between the switch and Port to where it could only flow one way, would I simply make the Ports instead of High and set as an Input to Low and set as an Output. Would I put into the code before the code for Switch_3 PORTB = 0x00; and TRISB = 0xFF;

Something like this:
Code:
PORTB = 0xFF;  //In original code this was PORTB = 0X00;
TRISB = 0x00;   //In original code this was TRISB = 0XFF;

while (SWITCH_3 == 0)                        //While SW1 and SW2 are 0(Low)(Pressed)...
            {                                        
                __delay_ms(10);                        
                while (SWITCH_3 == 0)                    
                    {                                
                        for (a = b; a < 1; a++)        
                            {                            
                                lcd_clear();        
                            }                        
                        lcd_goto(0);                
                        lcd_puts("Switch 3");        
                        b = 1;                        
                    }                                
            }
Just doing this as a learning thing in case I wanna switch things around:):)

Thanks again guys for the help!!
 
Last edited:

Thread Starter

beeson76

Joined Apr 19, 2010
211
Sorry for the delay in getting the schematic to you guys. As you can see from the schematic I have 2 Diodes on Switch 7 and 2 Diodes on Switch 8. IF I reverse the Diodes the program runs correctly and when Switches 7 and 8 are pressed they are Displayed correctly on the Display. I would like to have the Diodes reversed though. So I somehow need to reverse the way the Pins are reading from Input to Output...or from High to Low or vice versa. My concept of Inputs, Outputs, Highs, and Lows, are not quite up to par yet:):) Any help is greatly appreciated!!:):)

Here is the code in which runs perfectly IF the Diodes are reversed. It may not be able to even be done. I have spent hours trying to get this to work and I am now at a loss as to what I need to do. My brain is fried!!

Thanks again for any help you guys!!

Code:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define _LEGACY_HEADERS
#include <htc.h>
#include "lcd.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////DEFINE STATEMENTS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define    SWITCH1        RB0
#define    SWITCH2        RB1
#define SWITCH3        RB2
#define SWITCH4        RB3
#define SWITCH5        RB4
#define SWITCH6        RB5



#define DelayS(T)        {unsigned char i; for (i = 0; i < T * 10; i++) __delay_ms(100);}    //Delay Macro
//#define _XTAL_FREQ                4000000                                //Needs to be set for __delay_ms
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////CONFIGURATION FUSES (BITS)
////Master Clear Reset enabled & Internal RC No Clock & Watchdog Timer Disable & Power Up Timer On & Brown Out Reset Disabled &
////Low Voltage Porgramming Disabled & Code Unprotect
__CONFIG (MCLRDIS & INTIO & WDTDIS & PWRTEN & BORDIS & LVPDIS & UNPROTECT);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


main()
{
   PORTA = 0x00;                                        //PORTA is cleared   
   PORTA = 0xE9;                                        //(ob11101001 binary) (0xE9 hex)
                                                    //PORTA7:A0 is set to high,high,high,low,high,low,low,high
                                                    
   PORTB = 0x00;                                        //PORTB is cleared
   PORTB = 0xFF;                                        //(0b11111111 binary) (0xFF hex) 
                                                    //PORTB7:B0 is set to high,high,high,high,high,high,high,high
                                                    
   PORTC = 0x00;                                        //PORTC is cleared
   PORTC = 0xF0;                                        //PORTC is set to high, high, high, high, low, low, low, low


   TRISA = 0xE9;                                        //(0b11101001 binary)    (0xE9 hex)
                                                    //LCD--RA1, RA2, RA4 are Control lines for LCD
                                                    //RA0, RA3, RA5, RA6 are set for switches
                                                    //PORTA7:A0 is set to input,input,input,output,input,output,output,input

   TRISB = 0xFF;                                        //(0b11111111 binary) (0xFF hex)
                                                    //All of PORTB are set for switches
                                                    //PORTB7:B0 is set to input,input,input,input,input,input,input,input
                                                

   TRISC = 0x00;                                        //(0b00000000 binary) (0x00 hex)
                                                    //All of PORTC is set for LCD
                                                    //PORTC7:C0 is set to output,output,output,output,output,output,output,output
                                                    

   ANSEL = 0;                                            //Initialize A/D Ports off
   ANSELH = 0;                                            //Initialize ........
   ADCON0 = 0;
   ADCON1 = 0;
   CM1CON0 = 0;                                        //Initialize Comparator 1 off
   CM2CON0 = 0;                                        //Initialize Comparator 2 off
                                    

   OPTION = 0b01010101;                                //OPTION REG
                                                    //xbxxxxx101  1:64
                                                    //xbxxxx0xxx  Prescaler set to Timer0
                                                    //xbxxx1xxxx  (T0SE) set to Increment on high-to-low transition on T0CKI pin
                                                    //xbxx0xxxxx  (T0CS) Internal instruction cycle clock
                                                    //xbx1xxxxxx  (INTEDG) Interrupt on rising edge of INT pin
                                                    //xb0xxxxxxx  (RBPU) PORTB pull-ups are enabled by individual PORT latch values
   RBPU = 0;                                            //Don't think I need this, but to be safe...PORTB Weak Internal Pullups enabled
   WPUB0 = 1;                                            //COL_1 Weak pullup is individually ENABLED--INPUT
   WPUB1 = 1;                                            //COL_2 Weak Pullup is inidivdually ENABLED--INPUT
   WPUB2 = 1;                                            //Not being used...Weak Pullup is individually ENABLED
   WPUB3 = 1;                                            //ROW_1 Weak Pullup is individually ENABLED--INPUT
   WPUB4 = 1;                                            //ROW_2 Weak Pullup is individually ENABLED--INPUT
   WPUB5 = 1;                                             //ROW_3 Weak Pullup is individually ENABLED--INPUT
   WPUB6 = 1;                                            //ROW_4 Weak Pullup is individually ENABLED--INPUT
   WPUB7 = 1;                                            //COL_3 Weak Pullup is individually ENABLED--INPUT


   lcd_init();                                            //LCD Display is Initialized...See LCD.C file and LCD.h
   lcd_clear();

   char a;                                                    //
   char b;                                                    //

//The following Code will Display the name of the tester and the Rev. only once for a total of
//2 seconds.  This happens right when you turn it on.  I had to keep it out of the while statement
//because it keeps cycling over and over again.  Therefore it had to be before that.  After the
//Name and Rev. is displayed for 2 seconds, we have to clear the LCD of the Name and Rev..  Therefore
//the use of lcd_clear is required to clear the screen. 

   lcd_goto(0);                                            //
   lcd_puts("SWITCH_TESTER");                                //
   lcd_goto(40);                                            //
   lcd_puts("Rev. 0.0");                                        //
   __delay_ms(2000);                                        //Display Name and Rev. for 2000 ms (2 seconds)
   lcd_clear();                                            //The Screen is cleared of Name and Rev.

while (1)

    {
        lcd_goto(0);                                    //Go to Line 1 Position 1
        lcd_puts("SWITCH Tester");                        //Display ""
        lcd_goto(40);                                    //Go to Line 2 Position 1
        lcd_puts("Press SWITCH");                        //Display "Press SWITCH"
        b = 0;
        
        while ((SWITCH1 == 0) && (SWITCH5 == 1))                                //While SW1 is 0(Low)(Pressed)...
            {
                __delay_ms(10);                            //Delay 10 ms (Switch Debounce)
                while ((SWITCH1 == 0) && (SWITCH5 == 1))                        //Check to see if SW1 is low after Debounce
                    {
                        for (a = b; a < 1; a++)            //For Statement to cycle lcd_clear only once
                            {
                                lcd_clear();            //lcd_clear should only cycle once because a = (b = 0)
                            }
                
                        lcd_goto(0);                    //On LCD go to Position 1 (Line 1, Position 0)
                        lcd_puts("SWITCH 1");            //Display ""
                        b = 1;                            //Char B is now 1 so For Statement cycles once
                    }
            }
        
        while ((SWITCH2 == 0) && (SWITCH6 == 1))                             //While ROW_2 is 0(Low)(Pressed)...
            {                                            //
                __delay_ms(10);                            //
                while ((SWITCH2 == 0) && (SWITCH6 == 1))                        //
                    {                                    //
                        for (a = b; a < 1; a++)            //
                            {                            //
                                lcd_clear();            //...
                            }                            //...       
                        lcd_goto(0);                    //...
                        lcd_puts("SWITCH 2");            //Display ""
                        b = 1;                            //
                    }
                                                        //
            }                                            //
        
        while ((SWITCH5 == 0) && (SWITCH1 == 1))                                //While ROW_1 is 0(Low)(Pressed)...
            {                                            //
                __delay_ms(10);                            //
                while ((SWITCH5 == 0) && (SWITCH1 == 1))                        //
                    {                                    //...
                        for (a = b; a < 1; a++)            //
                            {                            //   
                                lcd_clear();            //...
                            }                            //...
                        lcd_goto(0);                    //...
                        lcd_puts("SWITCH 5");            //Display ""
                        b = 1;                            //
                    }                                    //
            }                                            //
        while ((SWITCH6 == 0) && (SWITCH2 == 1))                            //If ROW 4 is LOW
            {                                            //...
                __delay_ms(10);                            //
                while ((SWITCH6 == 0) && (SWITCH2 == 1))                            //
                    {                                    //
                        for (a = b; a < 1; a++)            //
                            {                            //
                                lcd_clear();            //...
                            }                            //...
                        lcd_goto(0);                    //...
                        lcd_puts("SWITCH 6");            //Display ""
                        b = 1;                            //
                    }                                    //   
            }                                            //
                
    while (SWITCH3 == 0)                                //and If ROW 1 is LOW...
            {                                            //...
                __delay_ms(10);                            //
                while (SWITCH3 == 0)                            //
                    {                                    //
                        for (a = b; a < 1; a++)            //
                            {                            //
                                lcd_clear();            //...
                            }                            //...
                        lcd_goto(0);                    //...
                        lcd_puts("SWITCH 3");                //Display ""
                        b = 1;                            //
                    }                                    //
            }                                            //
        
    while (SWITCH4 == 0)                            //if ROW 2 is LOW...
            {                                            //...
                __delay_ms(10);                            //
                while (SWITCH4 == 0)                        //
                    {                                    //
                        for (a = b; a < 1; a++)            //
                            {                            //
                                lcd_clear();            //...
                            }                            //...       
                        lcd_goto(0);                    //...
                        lcd_puts("SWITCH 4");            //Display ""
                        b = 1;                            //
                    }                                    //
            }                                            //
        
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Tried changing PortB to High by using
//PORTB - 0xFF; 
//Input by using
//TRISB = 0xFF;
//
//I am sure code codes goes here for Switchs 7 and 8
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        while ((SWITCH1 == 0) && (SWITCH5 == 0))                               
            {                                            //...
                __delay_ms(10);                            //
                while ((SWITCH1 == 0) && (SWITCH5 == 0))
                        {                                    //
                            for (a = b; a < 1; a++)            //
                                {                            //
                                    lcd_clear();            //...
                                }                            //...
                            lcd_goto(0);                    //...
                            lcd_puts("SWITCH 7");                //Display "SW7"
                            b = 1;                            //
                        }
            }                                    //

                                                        //

        while ((SWITCH2 == 0) && (SWITCH6 == 0))                            //
            {                                            //...
                __delay_ms(10);                            //
                while ((SWITCH2 == 0) && (SWITCH6 == 0))
                        {                                    //
                                for (a = b; a < 1; a++)            //
                            {                            //
                                lcd_clear();            //...
                            }                            //...
                        lcd_goto(0);                    //...
                        lcd_puts("SWITCH 8");        //Display ""
                        b = 1;                            //
                        }
            }

}
}
 

Attachments

Top