NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Can some clearly explain this to me? Regarding dip_arithmetic...
February 27, 2012 by jvince96 |
I having a hard time understanding the reason for a particular line of code in the dip_arithmetic lab and I hope that one of you can help explain. I'll start with what I think I know and then with my question: Step 1: Set the PC0 pin as an input DDRC &= ~(1<<PC0); Here is where we setup pin 23 on the chip as an input. The chip specification indicates that setting any of the bits on DDxn low will set the designated pin as an input. I get this part... Step 2: Turn on the pull up resistor for the PC0 input
PORTC |= (1<<PC0); Step 3: Set the integer a1 = (PINC & (1<<PC0)) >> PC0; Here is where we setup a1 to be populated with a 1 if the first bit from PINC and PC0 are both set to 1. I get this part... Now - here is where I am confused... a1 = (PINC & (1<<PC0)) >> PC0; Why do we need to shift anything back into PC0? Isn’t PC0 the result bit register that comes directly from the input pin? I don't understand why this is necessary... Thanks in advance, Joe |
---|---|
February 27, 2012 by Rick_S |
That line compares the value in the 1st bit of PINC to 1 and if it is, sets the 1st bit of a1. To break it down... a1= (PINC &(1<<PC0)) >> PC0 If PINC = 0b00110111 the value of the 1st bit (bit 0) is 1. So in this example, the result of (PINC &(1<<PC0)) would be 1 because 1 & 1 = 1 The next part takes that 1 and right shifts it PC0 or 0 so it = 0b00000001 which is then assigned to a1. Hope that clears it up... :) Rick |
February 27, 2012 by jvince96 |
Hi Rick, Thanks for the reply Rick but I still have a question. I guess I just don't understand why we need to right shift anything into PC0. My thought process tells me that we already looked at PC0 but did not change anything when we set the value for a1 by this code "a1 = (PINC &(1<<PC0)". I guess I would like an explanation as to why just using this code "a1 = (PINC &(1<<PC0)" is not sufficient and why do we need to shift something into PC0 that I thought was already there. Thanks, Joe |
February 27, 2012 by Rick_S |
For PC0 it really doesn't matter, but for the other bits (PC1, PC2) it does as it shifts the result by 1 or 2 in those cases. Then later in the program, these bits are all shifted and combined to make a number. Also, remember, PC0 isn't a variable, it is a contstant that is defined as 0. The variable that is being set is a1 in this case. So you are not shifting into PC0, you are shifting by PC0 or shifting by 0. This formula could be rewritten a1=(PINC &(1<<0))>>0 Rick |
February 27, 2012 by jvince96 |
I just had a light bulb turn on. What was throwing me off is that we are dividing the int into individual bits and we're trying to set them and them only! I was not originally looking at the a1, a2, & a3 that way. Thanks Rick! Joe |
Please log in to post a reply.
Did you know that two resistors can be used to make a voltage divider? Learn more...
|