In this explanation and next others, I will use Atmega 8535/16/32. It is because this AVR series is commonly used by students and developer. But, sometime I will use Atmega 8 for some simpler case. Beside that, I also use C programming because it is easy to use and many people choose this programming language.
These are some superiority of Atmega 8535:
Advanced RISC Architecture
– 130 Powerful Instructions
– Most Single Clock Cycle Execution
– 32 x 8 General Purpose Working Registers
– Fully Static Operation
– Up to 16 MIPS Throughput at 16 MHz
– On-chip 2-cycle Multiplier
Nonvolatile Program and Data Memories
– 8K Bytes of In-System Self-Programmable Flash
Endurance: 10,000 Write/Erase Cycles
– Optional Boot Code Section with Independent Lock Bits
In-System Programming by On-chip Boot Program
True Read-While-Write Operation
– 512 Bytes EEPROM
Endurance: 100,000 Write/Erase Cycles
– 512 Bytes Internal SRAM
– Programming Lock for Software Security
– Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes
– One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture
– Real Time Counter with Separate Oscillator
– Four PWM Channels
– 8-channel, 10-bit ADC
8 Single-ended Channels
7 Differential Channels for TQFP Package Only
2 Differential Channels with Programmable Gain at 1x, 10x, or 200x for TQFP
– Byte-oriented Two-wire Serial Interface
– Programmable Serial USART
– Master/Slave SPI Serial Interface
– Programmable Watchdog Timer with Separate On-chip Oscillator
– On-chip Analog Comparatoror
And Many More
Atmega 8535 has 32 I/O register. Port A, B, C, and D have 8 bit, so the total is 32 bit. If we want to make a pin working as an input pin, we set its DDR to ‘0’. But if we want to make a pin working as an output pin, we set its DDR to ‘1’.
When a port works as input, there are two options, making it as input with internal pull-up or making it as floating input. To activate the pull-up internal we set its PORT to ‘1’ but if we want make it as floating input we set its PORT to ‘0’.
When a port works as output, there are also two option, making it to have high voltage or to have low voltage. PORT is set to ‘1’ for high voltage and PORT is set to ‘0’ for low voltage.
Ok, let’s make a sample program for basic input output.
In programmer notepad or other source editors and based on WinAVR. Please check this for explanation how to use WinAVR and AVR Studio.
Let’s make a C code:
void main (void)
DDRA = 0b00000000, it makes 8 pin in PORTA to work as input.
PORTA=0b00001111, it makes PORTA.0 – PORTA.3 to use internal pull-up and PORTA.4 – PORTA.7 to be floating input.
DDRB= 0b00001111, it makes PORTB.0 – 3 to work as output and PORTA.4 –7 to work as input.
PORTB=0b00110011, it makes PORTA.0 –1 to have high voltage, PORTA.2 – 3 to have low voltage, PORTA.4 – 5 to use internal pull-up and PORTA.6 – 7 to be floating input.
DDRC=0b11111111, it makes 8 pin in PORTC to work as output.
PORTC=0b11110000, it makes PORTC0 –3 to have low voltage and PORTA4 –7 to have high voltage.
output of PORTD will be same as input to PORTA. To access the value of an input register we should use PIN not PORT. If PORTA0 – 3 are connected to Vcc and PORTA4 – 7 are connected to Ground, the output of PORTD0 – 3 are high and PORTD4 – 7 are low.
All bit in a I/O register can be access one by one for example: DDRA1, PORTC3, PIND5, etc. We can make PORTD3 = PINA5, DDRB1 = 0, etc.
Internal pull-up is usually activated when the input port is connected to switch. Floating input is usually used when the input port is connected with sensor.
In the program above, I use a macro, io.h. Why I use this macro? I use this because definition of PORT, PIN, and DDR are there. Beside that there are some function in this header file that will be usefull for our program in the next. Such as:
· _BV(n); to set the n-th bit of a registert. e.g: DDRC = _BV(5) means DDRC5 = 1.
· bit_is_clear(sfr, n); return value = 1, if the n-th bit of sfr (register) is clear. Clear means 0, Set means 1.
· bit_is_set(sfr, n); return value = 0, if the n-th bit of sfr (register) is clear.
· loop_until_bit_is_clear(sfr,n); do nothing until the n-th bit of register equal to ‘0’.
· loop_until_bit_is_set(sfr,n); do nothing while the n-th bit of register equal to ‘0’.
Note: To be more expert, you can use AVR Studio to simulate a progarm. You can see characteristic and every changes in every register in AVR.