0% found this document useful (0 votes)
35 views109 pages

Unit 2 MICRO CONROLLERS-Full

Uploaded by

Subi Abdulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views109 pages

Unit 2 MICRO CONROLLERS-Full

Uploaded by

Subi Abdulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 109

AVR I/O PORT

PROGRAMMING
Learning Objectives
 By the end of this session, you should be able
to:
◦ List all port of the AVR
◦ Describe the dual role of AVR pin
◦ Code ALP to use the ports for input and output
◦ Code I/O bit-manipulation program for AVR.

2
I/O Registers

 Each port has three I/O registers associated


as PORTx, DDRx, and PINx.

 For example, PORTB we have PORTB, DDRB,


and PINB.

 DDR stands for Data Direction Register, and


PIN stands for Port Input pins.
I/O Registers

 Each of the I/O registers is 8 bits wide, and


each port has a maximum of 8 pins; therefore
each bit of the I/O register affects one of the
pins.

 For example the content of the DDRB


represents the direction of the PB0 pin, and
so on.
DDRx register role in outputting data
 Each of the ports A-D in the ATmega32 can be
used for input or output.

 To make a port an output, we write 1s to the


DDRx register.

 In other words, to output data to all of the pins


of the Port B, we must first put 0b11111111
into DDRB register to make all of the pins
output.
Sample Program 1.3
.INCLUDE "M32DEF.INC"
.ORG 00

LDI R16,0XFF
OUT DDRB, R16

L1: LDI R16, 0X55


OUT PORTB, R16
NOP
LDI R16,0XAA
OUT PORTB, R16
NOP
JMP L1
DDRx register role in inputting data

 To make a port an input, we must put 0s to the


DDRx register for that port, and then bring in
(read) the data present at the pins.

 In other words, to input data to all of the pins


of the Port B, we must first put 0b00000000
into DDRB register to make all of the pins
output.
PINx register role in inputting data

 To read the data present at the pins, we should


read PIN register.

 It must be noted that to bring data into CPU


from pins we read the content of the PINx
register, whereas to send data out to pins we
use the PORTx register.
Write a program to toggle all bits of
PORTA
Write a program to read value from
PORTA and store it in to location $100
1. Write a program to toggle all bits of PORTB
and PORTC.
2. Write a program to read two values from
PORTA and PORTB and find its sum . Display
the result in PORTD.
3. Write a program to read a number from
PORTB and find its compliment and write it
in to PORTA.
Write an assembly language program to toggle all
bits of PORTB and PORTC
.INCLUDE "M32DEF.INC"
LDI R16,OXFF
OUT DDRB,R16
OUT DDRC,R16
L1: LDI R16,OX55
OUT PORTB,R16
OUT PORTC,R16
CALL DELAY
LDI R16,OXAA
OUT PORTB,R16
OUT PORTC,R16
CALL DELAY
RJMP L1
Write an assembly language program to read
two values from PORTA and PORTB and find its
sum . Display the result in PORTD
. INCLUDE "M32DEF.INC"
LDI R16, 0X00
LDI R17, 0X00
LDI R18, 0XFF
OUT DDRA, R16
OUT DDRB, R17
OUT DDRD, R18
IN R16, PINA
IN R17, PINB
ADD R16, R17
OUT PORTD, R16
Write a an assembly language program to read a number
from PORTB and find its compliment and write it in to PORTA

.INCLUDE "M32DEF.INC“
LDI R16,0x00
LDI R17, FF
OUT DDRB, R16
OUT DDRA, R17
IN R16, PINB
COM R16
OUT PORTA, R16
Write an AVR assembly code to write
the value 25 to PORTA
.include”m32def.inc”
LDI R20,0XFF
OUT DDRA,R20
LDI R16,0X25
OUT PORTA,R16
HERE:RJMP HERE
Write an avr code to write the value
00 to porta and 0xFF to portb
.include”m32def.inc”
LDI R20,0XFF
OUT DDRA,R20
OUT DDRB,R20
LDI R16,0X00
OUT PORTA,R16
OUT PORTB,R20
HERE:RJMP HERE
Write an AVR assembly code to
toggle all the bits of PORTC
LDI R16,HIGH(RAMEND) DELAY:
OUT SPH,R16 LDI R20,200
LDI R16,LOW(RAMEND) D1: LDI R22,100
OUT SPL,R16 D2: NOP
LDI R16,0xFF NOP
OUT DDRC,R16 DEC R22
L1: LDI R16,0x55 BRNE D2
OUT PORTC,R16 DEC R20
CALL DELAY BRNE D1
COM R16 RET
OUT PORTC,R16
CALL DELAY
RJMP L1
 Write an AVR assembly code to toggle all the
bits of PORTA and PORTD
I/O BIT MANIPULATION PROGRAMMING
I/O ports and bit-addressability
 Sometimes we need to access only 1 or 2 bits
of the port instead of the entire 8 bits.

 A powerful feature of AVR I/O ports is their


capability to access individual bits of the port
without altering the rest of the bits in that
port.
SBI (set bit in I/O register)

 To set HIGH a single bit of a given I/O


register, we have the following instruction

SBI ioReg, bit_num

where ioReg can be the lower 32 I/O register


( address 0 to 31), bit_num is the desired bit
number from 0 to7.
 SBI PORTB, 5

 SBI $18, 5

 SBI 0X18, 5
Instruction Format
CBI (clear bit in I/O register)

 To clear (LOW) a single bit of a given I/O


register, we have the following instruction

CBI ioReg, bit_num

where ioReg can be the lower 32 I/O register


( address 0 to 31) an bit_num is the desired
bit number from 0 to7.
 CBI PORTB, 5

 CBI $18, 5

 CBI 0X18, 5
Instruction Format
SBI (set bit in I/O register) contd...
SBIS (Skip if Bit in I/O register Set)

 To monitor the status of a single bit for HIGH

 This instruction tests the bit and skips the


next instruction if it is HIGH.
Instruction Format
 SBIS PORTB, 5

 SBIS $18, 5
SBIC (Skip if Bit in I/O register
Cleared)

 To monitor the status of a single bit for LOW

 This instruction tests the bit and skips the


next instruction if it is LOW.
Sample Problem 1
 Assume that PB3 is an input and represents
the condition of a door alarm. If it goes LOW,
it means that the door is open. Monitor the
bit continuously. Whenever it goes LOW, send
a HIGH-to-LOW pulse to port PC5 to turn a
buzzer.
Sample Problem 1- Diagram
Sample Problem 1- Program
Sample Problem 2
 A switch is connect to pin PB0 and LED to pin
PB7. Write a program to get the status of
switch and send it to the LED.
Sample Problem 2- Diagram
Sample Problem 2- Program
Sample Problem 3
 A switch is connect to pin PB0. Write a
program to get the status of switch and save
it in location 0x200.
Sample Problem 3- Diagram
Sample Problem 3- Program
Write a program to
1.
2.
3.

4.
5.

6.

7.

8.
9.

10.
AVR Programming in C
Why program avr in C ?

 While Assembly Language (AL) produces a


hex code file that is much smaller than C,
programming in AL is often tedious and time
consuming.

 On the other hand C programming is less


time consuming and easier to write, but the
hex file size produced is much larger than if
we used AL.
 The following are some major reasons for writing
program in C instead of AL.

◦ It is easier and less time consuming to write in C than in


assembly.

◦ C is easier to modify and update.

◦ You can use code available in function libraries.

◦ C code is portable to other microcontrollers with little or low


modification.
C data types for AVR C
Time delay in AVR C
 There are three ways to create a time delay in
avr C
◦ Using a simple for loop

◦ Using predefined C function

◦ Using avr timer.


Time delay using simple for loops
Contd...

 In creating a time delay using for loop, we


must be mindful of two factors that can affect
the accuracy of the delay.

◦ The crystal frequency connect to the XTAL1-XTAL2


inputs.

◦ Compiler used to compile the C program.


Time delay using simple for loops
Using predefine function
IO programming in C
I/O Programming in C
Bit size IO
 The IO ports of ATMega32 are bit accessible,
but some compilers are not support this
feature. For example, the following line of
code can be used to set first pin of Port B to
one:
PORTB.0 = 1;
LOGIC OPERATIONS IN C
&
DATA CONVERSION IN C
Bitwise logical operation examples
Compound Assignment Operator

DDRB &=0b 11011111


Bitwise shift operation in C
DATA CONVERSION PROGRAMMING IN C
ASCII numbers

 On ASCII keyboards, when the “0” key is


activated, “0011 0000” (30H) is provided to
the computer.

 Similarly, 31H (0011 0001) is provided for the


“1” key.
Packed BCD to ASCII conversion

 The Real-Time Clock (RTC) provides the time


of day (hour, minute, second) and the date
(year, month, day) continuously, regardless of
whether the power is on or off.

 This data is provided in packed BCD.


Packed BCD to ASCII conversion
Contd...

 To convert packed BCD to ASCII, you must


first convert it to unpacked BCD.

 Then the unpacked BCD is tagged with 0011


0000 (30H).
Packed BCD to ASCII conversion
Contd...
Packed BCD to ASCII conversion
Contd...

Write an AVR C program to convert packed BCD


0x29 to ASCII and display the bytes on PORTB
and PORTC.
Packed BCD to ASCII conversion
Contd...
ASCII to Packed BCD conversion

 To convert ASCII to packed BCD, you first


convert it to unpacked BCD ( to get rid of 3).

 Combine the numbers to make packed BCD.


ASCII to Packed BCD conversion
Contd...
Write an AVR C program to convert ASCII digits
of ‘4’ and ‘7’ to packet BCD and display them
on PORTB.

You might also like