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.

Need help with a Switch setup with PIC16F886

Status
Not open for further replies.

beeson76

Newbie
Joined
Dec 15, 2021
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
39
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 so on. For Switches 7 and 8 I would like to have a diode in Circuit as you can see from the Schematic.

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))          
            {                                            
                __delay_ms(10);                           
                while ((SWITCH2 == 0) && (SWITCH6 == 1))                       
                    {                                    
                        for (a = b; a < 1; a++)            
                            {                            
                                lcd_clear();            
                            }                                  
                        lcd_goto(0);                    
                        lcd_puts("SWITCH 2");            
                        b = 1;                            
                    }
                                                        
            }                                            
        
        while ((SWITCH5 == 0) && (SWITCH1 == 1))                               
            {                                           
                __delay_ms(10);                            
                while ((SWITCH5 == 0) && (SWITCH1 == 1))                       
                    {                                    
                        for (a = b; a < 1; a++)           
                            {                              
                                lcd_clear();            
                            }                            
                        lcd_goto(0);                    
                        lcd_puts("SWITCH 5");            
                        b = 1;                           
                    }                                    
            }                                            
        while ((SWITCH6 == 0) && (SWITCH2 == 1))                            
            {                                           
                __delay_ms(10);                           
                while ((SWITCH6 == 0) && (SWITCH2 == 1))                           
                    {                                    
                        for (a = b; a < 1; a++)            
                            {                          
                                lcd_clear();            
                            }                            
                        lcd_goto(0);                    
                        lcd_puts("SWITCH 6");            
                        b = 1;                           
                    }                                      
            }                                            
                
    while (SWITCH3 == 0)                               
            {                                          
                __delay_ms(10);                         
                while (SWITCH3 == 0)                          
                    {                                    
                        for (a = b; a < 1; a++)           
                            {                           
                                lcd_clear();          
                            }                            
                        lcd_goto(0);                   
                        lcd_puts("SWITCH 3");               
                        b = 1;                            
                    }                                   
            }                                           
        
    while (SWITCH4 == 0)                           
            {                                            //...
                __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

  • Switch Test.pdf
    110.8 KB · Views: 143

its been over 25 ears since i last used a PIC. nice gadget.

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 so on. For Switches 7 and 8 I would like to have a diode in Circuit as you can see from the Schematic.

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!!:):)

you wrote "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 assume switches 1 through 6 also display correctly.

then you wrote "i would like to have the diodes reversed..."
??do you mean you want the diodes the way they are in the schematic,
even though that doesn't work, while reversing them does work?

In either case, i think you need to understand how the I/O on the PIC works and
how the diode works.

in the following picture, R1 is a pull up resistor. it looks like you set portb to have weak
pull ups, therefore, the resistor is INSIDE the PIC, your switch is outside.

when the switch is not pressed, the input looks high (resistor to VCC)
current flows from VCC through the resistor into the input pin

when the switch is pressed, the input is pulled low (GND to input)
current flows out of the input pin to GND


1639595709030.jpeg


diodes only conduct in the direction of the arrow
the voltage at the anode has to be about 1 V greater than the voltage at the cathode
for current to "flow" from anode to cathode.
current will not flow cathode to anode.

diode.png


when the diodes are arranged as in your schematic, anode is connected
to GND, while the cathode is connected to an input pin. thus the cathode
is at greater voltage than anode, no current with switch open or closed.

when you reverse the diode, and close the switch, current leaves the
input pin, goes through the diode to ground, so the PIC sees the
low voltage, almost as if there was no diode, and just a switch.

issues:
all diodes, including 1N4007, have a forward drop, so you may be getting close to the PIC's
maximum voltage for a low. you might want a diode with a smaller forward drop.

there are two more pins in port b, why not use them for switches 7 and 8?
this treat all switches the same, and removes a complication in the code
and lose 4 diodes - saves space and reduces component count

"So I somehow need to reverse the way the Pins are reading from Input to Output...or from High to Low or vice versa."
i don't think so
you cannot ignore the laws of physics
you have to use components within their designed operation
 
Thank you very much wwfeldman. You explained if very nicely indeed:):)

Here are answers to your questions:

Yep, the buttons all display correctly on the Display when pressed. And as mentioned with the diodes REVERSED FROM the way the schematic shows, they work perfectly. I was just wanting to reverse the diodes (like what the schematic shows) to see how it can be done. The reason for it cause in the upcoming future, I may want to do a little project that does something similar to this.

So a couple of questions:):)

1. I have the Pins listed as Inputs and Highs with the weak pullups internally enabled. So just changing these pins that Switch 7 and 8 are attached to Outputs and Lows will no do the trick? I think I figured that out from wracking my brain on it...unless the code was just simply wrong.

2. Could this be the same situation as having 2 LED's (red and green) attached to the same pin, but each in a different direction. And then by which color the LED is...that would be an indication on if the pin is High or Low?? or Input or Output??

Thanks again for the nice reply. I really appreciate it very much!!:):)

Mike
 

1. I have the Pins listed as Inputs and Highs with the weak pullups internally enabled. So just changing these pins that Switch 7 and 8 are attached to Outputs and Lows will no do the trick? I think I figured that out from wracking my brain on it...unless the code was just simply wrong.

nope
if you change the pins to outputs, the PIC will try to output on them and will not look for inputs
you need them to be inputs to read the switches

if the code was wrong, then switches 1 through 6 would not work either.
if the code was wrong, then switches 7 and 8 would not work when the diodes were reversed from your schematic.

2. Could this be the same situation as having 2 LED's (red and green) attached to the same pin, but each in a different direction. And then by which color the LED is...that would be an indication on if the pin is High or Low?? or Input or Output??

LED requires the anode voltage to be greater than the cathode voltage, like any diode.
so if the red LED indicated low, its anode would have to be at VCC and cathode at the pin
then for the green LED to indicate high, its anode would be at the pin and its cathode at ground.
 
Thanks again wwfeldman for a nice response:):)!! Im still trying to take this all in so if if questions seem a little weird, its because I am learning:):)

1. Would there be anyway of coding this to where the Diodes could be reversed could be reversed like the Schematic shows?
2. Can I change the way this Pins act, or once they are coded for Input, Output, and High and Low, they have to stay that way.
3. What does it mean to be High and Low at the pin mean? Would Low at the pin be (in simple terms) connected to ground and High at the pin be connected to voltage. Could I somehow code for the 2 LEDs in this situation?

4. I thought of using Pins 7 and 8 for the 2 diodes circuits but, could I still keep the diodes in the circuit when I program it. That is where I am getting stumped. If I would do this, how would I code the Pins 7 and 8? Going by the schematic, would I code them as Inputs, but would I define them as High or Low?

Thanks for patience in helping me you guys. I am just trying to learn:):) And I certainly appreciate the help very much:):)
 

1. Would there be anyway of coding this to where the Diodes could be reversed could be reversed like the Schematic shows?
no - you cannot violate the laws of physics, nor can you make electronic components do things they can't do
2. Can I change the way this Pins act, or once they are coded for Input, Output, and High and Low, they have to stay that way.
? once you code the pin behavior, it is not likely you can change it
i am not up to date on PICs, but i expect not
3. What does it mean to be High and Low at the pin mean? Would Low at the pin be (in simple terms) connected to ground and High at the pin be connected to voltage. Could I somehow code for the 2 LEDs in this situation?
in simple terms, yes
in more detail, low is usually less than 0.8 V, high is usually more than 2.4 V, at least for 5V logic
behavior is undefined in the range from 0.8V to 2.4V
3V, 3.3V etc will have different limits, but the same concept

to work the two LEDs, the pin has to be an output pin, you control the high or low with the program
NOTE: typical LEDs drop 2V when conducting and need at least 5mA to be bright enough to see
10 mA is better. examine the LED data sheet for minimum and maximum current

AND examine PIC data to see how much current an output pin can handle
if the PIC output configuration cannot handle enough current, it won't work

you should add a resistor in series with each LED to ensure you don't make the
pin handle more current than the spec sheet says

4. I thought of using Pins 7 and 8 for the 2 diodes circuits but, could I still keep the diodes in the circuit when I program it. That is where I am getting stumped. If I would do this, how would I code the Pins 7 and 8? Going by the schematic, would I code them as Inputs, but would I define them as High or Low?
why do you want those diodes?
why do you want them oriented in a way that does not work?

when you make a pin an output, the program in the PIC controls the output level
when you make a pin an input pin, the external circuit determines the level and the PIC reads it
it is not likley that the PIC can control the level of an input pin

if you remove the four 1N4007s, and connect switches 7 and 8 to RB6 and RB7,
just as switches 1 through 6 are connected to RB0 through RB5, all 8 switches
should work the same way
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top