Unit 2 MICRO CONROLLERS-Full
Unit 2 MICRO CONROLLERS-Full
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
LDI R16,0XFF
OUT DDRB, R16
.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.
SBI $18, 5
SBI 0X18, 5
Instruction Format
CBI (clear bit in I/O register)
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)
SBIS $18, 5
SBIC (Skip if Bit in I/O register
Cleared)
4.
5.
6.
7.
8.
9.
10.
AVR Programming in C
Why program avr in C ?