Jump to content
Sign in to follow this  
dmf

Reading multiple digital inputs

Recommended Posts

I am trying to find a simple piece of code to read 4 digital input pins (P1.2 + P1.3 + P1.4 + P1.5) and then output them to an int. Can anyone help?

Share this post


Link to post
Share on other sites
Thats way easier than mine. Use OCY's idea. You may have to and the remaining bits with "1".

It's RobG :) and it's in addition to what you did, you still have to mask upper bits.

unsigned int uiPortValue = 0;
uiPortValue = P1IN & ( BIT2 | BIT3 | BIT4 |BIT5 );
uiPortValue >>= 2;

 

Here is another example. Let's say you have P1.0-P1.3 representing 4 data bits D0-D3 and P1.4-P1.7 representing 4 address bits A0-A3

char data = 0;
char addr = 0;
data = P1IN & 0x0F; // 0x0F is a mask to hide upper bits and it is equivalent to BIT0 + BIT1 + BIT2 + BIT3
addr = P1IN >> 4;

Share this post


Link to post
Share on other sites

Here is a macro you can use if your bits are not in order. It will also work if they are in order. It just not as fast

eg. p1.1 p1.3 p1.4

 

#define BITASSIGN( BITNUM )  (P1IN & BIT ## BITNUM) >> BITNUM
#define BITPLACE(PIN, PLACE) (BITASSIGN(0) << PLACE)

 

 

then all you need is to call BITPLACE(the pin on P1, Where you want your bit to be)

in your case it would be something like

BITPLACE(2,0) | BITPLACE(3,1) | BITPLACE(4,2) | BITPLACE(5,3);

Share this post


Link to post
Share on other sites

Thank you all for the tips, I am still having problems with it though.

It is returning some really odd values with all inputs turned off it is still reading a value of 12.

 

//******************************************************************************
//
// MSP430 DSLR Camera Interval Timer
// 1 - 15 Minute Exposure Timer for BULB Mode
// With 3 Second Delay Auto Mirror Lock Up
//
// Martin Trigg
//
//******************************************************************************
#include 

unsigned int wdtCounter = 0;
unsigned int mode = 1;
unsigned int exposure = 0;

void main(void)
{
WDTCTL = WDT_MDLY_32;		// Set Watchdog Timer interval to ~32ms
IE1 |= WDTIE;				// Enable WDT interrupt
P1SEL = 0x00;
P1DIR |= 0x01;				// Set P1.0 to output direction
P1OUT = 0x00;				// release

exposure = P1IN & ( BIT2 | BIT3 | BIT4 | BIT5 ); // check dip switches
exposure >>= 2;
exposure = exposure * 31;	// multiply dip switches by 1 second
exposure = exposure * 59;	// multiply by 1 minute

P1OUT |= 0x01; 				// press shutter to activate mlu
__enable_interrupt();

for(;
{

}
}

// Watchdog Timer interrupt service routine
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{

   if(mode == 1)
   {
  		if(wdtCounter == 15) 		// half second delay to trigger mlu
   		{
		    P1OUT = 0x00; 		// release
		    wdtCounter = 0; 	// reset counter
  			    mode = 2; 			// change to mlu delay.
  			}
  		else
   		{
     			wdtCounter++;
   		}               
   }
else if(mode == 2)
{
	if(wdtCounter == 124) 		// 3 second delay to stabilise the camera
   		{
       		P1OUT |= 0x01; 		// press and hold
       		wdtCounter = 0; 	// reset counter
       		mode = 3; 			// change to shoot mode
   		}
   		else
   		{
       		wdtCounter++;
   		}
   }
else if(mode == 3)
   {
  		if(wdtCounter == exposure) 	// count to exposure time
   		{
       		P1OUT = 0x00; 		// release
       		wdtCounter = 0; 	// reset counter
       		mode = 4; 			// restart

   		}
  			else
   		{
       		wdtCounter++;
   		}
   }
else if(mode == 4)
{
	if(wdtCounter == 31) 		// 1 second delay to restart mlu
   		{
           	P1OUT |= 0x01; 		// press shutter to activate mlu
       		wdtCounter = 0; 	// reset counter
       		mode = 1; 			// restart
   		}
  			else
   		{
       		wdtCounter++;
   		}
}
}

Share this post


Link to post
Share on other sites

How are the dip switches connected? Each between a P1 pin and ground? Are there pull-up resistors? If not, you could use the on-chip P1REN and P1OUT to pull them up.

 

Depend on pull-up, R, and the parasitic capacitance, C, you may also need to delay ~2*R*C after power up before you read the input.

Share this post


Link to post
Share on other sites

I don't know if this can affect it but you don't set the direction of your other pins. i don't know what the default is but maybe trying

P1DIR = 0x01;            // Set P1.0 to output direction

also how are your dip switches connected to your mcu?

Share this post


Link to post
Share on other sites

Thanks OCY that was it. I was pulling them the wrong direction. Just flipped around the external resistors I am using for testing and it now works.

Share this post


Link to post
Share on other sites

Well once the tracking mount I ordered turns up next week and there is a clear night to use it. I will make sure to come back and update the first post.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×