Vous êtes sur la page 1sur 224

AVR

(info@avr.ir) :

www.avr.ir
www.pnkavosh.com


Reset
C
: 1
:2 7-Segment
: 3 )(LCD
:4
:5 LED Dot Matrix

:6
/
: 7
:8 DC PWM

:9 RTC
/
:10

:11
)(RS-232
:12
:13 USB RS232
)I2C Bus (TWI
:12 EEPROM I2C

:13 LM35

SPI Bus
Mode Sleep Watchdog

:1 I/O
:2
:3 AVR
Pinout :4 AVR
:5 ATmega16

Page |4

www.pnkavosh.com

www.avr.ir

Reset

AVR
Erase . .
Mega16 2 :

Reset Boot

High Byte
BOOTRST

BOOTSZ0

BOOTSZ1

EEPROM Erase

EESAVE

CKOPT

SPI

SPIEN

JTAG

JTAGEN

JTAG

OCDEN

Bootloader

--

Page |5

www.pnkavosh.com

www.avr.ir

Low Byte

CKSEL0

CKSEL1

CKSEL2

CKSEL3

SUT0

SUT1

BODEN

BODLEVEL

Startup

1
1

Brown-out

Brown-out

:BOOTRST BOOT
0000 Reset ) .
](BOOTSZ[1:0

Pages Reset BOOTSZ1 BOOTSZ0 Boot


$1F80

Word 128

$F00

Word 256

$E00

Word 512

$C00

16

Word 1024

--

Page |6

www.pnkavosh.com

www.avr.ir

:BODEN Brown-out Detector


Brown-out .

BODEN BODLEVEL Brown-out

Vcc=2.7v

Vcc=4.0v

: RC
RC / .

--

Page |7

www.pnkavosh.com

www.avr.ir

CKSEL .
CKSEL RC .

--

Page |8

www.pnkavosh.com

www.avr.ir

:
XTAL1 . CKSEL ) (
CKOPT 36
.

XTAL1

CKOPT
36 pF

XTAL2

RC : 4 2 1 8
25 5 3
. CKSEL
. CKOPT .

--

Page |9

www.pnkavosh.com

www.avr.ir

RC : .
1

3 RC

= f . C 22

CKOPT 36 .

R
XTAL1
C

CKOPT
36 pF

XTAL2

Mode
CKSEL .

:
32768 . 1001 CKSEL

--

P a g e | 10

www.pnkavosh.com

www.avr.ir

. CKOPT
.

36 pF

CKOPT
XTAL1

C1

XTAL

Rf
XTAL2

C2
CKOPT
36 pF

: XTAL1 XTAL2
On-chip .

- -

P a g e | 11

www.pnkavosh.com

www.avr.ir

XTAL1

C1

XTAL

Rf
XTAL2

C2


Startup
.

36 CKOPT .

XTAL2 . CKOPT
.

8 CKOPT .


CKSEL .

- -

P a g e | 12

www.pnkavosh.com

www.avr.ir

CKSEL ] SUT[1:0
Start-up .
Start-up
System Clock and Clock Options Datatsheet .

Reset

Reset I/O CPU


Reset . 5 Mega16 Reset :

- -

Power-on Reset

1.

External Reset

2.

Brown-out Reset

3.

Watchdog Reset

4.

JTAG AVR Reset

5.

P a g e | 13

www.pnkavosh.com

Reset :

Reset :

- -

www.avr.ir

P a g e | 14

www.pnkavosh.com

.1

www.avr.ir

:Power-on Reset Vcc .

Reset .
Reset .
RESET Vcc ) . Pull-up (.

Reset Vcc :

- -

P a g e | 15

www.pnkavosh.com

www.avr.ir

:External Reset .2 Reset RESET


1.5 .
Time-out .

.3

:Brown-out Detection ATmega16 Brown-out Detection

Vcc . 2.7
BODLEVEL 4.0 .
Reset
. Spike

- -

P a g e | 16

www.avr.ir

www.pnkavosh.com
VBOT+ = VBOT + VHYST / 2

VBOT = VBOT VHYST / 2 .

Brown-out Detection
BODEN )( .

:Watchdog Reset .4 Watchdog


.
.

- -

P a g e | 17

www.avr.ir

www.pnkavosh.com

MCU Control and Status Register

MCUCSR

JTD ISC2 - JTRF WDRF BORF EXTRF PORF

CPU Reset
Reset .
.

Bit 0 PORF: Power-on Reset Flag


Bit 1 EXTRF: External Reset Flag
Bit 2 BORF: Brown-out Reset Flag
Bit 3 WDRF: Watchdog Reset Flag
Bit 4 JTRF: JTAG Reset Flag

- -

P a g e | 18

www.pnkavosh.com

www.avr.ir

C
C )( main) . (.
:

)(void main
{
{)while(1
.
.
.
}
}

Hello World .

- -

P a g e | 19

www.pnkavosh.com

www.avr.ir

>#include<stdio.h
)(void main
{
;)"!printf("Hello World
;)while(1
}
.
) while(1 .
.
Brace .
" " .
// /* */ .
.

31 .
C Case Sensitive .
) . (if, char, while,

- -

P a g e | 20

www.avr.ir

www.pnkavosh.com



char a ;

int b@0xA3;

Type

Size (Bits)

bit

0,1

char

-128 to 127

unsigned char

0 to 255

signed char

-128 to 127

int

16

-32768 to 32767

short int

16

-32768 to 32767

unsigned int

16

0 to 65535

signed int

16

-32768 to 32767

long int

32

-2147483648 to 2147483647

unsigned long int

32

0 to 4294967295

signed long int

32

-2147483648 to 2147483647

float

32

1.175e-38 to 3.402e38

double

32

1.175e-38 to 3.402e38

Range

. @

. eeprom EEPROM
eeprom int code;

- -

P a g e | 21

www.pnkavosh.com

www.avr.ir

;'char s = 'a

const flash .:

; const float pi = 3.14

#define
.
:

; #define code 100

3*2

5 /2

2.5

3 +6

8-3

10%3

&

AND

0xF0 & 0x0F

0x00

OR

0x00 | 0x03

0x03

XOR

0x0F ^ 0xFF

0xF0

)~(0xF0

0x0F

>>

0xF0 >> 4

0x0F

<<

0x0F << 4

0xF0

- -

P a g e | 22

www.pnkavosh.com

www.avr.ir

-a

a = a -1

++

a++

a=a+1

--

a--

a=a-1

>

2>3

False

<

''m'>'e

True

=>

5>=5

True

=<

2.5<=4

True

==

''A' =='B

False

=!

2!=3

True

- -

P a g e | 23

www.pnkavosh.com

www.avr.ir

&&

AND

)(2>3)&&(1!=3

False

||

OR

)('a'<3)||(10

True

)!(7>5

False

a=b

ab

=*

a*=b

a=a*b

=/

a/=b

a=a/b

=+

a+= b

a=a+b

=-

a-=b

a=a-b

a=(value)?x:y

value
a x

y .

- -

P a g e | 24

www.pnkavosh.com

www.avr.ir


.
:

; ] [

:
:

;}int a[5] = {1,2,3,4,5

]; : [] [
;}}int a[2][3]={{1,2,3},{4,5,6

;]int a[5

C .

:
:

;"char name[ ] = "Test


;}'char name[5]={'T','e','s','t','\0

Null .

- -

P a g e | 25

www.pnkavosh.com

www.avr.ir

c :goto
.

d :if - else
)(if
{
;1
}
else
{
; 2
}
9

brace .

else .

>#include<stdio.h

{)(void main
- -

P a g e | 26

www.avr.ir

www.pnkavosh.com

int a;

printf("Enter a Number: ");


scanf("%d",&a);

if(a= =0)
printf("You've entered zero\n");
else if(a>0)
printf("You've entered a positive number\n");
else
printf("You've entered a negative number\n");
}

:Switch - Case e
switch()
{
case

1:

1;
break;
case

2:

2;
break;
- -

P a g e | 27

www.avr.ir

www.pnkavosh.com

.
.
default:
n ;
}
:
#include<stdio.h>
void main(){
int a;
printf("Enter Month of your birth: ");
scanf("%d",&a);
switch(a){
case 1:
case 2:
case 3: printf("You've born is spring\n");
break;
case 4:
case 5:
case 6: printf("You've born is summer\n");
break;
case 7:
case 8:
case 9: printf("You've born is autumn\n");
- -

P a g e | 28

www.avr.ir

www.pnkavosh.com

break;
case 10:
case 11:
case 12: printf("You've born is winter\n");
break;
default: printf("Error! Enter a number between 112\n");
}
}

:While c
while()
{
;
}

:
#include<stdio.h>
void main(){
- -

P a g e | 29

www.pnkavosh.com

www.avr.ir

;char a
;)"printf("Enter E to exit\n
;)(while(a != 'E') a=getchar
}

d :Do/While
{do
;
)(} while

9 Do/While While
.

e :For
) ; ; (for
{
;
}

- -

P a g e | 30

www.avr.ir

www.pnkavosh.com

#include<stdio.h>

void main(){

int a,i;
long int fact=1;

printf("Enter a Number: ");


scanf("%d",&a);

if(a<0)
printf("Error! You must Enter a positive number\n");
else if(a==0)
printf("Factorial of 1 is 1\n");
else{
for(i=1;i<=a;i++)
fact*=i;
printf("Factorial of %d is %d\n",a,fact);
}
}

- -

P a g e | 31

www.pnkavosh.com

www.avr.ir

break .

continue .

) (

= .

>#include <stdio.h

;)long int cube(int x

{)(void main
- -

P a g e | 32

www.avr.ir

www.pnkavosh.com

int a;

printf("Enter a number: ");


scanf("%d",&a);
printf("Cube of %d is %d\n",a,cube(a));

long int cube(int x){


return x*x*x;
}

#include<stdio.h>
int _max(int a,int b);
void main(){
int a,b;
printf("Enter Two Numbers: ");
scanf("%d%d",&a,&b);
printf("Maximum of %d and %d is %d\n",a,b,_max(a,b));
- -

P a g e | 33

www.avr.ir

www.pnkavosh.com

}
int _max(int a,int b){
if(a>b)
return a;
else
return b;
}

- -

P a g e | 34

www.avr.ir

www.pnkavosh.com

:1

/*****************************************************
Project : LED Flasher
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include<mega16.h>
#include<delay.h>
#define xtal 1000000
int i;
void main (void)
{
DDRA = 0xFF;

//. 1

while(1)
{
for(i = 1; i <= 128; i = i*2)
{
PORTD = i;
delay_ms(100);
}
- -

P a g e | 35

www.avr.ir

www.pnkavosh.com

for(i = 64; i > 1; i = i/2)


{
PORTD = i;
delay_ms(100);
}
}
}

- -

P a g e | 36

www.avr.ir

www.pnkavosh.com

7-Segment :2

/*****************************************************
Project : Key Counter
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <mega16.h>
#define xtal 4000000
flash char digits[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char p_state;
unsigned char key;
unsigned char i;
void main(void)
{
DDRD = 0xFF;
PORTD = digits[0];
DDRC = 0x00;
PORTC = 0xFF;
while(1)
{
key = PINC & 0b00000001;
- -

P a g e | 37

www.avr.ir

www.pnkavosh.com

delay_ms(10);
if(key==0)
{
if(key!=p_state)
{
if(i==15)
{
i=0;
PORTD=digits[i];
}
else
i++;
PORTD = digits[i];
p_state=0;
};
}
else
p_state=1;
}
}

- -

P a g e | 38

www.pnkavosh.com

www.avr.ir

7-Seg ) .
(.

HEX

Dp

0x3F

0x06

0x5B

0x4F

0x66

0x6D

0x7D

0x07

0x7F

0x6F

0x77

0x7C

- -

P a g e | 39

www.avr.ir

www.pnkavosh.com

0x39

0x5E

0x79

0x71

- -

P a g e | 40

www.avr.ir

www.pnkavosh.com

(LCD) :3

/*****************************************************
Project : LCD Interfacing
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <stdio.h>
#include <mega16.h>
#include <delay.h>
#include <lcd.h>
#define xtal 4000000

#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
void main(void)
{
char buffer[10];
unsigned char w;
PORTB=0xFF;
DDRB=0x00;

- -

P a g e | 41

www.avr.ir

www.pnkavosh.com

lcd_init(16);
lcd_clear();
while (1){
w = ~PINB;
if(w!=0x00)
{
lcd_clear();
lcd_gotoxy(0,0);
sprintf(buffer,"Number=%d",w);
lcd_puts(buffer);
delay_ms(100);
}
else
{
lcd_clear();
lcd_putsf("Number=0");
delay_ms(100);
}
}
}

- -

P a g e | 42

www.avr.ir

www.pnkavosh.com

- -

P a g e | 43

www.avr.ir

www.pnkavosh.com

:4

/*****************************************************
Project : Keypad Scan
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <mega16.h>
#include <delay.h>
#define xtal 4000000
unsigned char key, butnum;
flash unsigned char keytbl[16]={0xee, 0xed, 0xeb, 0xe7,
0xde, 0xdd, 0xdb, 0xd7, 0xbe, 0xbd, 0xbb, 0xb7, 0x7e,
0x7d, 0x7b, 0x77};
void main(void)
{
DDRB = 0xff;
PORTB = 0xff;
while(1)
{
DDRC = 0x0f;
- -

P a g e | 44

www.avr.ir

www.pnkavosh.com

PORTC = 0xf0;
delay_us(5);
key = PINC;
DDRC = 0xf0;
PORTC = 0x0f;
delay_us(5);
key = key | PINC;
delay_ms(10);
if (key != 0xff)
{
for (butnum=0; butnum<16; butnum++)
{
if (keytbl[butnum]==key)
}
if (butnum==16) butnum=0;
else butnum++;
}
else butnum=0;
PORTB = ~ butnum ;
}
}

- -

break;

P a g e | 45

www.avr.ir

www.pnkavosh.com

- -

P a g e | 46

www.avr.ir

www.pnkavosh.com

LED Dot Matrix :5

/*****************************************************
Project : Dot Matrix Display
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <mega16.h>
#include <delay.h>
#define xtal 4000000
unsigned char k;
flash unsigned char arr[8]={0x18, 0x3C, 0x66, 0x66, 0x7E,
0x66, 0x66, 0x00};
void main(void)
{
DDRA=0xFF;
DDRB=0xFF;
while (1){
for(k=0;k<=7;k++){
PORTA=arr[k];
PORTB=~(1<<k);
delay_us(100);
- -

P a g e | 47

www.avr.ir

www.pnkavosh.com

PORTB=0xFF;
}
}
}

- -

P a g e | 48

www.avr.ir

www.pnkavosh.com

. 3 21 ATmega16

CodeVision
1

$000

RESET

External Pin, Power-on Reset, Brown-out Reset,


Watchdog Reset, and JTAG AVR Reset

EXT_INT0

$002

INT0

External Interrupt Request 0

EXT_INT1

$004

INT1

External Interrupt Request 1

TIM2_COMP

$006

TIMER2 COMP

Timer/Counter2 Compare Match

TIM2_OVF

$008

TIMER2 OVF

Timer/Counter2 Overflow

TIM1_CAPT

$00A

TIMER1 CAPT

Timer/Counter1 Capture Event

TIM1_COMPA

$00C

TIMER1 COMPA

Timer/Counter1 Compare Match A

TIM1_COMPB

$00E

TIMER1 COMPB

Timer/Counter1 Compare Match B

TIM1_OVF

$010

TIMER1 OVF

Timer/Counter1 Overflow

10

TIM0_OVF

$012

TIMER0 OVF

Timer/Counter0 Overflow

11

SPI_STC

$014

SPI, STC

Serial Transfer Complete

12

USART_RXC

$016

USART, RXC

USART, Rx Complete

13

USART_DRE

$018

USART, UDRE

USART Data Register Empty

14

USART_TXC

$01A

USART, TXC

USART, Tx Complete

15

ADC_INT

$01C

ADC

ADC Conversion Complete

16

EE_RDY

$01E

EE_RDY

EEPROM Ready

17

ANA_COMP

$020

ANA_COMP

Analog Comparator

18

TWI

$022

TWI

Two-wire Serial Interface

- -

P a g e | 49

www.pnkavosh.com

www.avr.ir

External Interrupt Request 2

INT2

$024

EXT_INT2

Timer/Counter0 Compare Match

TIMER0 COMP

$026

TIM0_COMP

20

Store Program Memory Ready

SPM_RDY

$028

SPM_READY

21

19

SEI
SREG :

Bit

SREG



. ISR .

GICR :

Bit

INT2

INT0

INT1

GICR

- -

P a g e | 50

www.pnkavosh.com

www.avr.ir

10 2 PD3 PD2 PB2 .

0 1 MCUCR :

ISC00

ICS01

ISC10

ISC11

Bit

MCUCSR

]:MCUCR[3:0

ISC00

ISC01

INT0

INT0

INT0

INT0

ISC10

ISC11

INT1

INT1

INT1

INT1

- -

P a g e | 51

www.pnkavosh.com

www.avr.ir

2 6 MCUCSR :

Bit

MCUCSR

ISC2

2 0 1
. ISC2
.


GICR ) (I
. ISR
.

Bit

INTF2

INTF0

INTF1

GIFR

CodeVision :

- -

P a g e | 52

www.avr.ir

interrupt [ ] void

www.pnkavosh.com

( void)

{

}
)21 2 ATMEGA16
. (

:1

#include <mega16.h>
#include <delay.h>
interrupt [2] void LED_ON(void)
{
PORTA=0x01;
delay_ms(1000);
PORTA=0x00;
}
void main(void)
{
DDRB=0xFF;
PORTB=0x00;
DDRA=0xFF;
PORTA=0x00;
DDRD=0x00;
- -

P a g e | 53

www.avr.ir

www.pnkavosh.com

PORTD=0xFF;
GICR=0b01000000;

// INT0: On

MCUCR=0b00000010;

// INT0 Mode: Falling Edge

#asm("sei")

// Global enable interrupts

while (1)
{
PORTB=0x01;
delay_ms(500);
PORTB=0x00;
delay_ms(500);
};
}

- -

P a g e | 54

www.pnkavosh.com

www.avr.ir


ISR
.

INT0 INT1 INT2



Mode . 50
.

CodeVision SREG CPU


ISR Stack ) (PUSH
ISR ) (POP .

#-pragma savereg .

- -

P a g e | 55

www.avr.ir

www.pnkavosh.com

:6

/*****************************************************
Project : Zero Cross Detector
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <mega16.h>
#include <delay.h>
#define xtal 4000000
interrupt [2] void switch_(void)
{
PORTA=0x01;
delay_ms(1);
PORTA=0x00;
}
void main(void)
{
DDRA=0xFF;
PORTA=0x00;
DDRD=0x00;
PORTD=0xFF;
GICR=0b01000000;

// INT0: On

MCUCR=0b00000011;

// INT0 Mode: Rising Edge


- -

P a g e | 56

www.avr.ir

#asm("sei")

www.pnkavosh.com

// Global enable interrupts

while (1);
}
Output vs. Input:

- -

P a g e | 57

www.avr.ir

www.pnkavosh.com

- -

P a g e | 58

www.pnkavosh.com

www.avr.ir

/ 8 Mode Fast PWM CTC Normal


Correct PWM Phase . T0 OC0
. TCNT0 TCCR0 OCR0
.
TIFR TIMSK .

TTCR0 Clock Select


Wave Generation Mode Mode Compare Match Output
Mode OC0 . FOC0 .

FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00

Mode WGM01 WGM00 :

- -

TCCR0

P a g e | 59

www.avr.ir

Mode

www.pnkavosh.com

Mode

WGM01 WGM00

Normal

PWM, Phase Correct

Clear Timer on Compare Match


(CTC)
Fast PWM

Noramal Mode c

8 /

: TIMER0

TCCR0

FOC0

WGM00

COM01

COM00

WGM01

CS02

CS01

CS00

Clock Prescaler /
: Select

- -

P a g e | 60

www.pnkavosh.com

www.avr.ir

CS00

CS01

CS02

)(

) (

/8

/64

/256

/1024

)(T0

)(T0

TCNT :

Bit

TCNT0

]TCNT0[7:0

TOV0 TIFR .

TIFR

TOV0

OCF0

TOV1

OCF1B

OCF1A

ICF1

TOV2

OCF2

- -

P a g e | 61

www.avr.ir

www.pnkavosh.com

:( s T = 512 ) :1

#include<mega16.h>
#define xtal 8000000
void delay()
{
TCCR0=0B00000010;

// Timer Clock = CLK/8

while(!TIFR&0x01);

// Wait Until Overflow

TIFR=TIFR|0B00000001;

// Clear TOV0

TCCR0=0x00;

// Stop Timer0

}
void main()
{
DDRA=0xFF;
PORTA=0x00;
TCCR0=0x00;
TCNT0=0x00;
while(1){
PORTA.0=1;
delay();
PORTA.0=0;
delay();
}
}
- -

P a g e | 62

www.avr.ir

www.pnkavosh.com

( 400s ) :2
void delay()
{
TCNT0=0x38;

//TCNT=55

TCCR0=0B00000010;
while(!TIFR&0x01);
TIFR=TIFR|0B00000001;
TCCR0=0x00;
}

TOIE2 TOIE0 ( I)

: TIMSK

TIMSK

OCIE2

TOIE2

TICIE1

OCIE1A

OCIE1B

TOIE1

OCIE0

TOIE0

( T = 400s ) :3
#include <mega16.h>
#define xtal 8000000
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
PORTA^=0xFF;
TCNT0=0x38;

//TCNT=55
- -

P a g e | 63

www.pnkavosh.com

www.avr.ir

}
)void main(void
{
;DDRA=0xFF
;PORTA=0x00
// Timer Clock = CLK/8

;TCCR0=0B00000010

//Enable TIMER0 Overflow

;TIMSK=0x01
Interrupt
)"#asm("sei

// Global enable interrupts

;TCNT0=0x38
;)while (1
}

Bit

OCR0/2

]OCR0[7:0

OCR0 TCNT0
OC0 . OCF0 OCF1
.
.

3
- -

TIFR

P a g e | 64

www.pnkavosh.com

www.avr.ir

TOV0

OCF0

TOV1

OCF1B

OCF1A

ICF1

TOV2

OCF2

OC0 COM00 COM01 :

TCCR0

CS00

CS01

CS02

WGM01

COM00

COM01

WGM00

FOC0

OC0

COM01 COM00

) I/O(

Toggle

Clear

Set

FOC0 FOC1 TCNT0 OCR0


OC0 .
OCF0 OCF1 .
OC
.

- -

P a g e | 65

www.avr.ir

www.pnkavosh.com

(OC0 s T = 512 ) :4
#include<mega16.h>
#define xtal 8000000
void main()
{
DDRB=0xFF;
PORTB=0x00;
TCNT0=0x00;
TCCR0=0B00010010; //toggle OC0 on compare match
OCR0=0x63; //OCR0=99
while(1);
}

Set
.

TIMSK

OCIE2

TOIE2

TICIE1

OCIE1A

OCIE1B

TOIE1

OCIE0

TOIE0

- -

P a g e | 66

www.pnkavosh.com

www.avr.ir

ISR :

)interrupt [TIM0_COMP] void timer0_comp_isr(void


{

}

CTC Mode d
Mode
OCR0 TCNT0 TCNT0
TCNT0 OCR0 . WGM00 WGM01
Mode 0 1 .

TCCR0

CS00

CS01

CS02

WGM01

COM00

COM01

WGM00

FOC0

OC0 :

f CLK _ I / O
)2.N .(1 + OCR 0

- -

= f OC 0

P a g e | 67

www.pnkavosh.com

www.avr.ir

) :5 5KHz :(OC0
>#include<mega16.h
#define xtal 8000000
)(void main
{
;DDRB=0xFF
;PORTB=0x00
;TCNT0=0x00
OCR0=0x63; //OCR0=99
TCCR0=0B00011010; //toggle OC0 on compare match
;)while(1
}

8000000
= 5000Hz
)2.8.(1 + 99

= f OC0

CTC Mode .
OCIE0 ISR OCR0
.

OCR0 Mode PWM


. OCR0 TCNT0

- -

P a g e | 68

www.avr.ir

www.pnkavosh.com


. OCR0

:7

/*****************************************************
Project : Frequency Meter
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
#include <lcd.h>
#define xtal 8000000
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
unsigned long int timer0_ov;
unsigned long int in_freq;
unsigned char lcd_buff[20];
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
- -

P a g e | 69

www.avr.ir

www.pnkavosh.com

timer0_ov ++;
}
void main(void)
{
// Timer/Counter 0 initialization
// Clock source: T0 pin Falling Edge
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCNT0=0x00;
OCR0=0x00;
TCCR0=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// LCD module initialization
lcd_init(16);
while (1)
{
TCCR0=0x06;
#asm("sei")

// Start Timer T0 pin Falling Edge


// Global enable interrupts

delay_ms(1000);
#asm("cli");

// Global disable interrupts

- -

P a g e | 70

www.avr.ir

www.pnkavosh.com

in_freq = timer0_ov * 256 + TCNT0;


sprintf(lcd_buff,"Freqency=%d",in_freq);
lcd_clear();
lcd_puts(lcd_buff);
TCCR0=0x00;

//Stopt Timer0

timer0_ov=0;

//Prepare for next count

TCNT0=0;

//Clear Timer0

};
}

- -

P a g e | 71

www.pnkavosh.com

www.avr.ir

Fast PWM Mode e

Mode OC0
TCNT0 OCR0 TCNT0
.

TCNT0

OCR0

PWM

WGM00 WGM01 Mode 1 .

Bit

CS00

CS01

CS02

COM00

COM01

TCCR0

Mode PWM COM00 COM01


:

OC0

- -

COM00

COM01

P a g e | 72

www.pnkavosh.com

www.avr.ir

) I/O(

Clear Set ) PWM (

Set Clear ) PWM(

PWM :
X = 256

N = Prescale = 1, 8, 64, 256, 1024

f clk _ I / O
N .X

= f PWM

TCNT0 OCR0
OCR0 .

TCNT0 PWM (.

) :6 PWM 4KHz 20(

>#include <mega16.h
#define xtal 8000000

)interrupt [TIM0_OVF] void timer0_ovf_isr(void


{
;TCNT0=0x06
}
)void main(void
{

- -

P a g e | 73

www.avr.ir

www.pnkavosh.com

PORTB=0x00;
DDRB=0x08;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0x6A; //0x7A for inverted PWM
TCNT0=0x06;
OCR0=0x38;

//OCR0 = 56

// Timer(s)/Counter(s) Interrupt(s) initialization


TIMSK=0x01;
// Global enable interrupts
#asm("sei")
while (1);
}

f PWM =

8000000
= 4000Hz
8(256 6)

DutyCycle =

OCR0
50
100% =
100% = 20%
250
250

- -

P a g e | 74

www.pnkavosh.com

www.avr.ir

OCR0
.
256 OCR0 COM00
COM01 .

Phase Correct PWM Mode f


Mode Fast PWM ) (Dual Slope
.
0xFF OCR0 OCR0
.
ISR
.
TCNT0 OCR0
OCF0 OC0 .

OCR0

TCNT0

PWM

- -

P a g e | 75

www.pnkavosh.com

www.avr.ir

OC0 :

OC0

COM00

COM01

) I/O(

Clear ) PWM (
0

Set

Set ) PWM(
1

Clear

OCR0
0xFF OCR0 .
TCNT0 0xFF

.
PWM Phase Correct :

N = 1, 8, 64, 256, 1024

f clk _ I / O
N 510

= f PWM

PWM OCR0
TCNT0.

- -

P a g e | 76

www.avr.ir

www.pnkavosh.com

PWM OCR0 PWM


. PWM

PWM DC :8

/*****************************************************
Project : DC Motor Control
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


*****************************************************/
#include <mega16.h>
#define xtal 1000000
char digits[8]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07};
unsigned char;
unsigned char p_state;
unsigned char key;
unsigned char i;
void main(void)
{
PORTB=0x00;
DDRB=0xFF;
DDRD = 0xFF;
PORTD = digits[0];
DDRC = 0x00;
- -

P a g e | 77

www.avr.ir

www.pnkavosh.com

PORTC = 0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 15.625 kHz
// Mode: Phase correct PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0x63;
TCNT0=0x00;
OCR0=10;
while (1)
{
if(!PINC.0)
{
if(key!=p_state)
{
if(i==7)
{
i=0;
PORTC=digits[0];
}
else
i++;
PORTD = digits[i];
OCR0 = i*10+10;
p_state=key;
};
}
- -

P a g e | 78

www.pnkavosh.com

www.avr.ir

else
;p_state=0xFF
;}
}

: LED .

- -

P a g e | 79

www.pnkavosh.com

www.avr.ir

2 2
2 T0 T1
32.768 TOSC1 TOSC2
RTC . 2 Prescaler 01
32768 128 1
. Prescale :

CS00

CS01

CS02

)(

) (

/8

/32

/64

/128

/256

/1024

RTC ASSR :

- -

P a g e | 80

www.pnkavosh.com

www.avr.ir

TCR2UB

OCR2UB

TCN2UB

AS0

Bit
ASSR

:Asynchronous Timer/Counter2 Set 2


TOSC1 TOSC2 .
OCR2 TCNT2 TCCR2 .

:Timer/Counter2 Update Busy


2 0 1 . TCNT2
TCN2UB
TCNT2 . TCNT2 .
OCR2UB TCNT2 .

:Output Compare Register2 Update Busy TCN2UB


OCR2 .

:Timer/Counter Control Register2 Update Busy TCN2UB


TCCR2 .


.
- -

P a g e | 81

www.pnkavosh.com

www.avr.ir

TCNT2
OCR2 TCCR2 .
2 Power-Save
Power-Save ISR
Power-Save .

:9 RTC

*****************************************************/
Project : Real Time Clock
: Reza Sepas Yar

Author

Company : Pishro Noavaran Kavosh


*****************************************************/
>#include <mega16.h
>#include <lcd.h
>#include <stdio.h
#define xtal 8000000
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
;unsigned char second, minute,hour
;]unsigned char lcd_buff[10
- -

P a g e | 82

www.avr.ir

www.pnkavosh.com

interrupt [TIM2_OVF] void timer2_ovf_isr(void)


{
if(second==59)
{
second=0;
if(minute==59)
{
minute=0;
if(hour==23)
hour=0;
else
hour++;
}
else
minute++;
}
else
second++;
sprintf(lcd_buff,"Time = %d:%d:%d",hour, minute,
second);
lcd_clear();
lcd_puts(lcd_buff);
}
void main(void)
{
// Clock source: TOSC1 pin
// Clock value: PCK2/128
- -

P a g e | 83

www.avr.ir

www.pnkavosh.com

// Mode: Normal top=FFh


// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x05;
TCNT2=0x00;
OCR2=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;
lcd_init(16);
#asm("sei") // Global enable interrupts
while (1);
}

- -

P a g e | 84

www.pnkavosh.com

www.avr.ir

16 Capture
.
. Mode
Phase Correct PWM Mode Fast PWM CTC Normal Phase and Mode
Frequency Correct Mode . PWM 1 12 PWM
. T1 OC1A OC1B
. ICP1 Capture .
16 TCNT1 OCR1A OCR1B
L H .
OCR1A OCR1B TCNT1
OC1A OC1B . ICR1 Capture
16 .

OCR1B OCR1A TCNT1 :1 ICR1

- -

P a g e | 85

www.avr.ir

www.pnkavosh.com

: TCCR1B TCCR1A 8

TCCR1A

COM1A1

COM1A0

COM1B1

COM1B0

FOC1A

FOC1B

WGM11

WGM10

TCCR1B

ICNC1

ICES1

WGM13

WGM12

CS12

CS11

CS10

: WGM13 WGM12 ,WGM11 ,WGM10 Mode

WGM13

WGM12

WGM11

WGM10

Mode

TOP

TOV=1

Normal

0xFFFF

0xFFFF

PWM, Phase Correct, 8-bit

0x00FF

PWM, Phase Correct, 9-bit

0x01FF

PWM, Phase Correct, 10-bit

0x03FF

CTC

OCR1A

0xFFFF

Fast PWM, 8-bit

0x00FF

TOP

Fast PWM, 9-bit

0x01FF

TOP

Fast PWM, 10-bit

0x03FF

TOP

PWM, Phase and Frequency Correct

ICR1

PWM, Phase and Frequency Correct

OCR1A

10

PWM, Phase Correct

ICR1

11

PWM, Phase Correct

OCR1A

12

CTC

ICR1

0xFFFF

13

14

Fast PWM

- -

ICR1

TOP

P a g e | 86

www.pnkavosh.com
TOP

OCR1A

Fast PWM

www.avr.ir
1

15

:TOP TOP
. 0x01FF ,0x03FF 0x00FF
OCR1A ICR1.

FOC1A :FOC1B Force FOC0


. Mode PWM
OC1A OC1B COM
TCCR1 .

COM1B0 ,COM1A1 ,COM1A0 :COM1B1 OC1A


OC1B Mode
. Mode .

CS11 CS10 :CS12 :

CS00

CS01

CS02

)(

) (

/8

- -

P a g e | 87

www.pnkavosh.com

www.avr.ir

/64

/256

/1024

)(T1

)(T1

:ICES1 Capture .ICP1


.

:ICNC1 ICP1

1 4 TIFR ) TOV1 ( ) OCF1A


) OCF1B (A (B ) ICF1 Capture (1
:

TIFR

TOV0

OCF0

TOV1

OCF1B

OCF1A

ICF1

TOV2

OCF2

) (I
TIMSK ISR :

- -

P a g e | 88

www.pnkavosh.com

www.avr.ir

TIMSK

TOIE0

OCIE0

TOIE1

OCIE1B

OCIE1A

TICIE1

TOIE2

OCIE2

ISR
.

Normal Mode c

Mode 0xFFFF
TOV1
ISR . Mode A B
TCNT1 OCR1A OCR1B . OCF1A
OCF1B OC1A OC1B
.

- -

P a g e | 89

www.pnkavosh.com

OC1A OC1B

www.avr.ir

COM1A0/COM1B0

COM1A1/COM1B1

) I/O(

Toggle

Clear

Set

OC1A OC1B
.

) :7 131 10 (

>#include <mega16.h
#define xtal 8000000
)void main(void
{
;PORTD=0x00
;DDRD=0x30
// Mode: Normal top=FFFFh
//toggle OC1A & OC1B

;TCCR1A=0x50

//Clock/8

;TCCR1B=0x02
;OCR1AH=0x00

//OCR1A=255
- -

;OCR1AL=0xFF

P a g e | 90

www.pnkavosh.com

www.avr.ir

;OCR1BH=0x28
//OCR1B=10255

;OCR1BL=0x0F
;)while (1
}

T = 2 216 1s = 131072s = 131ms

= OCR1B OCR1A = 10255 255 = 10000s = 10ms

CTC Mode d

TCNT1 OCR1A ICR1


TCNT1 . TOP
WGM OCR1A ICR1 .
TOP OCR1A ICR1
OCF1A ICF1
. Mode
PWM . OC1A
OC1B :

f CLK _ I / O

)2.N .(1 + OCR1A


- -

= f OC1x

P a g e | 91

www.avr.ir

www.pnkavosh.com

(OC1A 1 ) :8

#include <mega16.h>
#define xtal 8000000
void main(void)
{
PORTD=0x00;
DDRD=0x30;
// Mode: CTC top=01F3h
TCCR1A=0x40;
TCCR1B=0x0A;
OCR1AH=0x01;
OCR1AL=0xF3;

//OCR1A=499

while (1);
}

f=

8000000
= 1000 = 1KHz
2 8(1 + 499)

- -

P a g e | 92

www.pnkavosh.com

www.avr.ir

Fast PWM Mode d

PWM 8 1
PWM
Duty Cycle ) (TCNT1

PWM Mode (WGM1[3:0] = 15 14 7 6 5) :

.1

PWM 8 )(0xFF = TOP

.2

PWM 9 )(0x1FF = TOP

.3

PWM 10 )(0x03FF = TOP

.4

PWM ICR1 = TOP

.5

PWM OCR1A = TOP

Mode TOP
. PWM
TCNT1 OCR1x

OC1x TOP .

PWM . PWM
9 8 10 ICR1 OCR1A

- -

P a g e | 93

www.pnkavosh.com

www.avr.ir

. 2 ) 0x0003 ICR1 (OCR1x


16 ) 0xFFFF ICR1 .(OCR1x

PWM :

)log(TOP + 1
)log(2

= resolution

TOP TOV1 OCF1A


OCF1B . ISR
. TOP
.

OC1A OC1B :

OC1A OC1B
) I/O(

COM1A0

COM1A1

/COM1B0

/COM1B1

WGM1[3:0] = 15 Toggle : OC1A OC1B I/O


1

Clear Set PWM) TOP (

Set Clear PWM) TOP(

] :WGM1[3:0 ) I/O(

- -

P a g e | 94

www.avr.ir

www.pnkavosh.com

: PWM
f PWM =

f Clk _ I / O
N.(1 + TOP )

( 25 1 PWM ):9

#include <mega16.h>
#define xtal 8000000
void main(void)
{
PORTD=0x00;
DDRD=0x20;
// Mode: Fast PWM top=03FFh
// OC1A output: Non-Inv.
// OC1B output: Disconnected
TCCR1A=0x83;
TCCR1B=0x0A; //10 Bit PWM
OCR1AH=0x00;
OCR1AL=0xFF;
while (1);
}

f PWM =

8000000
= 976 1KHz
8.(1 + 1023 )

DutyCycle =

- -

256
100% = 25%
1024

P a g e | 95

www.avr.ir

www.pnkavosh.com

: 1 ISR TCNT1

interrupt [TIM1_OVF] void timer1_ovf_isr(void)


{
TCNT1=24;
}
TIMSK=0x04; //Enable TOV1
#asm("sei") //Enable Interrupts

Phase Correct Mode e

(WGM1[3:0] = 11 10 3 2 1) : Mode PWM

(0xFF = TOP) 8 PWM

.1

(0x1FF = TOP) 9 PWM

.2

(0x03FF = TOP) 10 PWM

.3

ICR1 = TOP PWM

.4

OCR1A = TOP PWM

.5

- -

P a g e | 96

www.pnkavosh.com

www.avr.ir

Mode TOP TOP . PWM


TCNT1
OCR1x OC1x .
PWM .
PWM 9 8 10 ICR1
OCR1A . 2 ) 0x0003
ICR1 (OCR1x 16 ) 0xFFFF
ICR1 .(OCR1x

PWM :

)log(TOP + 1
)log(2

= resolution

TOV1 OCF1A
OCF1B . ISR
. ) (OCRx ISR
OCR1A OCR1B TOP Load
OCR1x TOP
PWM :

- -

P a g e | 97

www.pnkavosh.com

OCR1A=110

www.avr.ir
TOP
OCR1A=180

PWM OCR1x
.

OC1A OC1B :

OC1A OC1B
) I/O(

COM1A0

COM1A1

/COM1B0

/COM1B1

WGM1[3:0] = 9, 14 Toggle : OC1A OC1B I/O


1

Clear Set .

Set Clear .

] :WGM1[3:0 ) I/O(

PWM Fast PWM :


f Clk _ I / O
) 2.N.(1 + TOP

- -

= f PWM

P a g e | 98

www.pnkavosh.com

www.avr.ir

:10 Mode Fast PWM Phase Correct PWM


PWM :
;TCCR1A=0x83
;TCCR1B=0x02
;OCR1AL=0xFF

Phase and Frequency Correct Mode g

OCR1x TOP
Mode .
Mode = 9 8) :
](WGM1[3:0

.1

PWM ICR1 = TOP

.2

PWM OCR1A = TOP

- -

P a g e | 99

www.pnkavosh.com

www.avr.ir

Capture

Capture ICP1
TCNT1 ICR1 Capture
) (ICF1 . (TICIE1) Capture
. ISR ICF1
.

Bit

]ICR1[15:8

ICR1H

]ICR1[7:0

ICR1L

ICR1 TOP ) Mode 10 8


12 (14 .

Capture
ACIC ACSR . ICP1
. ICP1 ICES1
TCCR1B
.

- -

P a g e | 100

www.avr.ir

TCCR1B

www.pnkavosh.com

ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10

Capture
. TCCR1B ICNC1 .
. ICP1

:10

/*****************************************************
Project : Servo Motor Controller
Author

: Reza Sepas Yar

Company : Pishro Noavaran Kavosh


Chip type

: ATmega16

Clock frequency

: 16.000000 MHz

*****************************************************/
#include <mega16.h>
#include <delay.h>
#define xtal 16000000
void main(void)
{
PORTD=0x00;
- -

P a g e | 101

www.avr.ir

www.pnkavosh.com

DDRD=0x20;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 2000.000 kHz
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x80;
TCCR1B=0x12;
ICR1H=0x4E;
ICR1L=0x20;

//ICR=20000

OCR1AH=0x03;
OCR1AL=0xE8; //1000
while (1)
{
for(OCR1A=1000;OCR1A<2000;OCR1A++)
delay_ms(1);
for(OCR1A=2000;OCR1A>1000;OCR1A--)
delay_ms(1);
};
}

- -

P a g e | 102

www.avr.ir

www.pnkavosh.com

:11

#include <mega16.h>
#define xtal 8000000
flash char sinewave[256]={
0x80,0x83,0x86,0x89,0x8C,0x8F,0x92,0x95,0x99,0x9C,0x9F,0xA2,0xA5,0xA8,0xAB,0xAE,
0xB1,0xB4,0xB6,0xB9,0xBC,0xBF,0xC1,0xC4,0xC7,0xC9,0xCC,0xCE,0xD1,0xD3,0xD6,0xD8,
0xDA,0xDC,0xDF,0xE1,0xE3,0xE5,0xE6,0xE8,0xEA,0xEC,0xED,0xEF,0xF1,0xF2,0xF3,0xF5,
0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFC,0xFD,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFE,0xFD,0xFC,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,
0xF5,0xF4,0xF3,0xF2,0xF0,0xEF,0xED,0xEB,0xEA,0xE8,0xE6,0xE4,0xE2,0xE0,0xDE,0xDC,
0xD9,0xD7,0xD5,0xD2,0xD0,0xCE,0xCB,0xC8,0xC6,0xC3,0xC1,0xBE,0xBB,0xB8,0xB5,0xB3,
0xB0,0xAD,0xAA,0xA7,0xA4,0xA1,0x9E,0x9B,0x97,0x94,0x91,0x8E,0x8B,0x88,0x85,0x82,
0x7E,0x7B,0x78,0x75,0x72,0x6F,0x6C,0x69,0x65,0x62,0x5F,0x5C,0x59,0x56,0x53,0x50,
0x4D,0x4B,0x48,0x45,0x42,0x3F,0x3D,0x3A,0x37,0x35,0x32,0x30,0x2D,0x2B,0x29,0x26,
0x24,0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x14,0x13,0x11,0x0F,0x0E,0x0D,0x0B,0x0A,
0x09,0x08,0x06,0x05,0x05,0x04,0x03,0x02,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- -

P a g e | 103

www.avr.ir

www.pnkavosh.com

0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0A,0x0B,0x0D,0x0E,0x0F,0x11,0x13,0x14,0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x24,
0x26,0x29,0x2B,0x2D,0x30,0x32,0x35,0x37,0x3A,0x3D,0x3F,0x42,0x45,0x48,0x4B,0x4D,
0x50,0x53,0x56,0x59,0x5C,0x5F,0x62,0x65,0x69,0x6C,0x6F,0x72,0x75,0x78,0x7B,0x7E
};

char i=0;
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
OCR1A=sinewave[i];
i++;
if(i==255)
i=0;
}
void main(void) {
DDRD=0xFF;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x81;
TCCR1B=0x09;
// Timer(s)/Counter(s) Interrupt(s) initialization
- -

P a g e | 104

www.avr.ir

www.pnkavosh.com

TIMSK=0x10;
//enable global interrups
#asm("sei");
while (1);
}

- -

P a g e | 105

www.pnkavosh.com

www.avr.ir

)(RS-232

RS-232c 60
.
Full Duplex .
UART
.

9
Handshaking . RS-232
Null-Modem .
Rx ) Tx ( . D9
:

- -

P a g e | 106

www.pnkavosh.com

www.avr.ir

Pin

Carrier Detect

Receive Data

Transmit Data

Data Terminal Ready

Signal Ground

Data Set Ready

Request To Send

Clear To Send

Ring Indicator

:
RS-232 +3 +12 Space -3 -12
Mark :

- -

P a g e | 107

www.pnkavosh.com

www.avr.ir

TTL 0 5
PC TTL .
RS-232 TTL MAX232 HIN232 MAX232 .
16 2 2 . IC
:

- -

P a g e | 108

www.pnkavosh.com

www.avr.ir

:
Frame 5 8
.
) 1 2 (. . Frame 10
Data
:

USART AVR

ATmega16 USART RS-232


. AVR RXD TXD XCK
) . XCK Mode (.
USART AVR UART
AVR 5 9

- -

P a g e | 109

www.pnkavosh.com

www.avr.ir

Data . ] UPM[1:0
UCSRC .
USART
.1

(UDR) USART Data Register

Bit

]RXB[7:0

)UDR (Read

]TXB[7:0

)UDR (Write

UDR I/O Registers .


UDR
. UDR UDRE
UCSRA USART .
USART TXEN UCSRB
TXD .

USART Control and Status Register A

MPC

U2

FE DOR PE
- -

UDR

TXC

RX

UCSR
A

P a g e | 110

www.pnkavosh.com

www.avr.ir

:Multi-processor Communication Mode


.
:Double the USART Transmission Speed Mode Baud
Rate . Mode .
:Parity Error ] UPM[1:0
.
:Data Overrun Overrun . Overrun

.
DOR .
:Frame Error Frame
.
:USART Data Register Empty
.
UDRIE UCSRB UDRE
ISR UDR UDRE
. .
- -

P a g e | 111

www.pnkavosh.com

www.avr.ir

:USART Transmit Complete


.
TXCIE ISR TXC
.
:USART Receive Complete Frame UDR
UDR .
.

USART Control and Status Register B

RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8

UCSRB

:Transmit Data Bit 8 9 Mode


. UDR
.
:Receive Data Bit 8 9 Mode
. UDR .

:Character Size ] UCSZ[1:0 UCSRC


) ( Frame .
- -

P a g e | 112

www.pnkavosh.com

www.avr.ir

:Transmitter Enable TxD


I/O . UART
.
:Receiver Enable RxD
I/O .
FE DOR PE .
:USART Data Register Empty Interrupt Enable
) (I ) (UDRE
.
:TX Complete Interrupt Enable )(I
) (TXC .
:RX Complete Interrupt Enable )(I
) (RXC .
USART Control and Status Register C

URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL

- -

UCSRC

P a g e | 113

www.pnkavosh.com

www.avr.ir

UCSRC UBRRH I/O


URSEL .

:Clock Polarity Mode Mode


. :

RxD UCPOL TxD


XCK

XCK

XCK

XCK

] :Character Size[1:0 UCSZ2


8 .

UCSZ0

UCSZ1

UCSZ2

- -

P a g e | 114

www.avr.ir

www.pnkavosh.com

:Stop Bit Select .


1 2 .

] :Parity Mode[1:0 .
Frame )
(.
PE UCSRA .

UPM1 UPM0

UMSEL :USART Mode Select UART .


.

- -

P a g e | 115

www.pnkavosh.com

www.avr.ir

:Register Select UCSRC UBRRH )(0x40


.
UCSRC MSB UBRRH
MSB . 1 :
;UBRRH = 0x02
;UCSRC = 0x82
0x40
) UCSRC (UBRRH UBRRH :
;a = UBRRH
UCSRC
UCSRC :
;b = UBRRH
;b = UCSRC
9


)" #asm("cli .

UBRRH URSEL UCSRC


.

USART Baud Rate Registers

- -

P a g e | 116

www.pnkavosh.com
0

]UBRR[11:8

www.avr.ir

Bit

URSEL

UBRRH
UBRRL

]UBRR[7:0

:URSEL UBRRH UCSRC .

] :UBRRH[7:4
.

] :UBRR[11:0 Baud Rate USART .


:

UBRR Baud Rate

Baud Rate UBRR

f osc
1
16 Baud

= UBRR

f osc
)16 ( UBRR + 1

= Baud

f osc
1
8 Baud

= UBRR

f osc
)8( UBRR + 1

= Baud

f osc
1
2 Baud

= UBRR

f osc
)2( UBRR + 1

= Baud

Mode
)(U2X=0

)(U2X=1

:
BaudRate ClosestMatch
1) 100%
BaudRate

- -

( = ]Error[%

P a g e | 117

www.pnkavosh.com

www.avr.ir

:1 8 Baud Rate = 2400 Mode UBRR .

8000000
1 = 207.33 UBRR = 207
16 2400

= UBRR

8000000
= 2404
)16(207 + 1

= BaudRate

2404
1) 100% = 0.2%
2400

( = ]Error[%

7.3728
2 .

Baud Rate 3.6864 1.8432


14.7456 11.0592 7.3728 .

UBRR 166 -169


ATmega16 .

USART CodeVision

stdio.h USART .

)( :getchar Polling RXC


UDR .
- -

P a g e | 118

www.avr.ir

www.pnkavosh.com

UDRE Polling :putchar()


. UDR

7 3 ) :2
(. PORTA Polling
:
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
#define xtal 8000000

void main(void)
{

UCSRA=0x00;
UCSRB=0x08; // USART Transmitter: On
UCSRC=0x86; //8 Data, 1 Stop, No Parity
UBRRH=0x00;
UBRRL=0x33; // USART Baud rate: 9600

delay_ms(3000);
putchar(7);
while (1);
}
:
#include <mega16.h>
#include <stdio.h>
- -

P a g e | 119

www.avr.ir

www.pnkavosh.com

#define xtal 8000000

void main(void)
{

char a;
DDRA=0xFF;
PORTA=0xFF;

UCSRA=0x00;
UCSRB=0x10; // USART Receiver: On
UCSRC=0x86; //8 Data, 1 Stop, No Parity
UBRRH=0x00;
UBRRL=0x33; // USART Baud rate: 9600

a = getchar(); // Polling RXC


PORTA = a;
while (1);

}
. USART
.
:
while(!(UCSRA&0x80));
PORTA=UDR;
while (1);

- -

P a g e | 120

www.pnkavosh.com

www.avr.ir

)( puts )( :putsf USART .


)( puts SRAM )( putsf Flash
. USART )( putchar )( getchar
Y UDR .

(0x10) LF .

) :3 " "Kavosh " "AVR (.

>#include <mega16.h
>#include <delay.h
>#include <stdio.h
#define xtal 8000000
;"flash char string1[7]="Kavosh
;"char string2[7]="AVR
)void main(void
{
;UCSRA=0x00
UCSRB=0x08; // USART Transmitter: On
UCSRC=0x86; //8 Data, 1 Stop, No Parity
- -

P a g e | 121

www.pnkavosh.com

www.avr.ir

;UBRRH=0x00
UBRRL=0x33; // USART Baud rate: 9600
;)putsf(string1
;)puts(string2
;)while (1
}
)( :gets
.

) :4 1 Kavosh
Polling LCD
(.
:1
>#include <mega16.h
>#include <delay.h
>#include <stdio.h
#define xtal 8000000

)void main(void
{

;UCSRA=0x00
UCSRB=0x08; // USART Transmitter: On
UCSRC=0x86; //8 Data, 1 Stop, No Parity
- -

P a g e | 122

www.avr.ir

www.pnkavosh.com

UBRRH=0x00;
UBRRL=0x33; // USART Baud rate: 9600

delay_ms(1000);
putsf(" Kavosh ");
while (1);
}

:2
#include <mega16.h>
#include <stdio.h>
#include <lcd.h>
#define xtal 8000000

#asm
.equ __lcd_port=0x1B ;PORTA
#endasm

char a[10];

void main(void)
{

UCSRA=0x00;
UCSRB=0x10; // USART Receiver: On
UCSRC=0x86; //8 Data, 1 Stop, No Parity
UBRRH=0x00;
- -

P a g e | 123

www.pnkavosh.com

www.avr.ir

UBRRL=0x33; // USART Baud rate: 9600

;)lcd_init(16
;)(lcd_clear
;)"lcd_putsf("Waiting...
;)gets(a,10
;)(lcd_clear
;)lcd_puts(a
;)while(1

}
)( :printf .
:


%c

%d %i

10

%e %E

%f
%s


Null
SRAM

%p

Null
Flash
- -

P a g e | 124

www.avr.ir

www.pnkavosh.com

10

%u

16

%X %x

%%

(. ) :5

#include <mega16.h>
#include <delay.h>
#include <stdio.h>
#define xtal 8000000

int a = 100;
char b = 'A';
float pi = 3.14;

void main(void)
{

UCSRA=0x00;
UCSRB=0x08; // USART Transmitter: On
UCSRC=0x86; //8 Data, 1 Stop, No Parity
UBRRH=0x00;
UBRRL=0x33; // USART Baud rate: 9600

printf("a=%d b=%c pi=%f",a,b,pi);


- -

P a g e | 125

www.pnkavosh.com

www.avr.ir

;)while (1
}
)( printf
%width.precision width .

" ". precision .
. a = 3.145 a=3.14 .
;)printf("a=%4.2",a
)( printf CodeVision :
Project/Configure/C Compiler/(s)printf Features
.

)( :scanf .
:
;) ", "(scanf

- -

P a g e | 126

www.pnkavosh.com

www.avr.ir


. 256
. :
)scanf("%10d",a
10 a .

)( scanf )( printf :

Project/Configure/C Compiler/(s)scanf Features


.

: )( printf )( scanf Flash



) int (int, width .

- -

P a g e | 127

www.pnkavosh.com

www.avr.ir

UMSEL UCSRC
:

TxD

TxD

RxD

RxD

XCK

XCK

UCPOL
. Mode

.

Multi-processor
Mode . Master
Slave .

- -

P a g e | 128

www.pnkavosh.com

www.avr.ir

TxD

RxD

Master MCU

Slave 1
Slave 2
Slave 3
Slave 4

MPCM UCSRA Mode .


Master Slave Slave Master
)(TXB8 . Slave
MPCM Mode Master .
Master 9
. Slave
Slave . MPCM
Slave MPCM .
Slave Master
Slave . Master
Slave . MPCM
.

- -

P a g e | 129

www.pnkavosh.com

www.avr.ir

* :12

PC Null-Modem RS-
232 TTL MAX232 .

COM MSComm
. mscomm32.ocx
. OCX :
.1

Standard EXE:

- -

P a g e | 130

www.pnkavosh.com

.2

www.avr.ir

Project Component

Microsoft Comm Control 6.0 OK .

.3

MScomm Toolbox :

- -

P a g e | 131

www.avr.ir

www.pnkavosh.com

. Load

.6

Private Sub Form_Load()


MSComm1.Settings = "9600,N,8,1"
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.Output = "Test" + Chr(16)
End Sub
Baud Rate=1200 Test . F5

. COM1 8

- -

P a g e | 132

www.avr.ir

www.pnkavosh.com

RS232 USB :13

- -

P a g e | 133

www.pnkavosh.com

www.avr.ir

Package FT232BM LQFP Pin out :

Win XP :

(1

http://www.ftdichip.com/Drivers/VCP.htm

(2

Unzip) . (E:\CDM 2.00.00 :

- -

P a g e | 134

www.pnkavosh.com

(3

www.avr.ir

USB . Found New

Hardware Found New Hardware Wizard


. Next .

(4

Next .

- -

P a g e | 135

www.pnkavosh.com

www.avr.ir


Continue
Anyway .

- -

P a g e | 136

www.pnkavosh.com

(5

www.avr.ir

- -

P a g e | 137

www.pnkavosh.com

(6

www.avr.ir

Finish

(7

) Device Manager Control Panel\System (Hardware

Port ] [+ . COM COMx


. 12 COMx
.

- -

P a g e | 138

www.avr.ir

www.pnkavosh.com

- -

P a g e | 139

www.pnkavosh.com

www.avr.ir

(TWI) I2C Bus

I2C 1980 Philips .


.
I2C Inter-IC
.
. 1000
IC I2C . .
Track
.
.

I2C

I2C .1 .
.2 7 ) 127 ( 10
) 1024 (.

- -

P a g e | 140

www.pnkavosh.com

www.avr.ir

.3 SCL SDA . SCL Master SDA


Data Master .
.4 8 400 ) . 3.4
(High Speed
.5 400 4.
.6 Spike SDA
50 .
.7 SDA SCL Slew Rate I2C
) 400( .
EMI .
.8
I2C .

:Master
.
:Slave Master .
: .
: .

- -

P a g e | 141

www.pnkavosh.com

www.avr.ir

:Arbitration Master
.

I2C

Drive Low

- -

)Float High (Logic 1

1.

)Drive Low (Logic 0

2.

Float High

P a g e | 142

www.pnkavosh.com

www.avr.ir

idle Float High


Pull-up
:

Pull-up
Wired-AND Low.

Open-collector Pull-up Wired-


AND
RC Pull-up

- -

P a g e | 143

www.pnkavosh.com

www.avr.ir

. High
.

I2C

I2C
. :

ACK/NACK

Data

Restart

Stop

Start

:Start Condition
Busy . :

- -

P a g e | 144

www.pnkavosh.com

www.avr.ir

:Stop Condition Device


:

:Restart Condition
Slave .
:

- -

P a g e | 145

www.pnkavosh.com

www.avr.ir

:Data 8 .
SDA SCL SDA
. SDA SDA
SCL SDA SCL
.

:ACK/NACK 8
SDA Acknowledge . SDA
Passive Pull-up .

- -

P a g e | 146

www.pnkavosh.com

www.avr.ir

7
7
. Master
.

I2C 7 128
. 16 112
.
SDA Master 8 . Slave
Broadcast . Master Slave
.
112 400
.

- -

P a g e | 147

www.pnkavosh.com

www.avr.ir

Master Slave
:

Slave
Master Slave
.
Slave
SDA ACK Master .
Master Slave Master
Stop Bus .

- -

P a g e | 148

www.pnkavosh.com

www.avr.ir

TWI AVR


SCL Master .
TWBR TWSR .
Slave CPU Slave
16 SCL :

CPU Clock Frequency


16 + 2(TWBR ) 4TWPS

= f SCL

: TWI Master TWBR 10


Master SCL .

TWI

TWI TWDR TWSR TWCR TWBR TWAR .

- -

P a g e | 149

www.pnkavosh.com

www.avr.ir

:TWI Bit Rate Register

Bit
TWBR

]TWBR[7:0

SCL :

CPU Clock Frequency - 16 f SCL


2 f SCK 4TWPS

= TWBR

) :1 TWBR 8 SCK 100 (TWPS = 0

8000000 16 100000
= 32
2 100000 40

= TWBR

:TWI Control Register

0
TWI
E

1
-

TWE

TWW

TWST

TWST

TWE

TWIN

TWI :
- -

TWC
R

P a g e | 150

www.pnkavosh.com

www.avr.ir

:TWI Interrupt Enable Set


TWINT TWI .

:TWI Enable Bit TWI TWI SCL


SDA . Slew Rate
TWI I/O .

:TWI Write Collision Flag TWDR


TWINT . TWDR TWINT
.

:TWI STOP Condition Bit Master


I2C TWSTO .
Slave SDA SCL
.

:TWI START Condition Bit Master


.

- -

P a g e | 151

www.pnkavosh.com

www.avr.ir

TWI
. TWSTA .

TWEA :TWI Enable Acknowledge Bit Acknowledge .


ACK :

.1

Slave .

.2

TWGCE TWAR .

.3

Master Slave .

:TWI Interrupt Flag TWI


TWIE TWI . ISR
.
TWINT
) (TWDR TWCR TWINT
TWI . TWINT SCL
TWI .

:TWI Status Register

- -

P a g e | 152

www.pnkavosh.com

TWPS

TWPS

2
-

www.avr.ir

TWS

TWS

TWS

TWS

TWS

TWS

] :TWI Prescaler Bits[1:0


:

TWPS

TWPS

16

64

] :TWI Status[7:3 TWI Mode


.

:TWI Data Register

]TWDR[7:0

- -

Bit
TWDR

P a g e | 153

www.pnkavosh.com

www.avr.ir

TWDR
. TWI TWINT
.

:TWI (Slave) Address Register Slave ST SR .

TWGC

TWA

TWA

TWA

TWA

TWA

TWA

TWA

TWA
R

:TWI General Call Recognition Enable 0x00


TWGCE .

] :TWI (Slave) Address[7:1 7 Slave .

:
START condition

S:

REPEATED START condition

Rs:

)Read bit (high level at SDA

R:

)Write bit (low level at SDA

W:

)Acknowledge bit (low level at SDA

ACK:

)NACK: Not acknowledge bit (high level at SDA


- -

P a g e | 154

www.pnkavosh.com

www.avr.ir

8-bit data byte

Data:

STOP condition

P:

Slave Address

SLA:

Mode :TWI

)Master Transmitter (MT

1.

)Master Receiver (MR

2.

)Slave Transmitter (ST

3.

)Slave Receiver (SR

4.

(MT) Master Transmitter c


Mode Master
Master . SLA+W
Master SLA+R .
TWCR :

0
TWI
E
X

1
0

TWE

TWW

TWST

TWST

TWE

TWIN

- -

TWC
R

P a g e | 155

www.pnkavosh.com

www.avr.ir

TWIE TWI .
TWEN TWI .
TWWC
TWDR Collision .
TWSTO .
TWSA TWI
.
TWEA Set ACK
.
TWINT TWI TWCR.

TWI

TWINT TWSR 0x08
. MT TWDR SLA+W
TWINT . SLA+W TWDR
TWCR :

TWI

TWE

TWW

TWST

TWST

TWE

TWIN

- -

TWC
R

P a g e | 156

www.pnkavosh.com

E
X

www.avr.ir

TWSTA .
SLA+W ACK TWINT
0x18 0x20 0x38 TWSR . 0x18
ACK . .
TWDR TWINT 8 TWI
. TWDR TWINT
TWWC . TWCR
:

0
TWI
E
X

1
0

TWE

TWW

TWST

TWST

TWE

TWIN

TWC
R


. TWCR :

4
- -

TWC

P a g e | 157

www.pnkavosh.com

www.avr.ir

R
TWI
E
X

TWE

TWW

TWST

TWST

TWE

TWIN

0
TWI
E
X

1
0

TWE

TWW

TWST

TWST

TWE

TWIN

TWC
R

TWINT0 .

TWSR 0x10 Master


Slave .

TWSR Master Transmitter :

TWI

SLA+W ACK NACK

TWSR

0x08

0x10

SLA+W ACK NACK


SLA+R ACK NACK

- -

P a g e | 158

www.pnkavosh.com

www.avr.ir

ACK NACK
SLA+W ACK .

0x18

SLA+W ACK

0x20
ACK NACK
ACK .

0x28


ACK NACK
ACK .

0x30

0x38

) :2 0x77 Slave 0xA0 100(

>#include <mega16.h
#define xtal 8000000
)(void main
{
//--- Start Condition and Transmitting SLA+W ---TWBR = 32; // Bit rate = 100Khz
TWCR = 0xA4; // Transmit Start Condition
- -

P a g e | 159

www.avr.ir

www.pnkavosh.com

while(TWCR&0x80==0); // Waiting for TWINT flag


if(TWSR&0xF8==0x08) // Start Condition Transmitted?
{
TWDR=0xA0; // SLA+W
TWCR=0x84; // Enable TWI and Clear TWINT
}
else
goto error;
//--------------- Transmitting Data ------------while(TWCR&0x80==0); // Waiting for TWINT flag
if(TWSR&0xF8==0x18) // SLA+W has been send with ACK?
{
TWDR=0x77; // Data=0x77
TWCR=0x84; // Enable TWI and Clear TWINT
}
else
goto error;
//--------- Transmitting Stop Condition ---------while(TWCR&0x80==0); // Waiting for TWINT flag
if(TWSR&0xF8==0x28) // Data has been send with ACK?
TWCR=0x94; // Transmit Stop Condition

- -

P a g e | 160

www.pnkavosh.com

www.avr.ir

//-----------------------------------------------error:
;)while(1
}

(MR) Master Receiver d


Master Slave
SLA+R Slave .
TWINT TWSR 0x08 SLA+R
TWDR TWINT .
SLA+R ACK TWINT
0x40 0x38 0x48 TWSR :

TWI

SLA+R ACK NACK

TWSR

0x08

0x10

0x38

ACK NACK

SLA+R ACK .

0x40

SLA+R ACK

SLA+R ACK NACK


SLA+W ACK NACK

0x48

- -

P a g e | 161

www.pnkavosh.com
ACK NACK

www.avr.ir

ACK

0x50

NACK

0x58

SLA+R ACK TWINT


TWDR . Master
NACK Slave
.

) :3 2 Slave 0xA0 100(

>#include <mega16.h
#define xtal 8000000
;char incoming_data
)(void main
{
//----------- Sending Start Condition ----------TWBR = 32; // Bit rate = 100Khz
TWCR = 0xA4; // Transmit Start Condition
while(TWCR&0x80==0); // Waiting for TWINT flag
//-------------- Sending SLA + R ---------------- -

P a g e | 162

www.avr.ir

www.pnkavosh.com

if(TWSR&0xF8==0x08) // Start Condition Transmitted?


{
TWDR=0xA1; // SLA+R
TWCR=0xC4; // Enable TWI and Clear TWINT
}
else
goto error;
//-------- Enable Master Acknowledging ---------while(TWCR&0x80==0); // Waiting for TWINT flag
if(TWSR&0xF8==0x40) // SLA+R has been send with ACK?
TWCR=0xC4; // Master Acknowledging and clear TWINT
//--------------- Reading 1st byte -------------while(TWCR&0x80==0); // Waiting for TWINT flag
if(TWSR&0xF8==0x50) // Master has been Received Data?
{
incoming_data = TWDR; // Reading Data
TWCR=0xC4; // Master Acknowledging and clear TWINT
}
else
goto error;
//---- Reading 2nd byte and Stop Condition ------

- -

P a g e | 163

www.avr.ir

www.pnkavosh.com

while(TWCR&0x80==0); // Waiting for TWINT flag


if(TWSR&0xF8==0x50) // Master has been Received Data?
{
Incoming_data=TWDR;
TWCR=0x94; // Master Not Acknowledging and clear
TWINT
}
else
goto error;
TWCR=0x94; // Transmit Stop Condition
//-----------------------------------------------error:
while(1);
}
(SR) Slave Receiver e
. Master Transmitter Slave
LSB . TWAR 7 Slave
. Master

TWAR

TWAR[7:1]
- -

0
X

P a g e | 164

www.pnkavosh.com

www.avr.ir

TWCR :

0
TWI
E
X

1
0

TWE

TWW

TWST

TWST

TWE

TWIN

TWC
R

TWIE TWI .
TWEA ACK .
TWSTA TWSTO Master .

Slave Master .
) (W/R Master
Slave Slave ) Slave Read (SR
) Slave Transmiter (ST . SLA+W TWINT
TWSR :

TWI
ACK NACK

ACK NACK

SLA+W ACK .

TWSR
0x60

SLA + W Master
0x68
.

- -

P a g e | 165

www.pnkavosh.com
ACK NACK

ACK NACK

www.avr.ir

ACK .

0x70

Master
0x78
.

ACK NACK

Slave Master
ACK NACK

Slave Master

ACK

0x80

NACK

0x88

ACK

0x90

NACK

0x98

0xA0


Slave Master

0x60 Slave Master


) (ACK . Slave
TWSR
0xA0 .

) :4 Slave 0x01 100(

>#include <mega16.h
#define xtal 8000000
;char incoming_data
)(void main
{
- -

P a g e | 166

www.avr.ir

www.pnkavosh.com

//-------------- Initial Setting --------------TWAR = 0x01; // Slave Address


TWBR = 32; // Bit rate = 100Khz
TWCR = 0xC4; // Clear Int, Set TWEA and TWEN
while(TWCR&0x80==0); // Waiting for TWINT flag
//--------- Transmit a Byte to Master ---------if(TWSR&0xF8==0x60) // SLA+W Received?
{
TWCR = 0xC4; // Clear Int, Set TWEA and TWEN
}
else
goto error;
//------------ Slave Receive a Byte -----------while(TWCR&0x80==0); // Waiting for TWINT flag
if(TWSR&0xF8==0x80) // Slave has been Receive Byte?
{
incoming_data=TWDR; //Receive incoming data
TWCR=0x84; // Clear TWINT
}
else
goto error;

- -

P a g e | 167

www.pnkavosh.com

www.avr.ir

//---------- Receiving Stop Condition ---------while(TWCR&0x80==0); // Waiting for TWINT flag


?if ((TWSR & 0xF8)== 0x0A0) //Stop Condition Received
TWCR=0x84; // Clear TWINT
error:
;)while(1
}

(ST) Slave Transmitter f


SR W/R .
Master Slave Slave .
TWINT TWSR
Master Slave ACK Master
NACK ).(TWSR=0xC0

TWI
ACK NACK

SLA+R ACK .

- -

TWSR
0xA8

P a g e | 168

www.pnkavosh.com

www.avr.ir

SLA + W Master

ACK NACK

0xB0
.

ACK NACK

Slave Master

Slave Master

ACK

0xB8

NACK

0xC0

(TWEA=0) ACK

0xC8

) :5 Slave 0x01 100(

>#include <mega16.h
#define xtal 8000000
)(void main
{
//-------------- Initial Setting --------------TWAR = 0x01 // Slave Address
TWBR = 32; // Bit rate = 100Khz
TWCR = 0xC4; // Clear Int, Set TWEA and TWEN
while(TWCR&0x80==0); // Waiting for TWINT flag
//--------- Transmit a Byte to Master ---------?if(TWSR&0xF8==0xA8) // SLA+R Received
{
TWDR=0x77; // Transmitt 0x77
- -

P a g e | 169

www.avr.ir

www.pnkavosh.com

TWCR = 0xC4; // Clear Int, Set TWEA and TWEN


}
else
goto error;
//------------- Wait for ACK or NACK ----------while(TWCR&0x80==0); // Waiting for TWINT flag
if(TWSR&0xF8!=0xC0) // Slave has been send NACK?
goto error;
//-----------------Error Sub ------------------error:
while(1);
}
:
SCL Master . Master
AND I2C wired-AND
.

- -

P a g e | 170

www.pnkavosh.com

www.avr.ir

Arbitration Master
Master SDA . I2C
Master
.
Wired-AND Master SDA
AND
SDA Master Master
) Pull-up (
Master .

- -

P a g e | 171

www.pnkavosh.com

www.avr.ir

Arbitration Master
:
.

Slave Slave

SDA .
.

Master SDA.

I2C CodeVision
CodeVision I2C
. SDA SCL :
#asm
- -

P a g e | 172

www.pnkavosh.com

www.avr.ir

.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4
#endasm
3 4 PORTB SDA SCL .

I2C CodeVision

)(:i2c_init
I2C .

)(:i2c_start

.

)(:i2c_stop
I2C .

)(:i2c_read

- -

P a g e | 173

www.pnkavosh.com

www.avr.ir

I2C :

)unsigned char i2c_read(unsigned char ack


ack acknowledgement .
ACK ACK
NACK .

)(:i2c_write
I2C :

)unsigned char i2c_write(unsigned char data


data ACK Slave
.

:12 EEPROM I2C

- -

P a g e | 174

www.avr.ir

www.pnkavosh.com

#include<mega16.h>
#define xtal 1000000
/* the I2C bus is connected to PORTB */
/* the SDA signal is bit 3 */
/* the SCL signal is bit 4 */
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
- -

P a g e | 175

www.avr.ir

www.pnkavosh.com

.equ __scl_bit=4
#endasm
/* now you can include the I2C Functions */
#include <i2c.h>
/* function declaration for delay_ms */
#include <delay.h>
#define EEPROM_BUS_ADDRESS 0xa0
/* read a byte from the EEPROM */
unsigned char eeprom_read(unsigned char address) {
unsigned char data;
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address);
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS | 1);
data=i2c_read(0);
i2c_stop();
return data;
}
/* write a byte to the EEPROM */
void eeprom_write(unsigned char address, unsigned char
data) {

- -

P a g e | 176

www.avr.ir

www.pnkavosh.com

i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
/* 10ms delay to complete the write operation */
delay_ms(10);
}
void main(void) {
unsigned char i;
DDRD=0xFF;
/* initialize the I2C bus */
i2c_init();
/* write the byte 55h at address 10h */
eeprom_write(0x10,0x55);
/* read the byte from address AAh */
i=eeprom_read(0x10);
PORTD=i;
while (1); /* loop forever */
}


- -

P a g e | 177

www.pnkavosh.com

www.avr.ir

: Flash
Successive Approximation Ramp-Compare Delta-Encoded
Sigma-Delta Pipeline ADC AVR
.

:Sample and Hold


Stable Sample and Hold .

- -

P a g e | 178

www.pnkavosh.com

www.avr.ir

:Successive Approximation Register


DAC .

:
SAR 10000000 DAC
.
SAR MSB 01000000 DAC
.
11000000 DAC .
End of Conversion .

ADC :ATmega16
10-bit Resolution
- -

P a g e | 179

www.avr.ir

www.pnkavosh.com

2 LSB Absolute Accuracy


65 - 260 s Conversion Time
Up to 15 kSPS at Maximum Resolution
8 Multiplexed Single Ended Input Channels
7 Differential Input Channels
2 Differential Input Channels with Optional Gain of 10x and 200x
0 - VCC ADC Input Voltage Range
Selectable 2.56V ADC Reference Voltage
Free Running or Single Conversion Mode
ADC Start Conversion by Auto Triggering on Interrupt Sources
Interrupt on ADC Conversion Complete
Sleep Mode Noise Canceler

ADC PORA ADC


AREF AVCC .
ADC . 2.56
. VCC 0.3 . AVCC
VCC LC AVCC VCC
.

- -

P a g e | 180

www.pnkavosh.com

www.avr.ir

ADC

ADC Multiplexer Selection Register


0

REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0

ADMUX

] :Analog Channel and Gain Selection Bits[4:0 8


ADC .
Single-ended 10 ADC 1x 10x 8
200x 7 .
ADC
. 4 ) : Package
TQFP MLF (.
- -

P a g e | 181

www.avr.ir

www.pnkavosh.com

- -

P a g e | 182

www.pnkavosh.com

www.avr.ir

:ADC Left Adjust Result ADLR ADC


. Left Adjust
Right Adjust . .

] :Reference Selection Bits[1:0 ADC .


.
AREF . AREF
2.56 100 AREF
.

REFS1 REFS0

AREF

AVCC

2.56

- -

P a g e | 183

www.pnkavosh.com

www.avr.ir

ADC Control and Status Register A

ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0

ADCSRA

] :ADC Prescaler Select Bits[2:0


ADC .

ADPS0

ADPS1

ADPS2

16

32

64

128

:ADC Interrupt Enable ) (I


.

- -

P a g e | 184

www.pnkavosh.com

www.avr.ir

:ADC Interrupt Flag ISR


.

:ADC Auto Trigger Enable Single Auto


Trigger ADC ADC
. ADC Auto Trigger
. ] ADTS[2:0 SFIOR
.

:ADC Start Conversion Single


) (Free Running .

:ADC Enable ADC ADC .


ADC .

The ADC Data Register

ADC
2 .

- -

P a g e | 185

www.pnkavosh.com

www.avr.ir

ADLR ADMUX LA RA :

ADLR=0
0

Bit

ADCL ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0


ADC9 ADC8

ADCH

ADLR=1
1

Bit

ADCL ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2


-

ADCH ADC1 ADC0

Special FunctionIO Register

ADTS2 ADTS1 ADTS0 - ACME PUD PSR2 PSR10

SFIOR

:ADC Auto Trigger Source ADATE ADCSRA


ADTS ADC .
ADC
ADC Free Running
.
- -

P a g e | 186

www.pnkavosh.com

www.avr.ir

ADTS2 ADTS1 ADTS0

Free Running

Capture

ADC

ADMUX ADEN
ADSC .
.
ADC .
ADC ADATE
ADCSRA . ADTS SFIOR

- -

P a g e | 187

www.pnkavosh.com

www.avr.ir

. Reset ADC
ADC .
ADIF ADC
.
ADTS Free Running
ADSC .
ADIE ) (ADIF
ISR . ADC
Single Conversion ADIF
Free Running ADIF
.

- -

P a g e | 188

www.pnkavosh.com

www.avr.ir


Resolution 50 200 .
ADC
ADPS ADCSRA . ADC 50
200
.

- -

P a g e | 189

www.pnkavosh.com

www.avr.ir

Mode ADC 25
Free Running 13 .


ADC
Mode ADC Noise
Reduction Idle CPU .

:13 LM35

*****************************************************/
Project : Temprature Measurement with LM35
: Reza Sepas Yar

Author

Company : Pishro Noavaran Kavosh


: ATmega16

Chip type

: 1.000000 MHz

Clock frequency

*****************************************************/
>#include <mega16.h
>#include <delay.h
>#include <stdio.h
#define xtal 8000000
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x12 ;PORTD
- -

P a g e | 190

www.avr.ir

www.pnkavosh.com

#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0xC0
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
void main(void)
{
char lcd_buff[10];
int adc_in;
float temp;
PORTA=0x00;
DDRA=0x00;
// ADC initialization
// ADC Clock frequency: 45 kHz
// ADC Voltage Reference: Int., cap. on AREF
- -

P a g e | 191

www.avr.ir

www.pnkavosh.com

// ADC Auto Trigger Source: None


ADMUX=ADC_VREF_TYPE;
ADCSRA=0x86;
// LCD module initialization
lcd_init(16);
while (1)
{
adc_in=read_adc(0);
temp=adc_in/4;
sprintf(lcd_buff,"Temp=%5.1f C",temp);
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts(lcd_buff);
delay_ms(1000);
};
}

- -

P a g e | 192

www.avr.ir

www.pnkavosh.com

- -

P a g e | 193

www.pnkavosh.com

www.avr.ir

AIN0 AIN1

AIN0 AIN1 ACO .
Capture .

- -

P a g e | 194

www.pnkavosh.com

www.avr.ir

Special Function IO Register

ADTS2 ADTS1 ADTS0 - ACME PUD PSR2 PSR10

SFIOR

:Analog Comparator Multiplexer Enable


ADC0 ADC7 . ACME
) ADC ADEN ADCSRA ( ADC
AIN1 .

Analog Comparator Control and Status Register

ACSR

ACD ACBG ACO ACI ACIE ACIC ACIS1 ACIS0

] :Analog Comparator Interrupt Mode Select[1:0


.

- -

P a g e | 195

www.pnkavosh.com

www.avr.ir

ACIS0

ACIS1

Toggle

:Analog Comparator Input Capture Enable Capture


.

:Analog Comparator Interrupt Enable


.

:Analog Comparator Interrupt Flag


ACIS1 ACIS0 . ACIE
ISR ACI
.

:Analog Comparator Output


.

- -

P a g e | 196

www.pnkavosh.com

www.avr.ir

:Analog Comparator Bandgap Select Bandgap


. ACGB AIN0
.

:Analog Comparator Disable .


Mode . ACD
ACIE ACSR
.

ADC0 ADC7
. ) (ADEN
ACME SFIOR . ] MUX[2:0 ADMUX
. ACME ADEN
AIN1 .

]ACME ADEN MUX[2:0


AIN1

xxx

AIN1

xxx

- -

P a g e | 197

www.avr.ir

www.pnkavosh.com

000

ADC0

001

ADC1

010

ADC2

011

ADC3

100

ADC4

101

ADC5

110

ADC6

111

ADC7

( ) :

#include <mega16.h>
// Analog Comparator interrupt service routine
interrupt [ANA_COMP] void ana_comp_isr(void)
{
PORTA=PORTA^0x01;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here

- -

P a g e | 198

www.avr.ir

www.pnkavosh.com

PORTA=0x00;
DDRA=0x01;
// Analog Comparator initialization
// Analog Comparator: On
// Interrupt on Output Toggle
// Analog Comparator Input Capture by Timer/Counter 1:
Off
ACSR=0x08;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1);
}

- -

P a g e | 199

www.avr.ir

www.pnkavosh.com

- -

P a g e | 200

www.avr.ir

www.pnkavosh.com

SPI Bus

Serial Peripheral Interface SPI


.
...
SPI .
.
: SPI

SSM2163

8x2 Audio Mixer

63dB attenuation in 1dB steps

Analog Devices

AD7303

DAC

8Bit clock rate up to 30MHz

Analog Devices

AD7811

ADC

10Bit 4/8 channel 300ksps

Analog Devices

AD7816

ADC + Temperature Sensor 10Bit

AD7858

ADC

AD8400

Digital Pot

Analog Devices

12Bit, 8 channel 200ksps


1/2/4 channel 256 positions
1, 10, 50 100kOhm 10MHz update rate

Analog Devices
Analog Devices

Low voltage operation 1.8V/2.7V/5.0V


AT25010

EEPROM

block write protection 100 years data

ATMEL

retention
AT45D011

FLASH

5V 1MBit 5MHz clock rate

ATMEL

AT45D021

FLASH

5V 2MBit 10MHz

ATMEL

AT45DB021

FLASH

2.7V 2MBit 5MHz clock rate

ATMEL

- -

P a g e | 201

www.avr.ir

www.pnkavosh.com

AT45DB041

FLASH

5V 4MBit 10MHz

ATMEL

AT45D081

FLASH

5V 8MBit 10MHz

ATMEL

AT45DB161

FLASH

2.7V 16MBit 13MHz

ATMEL

ADS1210

ADC

24Bit

BURR-BROWN

ADS7835

ADC

12Bit low power 500ksps

BURR-BROWN

ADS7846

Touch-screen controller

2.2V to 5.25V

BURR-BROWN

DS1267

Digital potentiometer

DS1305

Dual 10k, 50k and 100k

DALLAS

RTC

96-byte User-RAM

DALLAS

DS1306

RTC

96-byte User-RAM

DALLAS

DS1722

Digital Thermometer

-55 C to 120 C accuracy +/- 2C

DALLAS

DS1844

Digital Pot

4 channel, linear 64 positions

DALLAS

NM25C020

EEPROM

KP100

Pressure Sensor

Range 60 ... 130kPa

82527

CAN Controller

Flexible CPU-interface CAN 2.0

MAX349

MUX

8-to-1 dual 4-to-1

Maxim

MAX504

DAC

10Bit low power internal reference

Maxim

MAX522

DAC

8Bit 5MHz

Maxim

MAX535

DAC

13Bit Schmitt-trigger inputs

Maxim

MAX3100

UART

Up to 230kBaud Schmitt-trigger inputs

Maxim

MAX4548

Switch

Triple 3x2-crosspoint switch

Maxim

MAX4550

Switch

Dual 4x2 cross point switch

Maxim

MAX4562

Switch

Clickless Audio/Video Switch

Maxim

MAX4571

Switch

Audio/Video

Maxim

MAX4588

MUX

Dual 4 channel 180MHz bandwidth

Maxim

MAX7219

LED display driver

25AA040

EEPROM

data retention >40 years


hard- and software write protection

8-digit 10MHz clock rate


digital/analog brightness control
4k max. 3MHz clock data retention >200

- -

Fairchild
Infineon
Intel

Maxim
Microchip

P a g e | 202

www.avr.ir

www.pnkavosh.com

years
MCP3001

ADC

MCP2510

CAN Controller

MC68HC86T1

RTC + RAM

CLC5506

10Bit, 2.7V to 5V 200ksps @ 5V low power

LCD Controller

LM74

Temperature Sensor

MM5483

LCD Controller

USBN9602

0... 8 Bytes message frame


32x8Bit static-RAM

GTA (Gain Trim Amplifier) 600MHz bandwidth control range 16dB

COP472-3

MM58342

Programmable Bit rate up tp 1MHz

High Voltage Display


Driver
USB Controller

Keine SDO-Leitung

12Bit + sign 3V to 5V -55 C to +150 C

31 segment outputs cascadeable

35V max cascadeable

DMA-Support Several FIFOs

Microchip
Microchip
Motorola
National
Semiconductor
National
Semiconductor
National
Semiconductor
National
Semiconductor
National
Semiconductor
National
Semiconductor

SPI

:
Master

Slave

SCK

SCK

MOSI
MISO

MOSI
MISO

SS

SS

- -

P a g e | 203

www.pnkavosh.com

www.avr.ir

MISO MOSI SCK SS


.Master in slave out Master out slave in Slave Select . Master
Slave :

Shift Register Master . SS Slave


Master . Slave Shift
Register Master SCK .
MOSI Master MOSI Slave . Slave
MISO MISO Master . Packet SS

Master Slave Master

- -

P a g e | 204

www.pnkavosh.com

www.avr.ir

SPI

SPI SPSR SPDR SPCR


.

SPI Data Register

LSB

SPDR

MSB

SPI Status Register

SPI2X

SPIF WCOL

SPSR

:Double SPI Speed Bit SPI Master


SCK .
- -

P a g e | 205

www.pnkavosh.com

www.avr.ir

:Write COLlision Flag


SPDR . SPSR .

:SPI Interrupt Flag .1 :


SPIE
.2 . SS
SPIF . ISR
.

SPI Control Register

SPCR

SPIE SPE DORD MSTR CPOL SPHA SPR1 SPR0

:SPI Clock Rate Select 1 and 0 SCK Master


.

- -

P a g e | 206

www.pnkavosh.com

www.avr.ir

SPI2X SPR1 SPR0 SCK


f osc /4

f osc /16

f osc /64

f osc /128

f osc /2

f osc /8

f osc /32

f osc /64

Clock Phase :Clock Polarity


SPI :

CPOL CPHA

- -

P a g e | 207

www.pnkavosh.com

www.avr.ir

:Master/Slave Select SPI Master .


:Data Order MSB .LSB
:SPI Enable SPI
:SPI Interrupt Enable SPI

SPI

SPI Master SS
. SS
) (SPDR
Shift Register Master Slave 8
SPIF . Master
SPDR SS .

- -

P a g e | 208

www.avr.ir

www.pnkavosh.com
SPI Slave SS

MISO tri-stated . SPDR Slave


SS Master . Low SS
SPIF SPIE I
. Slave SPDR
.

SPI
SS Slave Master . SS

Slave .

- -

P a g e | 209

www.pnkavosh.com

www.avr.ir

)( spi CodeVision
spi.h spi.lib :

)unsigned char spi(unsigned char data


{
;SPDR=data
;)while ((SPSR & (1<<SPIF))==0
;return SPDR
}
SPI
. SPI
.
) : (SPI
:Master
>#include <mega16.h
>#include <delay.h

// SPI functions
>#include <spi.h

)void main(void
{

- -

P a g e | 210

www.avr.ir

www.pnkavosh.com

unsigned char incoming;

// SCK=Out MISO=In MOSI=Out SS=Out


PORTB=0b00000000;
DDRB=0b10110000;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 500.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
// SPI Enable: True
SPCR=0x71;

while(1)
{
incoming=spi(0x77);
delay_ms(50);
}
}
:Slave

#include <mega16.h>
#include <delay.h>
- -

P a g e | 211

www.avr.ir

www.pnkavosh.com

// SPI functions
#include <spi.h>

void main(void)
{

unsigned char incoming;

// SCK=In MISO=Out MOSI=In SS=In


DDRB=0b01000000;

// SPI initialization
// SPI Type: Slave
// SPI Clock Rate: 500.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
// SPI Enable: True
SPCR=0x61;

while(1)
{
incoming=spi(0x33);
delay_ms(50);
}
}
- -

P a g e | 212

www.pnkavosh.com

www.avr.ir

Mode Sleep Watchdog

AVR Mode 6 .
6 SE MCUCE SLEEP
. ] SM[2:0 Mode
. 4
Startup SLEEP
.

MCU Control Register


0

SE SM2 SM1 SM0 ISC11 ISC10 ISC01 ISC00

MCUCR

] :Sleep Mode Select Bits[2:0 6 SLEEP


. Mode Standby Extended Standby
.

- -

P a g e | 213

www.pnkavosh.com

www.avr.ir

SM2 SM1 SM0


Idle

ADC Noise Reduction

Power-down

Power-save

Standby

Extende Standby

:Sleep Enable SLEEP .

Mode

:Idle Mode CPU ,SPI, USART

,ADC, TWI Watchdog .


CPU Flash .
.

- -

P a g e | 214

www.pnkavosh.com

www.avr.ir

CPU :ADC Noise Reduction Mode ,ADC

,TWI 2 Wathdog . ADC


Mode .
Mode Reset Reset , Wachdog Brown-out
Reset Mode.

:Power-down Mode

TWI Watchdog Reset . Reset


Brown-out Reset Watchdog TWI
. Mode
.

:Power-save Mode Mode Power-down 2

Mode . 2
Mode Power-down .

:Standby Mode Mode Power-down

.
Startup 6 .

:Extended Standby Mode Mode Power-save

.
- -

P a g e | 215

www.pnkavosh.com

www.avr.ir

CodeVision

sleep.h . .
)( :sleep_enable Mode
.
)( :sleep_disable Mode .
)( standby() powersave() powerdown() idle )( :extended_standby
Mode .

Watchdog

Watchdog 1
Watchdog Reset ] WDP[0:2 .
WDR Reset Reset ,Watchdog
Reset Reset .
Watchdog .

- -

P a g e | 216

www.avr.ir

www.pnkavosh.com

Watchdog Timer Control Register

WDTCR 7 6 5

- - - WDTOE WDE WDP2 WDP1 WDP0

Watchdog :Watchdog Timer Prescaler[2:0]


.

- -

P a g e | 217

www.pnkavosh.com

www.avr.ir

Watchdog Turn-off Enable :Watchdog Enable WDE


Watchdog .
WDE :
.1 WDTOE WDE ) . WDE
(.
.2 WDE .

- -

P a g e | 218

www.pnkavosh.com

www.avr.ir

:1 I/O

C B A D ATmega16 PORTx DDRx PINx


x . PORTA DDRA
PINA :

:Data Direction

.
. = DDRA
0b10111101 .

DDRA

:PORTx
. .

- -

P a g e | 219

www.pnkavosh.com

www.avr.ir

Pull-up .
PORTA = 0b11010100 .

DDRA

PORTA

Pull-up

Pull-up

:PINx
. PORC
PINC 0b11010000
:

PINC

- -

P a g e | 220

www.pnkavosh.com

:2

- -

www.avr.ir

P a g e | 221

www.avr.ir

www.pnkavosh.com

AVR :3

Flash

EEPROM

SRAM

Max

F.max

(Kbytes)

(Kbytes)

(Bytes)

I/O

(MHz)

16-bit

8-bit

PWM

Timers

Timer

(channels)

AT90PWM1

0.5

512

19

16

2.7-5.5

AT90PWM2

0.5

512

19

16

2.7-5.5

AT90PWM3

0.5

512

27

16

2.7-5.5

ATmega128

128

4096

53

16

ATmega1280

128

8192

86

ATmega1281

128

8192

ATmega16

16

0.5

ATmega162

16

ATmega164P

Devices

Vcc (V)

10-bit

UART

TWI

No

--

--

Yes

--

--

10

Yes

--

11

--

2.7-5.5

Yes

34

16

1.8-5.5

16

Yes

16

57

32

54

16

1.8-5.5

Yes

48

17

1024

32

16

2.7-5.5

Yes

20

0.5

1024

35

16

1.8-5.5

--

--

28

16

0.512

1024

32

20

1.8-5.5

Yes

31

32

ATmega165

16

0.5

1024

54

16

1.8-5.5

USI

23

17

ATmega165P

16

0.5

1024

54

16

1.8-5.5

USI

23

17

ATmega168

16

0.5

1024

23

20

1.8-5.5

Yes

26

26

ATmega169

16

0.5

1024

54

16

1.8-5.5

USI

23

17

ATmega169P

16

0.5

1024

54

16

1.8-5.5

USI

23

17

ATmega2560

256

8192

86

16

1.8-5.5

16

Yes

16

57

32

ATmega2561

256

8192

54

16

1.8-5.5

Yes

48

17

ATmega32

32

2048

32

16

2.7-5.5

Yes

19

ATmega324P

32

2048

32

20

1.8-5.5

Yes

31

32

ATmega325

32

2048

54

16

1.8-5.5

USI

23

17

ATmega3250

32

2048

69

16

1.8-5.5

USI

32

17

ATmega3250P

32

2048

69

20

1.8-5.5

USI

32

17

ATmega325P

32

2048

54

20

1.8-5.5

USI

23

17

ATmega329

32

2048

54

16

1.8-5.5

USI

25

17

ATmega3290

32

2048

69

16

1.8-5.5

USI

25

32

ATmega3290P

32

2048

69

20

1.8-5.5

USI

25

32

ATmega329P

32

2048

54

20

1.8-5.5

USI

25

17

ATmega406

40

0.512

2048

18

4-25

--

--

Yes

--

23

ATmega48

0.256

512

23

20

1.8-5.5

Yes

26

26

ATmega64

64

4096

54

16

2.7-5.5

Yes

34

ATmega640

64

8192

86

16

1.8-5.5

16

Yes

16

57

32

ATmega644

64

4096

32

20

1.8-5.5

Yes

31

32

ATmega644P

64

4096

32

20

1.8-5.5

Yes

31

32

ATmega645

64

4096

54

16

1.8-5.5

USI

23

17

- -

A/D

Interrupts

Ext

SPI

Interrupts

P a g e | 222

www.avr.ir

www.pnkavosh.com

ATmega6450

64

4096

69

16

1.8-5.5

USI

32

17

ATmega649

64

4096

54

16

1.8-5.5

USI

25

17

ATmega6490

64

4096

69

16

1.8-5.5

USI

25

32

ATmega8

0.5

1024

23

16

2.7-5.5

Yes

18

ATmega8515

0.5

512

35

16

2.7-5.5

--

--

16

ATmega8535

0.5

512

32

16

2.7-5.5

Yes

20

ATmega88

0.5

1024

23

20

1.8-5.5

Yes

26

26

ATtiny11

--

--

2.7-5.5

--

--

--

--

--

--

ATtiny12

0.064

--

1.8-5.5

--

--

--

--

--

--

ATtiny13

0.064

64B

20

1.8-5.5

--

--

--

--

ATtiny15L

0.0625

--

1.6

2.7-5.5

--

--

--

--

1(+5)

ATtiny2313

0.128

128

18

20

1.8-5.5

USI

USI

--

ATtiny24

0.128

128

12

20

1.8-5.5

USI

--

USI

17

12

ATtiny25

0.128

128

20

1.8-5.5

--

USI

--

USI

15

ATtiny26

0.125

128

16

16

2.7-5.5

--

USI

--

USI

11

11

ATtiny261

0.128

128

16

20

1.8-5.5

Yes

--

USI

11

19

ATtiny28L

--

32

11

1.8-5.5

--

--

--

--

--

--

2(+8)

ATtiny44

0.256

256

12

20

1.8-5.5

USI

--

USI

17

12

ATtiny45

0.256

256

20

1.8-5.5

--

USI

--

USI

15

ATtiny461

0.256

256

16

20

1.8-5.5

Yes

--

USI

11

19

ATtiny84

0.512

512

12

20

1.8-5.5

USI

--

USI

17

12

ATtiny85

0.512

512

20

1.8-5.5

--

USI

--

USI

15

ATtiny861

0.512

512

16

20

1.8-5.5

Yes

--

USI

11

19

- -

P a g e | 223

www.avr.ir

www.pnkavosh.com

AVR Pinout :4
ATmega64,ATmega128

ATmega16, ATmega32

ATmega8

ATiny26

- -

P a g e | 224

www.avr.ir

www.pnkavosh.com

ATmega16 :5

- -