GPR’s of AVR

AVR I/O PORT PROGRAMMING-

In this section, we will learn about basic registers used in AVR programming and bit manipulation. First, we will see pin description of AVR Atmega 16/32.

PIN DIAGRAM OF AVR ATMEGA16/32-

 

PIN DESCRIPTION:

PIN NO. PIN NAME DESCRIPTION OTHER FUNCTION
1 PB0/(XCK/T0) I/O PINB0 T0: EXTERNAL CLOCK SOURCE

FOR TIMER 0

XCK: EXTERNAL CLOCK FOR

USART I/O

2 PB1/(T1) I/O PINB1 T1: EXTERNAL CLOCK SOURCE

FOR TIMER 1

 

3 PB2/(INT2/AIN0) I/O PINB2 AIN0: ANALOG COMPARATOR

POSITIVE INPUT

INT2: EXTERNAL INTERRUPT 2

4 PB3/(AIN0/OC0) I/O PINB3 AIN0: ANALOG COMPARATOR

NEGATIVE INPUT

OC0: OUTPUT COMPARE

MATCH TIMER0

5 PB4/(SS) I/O PINB4 SS: CHIP SELECT BIT USED IN

SPI PROTOCOL

6 PB5/(MOSI) I/O PINB5 MOSI: MASTER OUT SLAVE IN
7 PB6/(MISO) I/O PINB6 MISO: MASTER IN SLAVE OUT
8 PB7/(SCK) I/O PINB7 SCK: SERIAL CLOCK USED IN

SPI PROTOCOL

9 RESET RESTART PROGRAM EXECUTION NO OTHER FUNCTION
10 VCC(+5 VOLT) GIVE POWER SUPPLY TO AVR BOARD NO OTHER FUNCTION
11 GND GIVE GROUND TO AVR BOARD NO OTHER FUNCTION
12 XTAL2 INPUT FOR EXTERNAL CRYSTAL OSCILLATOR NO OTHER FUNCTION
13 XTAL1 INPUT FOR EXTERNAL CRYSTAL OSCILLATOR NO OTHER FUNCTION
14 PD0/(RXD) I/O PIND0 RXD: RECEIVE DATA PIN USED

IN USART

15 PD1/(TXD) I/O PIND1 TXD: TRANSMIT DATA PIN

USED IN USART

16 PD2/(INT0) I/O PIND2 INT0: EXTERNAL INTERRUPT 0
17 PD3/(INT1) I/O PIND3 INT1: EXTERNAL INTERRUPT 1
18 PD4/(OC1B) I/O PIND4 OC1B: OUTPUT COMPARE

MATCH TIMER 1 B

19 PD5/(OC1A) I/O PIND5 OC1A: OUTPUT COMPARE

MATCH TIMER 1 A

20 PD6/(ICP) I/O PIND6 ISP: INPUT CAPTURE PIN FOR

TIMER0

21 PD7/(OC2) I/O PIND7 OC2: OUTPUT COMPARE

MATCH TIMER 2

22 PC0/(SCL) I/O PINC0 SCL:I2C SERIAL CLOCK PIN
23 PC1/(SDA) I/O PINC1 SDA: SERIAL DATA PIN USED

IN I2C PROTOCOL

24 PC2/(TCK) I/O PINC2 USED IN JTAG
25 PC3/(TMS) I/O PINC3 USED IN JTAG
26 PC4/(TDO) I/O PINC4 USED IN JTAG
27 PC5/(TDI) I/O PINC5 USED IN JTAG
28 PC6/(TOSC1) I/O PINC6 OSCILLATOR INPUT PIN FOR TIMER
29 PC7/(TOSC2) I/O PINC7 OSCILLATOR INPUT PIN FOR TIMER
30 AVCC VOLATGE SUPPLY FOR ADC (+5VOLT) NO OTHER FUNCTION
31 GND GIVE GROUND TO AVR BOARD NO OTHER FUNCTION
32 AREF ANALOG REFERENCE PIN FOR ADC NO OTHER FUNCTION
33 PA7/(ADC7) I/O PINA7 CHANNEL7 ADC
34 PA6/(ADC6) I/O PINA6 CHANNEL6 ADC
35 PA5/(ADC5) I/O PINA5 CHANNEL5 ADC
36 PA4/(ADC4) I/O PINA4 CHANNEL4 ADC
37 PA3/(ADC3) I/O PINA3 CHANNEL3 ADC
38 PA2/(ADC2) I/O PINA2 CHANNEL2 ADC
39 PA1/(ADC1) I/O PINA1 CHANNEL1 ADC
40 PA0/(ADC0) I/O PINA0 CHANNEL0 ADC

 

I/O PORT PINS AND THEIR FUNCTIONS-

The number of ports in the AVR family varies depending on the number pins on the chip. The 8-pin AVR has only port B, while 64-pin version has ports from A through F, and 100-pin AVR has ports from A through L. The 40-pin AVR has four ports. They are PORTA, PORTB, PORTC, and PORTD. To use any of these, they must be programmed, as we learn how to program it.

Each port has three I/O registers associated with it. They are as follows-

  1. DDRx
  2. PORTx
  3. PINx

where ‘x’ stands for which port we are using. For example, if we are using port B then, we call DDRB, PORTB, and PINB. DDR stands for data directional register and PIN stands for Port Input pins. We know that there are 8-pins in each port (in the case of Atmega 16/32) so, each DDRx, PORTx, and PINx registers are 8-bit long.

DDRx REGISTER-

PORTx REGISTER-


PINx REGISTER-

ROLE OF DDRx REGISTER- 

  • DDRx REGISTER ROLE IN OUTPUTTING DATA:

We know that in atmega16/32 there are four ports and in each port there are 8-pins. Suppose we want to connect an LED to any one of these pins. LED is an output device so, the pin at which we connect LED is to be set as output. Here comes role of DDRx register, it set the pin as output or input. To set any pin as an output, we have set ‘1’ to a corresponding bit of that pin in DDRx register. Let we want to make pin 4 of port B as output then, we write value of DDRx register in following way-

DDRB REGISTER-

In binary we can write the value in DDRB as follows:

In hex we can write the value in DDRB as follows:

QUESTION: What is the value of DDRx register if we want to make following pins as output :

  1. Pin 5 and Pin 8 of PORTA.
  2. Pin 1, pin 4 and pin 7 of PORTB.
  3. Pin 3 and pin2 of PORTC.

write the value of DDRx register in binary and hex both.

ANSWER:

  1. We know that each bit in DDRx register is corresponding to each pin and LSB of DDRx register corresponding to the first pin while MSB corresponds to last pin.

DDRA REGISTER-

 

Value of DDRA in binary,

Value of DDRA in hex,

  • DDRB REGISTER-

Value of DDRB in binary,Value of DDRB in hex,

  • DDRC REGISTER-

 

Value of DDRC in binary,Value of DDRC in hex,

  • DDRx REGISTER ROLE IN INPUTTING DATA:

To make a pin as an input, we write ‘0’ to its corresponding bit in DDRx register. Suppose we want to make pin 3 of port B as input. So we write ‘0’ at bit 3 in DDRB register.

  • SUMMARY FOR DDRx REGISTER.

DDRx → DDRA for portA

DDRB for portB

DDRC for portC

DDRD for portD

Pin direction

Logic 1 for output

Logic 0 for input

DDRA=0b01001111 ← what these bits signify?

Answer:

PA0→ this pin is output

PA1 → this pin is output

PA2 → this pin is output

PA3 → this pin is output

PA4 → this pin is input

PA5 → this pin is input

PA6 → this pin is output

PA7 → this pin is input

 ROLE OF PINx REGISTER:

To read data present at the pins, we should read the PIN register. To get data from pin we have to read PIN register and to send data to out the pins we have to use PORT register.

ROLE OF PORTx REGISTER:

There is pull up resistor for each of the AVR pins. If we put 1s in bits of PORTx register, the pull up registers are activated. If we put 0s in PORTx register, pull up register inactive.

 

Consider the following example to understand the role of PORTx register.

PORTA=0b01001111← what these bits signify?

Answer:

PA0→ the voltage on this pin is 5V

PA1 → the voltage on this pin is 5V

PA2 → the voltage on this pin is 5V

PA3 → the voltage on this pin is 5V

PA4 → the voltage on this pin is 0V

PA5 → the voltage on this pin is 0V

PA6 → the voltage on this pin is 5V

PA7 → the voltage on this pin is 0V

Now we have gain knowledge of basic register used in AVR we can write our first program. But before that, we have to study some basic syntax used in AVR programming.

How to write program for various task-

First, we need to declare various header files before int main() function.

 

 

 

At the top of program, we need to declare clock cycle frequency of microcontroller that we are using.

It can be 1MHz,4MHz,8MHz etc.

#define F_CPU 8000000UL

Frequency is 8MHz.

UL=unsigned long int.

Now we know how to declare CPU clock frequency and header files so, we can write our first program now.

WRITING OUR FIRST PROGRAM TO LIGHT AN LED CONNECTED TO PIN4 OF PORTB IN AVR ATMEGA 16/32.

We have connected LED to pin4 of PORTB so, to light an LED we should make that pin on which LED connected as an output pin.

To glow an LED, the basic thing required is the 5volt power supply which is supplied by the microcontroller. The anode terminal of led is connected to any one pin of AVR and other is grounded.

In this situation, suppose we are using PIN4 of PORTB in AVR. To supply 5volt to led, DDRB register for PIN4 should be selected for output i.e. DDRB=0b00001000.

PORTB register should be given as

PORTB=0b00001000.

From above knowledge we can write algorithm of our first program as follows:

DDRB=0b00001000 //for selecting PINB4 as output pin

PORTB=0b0001000 //to give 5V to PINB4 pin

PROGRAM CODE:

CIRCUIT DIAGRAM:

BLINKING LED USING AVR-

To blink LED we have to introduce new header file into our program i.e.

#include<util/delay.h>

Delay function : _delay_ms(time in milliseconds)

First, we have to glow led for 1second than cut power supply for 1 second. And we have to repeat this process again and again. In this way, we can blink led.

 Algorithm to blink LED-

Repeat for infinite no. of times

{

Led is ON

_delay_ms(1000)  // keeping led ON for 1 second

Led is OFF

_delay_ms(1000) // led is OFF now

}

Suppose we are considering the previous example in which we connected LED to PINB4. In this example, we write a program to blink LED connected to PINB4 forever.

PROGRAM CODE-

Now we know how to write a program to blink an LED connected to any pin of AVR. So, now we are moving forward and interfacing switch with AVR microcontroller. We have seen push button in our daily life very frequently, for example- door bell switch, switches used for home appliances etc. But before interfacing switch, we first study about bitwise operators and also PINx register.

BITWISE OPERATOR:

 

Operator Symbol Form Operation
left shift << x << y all bits in x shifted left y bits
right shift >> x >> y all bits in x shifted right y bits
bitwise NOT ~ ~x all bits in x flipped
bitwise AND & x & y each bit in x AND each bit in y
bitwise OR | x | y each bit in x OR each bit in y
bitwise XOR ^ x ^ y each bit in x XOR each bit in y

 

OPERATOR SYMBOL FORM OPERATION
Left shift << x<<y All bits in x shifted left y bits
Right shift >> x>>y All bits in x shifted right y bits
Bitwise Not ~ ~x All bits in x are flipped
Bitwise AND & x & y All bit in x AND each bit in y
Bitwise OR | x | y All bit in x OR each bit in y
Bitwise XOR ^ x ^ y All bit in x XOR each bit in y

 

BITWISE LEFT SHIFT AND BITWISE RIGHT SHIFT OPERATORS-

  • BITWISE LEFT SHIFT OPERATOR ( x<<y):
  1. It is denoted by <<.
  2. Bit Pattern of the data can be shifted by specified number of Positions to Left.
  3. When Data is Shifted Left, trailing zero’s are filled with zero.
  4. Left shift Operator is Binary Operator.
  5. Binary means, Operator that require two arguments.

Consider the following example in which we are left shifting a binary number by 2 positions.

Original Number A   0000  0000  0011  1100
Left Shift  0000  0000  1111  0000
Trailing Zero’s  Replaced by 0
(Shown in RED)
Direction of Movement of Data <<<<<=======Left
  • BITWISE RIGHT SHIFT OPERATOR ( x>>y )-
  1. It is denoted by >>.
  2. Bit Pattern of the data can be shifted by specified number of Positions to Right.
  3. When Data is Shifted Right, leading zero’s are filled with zero.
  4. Right shift Operator is Binary Operator.
  5. Binary means, Operator that require two arguments.

 

Consider the following example in which we are right shifting a binary number by 2 positions.

Original Number A   0000  0000  0011  1100
Right Shift by 2  0000  0000  0000  1111
Leading 2 Blanks  Replaced by 0, Shown in RED
Direction of Movement of Data Right ========>>>>>>
  • BIT MASKING-

Consider we have a variable, x = 0001 0101 and we want to change bit at position number 2 from ‘0’ to ‘1’ without affecting other bits. Here we are assuming position 1 is LSB of variable x. We can complete this task using bit masking. Bit masking is of two types depending on its function-

  1. Bit masking using OR gates- It is used to set (logic ‘1’) a specific bit.
  2. Bit masking using AND gates- It is used to clear (logic ‘0’) a specific bit.

First, we study bit masking using OR gate. In this bit masking, we can change any bit from logic ‘0’ to ‘1’ without affecting bits. Suppose we want to change bit position number ‘n’ from ‘0’ to ‘1’ in register ‘R’ then, we perform the following operation-

We have used OR bitwise operator and left shift operation. In the previous example, we have a variable, x = 0001 0101 and we want to change bit at position number 2 from ‘0’ to ‘1’ without affecting other bits. So we do the following operation –

x=x|(1<<(2-1))

x=x|(1<<1)

 BIT MASKING USING AND GATE- Suppose we want to change the bit at position ’n’ from logic ‘1’ to ‘0’ then, we use bit masking using AND gate. Consider we have register ‘R’ and we want to change the bit at position ‘n’ from ‘1’ to ‘0’. Here we are assuming position 1 is LSB of register ‘R’. We do the following operation-

We have used AND bitwise operator and NOT bitwise operator with left shift operation. Now we did some examples to clarify topic of bitwise operator and bit masking.

EXAMPLE– Suppose we have a register named P = 1100 1010 and we want to change following bits-

  1. Set bit 3 from ‘0’ to ‘1’.
  2. Clear bit 7 and set bit 5.

Write the operation which we have to do for above task. After doing each task write the value stored in register P.

ANSWER-

  • We have to set bit 3 so, we are doing following operation-

 

To set bit ‘3’ from ‘0’ to ‘1’ of register ‘P’

 

New value stored in register P is P = 1100 1110

  • We have to clear bit 7 and set bit 5. First we clear bit 7 and then, set bit 5. To clear bit 7 we do the following operation-

To clear bit ‘7’ from ‘1’ to ‘0’ of register ‘P’

 

New value stored in register P is P = 0100 1110

To set bit 5 we do the following operation-

To set bit ‘5’ from ‘0’ to ‘1’ of register ‘P’

 

New value stored in register P is P = 0101 1110

PINx REGISTER- This register is used to check the status of any pin. Suppose we have connected +5 volt (logic ‘1’) to PINB3 and we want to check the status of that pin. But we can access this pin only when the DDRx register is as input for that pin. Let we are checking status of PINB3 so value of DDRB register is as follows-

DDRB=0b00000000    –    PINB3 is set as input.

By gaining knowledge of bit masking we can also write this as follows-

DDRB=DDRB & ~ (1<<3)

Setting bit 3 to logic ‘0’ so, we can access PINB3 register.

EXAMPLE- Write a program to check the status of PINC4 and if it is at logic ‘1’ then, set bit 5 of PORTB.

ANSWER- We have to follow following steps to complete given task-

  1. Set pin 5 of PORTB as output as we have to make it to logic ‘1’ when certain condition satisfied.
  2. Set pin 4 of PORTC as input because we have to check its status and access PINC4 register.
  3. Check the given condition.
  4. If given condition satisfied then, write logic ‘1’ at pin 5 of PORTB.

 

Program Code-

Finally, we can move to switch if we clear from all above concept.

WRITE A PROGRAM TO SWITCH ON LED WHEN SWITCH IS PRESSED. LED IS CONNECTED TO PINB0 AND SWITCH CONNECTED TO PINB1.

PUSH BUTTON :

PHYSICAL STRUCTURE:

Steps to be followed to complete given task-

  1. Make the direction of PINB0 as output because LED is connected to it.
  2. Make the direction of PINB1 as input because the switch is connected to it and we want to check the status of that pin.
  3. Check whether the switch is pressed or not. For this purpose, we used following concept-
    1. When the switch has not pressed the logic ‘0’ is given at PINB1.
    2. When the switch is pressed logic at PINB1 is ‘1’ given.
  4. We check whether logic ‘1’ is given at PINB1 and if condition satisfied we turn ON LED. We keep on checking status in an infinite
  5. We have to connect PULL UP resistor with switch initially.

 

PROGRAM CODE-

CIRCUIT DIAGRAM-