Professional Documents
Culture Documents
" 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
ANSI )(ANSI
) ( .
.Compiler
ARM
Registers
)
( .
ATmel
) (
.
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
AVR PIC = 16 ) 16 (
16
1.0
..............................................................................................
Pipeline AVR
PIC .
AVR
Hardware multiplier
2 )
( . PIC
40 .AVR
:
Assembly AVR
" Register"
32
)
PIC
(Accumulator AVR
.PIC
Array
17
1.0
..............................................................................................
) cylces (Clock
)(
atmega16 16
: AVR_introduction
Atmel
:
PIC AVR
) ( . Atmel
:
ATmel AVR compilers
ANSI C AVR-GCC
)
AVR PIC (.
18
1.0
..............................................................................................
Microchip MPLAB
ANSI C PIC16F
.
AVR
"" AVR
.PIC
:
AVR PIC
) :
..(
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
.
:
)(
23
.1
..............................................................................................
Bluetooth Wifi
Analog Electronics
SDR
1.2
MicroController .
Input devices
Microcontroller Motors
LCD .
24
.1
..............................................................................................
1.3
..
software .Hardware
Hex 0
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
..............................................................................................
)ATmega32 (DIP
ATmega32 (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
) (
PORTS ADC Timers
)(
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
= = 16 ) 16 (
16 .
36
.2 AVR
..............................................................................................
2x program
counter + 2x instruction decoder
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
38
.2 AVR
..............................................................................................
Special purpose microcontrollers
Automotive AVR
)
150 (
) (short circuit
Battery Management
mega, ATTiny
Performance
39
.2 AVR
..............................................................................................
Atmel
ATmega16
"
.complete
.Datasheet
ATmega16 ATtiny84
Datasheet
.
40
.2 AVR
..............................................................................................
2.4
Datasheet
Datasheet
. .
AVR
5 8
ATmega16
41
.2 AVR
..............................................................................................
2.5
ATmega16/ATmega32
( :
16 16
)
( .
.AVR
: 10 1
) = 1(
10 10 .
=1012 12 10 ).....+12+12+12+12(.
"" 10 ) ( =10
. AVR
.
42
.2 AVR
..............................................................................................
High endurance Memory
AVR
Flash RAM EEPROM :
) . ATmega32 32(.
1 )( SRAM
)(\ 10,000
25 25
80
.
) Bootloader (.
Programming locks
) Lockbits (.
43
.2 AVR
..............................................................................................
Jtag, Peripheral devices, Power consumption
Microcontroller packaging
" "
Epoxy
) Packaging(
( %10
44
.2 AVR
..............................................................................................
DIP SMD
ATmega16
DIP
SMD TQFP
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
(.
46
.2 AVR
..............................................................................................
VCC
) 2.7 5.5
(.
GND
. GND
Noise
Noise
)(.
XTAL1, XTAL2
(.
ADC
.VCC
A,B,C,D
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
.
) (.
USBasp
usb
)
(
USBasp AVR .
SMD USBasp
DIP USBasp
USBasp
www.fischl.de/usbasp
55
.3
..............................................................................................
( .
5 1.8
5 . 40
.
http://www.ATmel.com/tools/AVRISPMKII.aspx
AVRISP mkII
AVRISP mkII
: ) 10
(
avrisp mkII
.
56
.3
..............................................................................................
AVR Dragon
AVR "" .
) (.
Atmel :
www.ATmel.com/webdoc/avrdragon
55
.
57
.3
..............................................................................................
Universal Programmers
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
.
:
) + + (PCB holder
30-70
)( + .
64
+ )(.
.3
..............................................................................................
3.4
) Firmware-(
" " Development toolcahin
AVR
. :
++C/C )
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
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
; .
.
68
http://www.fischl.de/usbasp
.3
..............................................................................................
USBasp
USBasp
:
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
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
Hello World :
AVR
: A B
: 7Segment
74
Bouncing De-Bouncing
.4 GPIO Basics
..............................................................................................
Design Simulate
Prototype
) fuses (.
: ) Breadboard ( +
75
.4 GPIO Basics
..............................................................................................
4.1 :
Hello World
Blinking Led
.
Led 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 (.
81
.4 GPIO Basics
..............................................................................................
Schematic
)
P devices
82
.4 GPIO Basics
..............................................................................................
ATmega16
)LED (yellow
Resistor 330
LED bar
83
.4 GPIO Basics
..............................................................................................
Blinking led
ATmega16 :
terminal )
) \ .(ADC
84
.4 GPIO Basics
..............................................................................................
PA0
) Ground Terminals(
RESET
Power 10 ) (10k
:
85
.4 GPIO Basics
..............................................................................................
hex 0
CodeBlocks
( :
86
.4 GPIO Basics
..............................................................................................
CKSEL Fuses Int. RC 1 Mhz
OK
play
87
.4 GPIO Basics
..............................................................................................
4.2
AVR
3 :
Main Function
" "
AVR
;PORTA = 0b00000001
;)_delay_ms(500
;PORTA = 0b00000000
;)_delay_ms(500
}
;return 0
}
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
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
8 " "
DDRx
)
Input Output x
( A,B,C,D . DDRC
C DDRA A ..
output 0 .input
"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
PORTx Register
PORTx
DDRx
PA0
8 .
) PORTA 66 (.
92
.4 GPIO Basics
..............................................................................................
LOW - 0 HIGH 1
= 1 logic HIGH 5
) ( . =
)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
..............................................................................................
.
//
//
>#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
..............................................................................................
//
//
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
) 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 //
// 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
) (.
7
common anode
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
10 PB0
PA0 :
ATTiny84
106
.4 GPIO Basics
..............................................................................................
B DDRB = 0b00000000
PINB
) PIN x x A,B,C,D(
107
.4 GPIO Basics
..............................................................................................
PINB
.B
PINx 8
PINB ) PINx
(.
2 PB2.
=
. 5
PINB 0b00000111
PB0 PB1 PB2
0b11111111 ...
) if (condition
)if(PINB == 0b00000001
};{PORTA = 0b00000001
PINB ) 0b00000001
};{PORTA = 0b00000000
else PORTA = 0b00000000
108
.4 GPIO Basics
..............................................................................................
4.7
10 Digital Inputs
AND, OR, NOT .
10
VCC 3 ..
) ( :
Floating Area
Low 2 5 1 HIGH
) HIGH(1 ) LOW(0
) (.
Electric noise
input
.Floating Area
=
5
. 2.2 10.
109
.4 GPIO Basics
..............................................................................................
breadboard 10 .
)
(.
) ( :
Pull-Down
5 PB0
)(.
Pull-UP Pull-Down
Pull-Down
Pull-Up
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
" " .
.
debouncing
:
software debouncing
"" .
) (
debouncing
//
//
116
.4 GPIO Basics
..............................................................................................
. RTOS
) ROTS
(.
: )(
filter
10 + + ) ( .
AND gates .digital trigger
: .
Interrupts
117
RTOS
: .
.4 GPIO Basics
..............................................................................................
4.11
Load
) 330(
. ) (
15 20 5 .
) 5 15 (
25 ) (
!
: )(
.
Electrical Charchtaristics
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 .
.
5 )
75 = 155
(.
2n2222
NPN
) 10 (
) 500(
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 ) (.
;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
" "
) .. (
PORT Motor .. .
) (SRAM .
. .
signed numbers
unsigned
129
.5
..............................................................................................
) (Bytes
-32,768 +32,767
int
65,535
unsigned int
1.2E-38 to 3.4E+38
float
2.3E-308 to 1.7E+308
double
-2,147,483,648 +2,147,483,647
long
4,294,967,295
unsigned long
- 9.22337203710
+ 9.22337203710
long long
1.84467440710
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
:
) (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 .
8 //
)int main(void
{
;uint8_t counter
//
;DDRA = 0b11111111
0 // 255
132
.5
..............................................................................................
Binary:
Decimal
0b00000011 Decimal 3
;uint8_t variable = 3
- .Hexadecimal
// Binary
// Decimal
// Hexadecimal
-.
- 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 Function
.
.
Logic Function in ANSI C
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
Example 2:
;unit8_t Z = 0b11110000
;Z = ~ Z
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
140
.5
..............................................................................................
, for counter = 1
counter = 1
5.5
Single Bit
. 3
A DDRA = 0b00000111
.
) (PA0
DDRA
.
1 0
1 Set Bit
Set Bit 1 .
PA0
.A OR left shift
141
.5
..............................................................................................
PORTA
// turn on PA0
;)PORTA |= (1<<PA0
OR
PA1 PA2 :
;)DDRA |= (1<<PA1
;)DDRA |= (1<<PA2
0 Reset Bit
{ )(int main
)// set 1 in PA0 (output
;)DDRA |= (1<<PA0
)while (1
{
;)PORTA |= (1<<PA0
;)delay_ms(1000
142
.5
..............................................................................................
Toggle Bit
Toggle Bit
= 1 0 0 .1
)while (1
{
;)PORTA ^= (1<<PA0
;)_delay_ms(1000
}
"" set
reset )
(.
5.6
)push button(switch
.PINx
PINx :
:
)Reister & (1<<bitName
:
//1
};{PORTA = 0b00000001
143
- .
.6
..............................................................................................
.6
AVR
Clock Rate
.
Fuses ATmega16
)(
Lockbits
145
.6
..............................................................................................
6.1
AVR
16
.. .Fuses
) (Bits
) (Flash Memory
.
AVR 3 :
programmed = 0
unprogrammed = 1
0 1
unprogrammed programmed
0 .1
:
High voltage
programmers
.
146
.6
..............................................................................................
Datasheet
High Byte
) (
.
Bootload
- )(
- Arduino bootloader
. -
self programming
147
.6
..............................................................................................
programmer
. Arduino
Nano 3
BOOTRST bootloader
main function
bootloader = 1
-.
) BOOTSZ(0,1 -
) 257
- (.
EESAVE EEPROM
= 1 EEPROM
= 0 EEPROM
. = 1
. = 1
= 0
spi .
SPIEN
148
.6
..............................................................................................
JTAGEN, OCDEN
Jtag.
Jtag
.. Program counter.
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
..............................................................................................
) 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
..............................................................................................
.
) RC oscillator
(ceramic oscillator
.
= 65
SUT0 SUT1 = 1
152
.6
..............................................................................................
LockBits 6.2
lockbits
" " .
lockbits flash
eeprom
lockbits
LB1 = 0 LB2 =1 )(
LB1 = 0 LB2 =1 )
Flash EEPROM
( Flash EEPROM
153
.6
..............................................................................................
6.3
Clock Source
( .
) .(clock source
= )1( .
= 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
..............................................................................................
ATmega16 RC 1 8 )
( .
: - %3
delay_ms
. : load
= 1 ) (-\+ =
(.
) 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
..............................................................................................
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
) +
+(
158
.6
..............................................................................................
External Resonators
" +
"
1 24
: ) (
: ) %0.5 (
internal RC oscillator .
159
.6
..............................................................................................
: 3 )
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
..............................................................................................
.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
noise
Full rail-to-rail
noise.
" 36"
external RC circuit .
Pulse Oscillator
pulses TTL oscillator
163
.6
..............................................................................................
1 .CKSEL(3,2,1,0) = 0001
) 8(
.CKSEL(3,2,1,0) = 0100
Full rail-to-
) (
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 16 ) 7
( 7
6 . 1000
.
: = 7 )( 7 + )( 6 +
= 20 .
: = 40
Working Time = 0.8 * (1000/20) = 40 hours
: 0.8
167
.6
..............................................................................................
6.6
AVRdudess
programmer
) ( Ok Low
Byte Fuse HIGH Byte Fuse
168
.6
..............................................................................................
Write ) (
programmer .
.
Write.
Read Write Programmer
.
Fuse Setting
AVR
.
169
.6
..............................................................................................
6.7
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
173
: INT0 INT1
.7 Interrupt
..............................................................................................
7.1
The interrupt
. )
( interrupt input
pin
) ( .
.External interrupts
) (
External interrupt
) ADC ( Timer
.interrupt
.main program
()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
)
( .
RESET 60
1 ) 60,000
(.
.
.
ISR .
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
..............................................................................................
;)(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
.
.0 & 1 0 1 clock
.
178
.7 Interrupt
..............................................................................................
) clock = 1 Mhz ( = 1
1 .
)
(.
" Edges
) (.
.
LOW HIGH
179
.7 Interrupt
..............................................................................................
1:Rising
2:Falling
3:High
4:Low
AVR
0 & 1 .Edge
MCUCR
) ISCxx xx 0 1
68
( .
.ATmega16
180
.7 Interrupt
..............................................................................................
) 68 (ATmega16
: ISC00 ISC01
HIGH (.
: ISC00 1 ISC01
logic change
: ISC00 0 ISC01 1
edge detector
.7 Interrupt
..............................................................................................
.
INT0
INT0 HIGH LOW
. ISC00 ISC01
) 1 (.
INT0
69 .(ATmega16
) GICR
1 INT0
INT0 ..
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
183
.7 Interrupt
..............................................................................................
7.3 : INT0
INT1
INT0 INT1
2 INT0 INT1
)int main(void
{
//
184
.7 Interrupt
..............................................................................................
// INT0 & 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
)(
8 Parallel
8 Parallel 8
) ( Parallel-in-Serial-out shift
8 parallel 8
register
: Protocol
UART :
189
Synchronous
Asynchronous
.8 UART
..............................................................................................
) ( Block of data
.
Receiver
Synchronous-Asynchronous
AVR USART.
.Universal
:Simplex .1 . :
.
:Duplex .2 :
:Half-duplex .3
:
:Full-duplex .4 :
.
190
.8 UART
..............................................................................................
8.2
Asynchronous
0 .1
" "
ASCII A 10
1 8 . ) A( .
Parity bit
Baud rate:
191
.8 UART
..............................................................................................
) (AVR
ATmega16/32
RXD :
TXD :
192
.8 UART
..............................................................................................
8.3 UART
AVR
UART :
...
.UART
UART 5 :
-1
-2
-3
-4
-5
]UBRR [H: L
8 UBRRL 8 1
baud rate UBRRH .baud rate
.
:
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 .
195
.8 UART
..............................................................................................
16 .
: 16
16 + 22
.
16
ATmega16
UBRR 103.16667
.103 )
A ASCII (.
)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
:UBRR_Value
1
:UBRRL
1
UBRRH 8:
0
.baud rate
8.
.8 UART
..............................................................................................
1 . UCSZ0 & UCSZ2
UART .
UDR
UCSRA .1
5
1
.
. serial port
virtual terminal
A :
198
.8 UART
..............................................................................................
8.5 : UART
UART
). TXD virtual
terminal RXD (.
199
.8 UART
..............................................................................................
:
While (! (UCSRA & (1 << RXC))); // Waiting for Receiving buffer to be empty.
.
Virtual terminal .PORTC
a ) (0b01100001 .PORTC
: Virtual terminal
200
.8 UART
..............................................................................................
8.6 :
TXD RXD
ON " " N
.
//
//
)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
.8 UART
..............................................................................................
)'if(Received == 'F
;)PORTC &= ~(1<<PC0
}
;return 0
}
Character Received
" " N
PC0 " " F .
8.7
:
// U
// A
// R
// T
.
204
.8 UART
..............................................................................................
) ROM (
.
. Pointer
. .
.
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
;)(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
:F_CPU .
:Baud_rate .9600 :
:Lrint
include math.h .
207
.8 UART
..............................................................................................
:
//
)(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
UART .8
..............................................................................................
String
char UART_receive_char()
{
while (! (UCSRA & (1 << RXC) ) );
return UDR;
}
.()
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
) ( 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 .
: 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 .
vcc gnd
.
.PA1 8
ADC C :
;DDRA = 0x00
;DDRC = 0xff
216
.9 - ADC
..............................................................................................
// adc
;adcValue = ADCH
// C
;PORTC = adcValue
}
;return 0
8 uint8_t .adcValue
.ADC
volatile
217
.9 - ADC
..............................................................................................
.
.Code optimaization
) ( Assembly
1 3 10 .
) =
(.
.code optimaization
) ADCH
adcValue
( .
C :
// adcValue
;adcValue = ADCH
// C
;PORTC = adcValue
volatile
adcValue .
;PORTC = ADCH
.
. )
( .
. .volatile adcValue
:
volatile
.
218
.9 - ADC
..............................................................................................
. main
A C ) (.
;DDRA = 0x00
;DDRC = 0xff
ADC . ADC
ADC AVR .
clock .ADC .
ADC ) (.
Clock.
clock ) 1 (.
Prescaler Register clock
) (2 64 32 16 8 4 2
ADCSRA
ADPS0 ADPS1 ADPS2
219
.9 - ADC
..............................................................................................
.
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
.ADMUX =
1 ADLAR
) ADC 10(.
8 //
ADC 8
ADCH 10 )
220
.9 - ADC
..............................................................................................
( ADCL ADCH
10
left adjusted 8 ADCL 2 ADCH
) Input channel
( . PA1 1
MUX0
)( //
) MUX(1,2,3,4
.
ADC .
ADC 1 ADSC
ADC start conversion
//
221
.9 - ADC
..............................................................................................
ADC .
//
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
: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
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
)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
)int main(void
{
;set_port_output
)while(1
{
define
;PORTD=0xFF
;)_delay_ms(1000
;PORTD=0x00
;)_delay_ms(1000
}
;return 0
}
:
//set portD off
Preprocessor:
)while(1
{
;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
231
.10
..............................................................................................
function-like macros
:
1
PORTB |= (1<<5);
:
0 BIT_CLEAR
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
10 .
) Modules(
Header file
) Definition file (
UART
UART .
233
.10
..............................................................................................
10.7
driver
.
UART driver UART
.
: uart.h
++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
//
//
)(char UART_receive_char
__#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
. .
*/
;uint16_t UBRR_Value
: 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
ATmel studio
UART driver .
ATmel studio
237
.10
..............................................................................................
Solution Explorer
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
240
.10
..............................................................................................
uart.c
241
.10
..............................................................................................
.UART
.
242
.11 RTOS
..............................................................................................
.11 RTOS
Real Time OS
Multitasking .
FreeRTOS RTOS
) (.
RTOS
FreeRTOS Kernel
: 3
244
.11 RTOS
..............................................................................................
11.1
3:
.
.
.
Firm Real Time Systems Hard Real Time systems
. Wireless Router
) (
Data packets
245
.11 RTOS
..............................................................................................
11.2
. :
: 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
) RTOS scheduler
( RTOS kernel
) ( Context switching
Running task
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
.
( SAFE-RTOS
.
4 9 ROM
) ATmega16 16
ATmega32 32(.
FAT
storage media
TCP/IP UDP
IoT
250
.11 RTOS
..............................................................................................
11.5
RTOS
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
(
) FreeRTOS
(8.2.1 http://www.freertos.org
Download "
sourceforge.com
FreeRTOS
252
.11 RTOS
..............................................................................................
source .Demo
.
: +
FreeRTOS PIC, AVR, ARM cortex
AVR .
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
FreeRTOS/Source/portable/GCC/ATMega323/port.c
) #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10
253
.11 RTOS
..............................................................................................
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
..............................................................................................
1 = mask
00000010 0x02 . 0x10
0x02:
TIMSK
AVR 8bit
TIMSK0 TIMSK1 atmega328 atmega1284
mask
255
.11 RTOS
..............................................................................................
TIMSK )
(ATmega32/16 atmega328
atmega1284 TIMSK TIMSK0 ) TIMSK1
(.
: atmega1284/644/328/168/....etc
( . 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
#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 = 150
) ) ( (size_t ) ( 1500
#define configTOTAL_HEAP_SIZE
259
.11 RTOS
..............................................................................................
1500 byte
heap .
freeRTOS 300 400 kernel
heap
heap )(1500
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
(.
: makefile
Linux
toolchain ) IDE (ATmel studio
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 :
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
..............................................................................................
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
//toggle PB0
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
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
MakeFile
Hex
AVR
270
.12
..............................................................................................
: CodeBlocks
CodeBlocks
++ )
( ATmel Studio
.
GCC
avr-gcc .
CodeBlocks
:
http://www.codeblocks.org/downloads
3
codeblocks-13.12mingw-setup
(.
271
.12
..............................................................................................
""
) CodeBlocks
:
272
.12
..............................................................................................
WinAVR
avrdude
http://sourceforge.net/projects/winavr/fles
:
) AVRDUDESS (
avrdude . AVRDUDESS
http://blog.zakkemble.co.uk/avrdudess-a-gui-for-avrdude/
: AVRdudess
273
.12
..............................................................................................
AVR Blinking Led
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
toolchain ) WinAVR
(toolchain
http://sourceforge.net/projects/winavr/files
toolchain make
command prompt
cmd start
) terminal(
toolchain
277
.12
..............................................................................................
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 -
279
.12
..............................................................................................
blinkingled.hex
make
make clean
: makefile
Real
Time Operating system
.
280
.12
..............................................................................................
: Hex
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
: avrdude
man avrdude .
avrdude
CC-BY-SA-NC
285
.12
..............................................................................................
AVR
Arduino
5 .Arduino nano
AVR ) Atmega ATTiny(.
+ 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 (.
.12
..............................................................................................
Hex file
Atmel Studio
http://www.visualmicro.com/page/Arduino-for-Atmel-Studio.aspx
288
.12
..............................................................................................
makefile
) (makefile ARVdudess
Programmer AVR
ArduinoISP + AVRdudess
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
-------------------------------------------------- "
"
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