Using 2 different ports for the same input

Thread Starter

beeson76

Joined Apr 19, 2010
211
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.
 
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?
 

Markd77

Joined Sep 7, 2009
2,806
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.
 

John P

Joined Oct 14, 2008
2,026
Or if it's C (CCS variety):
Rich (BB code):
  if (!bit_test(porta, 1) && !bit_test(portb, 1))
    bit_clear(portc, 1);
  else
    bit_set(portc, 1);
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.
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
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.

Rich (BB code):
#include <htc.h>
#include "lcd.h"

#define  KEY_PORT				PORTB						
#define  KEY_SET(bits)			KEY_PORT |= (bits)			
#define  KEY_CLR				KEY_PORT &=~ (bits)			
#define	 KEY_FLP(bits)			KEY_PORT ^= (bits)			
#define  KEY_IN(bits)			TRISB |= (bits)				
#define  KEY_OUT(bits)			TRISB &=~ (bits)			
#define  KEY_COL				0b10000011					//10000011			 
#define  KEY_ROW				0b01111000					//01111000
#define  DelayS(T)				{unsigned char i; for (i = 0; i < T * 10; i++) __delay_ms(100);}
#define  _XTAL_FREQ				4000000						//Needs to be set for __delay_ms

__CONFIG (XT, WDTDIS, PWRTEN, BORDIS, LVPDIS, UNPROTECT);


unsigned char key_read(unsigned char in_bits, unsigned char out_bits)
	{
		KEY_IN(in_bits);
		KEY_SET(out_bits);
		KEY_OUT(out_bits);
		return KEY_PORT & in_bits;
	}

unsigned char key_detect(void)
	{
		unsigned char tmp1, tmp2;
		tmp2 = key_read(KEY_ROW, KEY_COL);
		tmp1 = key_read(KEY_COL, KEY_ROW);
		return tmp1 | tmp2;
	}

main ()											
{																				

TRISA = 0x00;										//Set PORTA to outputs
TRISB = 0xFF;										//Set PORTB to inputs
TRISC = 0x00;										//Set PORTC to outputs
ANSEL = 0;
ANSELH = 0;
CM1CON0 = 0;
CM2CON0 = 0;

OPTION = 0b01010101;

lcd_init();

char key;

while (key_detect() == 1)  							//Loops When Key is Pressed
continue;
{

key = key_detect();				//Referring to key_detect() function.  Returns key pressed.

switch (key)
	{
		case 0b00100001:								//Column 1 Row 4--Switch 12
			RA0 = 1;
			lcd_goto(0x00);
			lcd_puts("Switch 1");
			DelayS(1);
			lcd_clear();
			break;
		case 0b01000001:								//Column 1 Row 3--Switch 11
			//RA1 = 1;
			lcd_goto(0x00);
			lcd_puts("Switch 2");
			DelayS(1);
			lcd_clear();
			break;
		case 0b00010001:								//Column 1 Row 2--Switch 10
			//RA2 = 1;
			lcd_goto(0x00);
			lcd_puts("Switch 3");
			DelayS(1);
			lcd_clear();
			break;
		case 0b00001001:								//Column 1 Row 1--Switch 9
			lcd_goto(0x00);
			lcd_puts("Switch 4");
			DelayS(1);
			lcd_clear();
			break;
		case 0b00100010:								//Column 2 Row 4--Switch 8
			lcd_goto(0x00);
			lcd_puts("Switch 5");
			DelayS(1);
			lcd_clear();
			break;
		case 0b01000010:								//Column 2 Row 3--Switch 7
			lcd_goto(0x00);
			lcd_puts("Switch 6");
			DelayS(1);
			lcd_clear();
			break;
		case 0b00010010:								//Column 2 Row 2--Switch 6
			lcd_goto(0x00);
			lcd_puts("Switch 7");
			DelayS(1);
			lcd_clear();
			break;
		case 0b00001010:								//Column 2 Row 1--Switch 5
			lcd_goto(0x00);
			lcd_puts("Switch 8");
			DelayS(1);
			lcd_clear();
			break;
		case 0b10100000:								//Column 3 Row 4--Switch 4
			lcd_goto(0x00);
			lcd_puts("Switch 9");
			DelayS(1);
			lcd_clear();
			break;
		case 0b11000000:								//Column 3 Row 3--Switch 3
			lcd_goto(0x00);
			lcd_puts("Switch 10");
			DelayS(1);
			lcd_clear();
			break;
		case 0b10010000:								//Column 3 Row 2--Switch 2
			lcd_goto(0x00);
			lcd_puts("Switch 11");
			DelayS(1);
			lcd_clear();
			break;
		case 0b10001000:								//Column 3 Row 1--Switch 1
			lcd_goto(0x00);
			lcd_puts("Switch 12");
			DelayS(1);
			lcd_clear();
			break;
	}


}
}
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.
 
Top