Using 2 different ports for the same input

Discussion in 'Embedded Systems and Microcontrollers' started by beeson76, Jun 3, 2010.

  1. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    I am using a PIC16F886 chip.

    I have a question concerning using 2 different ports. Here is a simple example to illustrate (in a rough pseudo code) of what I would like to do.

    If RA1 is on
    and
    RB1 is on
    Turn RC1 on.

    I would like to have it to where 2 conditions from different ports are true for output on RC1.

    For example, if button on RA1 is pushed, and button on RB1 is pushed, it lights RC1.

    I would like to do it all in a define statement using possibly a macro if possible though. Any help is greatly appreciated.

    Thanks.
     
  2. wannaBinventor

    Member

    Apr 8, 2010
    179
    4
    I'm sorry that I can't help you on this, but I have to ask - mostly for my own learning: Why can't you just put the inputs on the same port?

    Or are you using more inputs than there are on one port?
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Something like this?
    movf PORTC, W
    movwf PORTCtemp
    movlw b'00000010'
    andwf PORTA, W
    andwf PORTB, W
    bcf PORTCtemp, 1
    iorwf PORTCtemp, W
    movwf PORTC

    Not too good on macros though.
     
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Or if it's C (CCS variety):
    Code ( (Unknown Language)):
    1.  
    2.   if (!bit_test(porta, 1) && !bit_test(portb, 1))
    3.     bit_clear(portc, 1);
    4.   else
    5.     bit_set(portc, 1);
    6.  
    Better to have both inputs on port B because it has internal pullups and you save a resistor.

    I don't know how much of the above code you can put in a #define statement.
     
  5. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Sorry for the late reply, but I was just wanting to give the program I ended up with. Hopefully it can help someone in the future. Any questions, please ask, and I will try to answer them.

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include "lcd.h"
    4.  
    5. #define  KEY_PORT               PORTB                      
    6. #define  KEY_SET(bits)          KEY_PORT |= (bits)         
    7. #define  KEY_CLR                KEY_PORT &=~ (bits)        
    8. #define  KEY_FLP(bits)          KEY_PORT ^= (bits)         
    9. #define  KEY_IN(bits)           TRISB |= (bits)            
    10. #define  KEY_OUT(bits)          TRISB &=~ (bits)           
    11. #define  KEY_COL                0b10000011                  //10000011           
    12. #define  KEY_ROW                0b01111000                  //01111000
    13. #define  DelayS(T)              {unsigned char i; for (i = 0; i < T * 10; i++) __delay_ms(100);}
    14. #define  _XTAL_FREQ             4000000                     //Needs to be set for __delay_ms
    15.  
    16. __CONFIG (XT, WDTDIS, PWRTEN, BORDIS, LVPDIS, UNPROTECT);
    17.  
    18.  
    19. unsigned char key_read(unsigned char in_bits, unsigned char out_bits)
    20.     {
    21.         KEY_IN(in_bits);
    22.         KEY_SET(out_bits);
    23.         KEY_OUT(out_bits);
    24.         return KEY_PORT & in_bits;
    25.     }
    26.  
    27. unsigned char key_detect(void)
    28.     {
    29.         unsigned char tmp1, tmp2;
    30.         tmp2 = key_read(KEY_ROW, KEY_COL);
    31.         tmp1 = key_read(KEY_COL, KEY_ROW);
    32.         return tmp1 | tmp2;
    33.     }
    34.  
    35. main ()                                        
    36. {                                                                              
    37.  
    38. TRISA = 0x00;                                       //Set PORTA to outputs
    39. TRISB = 0xFF;                                       //Set PORTB to inputs
    40. TRISC = 0x00;                                       //Set PORTC to outputs
    41. ANSEL = 0;
    42. ANSELH = 0;
    43. CM1CON0 = 0;
    44. CM2CON0 = 0;
    45.  
    46. OPTION = 0b01010101;
    47.  
    48. lcd_init();
    49.  
    50. char key;
    51.  
    52. while (key_detect() == 1)                           //Loops When Key is Pressed
    53. continue;
    54. {
    55.  
    56. key = key_detect();             //Referring to key_detect() function.  Returns key pressed.
    57.  
    58. switch (key)
    59.     {
    60.         case 0b00100001:                                //Column 1 Row 4--Switch 12
    61.             RA0 = 1;
    62.             lcd_goto(0x00);
    63.             lcd_puts("Switch 1");
    64.             DelayS(1);
    65.             lcd_clear();
    66.             break;
    67.         case 0b01000001:                                //Column 1 Row 3--Switch 11
    68.             //RA1 = 1;
    69.             lcd_goto(0x00);
    70.             lcd_puts("Switch 2");
    71.             DelayS(1);
    72.             lcd_clear();
    73.             break;
    74.         case 0b00010001:                                //Column 1 Row 2--Switch 10
    75.             //RA2 = 1;
    76.             lcd_goto(0x00);
    77.             lcd_puts("Switch 3");
    78.             DelayS(1);
    79.             lcd_clear();
    80.             break;
    81.         case 0b00001001:                                //Column 1 Row 1--Switch 9
    82.             lcd_goto(0x00);
    83.             lcd_puts("Switch 4");
    84.             DelayS(1);
    85.             lcd_clear();
    86.             break;
    87.         case 0b00100010:                                //Column 2 Row 4--Switch 8
    88.             lcd_goto(0x00);
    89.             lcd_puts("Switch 5");
    90.             DelayS(1);
    91.             lcd_clear();
    92.             break;
    93.         case 0b01000010:                                //Column 2 Row 3--Switch 7
    94.             lcd_goto(0x00);
    95.             lcd_puts("Switch 6");
    96.             DelayS(1);
    97.             lcd_clear();
    98.             break;
    99.         case 0b00010010:                                //Column 2 Row 2--Switch 6
    100.             lcd_goto(0x00);
    101.             lcd_puts("Switch 7");
    102.             DelayS(1);
    103.             lcd_clear();
    104.             break;
    105.         case 0b00001010:                                //Column 2 Row 1--Switch 5
    106.             lcd_goto(0x00);
    107.             lcd_puts("Switch 8");
    108.             DelayS(1);
    109.             lcd_clear();
    110.             break;
    111.         case 0b10100000:                                //Column 3 Row 4--Switch 4
    112.             lcd_goto(0x00);
    113.             lcd_puts("Switch 9");
    114.             DelayS(1);
    115.             lcd_clear();
    116.             break;
    117.         case 0b11000000:                                //Column 3 Row 3--Switch 3
    118.             lcd_goto(0x00);
    119.             lcd_puts("Switch 10");
    120.             DelayS(1);
    121.             lcd_clear();
    122.             break;
    123.         case 0b10010000:                                //Column 3 Row 2--Switch 2
    124.             lcd_goto(0x00);
    125.             lcd_puts("Switch 11");
    126.             DelayS(1);
    127.             lcd_clear();
    128.             break;
    129.         case 0b10001000:                                //Column 3 Row 1--Switch 1
    130.             lcd_goto(0x00);
    131.             lcd_puts("Switch 12");
    132.             DelayS(1);
    133.             lcd_clear();
    134.             break;
    135.     }
    136.  
    137.  
    138. }
    139. }
    140.  
    Sorry for lack of comments. I was in a hurry to get this posted. Again, please ask if you need something answered.

    Just so you know, I have a readout on a LCD display.
     
  6. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5
    FYI, it's not very good code practice to use a bunch of macros. Mostly because it's just harder to read and more complex than it needs to be.
     
Loading...