You are on page 1of 291

.

" AVR"
Creative Common v4 CC-NC-SA .

CC-NC- ) (


.
SA-
) (
) : ( .
creativecommons.org
" "
http://simplyarduino.com/?page_id=889

abdallah.ali.abdallah.elmasry@gmail.com

http://fb.com/NourHamda.Portfolio
nouraldean.sy@gmail.com

1437 2015.


..............................................................................................

..

. .

. .

((:


..............................................................................................


. UART

. ""

. . .


..............................................................................................

11.............................................................1.0
11............................................................................................

ANSI C15..........................................................................
- AVR PIC16..........................................

.1 22....................................................
1.1 23..................................................Embedded System
1.2 24...........................................................................
1.3 25................................................................

.2 34...............................................AVR
2.1 35......................................................AVR
2.2 37.........................................................................AVR
2.3 AVR39.....................................................
2.4 42...............................................................Datasheet
2.5 43......................................ATmega16/ATmega32
2.6 47...................................................................ATmega16
2.7 49.......................................................................................ATTiny
2.8 52.....................................................................................
2.9 53.....................................................................................

.3 55.............................................................
3.1 56..........................................................................................
3.2 62.............................................................................


..............................................................................................
3.3 65.....................................................................................
3.4 66.................................................................................
3.5 73.....................................................................................

.4 75..............................................GPIO Basics
4.1 77.....................................................................Hello World :
4.2 89................................................AVR
4.3 : 4 96......................................................
4.4 : 99.......................................PortA, Port B
4.5 : 102....................................7segment
4.6 : 107.................................Inputs reading
110.............................................................Pull Up & Pull Down Resistor 4.7
4.8 113..................................................................Internal Pull-Up
4.9 : 3 3 +114.........................................
117.......................................................Bouncing effect & De-bouncing 4.10
4.11 119...............................................

121.......................................................................

123................................................................................DC
4.12 125.........................................................

.5 129..........................................
5.1 130.........................................Data-types
5.2 135.............................................Arithmetic Operations
5.3 136.......................................................Logic Operation
5.4 139..........................................................Shift operations
5.5 142............................................Single Bit
5.6 144....................................................Read single bit

.6 146................................
7


..............................................................................................
147..............................................................................Fuses & Lockbits 6.1
154..........................................................................................LockBits 6.2
6.3 155.................................................................Clock Source
6.4 162..................................................................
6.5 166...........................................................
6.6 169...........................................................................
6.7 171..........................................

.7 174...........................................................Interrupt
7.1 175........................................................The interrupt
7.2 : 177........................................................INT0
7.3 : INT0 185...........................................INT1

.8 188.........................................UART
8.1 189................................................................
8.2 192...................................................Asynchronous
8.3 UART 194...................................................AVR
8.4 : UART 196................................................
8.5 : UART 200............................................
8.6 : 202.......................................
8.7 205.............................................
8.8 209.....................................................................................

.9 - 212.............................................. ADC
9.1 - 213.................................................ADC
9.2 ADC 215................................................ATmega16
9.3 : 217..............................
9.4 224.................................................................................ADC


..............................................................................................

.10 227..........................
10.1 228.....................................................

228............................................................C - preprocessor
10.2 229..................................C - preprocessor syntax
232......................................................................function-like macros 10.3
10.4 232.....................................................macros syntax
10.5 233.................................................................................
10.6 234.................................................
10.7 235......................................................................
10.8 238................................................ATmel studio

.11 245...............................................RTOS
11.1 246..............................Real Time Systems
11.2 247.....................................Real Time Embedded systems
11.3 250............................................................RTOS Kernel
11.4 251................................................................FreeRTOS
11.5 252....................................................................RTOS
11.6 FreeRTOS 253..........................................AVR
11.7 265............................................Blinking 3 leds with 3 tasks :

.12 271.............................................................
: CodeBlocks 272......................................
: 278.....................................................makefile

: Hex 282..................................................
: 287.....................................AVR

291........................................................................


...

...

1.0
..............................................................................................

1.0
Embedded Systems

1000

AVR Datasheet
.

:
" " Hardware ToolChain
)(Softwares

11

1.0
..............................................................................................
:

.GPIO
..LEDs, Switchs, 7-Segments.

:
.

: AVR
Clock Rate

: External
Interrupts

:
UART

: Analog
-

.AVR
.
: C preprocessor
include# define
.libraries uart

driver .

12

1.0
..............................................................................................
: Real Time OS
Multitasking .
FreeRTOS RTOS ) (.


:
.

C if - for while

..

)
(

4 Core2Due Core i3 i5 - i7

RAM 1

Windows ) Linux
(.

) ( Atmel stduio
)( .
http://www.atmel.com/tools/ATMELSTUDIO.aspx

13

1.0
..............................................................................................
) ( Protues
http://www.labcenter.com

.
:

AVR 8 bit

ATmel C
) ANSI C (C89, C99 AVR-GCC

ATmel Studio
.(Arduino IDE, Code vision, MikroC

14

1.0
..............................................................................................
..

ANSI C AVR ... PIC

ANSI C

ANSI - C Bascom Flow Code


ANSI )(ANSI

) ( .

.Compiler



ARM

Registers
)
( .

ATmel

Studio CodeBlocks CodeVision


ANSI C

) (
.

15

1.0
..............................................................................................

- AVR PIC

AVR PIC
..
"
" " "

""
) STM8 (8-bit

AVR PIC 8

.
..

AVR PIC
AVR 8 bit PIC 8 bit

:
AVR 8 bit 4

PIC 8 bit AVR


= PIC

AVR PIC = 16 ) 16 (

AVR 16 Instruction per


second PIC 4 .

16

1.0
..............................................................................................
Pipeline AVR

PIC .

AVR

Hardware multiplier
2 )
( . PIC

40 .AVR

:
Assembly AVR

" Register"
32

)
PIC
(Accumulator AVR

.PIC

Array

ATmega16 PIC18F MSP

)int max(int *array


{
;char a
;int maximum=-32768
)for (a=0;a<16;a++
)if (array[a]>maximum
;]maximum=array[a
;)return (maximum
}

17

1.0
..............................................................................................
) cylces (Clock
)(

atmega16 16

PIC16C74 PIC18F452 40.

: AVR_introduction
Atmel

:
PIC AVR
) ( . Atmel

AVR Pico Power


Save AVR .

:
ATmel AVR compilers
ANSI C AVR-GCC
)

AVR PIC (.

18

1.0
..............................................................................................
Microchip MPLAB
ANSI C PIC16F
.
AVR
"" AVR

.PIC

:
AVR PIC

) :
..(

ATmega16 25) PIC16F877a


3.5(

ATmega16 Pic16F :

4 + 3 PWM
ATmega16
.PIC16F877

AVR
8

19

1.0
..............................................................................................

:
) MicroChip (PIC ATmel
Microchip . AVR
AVR

8
.


AVR PIC

www.kanda.com/pic-vs-avr.php
www.youtube.com/watch?v=DBftApUQ8QI
arstechnica.com/civis/viewtopic.php?f=11&t=409115
stackoverflow.com/questions/140049/avr-or-pic-to-start-programmingmicrocontroller

20

Apple

.1
..............................................................................................

.1

22

.1
..............................................................................................

1.1

Embedded System

" "

" "Embedded

.
:


)(

Xbox, Gameboy, Wii


" "
.

23

.1
..............................................................................................

Bluetooth Wifi
Analog Electronics

Routers Embedded Linux Systems

SDR

1.2

MicroController .

Input devices

Output devices Actuators

Microcontroller Motors

LCD .

I/O )(Input/Output Devices

24

.1
..............................................................................................

1.3


..


software .Hardware


Hex0


Software Design



Prototype
+



Hardware Design


PCB Design

Test Kit

:


Binary
.

.
Assemblers
. Assembly Instructions

25

.1
..............................................................................................
) ADD ( ) SUB (
,
. "
".
High level language
.
Compilers .
:
.1 :

.2 : Hex

hex ) toolchain
(.

26

.1
..............................................................................................
.3 : )
(burn
uploader
binary
.

.4 :
Test Kit

.

27

.1
..............................................................................................

:

) Devices Interfacing (
)
(Sensors .
Development Kit
Breadboard .
Development Kit

+
LCD
Relay RS232

28

.1
..............................................................................................
.

.
Development kits

29

.1
..............................................................................................

AVR ATmega32/ATmega16
.
Breadboard


) (DIP

.

30

.1
..............................................................................................

: ) DIP (Dual in-line Package


PCB SMD
Surface Mount Device
PCB

)ATmega32 (DIP

ATmega32 (SMD

AVR DIP SMD

31

.2 AVR
..............................................................................................

.2 AVR


AVR

AVR
Datasheet
ATmega16
ATTiny84

33

.2 AVR
..............................................................................................

2.1

AVR

Computer On Chip
.
.

AVR

Micro-processor ALU
+

Register

. Oscillator

)(.

34

.2 AVR
..............................................................................................
:
Flash Memory Hard-disk

.Program memory EEPROM


SRAM Static RAM


.
Registers

) peripheral units (sets

) (
PORTS ADC Timers

USART, SPI, i2C AVR


.. CryptoAuthentication.

)(
Static Dynamic


Flip-Flop

Refreshing " dynamic RAM

Parasitic Capacitors

Refreshing ) 10
(DRAM
.

35

.2 AVR
..............................................................................................
Flash EEPROM EEPROM
EEPROM Electrical Erasable Programmable Read Only
Memroy .

EEPROM Flash
MegaBit/S
1 1 . EEPROM

1 1 )
1000 .(Flash

2.2

AVR

Architecture

AVR .
AVR

AVR

Harvard
RAM ROM

AVR RAM ROM


. Von Neumann
) (.

Single Cycle Execution AVR

= = 16 ) 16 (

16 .

36

.2 AVR
..............................................................................................
2x program
counter + 2x instruction decoder

Self programming memory AVR


)

EEPROM(

Flash memory
) .Programmer (burner

ATmel )

(http://www.ATmel.com/Images/doc2575.pdf .

peripheral units
8 16 32.

8 32

CPU
.

8 8

. 16

8 . 32

32 .

37

.2 AVR
..............................................................................................

2.3 AVR

.
:

Peripheral

GPIO

Devices

ATmel AVR 6

General purpose microcontrollers


.

General purpose microcontrollers


megaAVR 8 bit
ATTiny 8 bit
AVR Xmega 8 & 16 bit
AVR - 32 bit

38

.2 AVR
..............................................................................................
Special purpose microcontrollers
Automotive AVR
)
150 (

) (short circuit
Battery Management

mega, ATTiny


Performance

39

.2 AVR
..............................................................................................
Atmel

ATmega16

"

"Datasheet " summery "

.complete

.Datasheet
ATmega16 ATtiny84

Datasheet
.

40

.2 AVR
..............................................................................................

2.4

Datasheet

Datasheet

. .

AVR

5 8

ATmega16

41

.2 AVR
..............................................................................................

2.5

ATmega16/ATmega32

( :

Advanced RISC (Harvard based) Architecture


single cycle execution Instruction 131


131

Mega Instruction Per Second (MIPS) 16

16 16
)

( .
.AVR
: 10 1
) = 1(
10 10 .

On-Chip 2 cycle multiplier


=1012 12 10 ).....+12+12+12+12(.
"" 10 ) ( =10

. AVR
.

42

.2 AVR
..............................................................................................
High endurance Memory
AVR
Flash RAM EEPROM :

16 Self-programmable Flash memory


) . ATmega32 32(.

512 ) 8 *512( EEPROM

1 )( SRAM

)(\ 10,000

)(\ EEPROM 100,000.

Flash EEPROM 100

25 25

80
.

) Bootloader (.

True Read-While-Write Operation


ROM RAM

Programming locks

) Lockbits (.

43

.2 AVR
..............................................................................................
Jtag, Peripheral devices, Power consumption

Microcontroller packaging

" "

Epoxy

) Packaging(

( %10

44

.2 AVR
..............................................................................................
DIP SMD

atmega attiny DIP SMD


package

ATmega16

DIP
SMD TQFP

: SMD TQFP, BGA, QFN, MLF


SMD DIP


.SMD
AVR32 Xmega
SMD ) 120(
) ( DIP .

45

.2 AVR
..............................................................................................

2.6

ATmega16

ATmega16 40 pin 4
8 PORTA, PORTB, PORTC, PORTD
.

RESET RESET

) (

active low

GND LOW logic


VCC 10 ) RESET

(.

46

.2 AVR
..............................................................................................

VCC

) 2.7 5.5

(.

GND

. GND

Noise

Noise

)(.

XTAL1, XTAL2

(.

AVCC ADC VCC

ADC
.VCC

AREF Analog Refrence


ADC

A,B,C,D

) ( " \ " GPIO


Alternative functions
.. .

47

.2 AVR
..............................................................................................

2.7

ATTiny


ATmel

ATTiny
. ATmel Tiny


ATTiny85

ATTiny 45/84/85

megaAVR
20 20
)

.(ATmega16/32/128

48

.2 AVR
..............................................................................................
megaAVR GPIO

6 GPIO 4 2
.ATTiny4/5/9/10 1.8.

ATTiny10

Peripheral Devices

megaAVR ATTiny85 6
.GPIO, ADC, PWM, SPI

49

.2 AVR
..............................................................................................
) ATtiny84 (ATmega16

12 2 .

.
1.8 300 = 0.000300 ) 300
( 12

megaAVR 1 ) 1000( .

50

.2 AVR
..............................................................................................

2.8

ATmega16 ATmega32
ATTiny84 .

ATTiny84

100

8 ATmega16

ATmega16 ATTiny84

: MIPS
.

) ATmega16,
(ATmega32, attiny45, attiny85, ATtiny84 17

51

AVR .2
..............................................................................................

2.9
ATTiny
www.microfusion.de/e-/Microcontroller/AVR-Overview/ATtiny.html
en.wikipedia.org/wiki/Atmel_AVR_ATtiny_comparison_chart
megaAVR
ATmega32-avr.com/avr-comparison/

ATTiny
megaAVR

52

.3
..............................................................................................

.3


" " Hardware
)ToolChain (Softwares
) .(Burners

Toolchain

54

.3
..............................................................................................

3.1

.

Burner " " .

) (.

USBasp
usb

6 9 slow clock rate mode

)
(

USBasp AVR .

SMD USBasp

DIP USBasp

USBasp
www.fischl.de/usbasp

55

.3
..............................................................................................

AVRISP mkII Atmel



AVR 8 32 Xmega
500 8 )

( .

AVRISP mkII USBasp USBasp

5 1.8
5 . 40

.
http://www.ATmel.com/tools/AVRISPMKII.aspx

AVRISP mkII

AVRISP mkII

: ) 10
(
avrisp mkII
.

56

.3
..............................................................................................

AVR Dragon
AVR "" .

AVR Dragon AVR SPI OCD PDI

Jtag Debugger High voltage


burner 12 Volt

) (.

Atmel :
www.ATmel.com/webdoc/avrdragon

55
.

57

.3
..............................................................................................

Universal Programmers

PIC, AVR, ARM, 8051,


, EPROM .
.

100 1200

) 1200
8000 (.

.

58

.3
..............................................................................................

Arduino as ISP

AVR Atmega328
+ .USB-ttl converter

AVR:
: ArduioISP
USBasp AVR

ATmega32 ATmega16

59

.3
..............................................................................................
: AVR development board

ANSI- C .

ANSI - C .

.
) (:

www.youtube.com/watch?v=_ZL-YNOH_jA
www.arduino.cc/en/Tutorial/ArduinoISP
www.instructables.com/id/AVR-Programming-with-Arduino-AVRdude-andAVR-gcc/
USBasp
.
ATmega16 ATTiny
AVR atmega328
)
( Register
AVR board
.

Datasheet .

Atmega328
.

60

.3
..............................................................................................

3.2

)( )(
) (.
)(

ATmega32
ATmega16
ATTiny84

)( ATTiny84
)( 1 ) ( 16

16 :
)( 2 .

22
picofarad

10 20

) 20 (.
ISP

)( 1

)
(.

61

.3
..............................................................................................
)( 10 )
(

) (LED
" )( 2 :
"
seven
Segment

Segment 7

common cathode

LM35

LDR

)( 1

)( 1


potentiometer

)( 1 3

DC Motor

)( 1

Stepper
Motor
)(bipolar

)( 1 bipolar
4
500 ) (
.

62

.3
..............................................................................................
L293 H-bridge

)( ) (H

DC Stepper Motor

)( 4

Push button
DIP
switch
Resistors
10
330

.
)( 1 DIP ON- OFF
2 4

)( 8
) 10 330( :
watt 1/8
)( 20
Male to Male Jumpers
)

0.6(

10 15 .
)( 1 Breadboard

Breadboard

63

.3
..............................................................................................

3.3

.
:

)Digital Multimeter (AVO meter

) + + (PCB holder

30-70

)( + .

64

+ )(.

.3
..............................................................................................

3.4

) Firmware-(
" " Development toolcahin

AVR

. :

AVR-GCC Compiler GCC

++C/C )

Linker (Assembler ATmel GCC AVR

AVR32-GCC

AVR-GCC.

AVR-GCC-G++, AVR-GCC,

binutils

hex ) hex

binary (.

) Libraries (LibC-avr

GDP debugger
.

AVRdude

AVR
) (
EEPROM ) (.

65

.3
..............................................................................................
toolchain IDE
\ ++

ATmel Studio

) AVRdude(.

Linux Mac

ATmel Studio
.
Protues
.

Wine HQ
: Atmel Studio toolchain

AVR-GCC Arduino toolchain


IDE \++
AVR
.

66

.3
..............................................................................................


Atmel Atmel Studio
http://www.ATmel.com/tools/atmelstudio.aspx

) NET.

) (

.
.

67

.3
..............................................................................................
Atmel studio AVRdudess

avrdude

AVR :
http://blog.zakkemble.co.uk/avrdudess-a-gui-for-avrdude

USBasp
USBasp

; .
.

: ATmel AVRISP AVR Dragon


ATmel studio
USBasp
) (

68

http://www.fischl.de/usbasp

.3
..............................................................................................

USBasp

USBasp
:

Device Manager USBasp - Uknowen device install


new driver :

69

.3
..............................................................................................
) (

Install

! (: AVR
windows 8.1 Windows 10 )
(
http://www.atadiat.com/usbasp_win_driver/
https://openchrysalis.wordpress.com/2014/09/26/installing-usbasp-driversoftware-in-windows-8-1/

70

.3
..............................................................................................

\ \ FreeBSD toolchain
MakeFile

hex ) MakeFile (.

IDE ) CodeBlocks
( Eclips Sublime Geany Vim .Emacs
Ubuntu / Debian
)
(Terminal

sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude


Fedora / RedHat / CentOS
yum ) DNF 22 (

sudo yum install avr-gcc avr-binutils avr-libc avr-gdb avrdude


22

sudo dnf install avr-gcc avr-binutils avr-libc avr-gdb avrdude

USBasp
) (
udev rules
:

71

.3
..............................................................................................

3.5

http://www.ladyada.net/learn/avr/programmers.html
http://avrprogrammers.com/programmers/all
http://www.instructables.com/id/AVR-ISP-programmer
http://www.instructables.com/id/Turn-Your-Arduino-Into-an-ISP
http://elm-chan.org/works/avrx/report_e.html
http://www.instructables.com/id/Programming-an-Atmel-AtTiny85-usingArduino-IDE-an/

72

.4 GPIO Basics
..............................................................................................

.4 GPIO Basics

..LEDs, Switchs, 7-Segments.

Hello World :

AVR

: A B

: 7Segment

Internal & Externel Pull-Up

74

Bouncing De-Bouncing

.4 GPIO Basics
..............................................................................................
Design Simulate

Prototype

) fuses (.


: ) Breadboard ( +

75

.4 GPIO Basics
..............................................................................................

4.1 :

Hello World

Blinking Led
.

AVR microcontrollers ) GPIO (.

Led PA0

ATmega16 ) ATtiny84 (PA0


:

: Schematic

.
.

76

.4 GPIO Basics
..............................................................................................

Atmel Studio
Atmel stduio New Project

GCC C- Executable

.
.

77

.4 GPIO Basics
..............................................................................................

) ATmega16 .(ATtiny84

" "
Empty template

78

GPIO Basics .4
..............................................................................................

#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/delay.h>
int main(void)
{
DDRA = 0b00000001;
while(1)
{
PORTA = 0b00000001;
_delay_ms(500);
PORTA = 0b00000000;
_delay_ms(500);
}
return 0;
}

79

.4 GPIO Basics
..............................................................................................

F7
" " Build Build Solution

) (Breadboard

GCC
Atmel studio makefile

80

.4 GPIO Basics
..............................................................................................

) Wine (.

SP2 7.8 8.1



ATmega16_blink_led
:

Create a new schematic Default

81

.4 GPIO Basics
..............................................................................................

PCB Don't Create PCB


No Firmware
: 8
AVRASM gcc-avr
Atmel studio CodeBlocks gcc-avr

Schematic
)

P devices

82

.4 GPIO Basics
..............................................................................................
ATmega16

)LED (yellow

Resistor 330

LED bar

83

.4 GPIO Basics
..............................................................................................

Blinking led
ATmega16 :

terminal )

( power AREF AVCC

) \ .(ADC

84

.4 GPIO Basics
..............................................................................................

PA0

) Ground Terminals(

RESET

Power 10 ) (10k
:

85

.4 GPIO Basics
..............................................................................................
hex0

CodeBlocks

ATmega16 " " Program File


) Atmel stduio

( :

86

.4 GPIO Basics
..............................................................................................
CKSEL Fuses Int. RC 1 Mhz

OK

play

87

.4 GPIO Basics
..............................................................................................

4.2

AVR

3 :

Main Function

" "

AVR

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <avr/delay.h
{ )int main(void
;DDRA = 0b00000001
{ )while(1

;PORTA = 0b00000001
;)_delay_ms(500
;PORTA = 0b00000000
;)_delay_ms(500
}
;return 0
}

#defne F_CPU 1000000UL

F_CPU = 1000000 )

( .

1 16.
: UL #define F_CPU 1000000UL unsigned long
.

88

.4 GPIO Basics
..............................................................................................
. avr/io.h util/delay.h
#include

><....

>#include <avr/io.h
>#include <util/delay.h
io.h GPIO
PORTA PB0 PC1 )

(.

delay.h

: avr util "

Folders ATmel
.
Main

. Main :

Registers configurations

while loop
{ )int main(void
//

)while(1
{
//

//
}
;return 0

}
89

.4 GPIO Basics
..............................................................................................

Digtial I/O
AVR 3

:
Main function

DDRx Register

DDR x Data Direction Register.


PORT x Port Output Register.
PIN x Port Input Register.

8 " "
DDRx

)
Input Output x
( A,B,C,D . DDRC

C DDRA A ..

output 0 .input

" output signal


...Led, Motor, LCD, Relay, speaker . .

"input
signal switch .sensors
) DDRA 66

.(ATmega16 8 0 7

;DDRA = 0b00000001
90

.4 GPIO Basics
..............................................................................................
DDRA
00000001

= = PA0 output
input :

;DDRA = 0b00000011
PA0 PA1 P2 PA7

;DDRA = 0b00001111
4 PA0,1,2,3 4

91

.4 GPIO Basics
..............................................................................................
: 0b 0b00011100

binary 0x 0xff
.hexadecimal
" ".



) LSB ( 0bxxxxxxxx
) MSB ( b 0bxxxxxxxx
DDRA Initial value

.
Read/Write

write DDRA=0b00000001 Read



.

PORTx Register

PORTx

DDRx
PA0

8 .

) PORTA 66 (.

92

.4 GPIO Basics
..............................................................................................
LOW - 0 HIGH 1

= 1 logic HIGH 5
) ( . =

LOW 0 ) sink mode


sink mode (.

)while(1
{
;PORTA = 0b00000001
;)_delay_ms(1000
;PORTA = 0b00000000
;)_delay_ms(1000
}
PORTA:
PORTA = 0b00000001 PA0 1
0 ) HIGH (5 volt
PA0

)LOW (0 volt
_) delay_ms(1000 1000
) 1000 = 1(.
PORTA = 0b00000000
PA0 LOW (0

) volt .

_) delay_ms(1000 1000
)while (1

93

.4 GPIO Basics
..............................................................................................

.
//

#define F_CPU 1000000UL

//

>#include <avr/io.h
>#include <avr/delay.h
)int main(void
{
;DDRA = 0b00000001

//

//

)while(1
{

//

;PORTA = 0b00000001

1000 //

;)_delay_ms(1000

//

;PORTA = 0b00000000

1000 //

;)_delay_ms(1000
}
//

; return 0
}

: // /* */ comment

.
.

comment

94

.4 GPIO Basics
..............................................................................................

4.3 : 4

4 .
PA0 PA3 ATmega16

.ATtiny84

ATTiny84 :

95

.4 GPIO Basics
..............................................................................................


//
//

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <avr/delay.h
)int main(void
{

4 //

;DDRA = 0b00001111
)while(1
{

//

;PORTA = 0b00000001

//

;)_delay_ms(500

//

;PORTA = 0b00000011

//

;)_delay_ms(500

//

;PORTA = 0b00000111

//

;)_delay_ms(500

//

;PORTA = 0b00001111

//

;)_delay_ms(500
}
;return 0

DDRA 4 PA0, PA1,



PA2, PA3 .

) while (1 PORTA
500 ) ( . .

96

.4 GPIO Basics
..............................................................................................

97

.4 GPIO Basics
..............................................................................................

4.4 :

PortA, Port B

16 A B 8

ATTiny84 11 ) (3+8 A 8
B 3

98

.4 GPIO Basics
..............................................................................................

//
//

Port A //
Port B //

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <avr/delay.h
)int main(void
{
;DDRA = 0b11111111
;DDRB = 0b11111111
)while(1
{

// Port A

;PORTA = 0b11111111

// Port B

;PORTB = 0b11111111

//

;)_delay_ms(500

// Port A

;PORTA = 0b00000000

// Port A

;PORTB = 0b00000000

//

;)_delay_ms(500
}
;return 0
}


DDRA DDRB

A B . PORTA PORTB
Leds . .
ATmega16

99

.4 GPIO Basics
..............................................................................................

8 A
) .

00000001
00000011
00000111
00001111
10000000
11000000
11100000
11110000

ATmega16

ATtiny84

100

.4 GPIO Basics
..............................................................................................

4.5 :

7segment

" " 7segment


7
) (.



) (.

7
common anode

) common cathode .(common cathode


A,B,C,D,E,F,G

101

.4 GPIO Basics
..............................................................................................
a,b,c,d,e,f,g

) .(A

: - ) (


) (.
(.

ATmega16- .A
0 9 1

102

GPIO Basics .4
..............................................................................................


#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRA =0b11111111;
while(1)
{
PORTA = 0b00111111;
_delay_ms(1000);
PORTA = 0b00110000;
_delay_ms(1000);
PORTA = 0b01011011;
_delay_ms(1000);
PORTA = 0b01001111;
_delay_ms(1000);
PORTA = 0b01100110;
_delay_ms(1000);

// Number 0
// Number 1
// Number 2
// Number 3
// Number 4

103

GPIO Basics .4
..............................................................................................
PORTA = 0b01101101;
_delay_ms(1000);
PORTA = 0b01111101;

// Number 5
// Number 6

_delay_ms(1000);
PORTA = 0b00000111;

// Number 7

_delay_ms(1000);
PORTA = 0b11111111;

// Number 8

_delay_ms(1000);
PORTA = 0b01101111;

// Number 9

_delay_ms(1000);
}
return 0;
}
.

104

.4 GPIO Basics
..............................................................................................

0 9

.PORTA

PORTA

a,b,c,d,e,f,g .g,f,e,d,c,b,a

A,C,F,E,H,L
) .(while loop

;PORTA = 0b01110111
// Letter A
;)_delay_ms(1000
;PORTA = 0b00111001
// Letter C
;)_delay_ms(1000
;PORTA = 0b01110001
// Letter F
;)_delay_ms(1000
;PORTA = 0b00111000
// Letter L
;)_delay_ms(1000
;PORTA = 0b01110110
// Letter H
;)_delay_ms(1000
-

105

.4 GPIO Basics
..............................................................................................

4.6 :

Inputs reading

Digital Inputs

.Switchs Push button

10 PB0
PA0 :

ATTiny84

106

.4 GPIO Basics
..............................................................................................

#define F_CPU 1000000UL


>#include <avr/io.h
)int main(void
{
;DDRA =0b00000001
)while(1
{
)if (PINB == 0b00000001
};{PORTA = 0b00000001
else
};{PORTA = 0b00000000
}
;return 0
}

Buttn press BUTTON
release .

) Input port AVR

B DDRB = 0b00000000


PINB
) PIN x x A,B,C,D(

107

.4 GPIO Basics
..............................................................................................
PINB

.B

PINx 8

PINB ) PINx

(.

PINB0 PB0 1 PB1

2 PB2.

PINx Read only


PINB
:

.

=
. 5

PINB 0b00000111
PB0 PB1 PB2
0b11111111 ...

) if (condition

)if(PINB == 0b00000001
};{PORTA = 0b00000001
PINB ) 0b00000001

(PB0 PORTA = 0b00000001


else

};{PORTA = 0b00000000
else PORTA = 0b00000000
108

.4 GPIO Basics
..............................................................................................

4.7

Pull Up & Pull Down Resistor

Pull-Up & Pull-Down 2.2

10 Digital Inputs
AND, OR, NOT .

10
VCC 3 ..
) ( :

Floating Area

) Nosie Margin ( ) Logic (0 )Logic (1


pull-up or down .

input voltage 0.8 0

Low 2 5 1 HIGH

0.9 1.25 1.9

) HIGH(1 ) LOW(0
) (.
Electric noise
input

.Floating Area

=
5

. 2.2 10.

109

.4 GPIO Basics
..............................................................................................

breadboard 10 .
)

(.

) ( :

Pull-Down

5 PB0
)(.

Press Button = HIGH


Releas Button = LOW
Pull-up Resistor

PB0 HIGH )(.


Press Button = LOW
Releas Button = HIGH
Pull-up VCC

Pull-UP Pull-Down

Pull-Down

Pull-Up

: Pull-Up & Pull-Down

i2C 2.2 4.7

110

.4 GPIO Basics
..............................................................................................
) ( :

Microphone LDR
NTC

.
ADC
Pull-Down Pull-Up

111

.4 GPIO Basics
..............................................................................................

4.8

Internal Pull-Up

AVR )
(internal pull-up

+ Rpu .

PORTx
input

PORTx

pull-up .
PINx 0 1

internal pull-up

112

.4 GPIO Basics
..............................................................................................

4.9 : 3 3 +

3 Internal pull-up
3 10 . :

PB0

PB1 2

PB2 3

PINx

PINx = 0b11111111

PB0
PINB 0b11111110

113

.4 GPIO Basics
..............................................................................................


#define F_CPU 1000000UL
>#include <avr/io.h

//

)int main(void
{
;DDRA =0b00000111

//

;DDRB = 0b00000000

// B

;PORTB = 0b11111111

)while(1
{
if (PINB == 0b11111110) {PORTA = 0b00000001;} // Button 1 pressed
else if (PINB == 0b11111101) {PORTA = 0b00000011;} // Button 2 pressed
else if (PINB == 0b11111011) {PORTA = 0b00000111;} // Button 3 pressed
};else {PORTA = 0b00000000
}
;return 0

B )

( Pull-UP B

PORTB = 0b11111111
PINB

= ) 0b11111110 (PB0
PA0

114

.4 GPIO Basics
..............................................................................................
0b11111101
PB1 PA0 + PA1
) (PB2 .PA0, PA1, PA2

115

.4 GPIO Basics
..............................................................................................

4.10

Bouncing effect & De-bouncing

Bouncing effect switchs


Push Buttons
.contact

" " .

.
debouncing

:
software debouncing

"" .

) (
debouncing

};if (PINB == 0b11111110) { PORTA = 0b00000001


)software debouncing (delay

//
//

)if (PINB == 0b11111110


{
;)delay_ms(10
)if (PINB == 0b11111110

};{ PORTA = 0b00000001


}


116

.4 GPIO Basics
..............................................................................................
. RTOS
) ROTS

(.

: )(
filter

bouncing effect Hardware Debouncing


.

Ceramic - capacitor 0.1
) .(104

Pull up Pull down resistor

10 + + ) ( .
AND gates .digital trigger

: .
Interrupts

117

RTOS
: .

.4 GPIO Basics
..............................................................................................

4.11


Load
) 330(

. ) (

15 20 5 .
) 5 15 (

)Voltage(V )=Current (I ) Resistance ( R


330 .
.

25 ) (
!

: )(

.
Electrical Charchtaristics

) 291 .(ATmega16 " Absolute values


" .

118

.4 GPIO Basics
..............................................................................................
DC current per I/O = 40 mA
= 40


.
.

= %85

34 = 40 * 0.85 .
) (
147
.

119

.4 GPIO Basics
..............................................................................................


ARM

50 ) ARM

15 25 ( .
Driver circuits .

BJT NPN MOSFET


.

MOSFET 40 )NPN (or PNP


5 .
: 5

.
5 )
75 = 155
(.

2n2222

NPN
) 10 (
) 500(

2n3904 2n2222 380

- BC547 2n2222 100

120

.4 GPIO Basics
..............................................................................................

Base 150
1 .

collector 300
) ( Emitter

30 ) (

%85
2n2222 400 .

Blinking led
:
10

121

.4 GPIO Basics
..............................................................................................

DC

) (Motors
) DC motor
( .

Driver circuit.

: DC ) Active (Animated DC
.


1n4001
.

Coil

Relay

122

.4 GPIO Basics
..............................................................................................



: NPN 2n2222
) (GND

thermal stability
NPN
.


MOSFET NPN
) IRZ44N 41( :


.1n4001 .

123

.4 GPIO Basics
..............................................................................................

4.12

H-Bridge 4

H :

: A,B Default
Terminal


Terminal .Default


A,B,C,D
.

124

.4 GPIO Basics
..............................................................................................

H
H-bridge 4

S1 S4
) (

S2 S3
.

125

.4 GPIO Basics
..............................................................................................

PA0 PA1 2

PA0 PA1 ) (.

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <util/delay.h
)int main(void
{
;DDRA = 0b11111111
)while(1
{
//

;PORTA = 0b00000001

//

;)_delay_ms(2000

//

;PORTA = 0b00000010

//

;)_delay_ms(2000

//

;PORTA = 0b00000000

//

;)_delay_ms(2000
}
;return 0
}

126

.5
..............................................................................................

.5

Data-types

Arithmatic operations

Logic operations

128

.5
..............................................................................................

5.1

Data-types

. Bit-8

ROM ) RAM 128


.(RAM .

) ( 32 64

int double float

" "


) .. (

PORT Motor .. .
) (SRAM .

. .

signed numbers
unsigned

129

.5
..............................................................................................

) (Bytes

-32,768 +32,767

)2-bytes (16 bit

int

65,535

)2-bytes (16 bit

unsigned int

1.2E-38 to 3.4E+38

)4-bytes (32 bit

float

2.3E-308 to 1.7E+308

)8-bytes (32 bit

double

-2,147,483,648 +2,147,483,647

)4-bytes (32 bit

long

4,294,967,295

)4-bytes (32 bit

unsigned long

- 9.22337203710
+ 9.22337203710

)8-bytes (64 bit

long long

1.84467440710

)8-bytes (64 bit

unsigned long long

: float double Signed


unsigned

8 .

PORTx PINx DDRx 8.


ButtonStatus int

PINB 8
B

8 16 .
.

ANSI C - C99

130

Unsigned

0 255

uint8_t

-128 +127

int8_t

0 65,535

uint16_t

-32,768 +32,767

int16_t

0 4,294,967,295

uint32_t

-2,147,483,648
+2,147,483,647

int32_t

1.84467440710

uint64_t

- 9.22337203710
+ 9.22337203710

int64_t

Signed

.5
..............................................................................................
= int uint

int8_t int
= 8 ) 1( . uint16_t

unsigned int 16 ) 2(.

:
) (port A, B,C

PINx 8

PORTx 8

uint8_t 0b0000000
0b11111111
10,000

10000

255 8

16 .65,535

uint16_t

50 100

20 50 int8_t
128 - +

127 1 .

: 25.1 30.5

float = 4 )32
(.

- Variable precision

131

.5
..............................................................................................
int8_t

45 ) 5-
10 (

25.20

) 25( .

36.25 float .

8 A
0 255 100 .

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <avr/delay.h

8 //

)int main(void
{
;uint8_t counter

//

;DDRA = 0b11111111

0 // 255

)for (counter = 0; counter <= 255; counter++


{
;PORTA = counter
;)_delay_ms(100
}
;return 0
}

132

.5
..............................................................................................

Binary:

;uint8_t variable = 0b00000011

Decimal
0b00000011 Decimal 3

;uint8_t variable = 3
- .Hexadecimal

variable 0b11110000 120

// Binary
// Decimal
// Hexadecimal

;uint8_t variable = 0b11110000


;uint8_t variable = 120
;uint8_t variable = 0xF0


-.

- 0x ob


http://www.mjma3.com/programming/basics.html
:
https://www.youtube.com/watch?v=-yrwpEuRjl0
:
https://www.youtube.com/watch?v=lLbc5_JpTWI
: -
https://www.youtube.com/watch?v=B33Iof0bUKg

133

.5
..............................................................................................

5.2

Arithmetic Operations

. :

3 //X, Y, Z

;int X, Y, Z

// X

;X = 5 + 6

// Y

;Y = 50 x

// Z

;Z = X * Y

. x
= 11 4 .15

// 15

;X = X+4

: 4 X
X .15

2 //

;X = X 2

// 4

;X = X * 4

4 X =

// X = X+4

;X += 4

134

// X = X 4

;X -= 4

// X = X * 4

;X *= 4

// X = X / 2

;X /= 2

.5
..............................................................................................

5.3

Logic Operation

Logic operation AND, OR, XOR, Invert, Shift

) ( . )

(Logic Function
.

.
Logic Function in ANSI C

Logic Function name

OR

&

AND

NOT

XOR

<<

Shift Left

>>

Shift Right

XOR

NOT

OR

AND

0^0=0
0^1=1
1^0=1
1^1=0

~0=1
~1=0

0|0=0

0&0=0

0|1=1
1|0=1
1|1=1

0&1=0
1&0=0
1&1=1

135

.5
..............................................................................................
- NOT NOT invert value

Z:

Example 1:
;unit8_t Z = 0b00000000
;Z = ~ Z
)// invert all Z bits (Z = 0b11111111
: X

X = 0b11111111

)// invert all Z bits (Z = 0b00001111

Example 2:
;unit8_t Z = 0b11110000
;Z = ~ Z

)// invert all Z bits (Z = 0b11001100

Example 3:
;unit8_t Z = 0b00110011
;Z = ~ Z

- OR 8 xNumber = ) 12 (

0b00011000 :

Example 4:
;uint8_t xNumber = 0b00011000
;xNumber = xNumber | 0b00000001

// 0b000011000 OR 0b0000001

OR xNumber

xNumber .0b00011001
OR OR operation :
0

xNumber

OR

Example 5:
;xNumber = 0b11111000
;xNumber = xNumber | 0b00000011
OR = 0b11111011 :

136

.5
..............................................................................................
0

xNumber

OR

AND AND .

AND

Example 6:
;xNumber = 0b11111000
;// 0b11111000 AND 0b1000001 xNumber = xNumber & 0b10000001
0b10000000 AND =
1 1 = 1 & 1
0

xNumber

AND

= logic operations:
;// xNumber = xNumber | 0b00000001
;// xNumber = xNumber & 0b00000001
;// xNumber = xNumber ^ 0b00000001

;xNumber |= 0b00000001
;xNumber &= 0b00000001
;xNumber ^= 0b00000001

: ) (Invert
:
xNumber = ~ xNumber

137

.5
..............................................................................................

5.4

Shift operations

: 0b00000001 .
0b00000001

0b00000010

1 <<

0b00000100

2 <<

0b00001000

3 <<

: 0b00000101 .
0b00000101

0b00001010

1 <<

0b00010100

2 <<

0b00101000

3 <<

: 0b01100000 .
0b01100000

0b00110000

>> 1

0b00011000

>> 1

0b00001100

>> 1

138

.5
..............................................................................................
:

) << (

) >> (
: 8 A :

)int main(void
{
;uint8_t counter
;DDRA = 0xff
)while(1
{
)for(counter = 0; counter <= 7; counter++
{
;)PORTA = (1 << counter
;)_delay_ms(500
}
}
;return 0
}
139

.5
..............................................................................................
PA0

PA7 ) (:


counter

. A .

;uint8_t counter
;DDRA = 0xff

while loop for counter


0 .7

)for(counter = 0; counter <= 7; counter++


. A 1

;)PORTA = (1 << counter


;)_delay_ms(500
)1 ( )PORTA = (1 << counter

)PORTA = (0b00000001 << counter

140

.5
..............................................................................................
, for counter = 1

;)PORTA = (0b00000001 << 1

counter = 1

;)PORTA = (0b00000001 << 2


counter = 3

;)PORTA = (0b00000001 << 3

5.5

Single Bit

PINx, PORTx, DDRx


. 3

A DDRA = 0b00000111
.

) (PA0

DDRA

.
1 0

1 Set Bit

Set Bit 1 .

PA0
.A OR left shift

;)Register |= (1 << bitName


Register bitName
DDRA PA0:
.

// set PA0 to work as output

141

;)DDRA |= (1<< PA0

.5
..............................................................................................
PORTA

// turn on PA0

;)PORTA |= (1<<PA0

OR
PA1 PA2 :

;)DDRA |= (1<<PA1
;)DDRA |= (1<<PA2

0 Reset Bit

;)DDRA |= (1<<PA1) | (1<<PA2

Reset Bit Set Bit


3 AND left shift invert :

;)Resgister &= ~ (1 << bitName


: PC3 C

;)DDRC &= ~(1<<PC3



PA0 :

{ )(int main
)// set 1 in PA0 (output

;)DDRA |= (1<<PA0
)while (1
{

)// turn on led (set PA0

;)PORTA |= (1<<PA0
;)delay_ms(1000

)// turn off led (reset PA0

;)PORTA &= ~ (1<<PA0


;)delay_ms(1000
}
;return 0
}

142

.5
..............................................................................................

Toggle Bit

Toggle Bit
= 1 0 0 .1

;)Resgister ^= (1 << BitName


blinking led ) 4(.

)while (1
{
;)PORTA ^= (1<<PA0
;)_delay_ms(1000
}


"" set

reset )

(.

5.6

Read single bit

)push button(switch

.PINx

PINx :

)if (PINB == 0b00000001


};{PORTA = 0b00000001
else
};{PORTA = 0b00000000

:
)Reister & (1<<bitName
:
//1

))if (PINB & (1<<PA0

};{PORTA = 0b00000001

143

- .

.6
..............................................................................................

.6

AVR
Clock Rate


.
Fuses ATmega16
)(

Lockbits

145

.6
..............................................................................................

6.1

Fuses & Lockbits

AVR

16
.. .Fuses

) (Bits


) (Flash Memory
.

AVR 3 :

: Low Fuse Byte 8 1 AVR

:HIGH Fuse Byte 8 - 1


:Extended Fuse Byte 5

programmed = 0

unprogrammed = 1

0 1
unprogrammed programmed

0 .1

:
High voltage
programmers
.
146

.6
..............................................................................................
Datasheet

AVR ) Fuse LOW Byte , Fuse High Byte

260 Datasheet .(ATmega16

Fuse High Byte

High Byte

) (
.
Bootload
- )(

- Arduino bootloader
. -

UART ) ( flash memory

self programming

147

.6
..............................................................................................
programmer

. Arduino
Nano 3

atmega328 USB to ttl converter


:

BOOTRST bootloader

main function
bootloader = 1

-.

) BOOTSZ(0,1 -
) 257

- (.

EESAVE EEPROM

= 1 EEPROM
= 0 EEPROM
. = 1

CKOPT clock oscillator )


(.
SPIEN SPI

. = 1
= 0

spi .

SPIEN

148

.6
..............................................................................................
JTAGEN, OCDEN

Jtag.

Jtag

Jtag Universal protocol


PIC, AVR, ARM FPGA
Debugging .

bug Jtag debugger

.. Program counter.

Jtag single instruction execution



.

Jtag debugger

. jTag
.FPGA

:
en.wikipedia.org/wiki/Joint_Test_Action_Grou
www.ATmel.com/webdoc/atmelice/atmelice.using_ocd_physical_jtag.hl
ATmega16 jtag
PORTC IN/OUT
jtag JTAGEN =1

149

.6
..............................................................................................

LOW Byte Fuse

) CKSEL(0,1,2.3
BOD Brown Out
Detection .BOD

) AVR(

. ATmega16 1
1.8 16

= 5.5 .
.
delay .

EEPROM . BOD

150

.6
..............................................................................................
BOD 4

AVR 1.8
1 4.
BODEN BOD ) 1 (

BODLEVEl

= 1

2.7 = 0

4 )
BODEN (.
) SUT(0,1

.Reset

(.

0 4

65 ) 64
(.


151

.6
..............................................................................................
.

: Oscillator CPU Clock

) RC oscillator

(ceramic oscillator
.
= 65
SUT0 SUT1 = 1

152

.6
..............................................................................................

LockBits 6.2
lockbits
" " .

lockbits flash
eeprom

lockbits

) bootloader (arduino bootloader


hex .SPI
lockbit
Flash & EEPROM LB1
LB2

LB1 = 1 LB2 =1 lockbit


LB1 = 0 LB2 =1 )(

LB1 = 0 LB2 =1 )

Flash EEPROM

( Flash EEPROM

153

.6
..............................................................................................

6.3

Clock Source

( .

) .(clock source

= )1( .

AVR 1 .clock source

= 1 ) (AVR

1
1

= 1 1
1

= 2 2
) 500(.

= 4 4
250 nano second

= 8 8
125
) = Nano Second (nS
) = Micro Second (uS .

AVR

Clock Source
4 .

154

.6
..............................................................................................

Internal Calibrated RC oscillator


RC oscillator " "
Clock source AVR

ATmega16 RC 1 8 )

( .

: - %3
delay_ms
. : load
= 1 ) (-\+ =

) (0.03 = 1 * 0.03 1.03 1 0.97 = 0.03.


load 1 = 3600
(3600 * 0.03) -\+ 3600 3708 )
) ( 108

(.

) 10( .

External RC Circuit
RC

AVR 8
:

1
)( RC

=) F( frequency

R C

XTAL1 ) NC (

155

.6
..............................................................................................

: internal oscillator
.

:
%5

%5

22 ) ( ceramic
capacitor
: AVR 36
.

156

.6
..............................................................................................

External Crystal Quartz Oscillator



10
1 0.00001
1000 .RC oscillator

: 2 22
1 ) =

(.

16 12 8 24

16 ) ATmega16
ATTiny 20(.
:

157

.6
..............................................................................................
: "

"Quartz
R-L-C circuit Sin wave pierce oscillator

sin wave pulse


inverter pierce oscillator :
http://www.abracon.com/Support/facn_abracon_jul2011.pdf
.
en.wikipedia.org/wiki/Crystal_oscillator

) +
+(

158

.6
..............................................................................................

External Resonators
" +
"

1 24

: ) (

: ) %0.5 (
internal RC oscillator .

External Pure Pulse (TTL) Oscillator


5
1 0.000005
20 .

4 VCC CLK GND .

CLK XTAL1 AVR VCC


) AVR 5 3.3(.

159

.6
..............................................................................................
: 3 )

. AVR TTL oscillator

160

.6
..............................................................................................

6.4

ATmega16

DataSheet ) ( 24
ATmega16/ ATmega32
) - .(AVR
Clock Select CKSEL

Internal RC
,,
CKSEL1, CKSEL0

CKSEL2,

CKSEL .

CKSEL3, 1

Clock Frequency

)CKSEL(3,2,1,0

1 Mhz

0001

2 Mhz

0010

4 Mhz

0011

8 Mhz

0100

External RC
Clock Frequency

161

)CKSEL(3,2,1,0

0.9

0101

0.9 3

0110

3 8

0111

8 12

1000

.6
..............................................................................................

External Crystal or Ceramic

Crystal oscillator ceramic resonator


.ceramic resonator

CKSEL 1 3
CKSEL0 . CKOPT
8.

Clock Frequency

)CKSEL(3,2,1
0 is not used

ceramic

101

resonator
0.4

CKOPT = 1

0.9

0.9 3

12 22 picofarad

3 8

12 22 picofarad

8 16

12 22 picofarad

162

110
CKOPT = 1
111
CKOPT = 1
111
CKOPT = 0

.6
..............................................................................................
CKOPT

) (CKOPT = 1 unprogrammedfuse

8
.

CKOPT

Full rail-to-rail swing


noise

Full rail-to-rail

noise.

" 36"
external RC circuit .

Pulse Oscillator
pulses TTL oscillator

IC 555 CKSEL(3,2,1,0) = 0000

163

.6
..............................................................................................


1 .CKSEL(3,2,1,0) = 0001

) 8(
.CKSEL(3,2,1,0) = 0100

Full rail-to-

rail CKOPT 0 CKSEL(3,2,1) = 111


) (
1/8 Clock.
= 1
128 AVRdude
.

jumper ) USBasp slow
(
) AVRdudess = 500 (.
_) delay_us(time 8
)

delay_us (

164

.6
..............................................................................................

6.5


.
) (

:

ATTiny atmega
) (

)( .
.

1.8 1 4
1.8 .

165

.6
..............................................................................................

2.7 10

3.6 14.5

4 17 7

5.5 20

3 .

5.7 .

: ATmega16/ATmega32 16
ATmega328 ATTiny 20

AVR 125
100 ) 100
0.1 (.

299 Datasheet )
AVR(.

166

.6
..............................................................................................
load
ADC

= Total load * ) +
( + voltage regulator .


) 0.8Battery Capacity (mAH
) Total Load Current (mA

=Battery WorkingTime

3.7 ) mAH 3000 \( ) A/hour 1 1000


= 1( .
3000 1 .

: 3.7 16 ) 7
( 7

6 . 1000
.

: = 7 )( 7 + )( 6 +
= 20 .

: = 40
Working Time = 0.8 * (1000/20) = 40 hours
: 0.8

Battery Internal Resistor



167

.6
..............................................................................................

6.6

AVRdudess

programmer

Bit selector Fuses & lockbits


) ATmega16 :(Extended Fuses

) ( Ok Low
Byte Fuse HIGH Byte Fuse

168

.6
..............................................................................................

Write ) (
programmer .

AVRdudess Set fuses

.
Write.
Read Write Programmer

.
Fuse Setting
AVR
.

169

.6
..............................................................................................

6.7

: High Voltage programmers AVR

: AVR Fuse Doctor

Dragon .

5 .
:
http://mdiy.pl/atmega-fusebit-doctor-hvpp/?lang=en

170

.6
..............................................................................................
atmega8
ATTiny Fuse doctor
http://www.instructables.com/id/AVR-Attiny-fusebit-doctor-HVSP

171

.7 Interrupt
..............................................................................................

.7 Interrupt

External Interrupts

: INT0

Logic, Falling 7 Raising Edges

173

: INT0 INT1

.7 Interrupt
..............................................................................................

7.1

The interrupt

. )

( interrupt input
pin

) ( .

.External interrupts
) (
External interrupt

) ADC ( Timer

.interrupt

.main program

) Interrupt service routing (ISR

()main main
ISR .

ATmega16/ATmega32 3
.
)INT0 (pin 2 on port D
)INT1 (pin 3 on port D
)INT2 (pin 2 on port B

174

.7 Interrupt
..............................................................................................

RESET .External interrupt


: RESET .
) ( RESET Restart

. RESET

)

( .

RESET 60

1 ) 60,000
(.

.
.

ISR .

ISR .main program

175

.7 Interrupt
..............................................................................................
)(int main
{
........
}

)ISR(interrupt_type
{
........
}


:
.1 INT0 INT1 input

.2
) . (.

.3 INT0
.4
.5 ISR

7.2 :

INT0

2 + PA0
Blink .

PC0 .INT0

176

.7 Interrupt
..............................................................................................

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <util/delay.h
>#include <avr/interrupt.h
)int main(void
{
//

;)DDRA |= (1 << PA0


;)DDRB |= (1 << PB0
//

;)DDRD &= ~(1 << PD2

;)PORTD |= (1 << PD2


// INT0

;)MCUCR |= (1 << ISC01


;)GICR |= (1 << INT0
//

;)(sei
)while(1
{
;)PORTA ^= (1 << PA0
;)_delay_ms(100
}
;return 0
}
177

.7 Interrupt
..............................................................................................

)ISR(INT0_vect
{
;)PORTB ^= (1 << PB0
}

>#include <avr/interrupt.h
.

main
PA0 PB0 :
;)DDRA |= (1 << PA0
;)DDRB |= (1 << PB0
PD2

. internal pull up

:
;)DDRD &= ~(1 << PD2
;)PORTD |= (1 << PD2
.

logic level falling or rising Edge


HIGH LOW

.0 & 1 0 1 clock
.

178

.7 Interrupt
..............................................................................................
) clock = 1 Mhz ( = 1
1 .
)

(.

" Edges

Rising Edge .Falling Edge

) (.

LOW level HIGH level

HIGH level .LOW level

falling rising edges

.
LOW HIGH

179

.7 Interrupt
..............................................................................................

1:Rising
2:Falling
3:High
4:Low

( Electric Impulse .Electric Edges



) edge detector ( .

AVR

0 & 1 .Edge

MCUCR

) ISCxx xx 0 1

68
( .

.ATmega16

180

.7 Interrupt
..............................................................................................

ISC00 ISC01 INT0


ISC10 ISC11 INT1


) 68 (ATmega16

: ISC00 ISC01

LOW ) INT0 INT0

HIGH (.

: ISC00 1 ISC01

logic change

INT0 HIGH LOW

: ISC00 0 ISC01 1
edge detector

: ISC00 ISC01 1 edge


detector .

;)MCUCR |= (1 << ISC01


1 ISC01 ISC00
181

.7 Interrupt
..............................................................................................
.

INT0
INT0 HIGH LOW

. ISC00 ISC01

) 1 (.

INT0

69 .(ATmega16
) GICR

3 INT0 INT1 INT2


1 INT0

INT0 ..
INT0

;)GICR |= (1 << INT0


;)(sei


. cli
.
main
PA0 100 .

182

.7 Interrupt
..............................................................................................
)while(1
{
;)PORTA ^= (1 << PA0
;)_delay_ms(100
}
ISR

)ISR(INT0_vect
{
;)PORTB ^= (1 << PB0
}
)ISR(interrupt_vector
interrupt_vector External
interrupts 3 :
INT0_vect
INT1_vect
INT2_vect
INT0
)ISR(INT0_vect
{

INT1
)ISR(INT1_vect
{

}{ .
PB0

.INT0

;)PORTB ^= (1 << PB0

183

.7 Interrupt
..............................................................................................

7.3 : INT0

INT1

INT0 INT1

2 INT0 INT1

#define F_CPU 1000000UL


>#include <avr/io.h
>#include <util/delay.h
>#include <avr/interrupt.h

)int main(void
{
//

;)DDRA |= (1 << PA0


;)DDRB |= (1 << PB0) | (1 << PB1

// INT0 & INT1

;))DDRD &= ~((1 << PD2)|(1 << PD3


;)PORTD |= (1 << PD2)|(1 << PD3

184

.7 Interrupt
..............................................................................................
// INT0 & INT1

;)MCUCR |= (1 << ISC01) | (1 << ISC11


;)|= (1 << INT0) | (1 << INT1

GICR

)while(1
{
;)PORTA ^= (1 << PA0
;)_delay_ms(100
}
;return 0

}
//
)ISR(INT0_vect
{
;)PORTB ^= (1 << PB0
}
//

)ISR(INT1_vect
{
;)PORTB ^= (1 << PB1

INT0 INT1
PB0 .PB1

ISR INT0
INT1

185

.8 UART
..............................................................................................

.8
UART


UART

UART AVR

: AVR UART

: AVR UART

Strings

187

.8 UART
..............................................................................................

8.1


8) 1 ( .
Parallel port 8 ) ( bit data bus-8

8 .
.

.
" Parasitic
Capacitance " Parasitic inductance
. .

" pluse .


Serial communication

188

.8 UART
..............................................................................................

UART

UART SPI i2c .UART

)(

8 Parallel
8 Parallel 8

) ( Parallel-in-Serial-out shift

8 parallel 8
register

Serial-in-Parallel-out shift register 8


.

: Protocol

UART :

189

Synchronous
Asynchronous

.8 UART
..............................................................................................
) ( Block of data
.

UART .Universal Asynchronous Receiver Transmitter USART


Transmitter

Receiver

Synchronous-Asynchronous

AVR USART.

.Universal

:Simplex .1 . :
.

:Duplex .2 :

:Half-duplex .3
:

:Full-duplex .4 :
.

190

.8 UART
..............................................................................................

8.2

Asynchronous

0 .1
" "

" " .Frame

frst bit LOW


2 .HIGH

ASCII A 10

1 8 . ) A( .

Courtesy of: AVR microcontroller and embedded systems using assembly


and C by M.Ali Mazidi

Parity bit

Baud rate:

bps .bits per second


. IBM

100 .bps 9600


. 56kbps

191

.8 UART
..............................................................................................
) (AVR

Asynchronous 115200 ) 100


(.

ATmega16/32

RXD :

TXD :

192

.8 UART
..............................................................................................

8.3 UART

AVR

UART :
...

.UART

UART 5 :
-1

.UBRR [H: L]: USART Baud Rate Register

-2

.UCSRA: USART Control and Status Register A

-3

.UCSRB: USART Control and Status Register B

-4

.UCSRC: USART Control and Status Register C

-5

.UDR: USART I/O Data Register


]UBRR [H: L

8 UBRRL 8 1
baud rate UBRRH .baud rate

baud rate 0 .11

.
:

UCSRA

193

RXC :7 1 0

.8 UART
..............................................................................................
.

TXC :6 1 0 .

UDRE :5 0 1

UCSRB

RXCIE :7 1
Interrupt .

TXCIE :6 1
Interrupt .

UDRIE :5 1

RXEN :4 1

Interrupt .

TXEN :3 1

UCSZ2 :2

UCSRC

2 UCSZ1 :2

UCSZ0 :1 .

194

.8 UART
..............................................................................................

8 ) 1( .

8.4 : UART



578 9
.

8 .

baud rate UBRRH


.UBRRL .bps 9600
] UBRR[H:L

195

.8 UART
..............................................................................................

Fosc " " Crystal

BAUD .bps 9600

16 .

: 16
16 + 22

.
16
ATmega16

UBRR 103.16667

.103 )

A ASCII (.

#define F_CPU 16000000


>#include <avr/io.h
>#include <util/delay.h

)int main(void
{
;uint16_t UBRR_Value = 103
;UBRRL = (uint8_t) UBRR_Value
;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
)while(1
{
196

.8 UART
..............................................................................................
;) ) )while( ! (UCSRA & (1<<UDRE
;'UDR = 'A
;)_delay_ms(1000
}
;return 0
}

16 UBRR_Value

UBRRL
] . UBRR[H:L
8 . 8 .

UBRRH

;)UBRRH = (unsigned char) (UBRR_Value >> 8


.
8 .

:UBRR_Value
1

:UBRRL
1

UBRRH 8:
0

.baud rate

;)UCSRB = (1 << RXEN) | (1 << TXEN


.
; )UCSRC |= (3 << UCSZ0

8.

;) )UCSRC |= ( (1 << UCSZ2) | (1 << UCSZ0


197

.8 UART
..............................................................................................
1 . UCSZ0 & UCSZ2

UART .

UDR

;)))While (! (UCSRA & (1 << UDRE


UCSRA .1
5

1

.

. serial port

virtual terminal

A :

198

.8 UART
..............................................................................................

8.5 : UART

UART
). TXD virtual

terminal RXD (.

#define F_CPU 16000000


>#include <avr/io.h
>#include <util/delay.h
)int main(void
{
;uint16_t UBRR_Value = 103
;UBRRL = (uint8_t) UBRR_Value
;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
)while(1
{
;)))while (! (UCSRA & (1 << RXC
;PORTC = UDR
}
;return 0
}

199

.8 UART
..............................................................................................
:

While (! (UCSRA & (1 << RXC))); // Waiting for Receiving buffer to be empty.
.
Virtual terminal .PORTC

a ) (0b01100001 .PORTC

: Virtual terminal

.Echo Typed Characters

200

.8 UART
..............................................................................................

8.6 :

TXD RXD

ON " " N
.

OFF " " F


.

#define F_CPU 16000000


>#include <avr/io.h
>#include <util/delay.h

//
//

)int main(void
{
;))DDRC &= ~((1<<PC0) | (1<<PC3
;)PORTC |= (1<<PC0) | (1<<PC3
;uint16_t UBRR_Value = 103

201

UART .8
..............................................................................................
UBRRL = (uint8_t) UBRR_Value;
UBRRH = (uint8_t) (UBRR_Value >> 8);
UCSRB = (1<<RXEN) | (1<<TXEN);
UCSRC |= (3<<UCSZ0);
while(1)
{
if(bit_is_clear(PINC,0))
{
while(!(UCSRA & (1<<UDRE)));
UDR = 'N';
_delay_ms(300);
}
if(bit_is_clear(PINC,3))
{
while(!(UCSRA & (1<<UDRE)));
UDR = 'F';
_delay_ms(300);
}
}
return 0;
}
0 . PC3 PC0

202

.8 UART
..............................................................................................
.DDRC

))if(bit_is_clear(PINC0

PC0

" ." N PC3


" ." F

#define F_CPU 16000000


>#include <avr/io.h
>#include <util/delay.h
)int main(void
{
;)DDRC |= (1<<PC0
;uint16_t UBRR_Value = 103
;char Received
;UBRRL = (uint8_t) UBRR_Value
;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
)while(1
{
;)))while (! (UCSRA & (1 << RXC
;Received = UDR
)'if(Received == 'N
;)PORTC |= (1<<PC0
203

.8 UART
..............................................................................................
)'if(Received == 'F
;)PORTC &= ~(1<<PC0
}
;return 0
}

Character Received

" " N
PC0 " " F .

8.7

" " UART .

:
// U
// A
// R
// T
.

204

;)))while(!(UCSRA & (1<<UDRE


;'UDR = 'U
;)))while(!(UCSRA & (1<<UDRE
;'UDR = 'A
;)))while(!(UCSRA & (1<<UDRE
;'UDR = 'R
;)))while(!(UCSRA & (1<<UDRE
;'UDR = 'T

.8 UART
..............................................................................................

) ROM (
.

. Pointer
. .

;"char *word = "UART


)while(*word > 0
{
;)))while(!(UCSRA & (1<<UDRE
;UDR = *word++

.
String 0 " " null character

) while(*word > 0 0
.

UDR = *word++

205

.8 UART
..............................................................................................
. 0

String .

UART
UART .
.

.
. . .UART

)(void UART_init
{
;uint16_t UBRR_Value = 103

;UBRRL = (uint8_t) UBRR_Value


;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
}
.main main
:

;)(UART_init
) (:
#define F_CPU 16000000
>#include <avr/io.h
>#include <util/delay.h
)(void UART_init
{
;uint16_t UBRR_Value = 103
;UBRRL = (uint8_t) UBRR_Value
;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
206

.8 UART
..............................................................................................
}
)int main(void
{
; )(UART_init
)while(1
{
;) ) )while( ! (UCSRA & (1<<UDRE
;'UDR = 'A
;)_delay_ms(1000
}
;return 0
}
UART

UART_init

:UBRR

;)uint16_t UBRR_Value = lrint ( (F_CPU / (16L * baud_rate) ) -1

:F_CPU .
:Baud_rate .9600 :

:Lrint
include math.h .

207

.8 UART
..............................................................................................
:

//

#define F_CPU 16000000


>#include <avr/io.h
>#include <util/delay.h
>#include <math.h

)(void UART_init
{
;)uint16_t UBRR_Value = lrint ( (F_CPU / (16L * baud_rate) ) -1
;UBRRL = (uint8_t) UBRR_Value
;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
}
main :

)int main(void
{
; )UART_init(9600
......
......

8.8

)void UART_send_char(char data


{
;) ) )while( ! (UCSRA & (1<<UDRE
;UDR = data
}
208

UART .8
..............................................................................................

String

char UART_receive_char()
{
while (! (UCSRA & (1 << RXC) ) );
return UDR;
}

.()

void UART_send_string(char *data)


{
while(*data > 0)
UART_send_char(*data++);
UART_send_char('\0');
}

209

.9 - ADC
..............................................................................................

.9 - ADC

Analog
- .AVR

ADC

ADC

ADC ATmega16

211

ADC

.9 - ADC
..............................................................................................

9.1 -

ADC

Digital

Analog

) ..(
.


1 0 HIGH .LOW

Analog to Digital

Converter ADC

AVR

mega AVR .ATTiny


ADC .AVR

) ( AVR
ADC successive approximation adc
" " .

) 250(

.. .

212

.9 - ADC
..............................................................................................

) 3 (
3 = 750.

. ADC ADC
.

) (
.sample & hold (S/H) circuit


.ADC

analog comparator
. )

(sampling .

+ - .DAC
.

.comparator

DAC .. comparator
DAC

DAC

. ADCL ADCH
.ADC

213

.9 - ADC
..............................................................................................

9.2 ADC

ATmega16

ADC ) ATmega16 205 (.

ADC ATmega16 .
: ADC . bit 8 bit 10

" " .
8 .

214

.9 - ADC
..............................................................................................
: ADC analog multiplixer

.A

A ADC ATmega16 7
.input channel
) (.

ADC op-amp ) (
comparator ) .PA0, PA1, PA2
.(205


3 8.
ADC

ADC
:ADMUX
) analog Refrence

(.

ADC .
:ADCSRA

.
:ADCL & ADCH

ADC

ADC

. :

.1 ) ADC ADC
.(ADCSRA

.2 Clock ADC
.

.3 channel ) ( analog
multiplixer
.4 .

215

.9 - ADC
..............................................................................................

9.3 :

potentimeter .

POT-HG Active potentiometer

vcc gnd
.

.PA1 8

ADC C :

#define F_CPU 1000000UL


>#include <avr/io.h
//adc

;volatile uint8_t adcValue


)int main(void
{
//

;DDRA = 0x00
;DDRC = 0xff

216

.9 - ADC
..............................................................................................
// adc

;)ADCSRA |= (1 << ADEN


// clock

;)ADCSRA |= (1 << ADPS0) | (1 << ADPS1


8 //

;)ADMUX |= (1 << ADLAR


)( //

;)ADMUX |= (1 << MUX0


)while(1
{
//

;)ADCSRA |= (1 << ADSC


//

;))while(ADCSRA & (1<<ADSC


// adcValue

;adcValue = ADCH

// C

;PORTC = adcValue
}
;return 0


8 uint8_t .adcValue
.ADC

volatile

217

.9 - ADC
..............................................................................................
.

.Code optimaization

) ( Assembly

1 3 10 .
) =

(.

.code optimaization

gcc .IAR workbench


. .


) ADCH
adcValue

( .

C :

// adcValue

;adcValue = ADCH

// C

;PORTC = adcValue
volatile
adcValue .

ADCH PORTC .adcValue

;PORTC = ADCH
.
. )

( .

. .volatile adcValue

:
volatile
.

218

.9 - ADC
..............................................................................................
. main

A C ) (.

;DDRA = 0x00
;DDRC = 0xff
ADC . ADC

ADC AVR .

ADEN .ADC enable


ADCSRA

;)ADCSRA |= (1 << ADEN

clock .ADC .
ADC ) (.
Clock.

clock ) 1 (.
Prescaler Register clock
) (2 64 32 16 8 4 2

.128 Division Factor

ADCSRA
ADPS0 ADPS1 ADPS2

219

.9 - ADC
..............................................................................................
.

ADC atmega Clock

ADC 128 ) ( .

clock = 1 = 8
\ 125 = 8 128.
. clock 4 .

) 32 ( 4\ 125 = 32
. 64 126 ADC clock

128.

32 64 128
. ADC
clock .
1
= 8 1 ADPS0 ADPS1

;)ADCSRA |= (1 << ADPS0) | (1 << ADPS1


8) ADC 10( . 8

.ADMUX =
1 ADLAR
) ADC 10(.

8 //

;)ADMUX |= (1 << ADLAR

ADC 8

ADCH 10 )

220

.9 - ADC
..............................................................................................
( ADCL ADCH
10
left adjusted 8 ADCL 2 ADCH

) Input channel

( . PA1 1

MUX0

)( //

;)ADMUX |= (1 << MUX0

) MUX(1,2,3,4
.

: ADC0 PA0 ADC1 PA1


A

ADC .
ADC 1 ADSC
ADC start conversion

//

;)ADCSRA |= (1 << ADSC


ADC . ADC

221

.9 - ADC
..............................................................................................
ADC .
//

;))while(ADCSRA & (1<<ADSC


ADSC = 1
.

ADSC 0 .while

ADCH

) C

adcValue (.

// adcValue

;adcValue = ADCH

// C

;PORTC = adcValue
.
00000000

11111111 .

222

.9 - ADC
..............................................................................................

9.4

ADC

00000000

) 11111111 0 255 ( .

DigitalValueVref
2n

=Voltage

:Digital Value ) ADCH (

:Vref ) ADC (

:n ) 8 10(

Vref ADC .
) ADC (

REFS0 .RFS1 )(
.Vref = Vcc = 5 volt

ADMUX
Vref RFS0 , RFS1

223

.9 - ADC
..............................................................................................
= 128 ADC 8.

1285 1285
=
=2.5 volts
256
28

: = .56

565 565
=
=1.09 volts
8
256
2

ADC
. ADC ) 8 10(

.Vref .
.

Vref
2n

=sensitivity

n ) 8 10( . vref = vcc = 5 volt adc


8

5
5
=
=0.019 volt
8
2 256

0.019 ) 20 (.
Vref Vref = 3
ADC 3

224

.10
..............................................................................................

.10

C preprocessor
include# define

.libraries uart
driver .

C Preprocessor

Function-like macros

: UART driver

226

.10
..............................................................................................

10.1

if while for
AND OR NOT XOR ""

. ) Directive (
compiler

GCC delay_ms

delay.h

>#include <delay.h

C - preprocessor

include :

include .

define Pi
3.14 )(
arguments .function-like macros

pragma compiler

conditional compilation
:

.

227

.10
..............................................................................................

10.2

C - preprocessor syntax

hash # . preprocessor

#include
C - preprocessor
) ( .
:
main.c :

>#include <avr/delay.h
"#include "device_conf.h
)int main(void
{
;set_port_output
)while(1
{
;PORT_on
;)_delay_ms(1000
;PORT_off
;)_delay_ms(1000
}
;return 0
}
><

.
.

228

.10
..............................................................................................
PORT_on .PORT_of
device_conf :

>#include <avr/io.h
#define PORT_on PORTD=0xFF
#define PORT_off PORTD=0x00
#define set_port_output DDRD=0xFF
Compiling
) (

include :
) avr/delay.h :( avr/io.h


include

#define PORT_on PORTD=0xFF


#define PORT_off PORTD=0x00
#define set_port_output DDRD=0xFF

)int main(void
{
;set_port_output
)while(1
{
;PORT_on
;)_delay_ms(1000
;PORT_off
;)_delay_ms(1000
}
;return 0
}

#define
directive ) (

) (

preprocessor .
preprocessor PORT_on PORT_off

229

.10
..............................................................................................
set_port_output .
) :(define

#define PORT_on PORTD=0xFF


#define PORT_off PORTD=0x00
#define set_port_output DDRD=0xFF

)int main(void
{
;set_port_output
)while(1
{


define

;PORTD=0xFF
;)_delay_ms(1000
;PORTD=0x00
;)_delay_ms(1000

}
;return 0
}
:

//set portD off

#define PORT_on PORTD=0xFF


#define PORT_off PORTD=0x00
#define set_port_output DDRD=0xFF

Preprocessor:

)while(1
{

; //set portD off

;PORTD=0xFF
;)_delay_ms(1000
PORTD=0x00
;)_delay_ms(1000

preprocessor
.

230

.10
..............................................................................................

10.3

function-like macros

C preprocessor function-like
function-like macros
macros

function

Function-like macros

C preprocessor
C preprocessor function-like macros
C preprocessor

10.4

macros syntax

macro )(

function-like

macros .

macros

. macros


.ARM cortex

))#define BIT_SET(ADDRESS,BIT) (ADDRESS |= (1<<BIT


))#define BIT_CLEAR(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT
))#define BIT_CHECK(ADDRESS,BIT) (ADDRESS & (1<<BIT
))#define BIT_FLIP(ADDRESS,BIT) (ADDRESS ^= (1<<BIT

231

.10
..............................................................................................
function-like macros

#define BIT_SET(ADDRESS,BIT) (ADDRESS |= (1<<BIT))


ADDRESS, BIT BIT_SET macro
BIT_SET(PORTB,5);

:
1

PORTB |= (1<<5);

:
0 BIT_CLEAR

PORTB &= ~(1<<5);

10.5

( ) C preprocessor
http://www.atadiat.com/c-preprocessor-part1
http://www.atadiat.com/c-preprocessor-part-2
http://www.atadiat.com/c-preprocessor-part-3

http://www.mybitbox.com/2012/12/robust-c-code-part-3-wrapping-c/
http://www.mybitbox.com/2012/12/robust-c-code-part-2-advanced-cpreprecessor/
http://www.mybitbox.com/2012/11/robust-c-code-part-1-c-preprocessor/
http://www.cprogramming.com/tutorial/cpreprocessor.html
http://www2.hh.se/staff/vero/embeddedProgramming/lectures/printL2.pdf
http://www.phaedsys.com/principals/bytecraft/bytecraftdata/bcfirststeps.pdf
http://en.wikipedia.org/wiki/C_preprocessor

232

.10
..............................................................................................

10.6

" "

) (driver code

driver code library


.

: ADC , UART, i2C,


GPIO .Software driver

10 .

) Modules(


Header file

) Definition file (

file.h Implementation file


file.c

UART
UART .

233

.10
..............................................................................................

10.7

driver

.
UART driver UART
.

: uart.h

Header file C preprocessor


) function prototypes ( .
) .(++

++notepad

__#ifndef __LIBRARYNAME_H
__#define __LIBRARYNAME_H

#endif
LIBRARYNAME

) upper-case ( uart

) _ __H (:

234

.10
..............................................................................................
__#ifndef __UART_H
__#define __UART_H
......
#endif
. uartDriver :
__#ifndef __UARTDRIVER_H
__#define __UARTDRIVER_H
......
#endif

C preprocessor

. UART
:

//

uint16_t UBRR_Value

// UART

)(void UART_init

//

)void UART_send_char(char data

//

)(char UART_receive_char

void UART_send_string(char *data) //



);( ) ( . :

__#ifndef __UARTDRIVER_H
__#define __UARTDRIVER_H
;uint16_t UBRR_Value
;)void UART_init(uint16_t baud_rate
;)void UART_send_char(char data
;)(char UART_receive_char
;)void UART_send_string(char *data
#endif

235

.10
..............................................................................................

. ()UART_init math.h
UART avr/io.h

.

F_CPU UART_init

. .

*/

UART driver v.0.1


Date: 9-2015
This library designed to make a simple and re useable UART driver for both

ATmega16 and ATmega32


*/
__#ifndef __UARTDRIVER_H
__#define __UARTDRIVER_H
#define F_CPU 16000000UL
>#include <avr/io.h
>#include <math.h
// Variable to store the calculations needed to set speed

;uint16_t UBRR_Value

;)void UART_init(uint16_t baud_rate


// Initiate the UART
;)void UART_send_char(char data
// Send singe character
;)(char UART_receive_char
// Receive single character
void UART_send_string(char *data); // Send array of characters
#endif
: uart.h uart.c

: uart.c

c. uart.c
) (.

236

.10
..............................................................................................
include
) .(uart.c

#include uart.h
)void UART_init(uint16_t baud_rate
{
;)uint16_t UBRR_Value = lrint ( (F_CPU / (16L * baud_rate) ) -1
;UBRRL = (uint8_t) UBRR_Value
;)UBRRH = (uint8_t) (UBRR_Value >> 8
;)UCSRB = (1<<RXEN) | (1<<TXEN
;)UCSRC |= (3<<UCSZ0
}
)void UART_send_char(char data
{
;) ) )while( ! (UCSRA & (1<<UDRE
;UDR = data
}
)(char UART_receive_char
{
;) ) )while (! (UCSRA & (1 << RXC
;return UDR
}

10.8

)void UART_send_string(char *data


{
)while(*data > 0
;)UART_send_char(*data++
;)'UART_send_char('\0
}

ATmel studio

UART driver .
ATmel studio

uart.c uart.h C:/documents/atmel


.

237

.10
..............................................................................................
Solution Explorer

Add Existing item

238

.10
..............................................................................................
uart.c uart.h

Solution Explorer

H .uart driver
include uart.h ) define
F_CPU avr/io.h .uart.h

239

.10
..............................................................................................
#include uart.h
>#include <util/delay.h
)int main(void
{
;)UART_init(9600
)while(1
{
;)'UART_send_char('H
;)_delay_ms(1000
}
;return 0
}
ATmel studio

uart.h
delay.h
>< .toolchain

uart.h uart.c .ATmel Studio

240

.10
..............................................................................................

uart.c

241

.10
..............................................................................................

.UART
.

242

.11 RTOS
..............................................................................................

.11 RTOS

Real Time OS

Multitasking .

FreeRTOS RTOS

) (.

RTOS

FreeRTOS Kernel

FreeRTOS System Porting to any AVR


: 3

244

.11 RTOS
..............................................................................................

11.1

Real Time Systems


3:

Soft Real Time Systems


.

.
.

Hard Real Time Systems


.
.


.
Firm Real Time Systems Hard Real Time systems

. Wireless Router

) (
Data packets

245

.11 RTOS
..............................................................................................

11.2

Real Time Embedded systems

. :

: Main
function

.interrupts

"#include "library1
"#include "library2
)(int main
{
some_data
)while(1
{
//
}
;return 0
}
)ISR(type_of_ISR
{
//
}

)ISR(type_of_ISR
{
//
}

246

.11 RTOS
..............................................................................................
: Real Time Operating Systems

RTOS
Windows .Linux
" "Multi-tasking

247

.11 RTOS
..............................................................................................

Task
.switching


) ( ) (

) (

) time slice (
. Round
Robin scheduling

.Priotiry based scheduling


:

) RTOS scheduler
( RTOS kernel

) ( Context switching

Running task

.Waiting task RTOS

248

.11 RTOS
..............................................................................................

11.3

RTOS Kernel


. Kernel Interrupt
service routin )

.(System tick
" Scheduler
.

) Round-Robin (Priority-based

(.
)

249

.11 RTOS
..............................................................................................

11.4

FreeRTOS

FreeRTOS
.

PIC ARM AVR 8051 PowerPC x86


.

)

( SAFE-RTOS
.

4 9 ROM

) ATmega16 16
ATmega32 32(.

FAT

storage media

TCP/IP UDP

IoT

250

.11 RTOS
..............................................................................................

11.5

RTOS

RTOS task Main Function


.

RTOS 3

#include RTOS.h

//
//
//

)(int main
{
;)createTask( task1, task1_parameters
;)createTask( task2, task2_parameters
;)createTask( task3, task3_parameters

// RTOS

;)(startRTOS_scheduler

//

; )while(1
;return 0

{ ) (void task1
//

{ )while(1
}

{ ) (void task2
//

{ )while(1
}

{ ) (void task3
//

{ )while(1
}

251

.11 RTOS
..............................................................................................

11.6 FreeRTOS

AVR

FreeRTOS
atmega323. " " ) system porting
(

.AVR atmega 16/ 32 /328/ 644/1284 .

) FreeRTOS
(8.2.1 http://www.freertos.org

Download "
sourceforge.com

FreeRTOS

252

.11 RTOS
..............................................................................................
source .Demo
.
: +
FreeRTOS PIC, AVR, ARM cortex
AVR .

FreeRTOS GCC IAR Keil


GCC toolchain.
FreeRTOS 3 :

port.c
FreeRTOSConfig.h
makefile
main.c
:

FreeRTOS/Source/portable/GCC/ATMega323/port.c
FreeRTOS/Demo/AVR_ATMega323_WinAVR/FreeRTOSConfig.h
FreeRTOS/Demo/AVR_ATMega323_WinAVR/makefile

: port.c

port.c porting
) context switching .(tasks
.

FreeRTOS 1 AVR

1 OCRA1x

.timer 1 COMPARE A match interrupt

FreeRTOS/Source/portable/GCC/ATMega323/port.c

) #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10

253

.11 RTOS
..............................................................................................

mask timer 1 compare


TIMSK AVR
match A mask


TIMSK0
OCIE1A Timer1 Compare A Match interrupt

0x10 mask
:

ATmega32 .(82
OCIE1A 4

mask 1

= 0 00010000 0x10
) . (
atmega168 ) atmega328 (

OCIE1A . atmega328

) 135 .(atmega328

254

.11 RTOS
..............................................................................................

atmega644 atmega1284 134

1 = mask
00000010 0x02 . 0x10

0x02:

TIMSK
AVR 8bit
TIMSK0 TIMSK1 atmega328 atmega1284


mask

) prvSetupTimerInterrupt( void port.c


:

255

.11 RTOS
..............................................................................................

TIMSK )

(ATmega32/16 atmega328
atmega1284 TIMSK TIMSK0 ) TIMSK1
(.
: atmega1284/644/328/168/....etc

port.c ISR Timer1


Compare A Match ) AVR

( . port.c .

256

.11 RTOS
..............................................................................................

SIG_OUTPUT_COMPARE1A

TIMER1_COMPA_vect

port.c

257

.11 RTOS
..............................................................................................

: FreeRTOSConfig.h

kernel .tasks

.
) tasks (threads

.
Demo main.c

FreeRTOS/Demo/AVR_ATMega323_WinAVR/FreeRTOSConfig.h


#define configCPU_CLOCK_HZ
) ( ( unsigned long ) 8000000

) 8(.

#define configTICK_RATE_HZ
) ( ( TickType_t ) 1000

kernel .

10 Kernel 1000
100 .

kernel

. tick = 3000 Hz

kernel 3000

.context switch

16 20 .

258

.11 RTOS
..............................................................................................
.

) 500 (
1000 3000

AVR Xmega AVR 32 bit .5000

#define configMAX_PRIORITIES
)(4
.

.
2.

#define configMINIMAL_STACK_SIZE
) ( ( unsigned short ) 85
stack
SRAM . SRAM 1

50 2
4 .150

) Stack( .

) x = y+5/z*(x+1 stack

. stack

stack word byte

stack = 150

stack size= 150 word = 150 * (4 byte 1 Word) = 600 byte

) ) ( (size_t ) ( 1500

#define configTOTAL_HEAP_SIZE

Heap data-type RTOS ) (


.

259

.11 RTOS
..............................................................................................

1500 byte

heap .


freeRTOS 300 400 kernel

heap

ATmega16 SRAM = 1 Kbyte


heap 0.8 ).(800

ATmega32/328 SRAM = 2 Kbyte

heap )(1500

atmega644 SRAM = 4 Kbyte

heap 3.5 )(3584

atmega1284 SRAM = 16 Kbyte

heap 15.5 )(15360


FreeRTOSCOnfig.h
http://www.freertos.org/a0019.html

: ) serial.c (
FreeRTOS serial.c driver UART
AVR 8 bit

260

.11 RTOS
..............................................................................................
)

UCSRB UCSR0B .(UCSR1B

) (

USART

: driver ) atmega328
(.

UCSRB -> UCSR0B


UCSRC -> UCSR0C
UBRRL -> UBRR0L
UBRRH -> UBRR0H
UDR -> UDR0
:
UART

: makefile

Linux
toolchain ) IDE (ATmel studio

compiling using makefile .

makefile Demo
.


MCU = atmega323
ATmega16 ATmega32 atmega1284

261

.11 RTOS
..............................................................................................


compiling makefile
FreeRTOS

262

SRC
\=
\ main.c
\ $(SOURCE_DIR)/tasks.c
\ $(SOURCE_DIR)/queue.c
\ $(SOURCE_DIR)/list.c
\ $(SOURCE_DIR)/croutine.c
\ $(SOURCE_DIR)/timers.c
\ $(SOURCE_DIR)/portable/MemMang/heap_1.c
$(PORT_DIR)/port.c

.11 RTOS
..............................................................................................



AVRDUDE_PROGRAMMER = stk500
usbasp stk500 ) usbasp

(usbasp

: usbasp
)-P $(AVRDUDE_PORT

usbTiny ISP AVRISP mkII
AVRDUDE_PORT

263

.11 RTOS
..............................................................................................

11.7 :

Blinking 3 leds with 3 tasks

FreeRTOS

3
. PB0, PB1, PB2

) ATmega16 ATmega32

(.

main.c Demo

: FreeRTOS Examples/blinking_3_leds.c

blinking_3_leds.c
main.c

264

RTOS .11
..............................................................................................

#define F_CPU 8000000UL


#include <avr/io.h>
#include <util/delay.h>
/* FreeRTOS files. */
#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
/* Define all the tasks */
static void ledBlinkingtask1(void* pvParameters);
static void ledBlinkingtask2(void* pvParameters);
static void ledBlinkingtask3(void* pvParameters);
int main(void) {
/* Call FreeRTOS APIs to create tasks, all tasks has the same priority "1" with the
same stack size*/
xTaskCreate( ledBlinkingtask1, ( signed char * ) "LED1",
configMINIMAL_STACK_SIZE, NULL, 1, NULL );
xTaskCreate( ledBlinkingtask2, ( signed char * ) "LED2",
configMINIMAL_STACK_SIZE, NULL, 1, NULL );
xTaskCreate( ledBlinkingtask3, ( signed char * ) "LED3",
configMINIMAL_STACK_SIZE, NULL, 1, NULL );
// Start the RTOS kernel
vTaskStartScheduler();
/* Do nothing here and just run infinte loop */
while(1){};
return 0;
}

265

RTOS .11
..............................................................................................
static void ledBlinkingtask1(void* pvParameters){
/* Define all variables related to ledBlinkingtask1*/
const uint8_t blinkDelay = 50 ;
/* make PB0 work as output*/
DDRB |= (1<<PB0);
/* Start the infinte task 1 loop */
while (1)
{
PORTB ^= (1<<PB0);

//toggle PB0

vTaskDelay(blinkDelay); //wait some time


}
}
static void ledBlinkingtask2(void* pvParameters){
/* Define all variables related to ledBlinkingtask2*/
const uint8_t blinkDelay = 150;
/* make PB1 work as output*/
DDRB |= (1<<PB1);
/* Start the infinte task 2 loop */
while (1)
{
PORTB ^= (1<<PB1);

//toggle PB0

vTaskDelay(blinkDelay); //wait some time


}
}

266

.11 RTOS
..............................................................................................
{)static void ledBlinkingtask3(void* pvParameters
/* Define all variables related to ledBlinkingtask3*/
;const uint16_t blinkDelay = 600
/* make PB2 work as output*/
;)DDRB |= (1<<PB2
/* Start the infinte task 3 loop */
)while (1
{
//toggle PB0

;)PORTB ^= (1<<PB2

vTaskDelay(blinkDelay); //wait some time


}
}
main.c


make

267

.11 RTOS
..............................................................................................

simulation

.ATmega16

ATmega32 atmega328
makefile
: Hard Real time Firm Real time

RTOS FPGA
ASIC

RTOS
Single CPU core

268

.12
..............................................................................................

.12

CodeBlocks ATmel Studio

MakeFile

Hex

AVR

270

.12
..............................................................................................

: CodeBlocks

CodeBlocks
++ )

( ATmel Studio
.
GCC
avr-gcc .

CodeBlocks

:
http://www.codeblocks.org/downloads

3
codeblocks-13.12mingw-setup

CodeBlocks ) gcc-compiler 12-13

(.

271

.12
..............................................................................................
""

) CodeBlocks
:

272

.12
..............................................................................................

WinAVR

CodeBlocks WinAVR Toolchain


AVR-GCC +

avrdude

http://sourceforge.net/projects/winavr/fles
:

) AVRDUDESS (
avrdude . AVRDUDESS
http://blog.zakkemble.co.uk/avrdudess-a-gui-for-avrdude/
: AVRdudess

avrdude-gui : BitBurnner AVR8 Burn-O-Mat


avrdude avrdude
toolchain

273

.12
..............................................................................................
AVR Blinking Led

CodeBlocks New Project AVR

project :

ATmega16_blinking_led
.

274

.12
..............................................................................................
Create Release Create
Debug configuration

ATmega16 8
16 8000000

CodeBlocks
main.c fuses.c

.
main.c

275

.12
..............................................................................................
Build

) ( :

Compilation

. errors hex
Protues ATmega16

276

.12
..............................................................................................

makefile

) hex (.
avr-gcc
Atmel studio .Codeblocks
windows, linux, mac IDE

++NotePad sublime hex


) makefile (.


toolchain ) WinAVR

(toolchain

http://sourceforge.net/projects/winavr/files
toolchain make

command prompt
cmd start

) terminal(
toolchain

make: *** No targets specified and no makefile found. Stop.

277

.12
..............................................................................................

blinking led compile


with make file

main.c
makefile
main.c blinking led makefile
main.c .

++notepad

278

MCU=ATmega16

.12
..............................................................................................

) (.

PROGRAMMER_MCU=m16
avrdude

ATmega16 = m16
ATmega32 = m32
atmega328p = m328p

PROJECTNAME=blinkingled

PRJSRC=main.c
.

main.c makefile -

C:\Users\Abdallah\Documents\Blinking Led Example

cd path path Enter


279

.12
..............................................................................................

make hex main.c


blinkingled.hex

blinkingled.hex

make

make clean
: makefile
Real
Time Operating system

.

280

.12
..............................................................................................

: Hex

AVR 6 10) (ISP connector


USBasp

MOSI
) MISO SCK RST(RESET
5V VCC

AVCC
GND ) (.

281

.12
..............................................................................................
Breadboard

avrdude .AVRdudess
AVRdudess

USBasp :

( 1

93

282

.12
..............................................................................................
16
.

: AVRdudess
Detect .

Program

283

.12
..............................................................................................

avrdude
avrdude
) cmd shell (.
avrdude
.

284

.12
..............................................................................................

: -c avrdude :
.. arduino avrisp usbasp.
: -p avrdude .
: -b

avrdude
.

: -P .

dev/ttyACM COM .

: -U :
memtype:op:filename:format format

hex .bin filename op

operation ) read ( ) write


( verify
.

: avrdude

man avrdude .
avrdude
CC-BY-SA-NC

285

.12
..............................................................................................

AVR

Arduino
5 .Arduino nano
AVR ) Atmega ATTiny(.

Arduino IDE AVR-GCC


+ Processing

+ Wiring .avrdude
.ANSI C

" "

Atmega

port A, port B
0,1,2,3

.
Uno ) ( .

ATmega328 )
+ (.

286

.12
..............................................................................................


D0 .port D pin 0
Blinking Led ANSI C
: 13
) PB5 B (.

Arduino IDE Blinking led


. 16

#define F_CPU 16000000UL


Upload Arduino
287

.12
..............................................................................................
Hex file


Atmel Studio

visualmicro Atmel studio

http://www.visualmicro.com/page/Arduino-for-Atmel-Studio.aspx

288

.12
..............................................................................................
makefile

) (makefile ARVdudess

Programmer AVR
ArduinoISP + AVRdudess

Arduino IDE Blinking Led


https://ucexperiment.wordpress.com/2013/05/31/arduino-blink-using-gcc-inline/assembly

289


..............................................................................................

( ) .
https://www.youtube.com/playlist?list=PLww54WQ2wa5rOJ7FcXxi-CMNgmpybv7ei
--------------------------------------------------Bascom AVR
https://www.youtube.com/playlist?
list=PLww54WQ2wa5qWSTU7MYqjN0jHNaWuoXUk
-------------------------------------------------- ENG Unity
Digital Circuits Altium AVR
https://www.youtube.com/user/ENGUnity/playlists
--------------------------------------------------
C for Embedded System
Micrcontroller Architectre
https://www.youtube.com/channel/UCbZ7PLd5LAnje1hpyoiRW0A/playlists
--------------------------------------------------CodeVision AVR
http://www.qariya.info/vb/showthread.php?t=81782
-------------------------------------------------- ""
http://www.atadiat.com/%D8%A7%D9%84%D9%82%D8%B3%D9%85%D8%A7%D9%84%D8%AA%D8%B9%D9%84%D9%8A%D9%85%D9%8A/

290


..............................................................................................

( ) AVR
AVR Microcontroller and Embedded Systems: Using Assembly
and C (Pearson Custom Electronics Technology) - Muhammad
Ali Mazidi
http://www.amazon.com/AVR-Microcontroller-Embedded-SystemsElectronics/dp/0138003319
-------------------------------------------------- "

"

An Embedded Software Primer


http://www.amazon.com/Embedded-Software-Primer-David-Simon/dp/020161569X
-------------------------------------------------- 55 AVR NewBieHack

https://www.youtube.com/playlist?list=PLE72E4CFE73BD1DE1
--------------------------------------------------FreeRTOS
Using the FreeRTOS Real Time Kernel - Standard Edition
http://www.amazon.com/Using-FreeRTOS-Real-Time-Kernel/dp/1446169146/
---------------------------------------------------

291

You might also like