So lets start with understanding the functioning of AVR. We will first discuss about I/O Ports. Again I remind you that I will be using and writing about Atmega-16. Lets first have a look at the Pin configuration of Atmega-16. Image is attached, click to enlarge.
You can see it has 32 I/O (Input/Output) pins grouped as A,B,C & D with 8 pins in each group. This group is called as PORT.
- PA0 - PA7 (PORTA)
- PB0 - PB7 (PORTB)
- PC0 - PC7 (PORTC)
- PD0 - PD7 (PORTD)
Notice that all these pins have some function written in bracket. These are additonal function that pin can perform other than I/O. Some of them are.
- ADC (ADC0 - ADC7 on PORTA)
- UART (Rx,Tx on PORTD)
- TIMERS (OC0 - OC2)
- SPI (MISO, MOSI, SCK on PORTB)
- External Interrupts (INT0 - INT2)
REGISTERS:
All the configurations in microcontroller is set through 8 bit (1 byte) locations in RAM (RAM is a bank of memory bytes) of the microcontroller called as Registers. All the functions are mapped to its locations in RAM and the value we set at that location that is at that Register, configures the functioning of microcontroller. There are total 32 x 8bit registers in Atmega-16. As Register size of this microcontroller is 8 bit, it called as 8 bit microcontroller.
All the information is given in the datasheet. Just open the datasheet of Atmega 16 and go to last lines in page 4 and start reading the Pin Descriptions. You will understand about functioning of each pin.
I/O PORTS:
Input Output functions are set by Three Registers for each PORT.
- DDRX ----> Sets whether a pin is Input or Output of PORTX.
- PORTX ---> Sets the Output Value of PORTX.
- PINX -----> Reads the Value of PORTX.
Go to the page 50 in the datasheet or you can also see the I/O Ports tab in the Bookmarks.
DDRX (Data Direction Register)
First of all we need to set whether we want a pin to act as output or input. DDRX register sets this. Every bit corresponds to one pin of PORTX. Lets have a look on DDRA register.
Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
PIN | PA7 | PA6 | PA5 | PA4 | PA3 | PA2 | PA1 | PA0 |
Now to make a pin act as I/O we set its corresponding bit in its DDR register.
- To make Input set bit 0
- To make Output set bit 1
If I write DDRA = 0xFF (0x for Hexadecimal number system) that is setting all the bits of DDRA to be 1, will make all the pins of PORTA as Output.
Similarly by writing DDRD = 0x00 that is setting all the bits of DDRD to be 0, will make all the pins of PORTD as Input.
Now lets take another example. Consider I want to set the pins of PORTB as shown in table,
PORT-B | PB7 | PB6 | PB5 | PB4 | PB3 | PB2 | PB1 | PB0 |
Function | Output | Output | Input | Output | Input | Input | Input | Output |
DDRB | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 1 |
For this configuration we have to set DDRB as 11010001 which in hexadecimal is D1. So we will write DDRB=0xD1
Summary
- DDRX -----> to set PORTX as input/output with a byte.
- DDRX.y ---> to set yth pin of PORTX as input/output with a bit (works only with CVAVR).
PORTX (PORTX Data Register)
This register sets the value to the corresponding PORT. Now a pin can be Ouput or Input. So lets discuss both the cases.
1. Output Pin:
If a pin is set to be output, then by setting bit 1 we make output High that is +5V and by setting bit 0 we make output Low that is 0V.
Lets take an example. Consider I have set DDRA=0xFF, that is all the pins to be Output. Now I want to set Outputs as shown in table,
PORT-A | PA7 | PA6 | PA5 | PA4 | PA3 | PA2 | PA1 | PA0 |
Value | High(+5V) | High(+5V) | Low(0V) | Low(0V) | Low(0V) | High(+5V) | High(+5V) | Low(0V) |
PORTA | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
For this configuration we have to set PORTA as 11000110 which in hexadecimal is C6. So we will write PORTA=0xC6;
2. Input Pin:
If a pin is set to be input, then by setting its corresponding bit in PORTX register will make it as follows,
- Set bit 0 ---> Tri-Stated
- Set bit 1 ---> Pull Up
Tristated means the input will hang (no specific value) if no input voltage is specified on that pin. Pull Up means input will go to +5V if no input voltage is specified on that pin.
Summary
- PORTX ----> to set value of PORTX with a byte.
- PORTX.y --> to set value of yth pin of PORTX with a bit (works only with CVAVR).
PINX (Data Read Register)
This register is used to read the value of a PORT. If a pin is set as input then corresponding bit on PIN register is,
- 0 for Low Input that is V <>
- 1 for High Input that is V > 2.5V (Ideally, but actually 0.8 V - 2.8 V is error zone !)
For an example consider I have connected a sensor on PC4 and configured it as an input pin through DDR register. Now I want to read the value of PC4 whether it is Low or High. So I will just check 4th bit of PINC register.
We can only read bits of the PINX register, can never write on that as it is meant for reading the value of PORT.
Summary
- PINX ----> Read complete value of PORTX as a byte.
- PINX.y --> Read yth pin of PORTX as a bit (works only with CVAVR).
I hope you must have got basic idea about the functioning of I/O Ports. For detailed reading you can always refer to datasheet of Atmega.
No comments:
Post a Comment