So now (I'll skip hint 1 for readability ftm.): // in setup() The following should do it: "r = (PINB & (1<<PB7)) / 128 " So you would have to do some extra steps to get a one or zero. However, if pin 13 was a HIGH value, we would get 1000000 as a binary number, which in decimal is 128. "PINB" would read from all of the port B pins, but the "& (1<<PB7)" make is so we just get the value from pin 13. Calling "r = (PINB & (1<<PB7)) " is how you would read digital pin 13. Reading is slightly strange and is not as intuitive as one would hope. After you get the "r" shouldn't is go as the number in (num Reading is slightly different than writing, but I like your creative thinking.I'm not sure whether the PORTB can take a bool as the number in the (numFor your INPUT_PULLUP lines, the DDRB after the PORTB should be (0The first two DDRB commands should be combined into one like I mentioned earlier.So the left of the 8 bits is for port pin 7 and the right is for port pin 0. So for example, DDRB could look like 01101100 meaning that port B pins 7, 4, 1, and 0 are inputs and pins 6, 5, 3, 2 are inputs. DDRB = (0<<PB7) sets a pin as an input, PORTB = (0<<PB7) sends the pin low.ĭDR is the Data Direction Register, and PORT is the port register which controls whether the pin is tied to Vdd or ground.Įach is a byte (8 bits) that controls pin 7 down to 0. (0<<PB7) does not mean anything until it is used with a register. Using the DDRB and PORTB, what code would you use to do this for PB7? So your pin 11 and pin 12 commands should be combined into: "DDRB = (1<<PB6)|(1<<PB5) "Īs I mentioned in my code earlier, to set as INPUT_PULLUP, you have to set as an output, send the pin high, and then set the pin as an input. DDRA is for port A and so on.įor multiple DDRx commands, it is better to use the "OR" operator. So DDRB = (1<<PBy) is setting the direction of port B pin "y" to be an output. So digital pin 13 on the Mega is port B pin 7. The "x" in "DDRx" is the port, so for PB7, "B" is the port and "7" is the pin. Confusion is often the first step toward knowledge. Thank you, bos1714, that is already very heplful. I could easily write your code for you, but I think it would be infinitely better if you were to learn the experience yourself With that said, look at each pin and see what port it is located at, that will tell you what DDRx and PORTx to use. You can also go the pins_arduino.h file in your Arduino installation location, but I would recommend against that as messing with the file will give you a headache. You can also look at the header file I have attached below which shows what each port and pin each Mega pin is mapped to. It will also show you the port and pin numbers for each pin. What you can do is look through the schematic of the Mega that is posted on the Mega Product Page under "documentation" the schematic will show you a big rectangle that is the mega2560 chip. In my example, it says PB7 is pin 13, which it is. : then PULLUP.?Įach of the pins of the Mega (1, 15, 53, etc) has a port and pin number. n = 0 -7Īctually the variables are completely puzzling me because I still don't know anything about PORTx, PINx, DDRx, it's differences, and all that. Instructions for single bit data read/write operation (not tested) digitalWrite(DPin, HIGH/LOW) īitSet(DDRX, n) //X = A, B, C. Instructions for 8-bit data read/write operations with ports (not tested) byte z = PORTX ģ. Instructions to set directions of the IO lines (not tested) pinMode(DPin, INPUT) ĭDRX = 0xnn // nn = 00 - FF LH makes output LL makes inputĢ. Internal Structure of a typical Port-pin:ġ. Register Name Output Register Name Input Register Name Size DPins Port-A PORTA PINA 8ĭDRDX (X = A, B, C, D, E, F, G, H, J, K) : Data Direction Register Here is a brief summary of the Registers and DPins of ATmega2650 - (for DPins please see schematic)
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |