10/13/2023 0 Comments Arduino uno port map![]() Its as easy as it can be, and when compared with the Arduino digitalWrite and Read functions, using direct port access you save space in flash and also win a lot of speed, because the Arduino functions can take more than 40 clock cycles to read or write a single bit in a port and the same time to read another single bit, and the code is pretty complex with a load of lines that occupy at least some 40 bytes, it might be a small save in flash but its a huge steep to speed up any program, but they are easy to use by the people that don’t understand a lot about programming/micro-controllers, so every implementation as its advantages and drawbacks, but lets continue. My_var = PIND //Read the PORTD and put the values in the variable Now, the only thing left is how to read on pin so we can read data from sensors or even the mighty push-button, to read the state of a digital pin configured as input, we will used a third register called PINx, where again x is the port name of where the pin is located, so first using DDRx we say to the micro-controller that we want some pins as digital inputs, then using PINx we read their values, seems easy right, so lets dig into the code:ĭDRD = 0b00000000 //All pins in PORTD are inputsĬhar my_var = 0 //Create a variable to store the data read from PORTD ![]() PORTD = 0b00000000 //All pins in PORTD are low PORTD = 0b11111111 //All pins in PORTD are high We can already say to the Atmega how the pins should be handled, but we want to know how to read an write data to those said pins, so to write data to a given port, we use the PORTx register, this one is easy to remember, where x is the port name, after configuration a pin as an output its just a matter of putting 0 or 1 in the PORTx register to drive that pin low or high respectively, lets see some code for it:ĭDRD = 0b11111111 //All pins in PORTD are outputs There should be some care when using PORTD and Serial/USART because pins 0 and 1 from the PORTD are the ones used by the USART and if you put both of them as inputs or outputs the USART may be unable to read or write data in those pins. As every logic value, each bit in the DDRx registers can be either 1 or 0, being that putting a specific bit of DDRx at 1 configures the pin as output and putting it at 0 will configure the pin as an input, lets see a small example that configures pins 0,1,2,3 as digital inputs and pins 4,5,6,7 as digital outputs:Īnd if you need all outputs? Try it yourself, or wait a few days and I will release my bit-manipulation tutorial. Well, to begin there is a dedicated register for each PORT that defines if each pin is a input or an output, that register is the DDRx, where x is the letter from the PORT that we want to configure, in the case of the Arduino there is DDRB, DDRC and DDRD. So, how can we interact with our digital pins? In this image you can see all the alternative functions that each pin can have, the chip in question is the Atmega328p.Īnd here is the mapping between arduino port names and their real name: ![]() Our micro-controller, the Atmega328p as registers, those registers are associated with the input/output ports, each port as a specific name and the associated registers, in fact our atmega as port B,C and D, and each port as a different number of pins(this is a restriction of the 28 pins PDIP package and not from the micro-controller, because an 40 pins PDIP for example as 4 ports with the full 8 bits each one), the only port that as the full 8 input/output pins is PORTD.Īs you may already know, each pin can have multiple functions, like pwm generation, or ADC capabilities, the pins 7 and 7 from PORTB are also the input pins for the crystal oscillator, and pin 6 from PORTC is the reset button. Hello fellow readers, today I will write a bit about the digital pins and how to read and write digital values to then, its a simple subject but of a great importance, because almost everything that our micro-controller does is using its inputs and outputs to talk with leds, motor drivers, lcd’s, shift-register, to read data from digital sensors and a lot other things, so lets start reading about how to do it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |