

Since they are output pins, wherever I state ‘1’ in PORTC, that pin goes HIGH (1), giving an output voltage of VCC at that pin. Now, just concentrate on the highlighted bits only. The highlighted bits correspond to the output pins. The port C is initialized using the DDRx register. OUTPUT = 0b 10 01000 1 /*This is not a C executable line, this line is just for explanation*/ Now, let’s consider the following statements: DDRC = 0b 10 11000 1 The following declarations are one and the same. The register declaration is similar to the DDRx register, except that we change the names, that’s all! One such example is given above in the diagram. In simplified terms, once the DDRx register has defined the output pins, we need to set them to give an output of HIGH or LOW. The PORTx register determines whether the output should be HIGH or LOW of the output pins. Similarily, the pins marked as output have the capability to either become HIGH (voltage = VCC) or LOW (voltage = zero) as directed/written in the code. Generally, the threshold level is half the VCC. The pins marked as input now has the capability to read the voltage level at that pin and then treat it as HIGH if it is above the threshold level, or else it will treat it as LOW. This is represented in the adjoining figure. In the following statement (given below), port C is initialized such that the pins PC0, PC4, PC5 and PC7 are output pins whereas pins PC1, PC2, PC3 and PC6 are input pins. Always remember, in the case of DDRx, 1 stands for output and 0 stands for input.

Take the following example.Īll right, now that we are done with the declaration, let me explain you what it does. Next, every register has a name, and every bit of it also has a name. So basically, a register is a memory allocated in the CPU, usually having a size of 1 byte (8 bits). Hence, bits 0,1,2,3 are called lower nibble whereas bits 4,5,6,7 are called upper nibble. Now, we also know that 1 nibble = 4 bits. Hence, the last bit is the LSB (as the data doesn’t change significantly) whereas the first bit is the MSB (as the data changes significantly). We see that by changing the last bit, the result (B) is very close to the original data (A), whereas by changing the first bit, the result (C) varies quite significantly from the original data (A). Now, once again in A, let’s change the first bit (magenta colour bit) from 0 to 1. Now here, let’s change the value of the last bit (orange colour bit) from 1 to 0.

If nothing is prefixed, it means that it is in decimal number system. Please note that 0b stands for binary and 0x stands for hexadecimal. Let me explain you why LSB is the last bit. From the knowledge of digital logic, we know that the last bit is the LSB whereas the first bit is the MSB. This is because the bits are numbered from the Least Significant Bit (LSB) to the Most Significant Bit (MSB). Note the sequence in which the bits are numbered. Here, you can see that I have represented 8 bits together to form a memory of 1 byte. Let’s consider the following memory space. These registers are linked with the operation of the MCU. Basically, a processor register is a memory space within the CPU itself so that they can be accessed very frequently and fast. Okay, now I hope you are familiar with the term register. However, the concepts are equally good for any AVR MCU. The examples discussed here are in accordance with ATMEGA16/32 MCU. Before going further, I suggest that you read my previous post regarding AVR Basics. Hello friends! In this post, we will discuss about the port operations in AVR. We just want to give you a better viewing and learning experience! Thanks! wordpress from the address bar to reach the new location.Įxample: If the website address is, then removing. Please visit the new website here and search for this post. The articles are now no longer supported, updated and maintained. Dear readers, please note that this is the old website of ma圎mbedded.
