You are on page 1of 36

 

  


   

 
 

+$2&0)*- "&!"/*-:  


  

,#"./%,'"(1- 0)$,# /%-:  
  


 
" "$ !$
 " -   "

! $ - 


A 1
$ / #!  
5-?/:347 )/')947: + .) ' ,!'
6+)89.60)1>7 :3-6+%9.7: +"$*& ' ,!'
      $   :

_  _
 ( $ ()/,$ )." (. 1" *)1" ( (." ":
1.

#$ .(  )  ( %&$&)($0 )."  &$! )1"


VR (. )$*
"%)*# $0 (*())$' )$* &()&$* (STK200/CodeVision C).

2.

)"/( )' !)$*&'  ,&(' )." -+ 1" (/."-#/." )$*  &$! ) (.
%&$&)." () !1(( C.

_$ !_
 $"' (/$*-#/$* / %$*  ,&($%$($* %&,$") ()$ "%)*# / (0()
STK200  " 8  /%)'  8 +.)$$$ LED. (*"($!$ )$*' " ))$ 1() 9) LED 3)
;=94*424?3 >9)3 4 )164,&19.7 LEDn *6(81-9)0 89) 0 Volt.
")()$,, 4 )164,&19.7 SWn &<-0 894
%164 94: 5 Volt >9)3 4 ,0)1>59.7 8943 454(4 -(3)0 8:3,-,-&347 ,-3 -(3)0 5)9.&347.
  &$! )' ATmega16 ) (*"$!  32  &$ )' (/$*-#/$*, $ $%$$
)"$") ( 4 0&' (ports)  )' $"$(' A,B,C  D.   0& , 0&$' 8-bit,  ( 
" )' bit ")()$,) "'  &$ )' /.  !,$' )' !)$*&'  0&' %)*,")
(. 3 ),.&)1":
1.

M(. )$* ),.&) DDRx (/%$* x=A,B,C,D) $&$* %$$ %/ )$*'  &$ )' )' 0&'
x  " 

 ()$ ")()$,$ bit $&) 0)  %$$  " 

 ()$ ")()$,$ bit
$&) 1).  %&,  )" ")$!
DDRA = 0b11110000;
) %&1) 4 pin (0..3) )' 0&'  " ($$ "1 ) *%/!$% (4..7) " #$$.

2.

%/ )$" ),.&) PINx $* )$*'  &$ )' )' 0&', "#&)) " *)$ ,$*"
!. .' ($$  #$$.  %&,  )" ")$!
k = PINB;
 )!) k (.&$0 /) , !. .' unsigned char) %&, () %&$*' bits
)' )" )()( (!$ / 1  0) )." 8  &$ )1" )' 0&' .

3.

 )$" ),.&) PORTx &+$* ()$*'  &$ )' %$* ,$*" $&() .' #$$. "
%$$  &$ )' ,$*" &*() (" ($$,  )$" ),.&) *)/" "&$%$$0 )
*")/)) PULL UP (&+$")' 1 ()$ bit %$* ")()$, ()$" "!$$  &$ )).
 %&,  )' ")$!'
DDRC = 0xf0;

PORTC = 0x0f;

) %&1) 4 Pin (0..3) )' 0&' C " ($$  "&$%$"$ PULL UP "1 ) *%/!$%
(4..7) " #$$  !$ / 0 .

  1

H Codevision C & *'& *# ) &'%)&") )+!!' #.# bits (' !'%!*1#) ) ! ##
&/ +*%0( *%+( 3 !*-.'*(. )0#*$ &%+ -') %&% %0 ,#* )* !*. &'*:
DDRC.2

= 1;

O
  2  
 C 
   

PORTC.2 = 0;

O
  2  
 C   0 Volt

if (PINB.3 == 0)..

   

 
  3  
 

# &%"0 )#* !/ # !*#%)%+ /*  *# &'&#. )0#*$ &++#/)* ) %#.#%+(


!'%!*( $-.' )*,  %#*(  !%'%#*( *# !*)*) *%+( ("% !/ 1  0).
# /.( "%+, &.-. # !%')%+  *%+( !'%!*( *( 0'( C ) "% !/ 1, +*/ #*
  #*%" 1(
PORTC = 0b11111111; { 

 }
, )%0# PORTC = 255;

{ 

 }

, )%0# PORTC = 0xFF;

{ 


 }

*% #&*+$ !/ )0)* STK200 %  !/&*( (SW0 - SW7) # )+##%
)*%+( !'%!*( *( 0'( D *%+ !'%"!*, #1 % ,.*%%% (LED0 LED7)
# )+##( )*%+( !'%!*( *( 0'( B.

_  _
      CODEVISION C

!%"%+)* *# &'!*.   !),   # $% ! .*  *%# &'%'* )/ *.# AVR )*%
#&*+$ !/ )0)* *%+ E')*'%+:
1.

#%%+ *% &'/' CodeVisionAVR.

2.

&/ *% !+'.( #%0  %+'%0 #% '-% ( File -> New..).

3.

,#* # &'+'%  "/%+, & "%+ project ! &* .

4.

*# '1*) # "%+ # -') %&% )%+ *% +*/*% )0)* '0 )( '- !1#
&'*'.# (CodeWizard) & "%+ Yes.

5.

,#* )*# %/# # )0#*% &'+'%  "/%+.

6.

#%+ * ( &'!*. '+) (:


' %- Chip
(i) * ) Chip & "%+ ATmega16
(ii) * ) Clock ',%+ 4,0  z
(iii) * ) Program Type & "%+ Application

!37 47- &0*,/%:- $54:- +$)/1#'/5& 4/ 61(3*/0/*/9&-/ /-4!,/ *+1/&,&%+4", +$):2 &0#3(2 +$*
4( 356-84(4$ 4/5 1/,/%*/9 4/ /0/#/ 61/-#'&* 4( ,&*4/51%#$ 4/5, 8072 $548 0$1!6&4$* $08 4/
$-$045.*$+8 3934($ STK200

  1

7.

-7 1, (2.6/ #*,8 1,2 -.&8.,2 #-')!,2# File (' Generate, Save and Exit.

%',2.!01# 3(#), # 1, 7*, Micro1 (' -,&%(#80#1# #(# 4.%0',-,'9*1/ 1, 7*,


lab1     
.
8.

1% 02*4#', % CodeVision "%',2.!# 2171 * .4#, # 1, 7*, lab1.c 1, ,-,,
-#.'4#' 1,* (9"'( , ,-,,/ .4'(,-,'# 1,* '(.,#)#!(1 / # 0% 1'/ #-'),!/ 1,2
CodeWizard.  "2*171%1 21 "'#2(,)8*#' '"'1#. 1,* -.,!.1'07 1,2 '(.,#)#!(1,
7-6/ & ",8# (' 0# #-7#* .!01.'.

1% 30% 21 76/, (.*#1' 0(7-', * !.5,2# 7*,' / 1,* (9"'( 1,2 -),8
-.,!.1,/ 1, ,-,, & 2),-,'0,2#. -,*6/, 1,-,&#1,8# 1,* (.0,. 0 01,
-.&2., 1,2 (9"'(, #-')!,2# 7), 1, (##*, (# Ctrl-A) (' 1,* "'!.3,2#
-19*1/ delete.

1, (#*7 -),* -.&2.,, !.3,2# 1'/ (7),2&#/ !./ 1,2 (9"'( (-.4#1'
"-) % #-#+!%0% 1%/ (&# #*1,)/)

#include <mega16.h>
void main(void)
{
DDRB = 0b11111111;
PORTB = 0b11111111;

# 1%* 5#2",#*1,) #include 02-#.') *,2# 1, .4#,


#-'(#3)"/, 1, ,-,, *3.#1' 01, 02!(#(.'*, '(.,#)#!(1
8.' 02*.1%0% 1,2 -.,!.17/ /
&,.$,2# 7),2/ 1,2/ (.,"(1#/ (0..7) 1%/ &8./ B 6/ #+7",2/
&,.$,2# 0* .4'( 1' 0# 7),2/ 1,2/ (.,"(1#/ 1, ),!'(7 1, ,-71#
+#('*9*1/ 7) 1 LED #*' 0 %01

DDRD = 0b00000000;
PORTD = 0b11111111;

&,.$,2# 7),2/ 1,2/ (.,"(1#/ (0..7) 1%/ &8./ D 6/ #'07",2/


-')!,2# pull-up !' 7),2/ 1,2/ (.,"(1#/ 1%/ &8./ D

while(1)
{
if (PIND.0 == 0)
PORTB.0 = 0;
else
PORTB.0 = 1;
}

),-,%0% 1.,* .74,2 (% 02*&(% #)!4,2 #*' -*1 TRUE)


* #*' -1%*,/ , "'(7-1%/ SW0
- * *5#' 1, LED0
# *1&#1% -#.-160% (#* "%). , SW0  #*' -1%*,/)
- * 0 0#' 1, LED0
 -.-*6 )#!4,/ #(1#)#1'  !
),/ (2./ 02*.1%0%/ ((' -.,!.1,/)

 1

DOWNLOAD        


1.

*& +& ! +"&$  & LED  %*%& %'.

2.

! & &! +"& &!'#$ &!' Board, ! !"!!$ #%& " %&! ,% &#!(!!%$.

3.

"+ &! !, Project &!' CodeVisionAVR " & Configure.

4.

"+ &! "%! +!' "!' (& %&! & After Make *& +& 
##%! &! Program The Chip  "&%& OK.

5.

"+ &! !, Project &!' CodeVisionAVR " & Make. & "+ !, (&  "#'#!  "#!(!#$  & %  & % &!' compile &!' "#!#&!$.  
)!' # %'&& ,  '"#), %& % &!' "#,#!' &! !'" Program. 
&& "#"&*%, !#-!' &  %&! -  ")#!,  &! Make.

6.

"!&$ &! !'" Program &! "#+# $ (!#&-& %&! #!& %* &$
"#$ ,#$  #)  &% &!'.

  1

       AVRStudio


%$&$/ " ,&($%$($* )$ AVRStudio  " #$$0($* ) !)$*& )$* %&$&)$'.

*). " ,&($  )$" ")$%(. (+!)-" () !$  )$* 0  ', ) $%$ " %$&
-' " ")$%( $ compiler.  #$$-)' $ %(' ()$ #$ . !,$ )$* %&$&)$'
((*%&+$& ( .!$*' )$*' *")$/' (*"*($/' )()(-"  (.-"/#.-").


%. )" Codevision,  "0 ,$* " %)*,0' compile )$" 0 


', !$/ )$ AVRStudio %)0")' %. )" %& &!-" )$ $*%

+") ).) $ 0 ' )$* %&$&).' ' ( " %&*&$ )$* AVR Studio,  "
)&"$ !  () &()& )$* 0  ) "/ )" %." ")$! %&$' #$$-(.

 %&% %(' " *%&, "$,).  " %&*&$  )$ ."$  (" .,, %) Alt+5
 " +"()). 0 *%&,$*" %&$($$-" ) resources ( ),-&)', ,&$"()',
/&' /, !%.) )$*  &$! )  " %$&$/ " !%$* %-' *) %&$") %.
) &$ )$* %&$&)$'  )' "&'/#-)&  $".) (.%-' *) #$$0"$")
%. )$" ,&()).

%. )$ "$/ Debug *%&,$*" +$&' %!$'  )"  "(, )   )!(, 
%" "( )' #$$-(' )$* 0 . %$ ,&( *").))  &,&$*' ,&()'
"  Trace into ($!/  ,&( )$* %! )&$* F11)  $%$  )!  ")$!  +$&
%$* %) )$ F11 (%&)&$/ .) )$ )&"$ !  ))$%) "!$).
"!$ 
) %&%)-(, )*,." !!' () resources +"$") ()$ %&*&$ I/O.

0"$* .)  " %&$($$0($* ) !)$*& )-"  $%)0" )$* STK200,  %&%
&,  " )( &$* ) ")()$, $*)  ()$ ) "Input Pins" )$* PORTD ()$
%&*&$ .  )$" )&.%$ *). "%&()$/ $& )" &,  )()( )-"  $%)0"
)$* STK200, $ $%$$, (*"-' # "0")' )$ %&.&, " " %)"$ %. )$"
,&().

  1

_STK 200     _

_ / _

 1

_
  - _
*%!&1() ()$" "/ ,1&$ )$" 1  %$* *!$%$ )$ )$0"$ )$*  &.))$'.
1

 !!, $
 /%) SW2.

1 ' )$" $%$$ &-) %&$ "$* " " )$ LED3  /($ %) )$

( ()$" &/,$ )' while " &+$0" $ ")$!' PORTB = PIND. #() ) !)$*& )$*
%&$&)$' (%$&) " ")&#) () (!. 2  $!).

 &+ %&/& )$ $%$$  " )$ LED0 %)1")' )$  /%) SW0   )$ ("
%)1")' )$  /%) SW1.

 &+ %&/& )$ $%$$  " 


) LED %)1")' )$  /%) SW2   ) ("
%)1")' )$  /%) SW3.

 &+ %&/& )$ $%$$  "  ) LED{0,1,2} %)1")' )$  /%) SW2  
"  ) LED{5,6,7} %)1")' )$  /%) SW6.

 " #$$.( "/' %/ ) %&%". %&$&) ()$" #$$.) AVR Studio,  " "
)"$)  ,&(/))   !)$*& )$* ((*$*!*)) )' (0")$' $' %$* %&,$"))

  1

_
  - _
1

 !& !)! $    ## $ $#$# $% LED3 (  ##$)) #(
$% ) $ SW3 (!# toggle). +$ )$ %$)   !   ) $ !*
 '("   $ !##)$!%" if/else '%".   bitwise  ! ! 
   %$);

#include <mega16.h>
#include <delay.h>
void main(void)

PORTB = 0xFF;
DDRB = 0xFF;
PORTD = 0x00;
DDRD =0x00;
while(1)
{
if (PIND.3 == 0)
{
PORTB.3 = ~PINB.3;
delay_ms(50);
while(PIND.3 == 0);
delay_ms(50);
}
}
}

//
//
//
//
//

   SW3
   

   50ms,  
     
   50ms,  

 LED3
debounce
   SW3
debounce

  $ $ !)! #", !%  # toggle  #'*  ) $ * SWn
 LEDn (n=0..7)

#include <mega16.h>
#include <delay.h>
void main(void)

int keyState;
PORTB = 0xFF;
DDRB = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
while(1)
{
keyState = ~PIND;
if (keyState)
{
PORTB = PORTB ^ keyState;
delay_ms(50);
while(~PIND);
delay_ms(50);
}
}
}


1

 
   " "
   -   

 " - 


A 2
!  I/O
.'5)3,-0 %)$%2-0: ')%# (#
/&%12(/*%+40 3,'/&#2(0: ' &"# (#
      "    :

 _

$*# $%*( # % & "$%" & &% +   ')$  !"%") "+$ %#
& ! $# !, !" "%) ()"#  % ("$ interrupts, !"  & $% !*
"$%"   & ! "#  ' " ! $# ! & !'"  ("$ %) interrupts.

_ _
1

 "'  !"*" %


!   %%" !*$# ' "# ( !% !* %  ("$%
*!%# SW4. %%" + %  !"!  ! % (",#) $ 8-bit "' $%
LED % & STK200.
#include <mega16.h>
#include <delay.h>
void main(void)
{
unsigned char count = 0;
PORTB = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
while(1)
{ PORTB = ~count;
if (PIND.4 == 0)
{ delay_ms(50);
while(PIND.4 == 0);
delay_ms(50);
count = count ++;
}
}

// 
 SW4
//  
 50ms,   debounce
//  
  
 
SW4
//  
 50ms,   debounce

  2

 ! !  % &, ( ! &!  ! !" !!    
!" 10, ! ! !$ LED   "  100ms   60 ! ' " #.
 ! ! !  # ! & ! , "  $'! !  ! !"
&! SW4  &! ! LED  ".
#include <mega16.h>
#include <delay.h>
void main(void)
{
unsigned char count = 0;
unsigned char blink = 0;
PORTB = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
while(1)
{ PORTB = ~count;
if (PIND.4 == 0)
// 
 SW4
{ delay_ms(50);
//  
 50ms,   debounce
while(PIND.4 == 0);
//  
  
 
SW4
delay_ms(50);
//  
 50ms,   debounce
count = count ++;
blink = 0;
}
if ((count%10 == 0) && (blink<60))
{
delay_ms(100);
PORTB = 0xFF;
delay_ms(100);
blink++;
}
}
}

   2

 #( "#+# &! !"!!  &  & +!' &!'#:


&-&$ &! +"& SW0,  &!'# &!' #!& %%& % %&$  $ ,!
&%&%$:
   :  LED "!!' & &%&% &* !"&-
3

   :  LED{0,1,2,3,4,5,6}  %%&  &! LED7 !%  0.25 sec
#!&& %&! "#+# %$  )#%!"!%&  && flag &,"!' bit,  !"! 
#& & &#)!'% &%&% &!'#$ &!' #!&.

*#%& +& -&$ &! "#+# #%+%& %& &%&% .


#include <mega16.h>
#include <delay.h>
void main(void)
{ int count = 0;
bit flag = 0;
PORTB = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
while(1)
{
if (PIND.4 == 0)
{
delay_ms(50);
while(PIND.4 == 0);
delay_ms(50);
flag = ~flag;
}
if (flag == 1)
{
PORTB.7 = !PORTB.7;
delay_ms(250);
}
else
PORTB = PIND;
}

// 
 SW3
//  
 50ms,   debounce
//  
  
 
SW4
//  
 50ms,   debounce

  2

 2

 
    
  -  

   - 


H 3
   
,%3'1*+. #'"#0+.: ,! .*!(  -"(
-$#/0&-(#)2. 1*%-$!0&.: ,#%+'!(  -"(
         :

 _
 ) !% )*/-% *%+ ')*'%+ +*%0 # :
1.

). )*   !) )- )%0, +"%&%)( ! "-%+ ) &&% &'%)%%.)( ,'%1#
&%+ #* *.&%+# '" )* ! &'%"* &'%( &"+)  *%# !'%"!*.

2.

') '%+* #1# &/ * ( *% (  " %!( &%+ &'-%#*  *%# CodeVision C compiler.

  _


%"0 )+-# )*  %-# -') %&% * 0%(  !%&*1# &"(, % %&%% & *%0# &/ *%
- ' )* # -') %&% ) ! * 0% *%+ -'  &'%! #%+ # !! #) !&%  (& !#+# )+#.()
 ').
+*/ #* 1)* # &%,0%#* *+-* &%+  &%'%0)# # )+%0# #, &/
"  % '/#%( !%+&%0) *% # -' *%+ ) !&% % & !#+#% )% * )*  &%+ !! #%0)
*  ')  *% ""%.  -# )/( +*/( ),"( +"%&% * &/ 0%  !/&*(- )/%+( (P1 !
P2), % %&%% &'& # &*%0# *+*/-'%# &'%! #%+ # $! #)  !)*%*  ').

SAW

POWER

ON

OFF

SERVICE

RET
P1

P2

 1
.'%0  **%  +"%&%) )* &")  ( -#( !%&( $0"%+ (- 1). " *%+' *(
"-* &/ #  !/&* &'%-( () ! #  !/&*  !%&( (FF) *'%,%%)(. 0&'$
*'%,%%)( )* -# !%&( !* !#0* )  ,.* # # $ (POWER) )* !%#)/" "-%+.

  3

&$*( )&$+$$(', $  /%)' %!' P1  P2 %$&$0" " ,&($%$$0" %/ )$" ,&()
 )" 

"(  0 !$* $%'.  0 !$' $%' %$)!) %/ ) #' 0$ ():

i) "&$%$( )$* ()$' ) "('- $%' (SAW)  7sec, ,&$" / () ) )$ $%$$ 
%&$"$ $&! ) ") %&$' ) &()& (, 1)  / )$ $) )$* #0!$*.
ii) "&$%$( )$* ()$' ) "('-%()&$+' (RET)  3.5sec, %&$ "$* 
%&$"$ $&! " %()&- ()" &,  )' ( +$0 , $!$ !&.  $% )$* #0!$*.
)" $ ,&()' , "&$%$( (*"$!  1000

0 !$*'

$%', $  &$! )'  %&% "

 /%) *)/) )" %&$, )&$+$$(' ()$ ,"  " "  +.)" "#  )"
(*")&( )' ,"' (SERVICE).
 () !,$* )$* %&%". (*())$' ,$*" ))&%  ) %&)) * !1) 
(* ")&1"$") ()' -+ ' (/$*'/#/$*' "/'  &$! ) ATmega16.  )&(/' )$*'
+") ()$ , 2, /%$* $ (*$!(/' )."  &$ )1" "  ( )' 0&' )$*  &$! )
( %&, )$ pin B7 "+&) ()$"  &$ ) 7 )' 0&' B).
 
 /%)' ON
 /%)' OFF
 /%)' P1
 /%)' P2

PIN
D0
D7
D3
D4

   
 SAW
 RET
.)" "# POWER
.)" "# SERVICE

PIN
B0
B1
B7
B6

 2
(*"($!$ )/($ )." (/.", /($

 )." #/." )$* (*())$' ()$  &$! ) ,

*!$%$  "()&$+ !$  ( )' ")()$,  ) (0"( )."  $%)1"  )." LED ()$
STK200), $%/)


!$ $  /%)' "$*" !$ / 0 /)" " %)"$,  !$ / 1 () ")) %&%).(.

 +.)$$$ %&"$*" ""$ /($ )$ ( ()" ($/ )$*' " 0 Volt (!$ / 0).

 () !,$* (SAW  RET) "&$%$$0")  !$ / 0,  %"&$%$$0")  1.

  3

_ 
A _

  % # #  (  &$  #  #, #   $ #!  ' #"! #$ ! #$ "$"##!. ""# #$! (#!  # LED #$
#$) STK200,    "*"# #! "($!  #! ($! #$ "$"##!
 # # "# &  #! CodeVision C ( # #"#& help files)    #  
"'#*"# "# * "! # # ,    & #! $#! $
$) & ! $"# "!.

  
  ,           
 (
      ):
#include <mega16.h>
#include <delay.h>
#define MAXCYCLES 1000
#define ON 0
#define OFF 1
void main(void)
{
char pow;
unsigned int cycleCount=0;
PORTB = 0xFF;
DDRB = 0xFF;

// LED outputs on STK200

PORTD = 0xFF;
DDRD = 0x00;

// BUTTON inputs on STK200

while (cycleCount<MAXCYCLES)
{
if ( PIND.0 == ON )
{ pow = ON;
PORTB.7 = ON;
}
if (
{

}
if (
{

}
}
PORTB.7 =
PORTB.6 =
pow =
while(1);
}


3

##! )# "#  )  &$  ## $ * #  ) #( #"#(
*( !  "# "#  SRAM, "# # #+"!   *"  &  &)  # %"! #$  # "# #$  #! &!.

M  " #  #" "#  *  +#,   " "# #  #  ) 
&$,  #! & " #! $#)##! $"! ##+ "# EEPROM,  $ 
##(# #"#&! #"# "! ("$$$## # manual #! CodeVision).

$  $%  (!  #$ )## #$ #,   #  $" (
 #!  ##!  "!/! "#  )  &$),  '! #$ "$"##! # #
"$# " #! &!.

   3

 
! !"  "
 ! -  !

  " - 


A 4
E" INTERRUPTS
5+>.:247 '.%'947: )+'% *%
6)'89-6/'0=7 :2+6)#9-7: ) "($% *%
      "    :

 _
 &" &',*" '"( $&'$"( ('"-  :
1.

!"+&  '"(% * &"-% "#. '+ $"'.


'&, '"(% &'" #$" '"( CodeVision C compiler.

AVR   '"

#$"$-

2.

( &""  *"% &(&(. &+ '+ $. !+'$. "#. #"( #$* "
ATmega16.

"  _


! "
O "#% (interrupts) 5'6+('&24:2 89- 0'242/0% 64% +09$1+8-7 94: 564)6#'947 #$""(  " #$"'$,'' & '"($% #" & '%  #" *$"  $%. O * &,% (',%
&( +% !(#$' ' +5/04/2<2&' + +3<9+6/0$7 8:80+:$7, (! " '% ' '*-'' #,$&% &'
 -' '"(%. M ' ) &  ,% interrupt ( ),&" *  $"#" " * &,%
#,$&%),  CPU ,#' ' '& '"( ($+% #$"$'"%. K',# ''  64:9&2'
+3:5-6$9-8-7 */'045?2 (interrupt service routine - ISR),  "#" * #$"$'&'  
 '#"$ ' &'" &($ " & interrupt.
  # "#% (*$" &'%, !+'$
interrupts #.) #$"#'  ''" ISR. M ' ""$+& '% ISR, - 64% 94: 564)6#'947
8:2+;&,+9'/ '5= 94 8-+&4 +0+&24 894 454&4 */'0=5-0+ '5= 94 interrupt.
E . '% " $"'%, ,% " "#%   '5+2+6)454/-$2+7. ( +% "#, #, '%
#$+'$*% $&% #"( '  #'& '" #$,$ %    '6;/0454&-8- 
+2+6)454&-8- '+ &($ + "#. #"( *$&"#""- ' &' )$" %. H &
 $"#"&% '+ interrupts  ' & -" ':
1.

  # "#% #"( "(  *$&"#"&"( $("( '% #('% #$'$"(%
'"($%  ' +5/$64:7 +2+6)454&-8% '"(% "$" '% ' ' bits '+ ( +
'*+$'. #"(  '&'"*"- & ('%.

2.

' &( * +2+6)454/4>+ 0'.41/0# 94 -;'2/8= 9<2 */'045?2, "#,'   $"#""- '
, " " "#% #"( *"( $*"#" &'" #$""- " .

Status Register SREG


Bit
Read/Write
Initial Value

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

SREG

O '*+$'% '&'&% SREG (Status register) #$* ' ( ','' &( "%  $"#"&%
( 1)  # $"#"&% ( 0) '"( * &"- '+ "#. , "$" '%  " '" bit 7. M -"
%  '"% (SEI  CLI) #"$"-  "$&"( ' ' '"( Global Interrupt Enable bit (& 1 
1

  4

0 %$% (). A&%#  % #  $# + $ -$$ Assembly,  !"!  $( , $%
!"+" #  % )& %  #asm.
  
 
O ATmega16 % %"# "# *%"-  !- (INT0, INT1  INT2)  ! #
%$% ( , $% &# " %# 16, 17,  3 (! $().  % $,$  *%"# $&$&#, 
%$% (  " %#  !"!  ( &  "$% *# $   ($* % & % & %(*"%
DDR). -% +%, ! $% STK200  ," B ("$ ! %  %  ( %* LED,
" %# % & INT2   $ #, !+%  $% !" "% %* $$* 
("$ !  , + % *%" interrupts INT0  INT1.

Amega16

 %  ( %* *%"- interrupts ("$ !  ,%  !"%* %(*"%#:


General Interrupt Control Register GICR
Bit
Read/Write
Initial Value

INT1

INT0

INT2

---

---

---

IVSEL

IVCE

R/W

R/W

R/W

R/W

R/W

GICR

 %  %(*"% GICR ! " ,  " ! $ & %# "# %* *%"-  !- % &
Amega16 "' %# %  + 1   %# !" ! $ & "' %# %  + 0 $%# %#
$# % & %(*"%. O $&# " ! $#,  $ % $ $%  %$% ( " % 
 " !  *%"+ interrupt  " % (*"$%, $* bits $% &# %(*"%#
MCUCSR  MCUCR.
MCU Control Register MCUCR
Bit
Read/Write
Initial Value

SM2

SE

SM1

SM0

ISC11

ISC10

ISC01

ISC00

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

MCUCR

T bits 0-3  " & %# $&# '$# %* interrupts INT0  INT1,  $ % $ $% 
%$% ( " %, +!*# !""'% $% &# + & &# !#:
Interrupt 0 Sense Control
ISC01
ISC00

 (+ &+ $% pin INT0  &" %$  !#
0
0
  %$%$# $% pin INT0  &" %$  !#
0
1

!%-$ % & & , (%"(+ %*! ) $% pin INT0  &" %$  !#
1
0

  # % & & , ("(+ %*! ) $% pin INT0  &" %$  !#
1
1
Interrupt 1 Sense Control
ISC11
ISC10

 (+ &+ $% Pin INT1  &" %$  !#
0
0
  %$%$# $% Pin INT1  &" %$  !#
0
1

!%-$ % & & , (%"(+ %*! ) $% Pin INT1  &" %$  !#
1
0

  # % & & , ("(+ %*! ) $% pin INT1  &" %$  !#
1
1



4


  - I_
 $!& $ !$( !)! #  *! project. &* !!$#$ $
!$ #", $#$ ! !" &!" $ ) $ SW2.  !!&   )!# $%
#%#$$"   %  $%! $% !!$", #% !+$"  $
#') #$ +.

#include <mega16.h>
unsigned char count=0;
interrupt [EXT_INT0] void ext_int0_isr(void)
{ count++;
PORTB = ~count;
}
void main(void)
{ PORTB = 0xFF;
DDRB = 0xFF;
PORTD = 0xFF;
DDRD = 0x00;
GICR = 0b01000000;
MCUCR = 0b00000011;
#asm("sei")
while (1);
}

  (# $% ! ( !!$" #$ AVR Studio, !%  $  !
$#" $% + #$ !$.
MCUCR=0x02;

 $!   $ !)! +#$  " #$ LED  $ )" $ $ ) $.

 
    
MCUCR=0x03;
:
MCUCR=0x02;
4

 $!   $ !)! +#$  " #$ LED 


(  ) $ ) $ SW3).

!$ *$ #( $% 1

 
    
MCUCR=0x03;
:
MCUCR=0x02;



4

 !& !)! $    '!#  $ interrupt INT1  $ )% $%!:
$+$" $ ) $ %  #%" #$ INT1,  $%! $% !$
##$ # #$" " * $#$#":
5

   :  LED{0,1,2,3}    $ LED{4,5,6,7}  ##$


   :  LED{0,1,2,3,4,5,6}  ##$  $ LED7 #  0.25 sec
!$$ #$ !)! #"  '!# #$  $$ flag $* % bit,    
!$ $ $!'%# $#$# $%!" $% !$.

#include <mega16.h>
#include <delay.h>
bit flag;
interrupt [EXT_INT1] void ext_int1_isr(void) {
flag = !flag;
if (flag) PORTB = 0xFF;
}
void main(void)
PORTB = 0xFF;
DDRB = 0xFF;
DDRD = 0x00;
GICR = 0x80;
MCUCR = 0x08;

// toggle state flag

{
//
//
//
//
//

PORTB is output
LEDS switched off
enable INT1
PORTD is input
INT1 request 1->0

#asm("sei")
while (1)
{
if (flag)
{ PORTB.7 = !PINB.7;
delay_ms(250);
}
Else
{
PORTB = 0xF0;
}
}
}


4

 
 - II_
6

N %* $%-% (# #$##  ) #$# (& $%(, $('& (#)%&, !#!(& +%'
(,!  interrupt 0  1:
A!(!# (& (#)%& (#) %# ( !  $-!' !-& !!#) LED,  ' (#)
#$##) ( ( $- (# ' '(#)& %#(& INT0  INT1,  ' ( "&:
 !/!(&, (# !!# LED %'( '( ' LED0 (-  ( )$- #$ LED ! ''().
  *#% $#) +#)    
 '(#! %#( (#) INT0, (# !!# LED
(!( (  '    .


! (# !!# LED %'(  '( ' LED7, !( !. ' (#) '( ' LED0.

  *#% $#) +#)   


 '(#! %#( (#) INT1, (# !!# LED
(!( ' (!"%(( $- (! (%+#)' ' (#)) '( ' LED2.

#include <mega16.h>
unsigned char LEDpos=0x01;
interrupt [EXT_INT0] void ext_int0_isr(void)
{ if (LEDpos == 0x80)
LEDpos = 0x01;
else
LEDpos = LEDpos<<1;
PORT = ~LEDpos;
}
interrupt [EXT_INT1] void ext_int1_isr(void)
{ LEDpos = 0x04;
PORT = ~LEDpos;
}
void main(void)
{ PORTD=0xFF; DDRD=0x00;
DDRB = 0xFF;
PORT = ~LEDpos;
MCUCR = 0b00001011;
GICR = 0b11000000;
#asm("sei")
// Global enable interrupts
while (1);
}

N !#)! # $%((&  & '(# $%-%, $%#!#)  (!' (#) !!#) LED
! !(  -'# # !('(#+#& -$(& (#) 0 $%! $(!#&.

MCUCR = 0b00001000;

  4

N "  !!  / 


!" !" INT0,  ! ! %":

! %,  ", % !  !


 # " $"
     !  ! !" INT1, !   LED
!! !       .


  !   LED  !  !   LED0, ! &  !" !   LED7.

   !  %! SW0, !   LED !!   ( !! % !
! $"    !") !   LED2.
#include <mega16.h>
unsigned char LEDpos = 0x01;
interrupt [EXT_INT0] void ext_int0_isr(void)
{ if (LEDpos == 0x80)
LEDpos = 0x01;
else
LEDpos = LEDpos<<1;
PORT = ~LEDpos;
}
interrupt [EXT_INT1] void ext_int1_isr(void)
{ if (LEDpos == 0x01)
LEDpos = 0x80;
else
LEDpos = LEDpos>>1;
PORT = ~LEDpos;
}
void main(void)
{ PORTD=0xFF; DDRD=0x00;
PORT= ~LEDpos; DDRB=0xFF;
MCUCR = 0b00001011;
GICR = 0b11000000;
#asm("sei")
// Global enable interrupts
while (1)
{ if (!PIND.0)
{ LEDpos = 0x04;
PORTB = ~LEDpos;
}
}
}


4

 
    
  -  

   - 


H 5
  TIMER0
-%5'2+,/ #'"#1,/: + -) ' ,!'
.$#01&.(#)4/ 2+%.$!1&/: +"$*& ' ,!'
         :

 _
 ( $ ().,$ )$* &()&$* *)$/ ":
1.

(- () ,& )&()  !)$*&' )$* ,&$"() Timer0 )$*  &$! ) Tmega16

2.

A"%)*# $$!$' %&$&)($/ (- &$*)"-" #*%&)('  $%0" (ISRs).

  _


  TIMER0
O ATmega16 ) 3 #,-&()$/' ,&$"()'/%&)',  )' $"$(' Timer0, Timer1 
Timer2.  Timer0 , %1.&1" 8-bit (%. 0x00 -' 0xFF). H )&,$*( ) )$* )&) " %&$(%!( ).($  ""-(, .($   $&(. (&+) (- )$* ),-&) TCNT0. O Timer0
) %$!!' *").))'  !)$*&', $ $%$' &*$") (- )-"  .!$*-" ),-&)0":
Timer/Counter0 Control Register TCCR0
Bit
Read/Write
Initial Value

FOC0

WGM00

R/W

R/W

WGM01

CS02

CS01

CS00

R/W

R/W

R/W

R/W

R/W

COM01 COM00

TCCR0

T bits 3  6 $&$*" ) )()( !)$*&' (mode) )$* ,&$"(), -' #':


Waveform generation mode bits
WGM01 WGM00
%.($.#3"
"$"  )$*&
0
0

.&+-(' E/&$*' !0" $&' +(' (Phase Correct PWM)


0
1
M"($/ T/)(' * &) (CTC - Clear Timer on Compare)
1
0

.&+-(' E/&$*' !0" (PWM) ),' !)$*&' (fast PWM)


1
1

T bits 0:2 $&$*" ) % )$* ()$' &$!$$/ )' $"' )&(' )$* Timer0,  ( )$"
 .!$*$ %" :
Clock Selection Bits
CS02 CS01
CS00

0
0
0
0
1
1
1
1

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1

-(*,$" "#1,/ ,*,$(,5


A%"&$%$( M)&)
System Clock (CK)
System Clock, prescaler N = 8 (CK/8)
System Clock, prescaler N = 64 (CK/64)
System Clock, prescaler N = 256 (CK/256)
System Clock, prescaler N = 1024 (CK/1024)
E#-)& . &$!. ,&$"($/ ( )&,."$ )-%$)
E#-)& . &$!. ,&$"($/ ("&,."$ )-%$)

  5

  TIMER0


)#-!+,+&7)0. 0+) 3-+)&/0 /4 0+1 TCCR0, $ 0& 0+1 #0-$0 1*)#0& # /0%#- /13)50$0
f0 = FCK/N #0-70. %(#/)80 #.6 2- 0 #3,#+6&+-0 )#2 1 4/( 2- 0xFF. 0) + #0-$0. 3#& 0$) 0&
0xFF, /0$) /4. #,5#)$ 6*$/$, $ /$ TOV0 /0+) TIFR !)#0& 1 '& 01053-+) + #0-$0.
,-)#& 0$) 0& 0 (3.&/4&"*(1'). E) 0+ )0/0+&3+ interrupt #)& #)#-!+,+&$)+, #'0#(#0& $ ISR
1,#-3#(&/$. 0+1 Timer0, +,50# '& $ /$ TOV0 1050 !)#0& 0. H 0-3+1/ 0& 0+1 #0-$0
,+-# ) "& /0# '& ) #,)'%+-&/%# ) ,/ /0&!, /4 0+1 '034-$0 TCNT0.
-+'#&)+1 ) 12##2!1-3& 2- &2/'2! -'# ) #,&(*+1# CS00:CS20 = 0 /0+) '034-$0
TCCR0.
 
 "&3#-&/$ 04) "&'+,7) 0+1 Timer0 !)#0& /4 04) '034-$07) TIMSK '& TIFR.
Timer/Counter Interrupt Mask Register TIMSK
7

OCIE2

TOIE2

TICIE1

R/W

R/W

R/W

R/W

Bit
Read/Write
Initial Value

TOIE1

OCIE0

TOIE0

R/W

R/W

R/W

R/W

OCIE1A OCIE1B

TIMSK

Timer/Counter Interrupt Flag Register TIFR


Bit
Read/Write
Initial Value

OCF2

TOV2

ICF1

OCF1A

OCF1B

TOV1

OCF0

TOV0

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

TIFR

 bits 0 '& 1 0+1 TIMS #)#-!+,+&+6) (# (+!&'5 1) 0&. %(#)-. 0 Y.&/4&"*(1'0 M&2/'2! (Timer0
Overflow 0) '& T#72(1'0 3$)/(2! (Timer0 Output Compare Match OCIE0).  #2)&/$ 04)
"&'+,7) 107) /$0+"+0#0& (# (+!&'5 1) /0 flag bits TOV0 '& OCF0 )0/0+&3 0+1 TIFR.
(#)-.! .&/4&"*(1'0 M&2/'2! (Timer0 Overflow)
#0 )+)0. + #0-$0. ,5 0+ 0xFF /0+ 0x00 $ /$ TOV0 !)#0& 1. E25/+) '& 0 bits TOIE0
(#)#-!+,+$/$ "&'+,. Timer0 Overflow) '& GIE ('%+(&' #)#-!+,+$/$ "&'+,7)) #)& 1, %
#'0#(#/0# $ )0/0+&3$ ISR, # 0+ ,-. 0$. +,+. $ /$ TOV0 !)#0& 0 1050.

_

0
0
1
1
0
0

CS00

CS02

0
0
0
0
1
1

CS01

#"+)+1 50& $ /13)50$0 -+(+!&+6 0+1 &'-+#(#!'0 ., 5,4. ,-3#0& ,5 0+ ),01*&'5
STK200, #)& /$ # FCK = 4,00 MHz, ) /1,($-7/#0# 0+) ,-'04 ,)', ")+)0. 0$)
,-0$0$ ,-+/+3 /0+) /4/05 1,+(+!&/5 04) +)"4) 0-$/$..

0
1
0
1
0
1

Prescaler

13)50$0
+(+!&+6 Timer0

f0 = FCK/N

0
1
8
64
256
1024

-5)+. . +)"&.


6*$/$. 0+1 Timer0

4,00 MHz
500 kHz
62,5 kHz
15,625 kHz
3,9063 kHz

t0 = 1/F0
0.25 sec
2 sec
16 sec
64 sec
256 sec

-5)+. &. ,(-+1.


1,#-3#(&/$. 0+1 Timer0

T0 = 256/F0
64 sec
512 sec
4,096 msec
16,384 msec
65,536 msec

# /$ 0+1. ,-,)4 1,+(+!&/+6., /1,($-7/0# 0 '04%&:

2
};

13)50$0 -+(+!&+6 /1/00+. /0+ ),01*&'5

FCK = 4 MHz

13)50$0 -+(+!&+6 Timer0 # prescaler  = 8


-5)+. &. +)"&. 6*$/$. 0+1 TCNT0

f0 = FCK /8
t0 = 1/F0

1)+(&'5. 3-5)+. ,+1 3#& ,-(%#& #0 250 1*/#&. 0+1 TCNT0

T = 250t0 = 500sec

 5

! %! !  !& '   ( project ($& ! $  !" CodeWizard).
#( ! ! ! ! ,  #  '  !" " !!  
"  !" !"  !, ")! ! $' ! ).

#include <mega16.h>
unsigned char count = 0;
unsigned int ovf_count = 0;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{ TCNT0=0x06;
ovf_count++;
if (ovf_count == 2000)
{ ovf_count = 0;
count++;
PORTB = ~count;
}
}
void main(void)
{ PORTB = 0xFF;
DDRB = 0xFF;
PORTD = 0xFF;
DDRD = 0x00;
TCNT0 = 0x06;
TIMSK = 0x01;
TCCR0 = 0x02;
#asm("sei")
while (1);
}

 $ ! ' "   ! LED  !   1 !'.

     


       

...
if (++ovf_count == 250)
...
TCCR0 = 0x03;
...
5

 ! ! ' !   (3) "  !!   !", 


prescaler  = 64.   #  #  !  $'  ! CPU ;

     


       

...
unsigned long int ovf_count = 0;
...
if (++ovf_count == 2000*60)
...

   5

 $) #$,$  " '% *$& '+ Timer0  INT0, '" "#""  #' ' ,"(:
1.

+$"- -" '&'&%  ' '"($ '"( #$"$'"%:


    :  ' LED   &&',  " Timer0 # $"#" "%.

    : " LED7  "& 


  2 sec, &+ '"( Timer0.
2.

'. '% '" ,#' #"(   &(  "% &'" INT0,  &&,&' '!- '+
'&'&+ ()  (). +$&' ,' '" #$,$ !   ' '&'& ().

$"' ' &'" #$,$ &%  *$&"#"&'  '' flag '-#"( bit,  "#" 
$' ' '$*"(& '&'& '"($% '"( $"'.

#include <mega16.h>
unsigned int ovf_count;
bit flag;
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
TCNT0 = 0x06;
if (++ovf_count==250*2)
PORT.7 = ~P.7;
ovf_count = 0;

{
}

interrupt [EXT_INT0] void ext_int0_isr(void)


flag = ~flag;
if (flag)
{
PORT.7 = ~P.7;
TCNT0 = 0x06;
TCCR0 = 0x03;
ovf_count = 0;
}
else

{
TCCR0
= 0x00;
PORT.7 = 1;

void main(void)

}
{

// Port initializations
PORT = 0xFF;
DDR
= 0xFF;
PORTD = 0xFF;
DDRC
= 0x00;
GICR
MCUCR
MCUCSR
GIFR

=
=
=
=

0x40;
0x02;
0x00;
0x40;

TCCR0
TCNT0
TIMSK

= 0x00;
= 0x00;
= 0x01;

flag = 0;
#asm("sei")
while (1);

  5

 
    
  -  

   - 


AH 6
O  TIMER1
.&7(3,-0 $(#$2-0: )+'% *%
/%$12'/)$*60 3,&/%!2'0: ) "($% *%
         :

 _
 &" &',*" '"( $&'$"( ('"-  :
1.

&+ &' *$'$&' '"($% '"( *$" &' Timer1 '"( $"' Tmega16.

2.

A #'(! """% #$"$'&"- &+ $"(' . !(#$'&% "#. (ISRs).

  _


  TIMER1
O ATmega16 ' 3 !*+$&'"-% *$" &'%/#$'%,  '% " "&% Timer0, Timer1 
Timer2.  Timer1 * &2/'2# 16-bit ($.6 0x00 "50 0xFFFF) '"( "#""(  '$*"(& '   #$"&#& ',&"    +&, ,&"   "$&, ($)) &+ '"( '*+$' TCNT1. O Timer1
' #"% ( ',''%  '"($%, " "#"% $(" ' &+ '+ ,"(+ '*+$'. :
Timer/Counter1 Control Register B TCCR1B
Bit
Read/Write
Initial Value

ICNC1

ICES1

--

WGM13

WGM12

CS12

CS11

CS10

R/W

R/W

R/W

R/W

R/W

R/W

TCCR1B

T bits 3  4 "$"( ' '&'& '"($% (mode) '"( *$" &', +% !%:
Waveform generation mode bits
WGM13 WGM12
&/)%/$4#
 "  '"($
0
0
CTC-OCR1A: M &"- T-'&% ($' ( + ,$"  ' '"( OCR1A)
0
1
PWM "$% )&% & &(* ,''%
1
0
CTC-ICR1: M &"- T-'&% ($' ( + ,$"  ' '"( ICR1)
1
1

T bits 0:2 "$"( ' # '"( &'"% $"""-  '" '$' '"( Timer1,  & '" ,"("
# :
Clock Selection Bits
CS12 CS11
CS10

0
0
0
0
1
1
1
1

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1

.)+-%# #$2-0 -+-%)-7


A# $"#"& M'$'
System Clock (CK)
System Clock, prescaler N = 8 (CK/8)
System Clock, prescaler N = 64 (CK/64)
System Clock, prescaler N = 256 (CK/256)
System Clock, prescaler N = 1024 (CK/1024)
E!+'$, $", *$" &"- ('$*, " '+#")
E!+'$, $", *$" &"- ( $*, " '+#")

  6

  TOY TIMER1


$(&'&!2$+) +&$ .(&$!*+ */ +&, TCCR1,  +! +&, +(+ ,%$+!  *+ ( *,.$0++
f1 = FCK/N +(2+) %(#/)80 #.6 2- 0 #3,#+6&+-0 )#2 1 4/( 2- 0xFFFF (65535). H +(.&,* +!
+&, +(+ '&( $ '(&*'#*+ ! $$/*  (- $ '* *+! */ +&, 16-bit
"+./(+ TCNT1. +$&$+) + !*+ +!,  +&$ '0$& '#0 1%*) & +(+) %"!$! '0
+& 0x0000 '#! (,'(.#!*) "! $ +& *.+!"0 interrupt $! $(&'&!$&, "+#+!  $+*+&!.
ISR ! +$ ,'(.#!* +&, Timer1.
CTC-OCR1A  TOY TIMER1
+ #!+&,( CTC-OCR1A & +(+) +(! !("2) '0 +& 0 .(! +$ +! +&, 16-bit
"+./(+ OCR1A. +$&$+) *+$ $2++ ,+ +!, & +(+) %"!$! '0 +& 0x0000 '#! "!
-0*&$ +& *.+!"0 interrupt $! $(&'&!$&, "+#+!  $+*+&!. ISR ! +$ +1+!* *,"(!+
 * +&$ OCR1A +&, Timer1.
 
 $(&'&* +/$ !"&'2$ +&, Timer1 $+! */ +&, "+./(+ TIMSK.
Timer/Counter Interrupt Mask Register TIMSK
7

OCIE2

TOIE2

TICIE1

OCIE1A

OCIE1B

TOIE1

OCIE0

TOIE0

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

Bit
Read/Write
Initial Value

TIMSK

 bits 2 /) 4 +&, TIMS $(&'&!&1$ ( #&!"0 1) +!) !"&') Y'(.#!*) M+(+ (Timer1
Overflow  1), T1+!*) ,"(!+ A (Timer1 Output Compare Match OCIE1A) "! T1+!*)
,"(!+ B (OCIE1B).
(#)-.! Y.&/4&"*(1'0 M&2/'2! (Timer1 Overflow)
M+$&$+) & Timer1 '0 +& 0xFFFF *+& 0x0000 "! -0*&$ +& bit  1 +&, TIMSK $! 1,
!&,(+! +* ! !"&',  &'& %,'(++! '0 +$ $+*+&!. ISR -0*&$ $! " &#!"
$(&'&!$&) & .$!*0) +/$ !"&'2$.
(#)-.! #72(1'0 3$)/(2! A (Timer1 Output Compare)
+$ ,'(.! +1+!* +&, TCNT1  +&$ OCR1A "! -0*&$ +& bit OCIE1A +&, TIMSK $! 1,
!&,(+! +* ! !"&',  &'& %,'(++! '0 +$ $+*+&!. ISR -0*&$ $! " &#!"
$(&'&!$&) & .$!*0) +/$ !"&'2$.

_
1

-&1 "$+ +&,) ,'&#&!*&1), *,'#(2*+ + "+/ !: ('(&*&. *+!) &$) +(*)!)

};

 
  
 




FCK = 4 MHz

 
  Timer1 prescaler = 8

f1 = FCK/= 500 kHz

 

   
 1  TCNT1

t1 = 1/f1 = 2 sec

  
       Timer1

T1 = 65536 * t1= 0,1311 sec

 ,'&#&!*+ & (! 0) +/$ &$!/$ ,%*/$ +&, +(+, &! &'&) $+!*+&!.&1$ * 0,1 sec,
0+$  +! +&, prescaler $! N = 8.

Q = 0.1/t1=50.000

 6

 $)   #$,$   ' " ' ) &  ,%   "( LED,  & '"( "#""(
'' #$", +% !%:
  . '%, '"   " LED $&' &' & LED0 (, ' (#,"# LED   &&')
  0,1 sec '"   " LED ' ' '  & #$"% ' 
.


 '"   " LED $&'  &' & LED7,  ' ! &' & LED0.

 ' ("#"& '"( #'"- "( *$" "- &''"%  *$&"#" " Timer1 
prescaler N = 8  *$&"#". '% '   M! T!    + ,$" '
' '"( OCR1A (CTC-OCR1A).
+&: " interrupt '% CTC-OCR1A '"($% . ' +%:

interrupt [TIM1_COMPA] void timer1_compa_isr(void)


#include <mega16.h>
unsigned char count = 1;
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{ if (count==128)
count = 1;
else
count = count<<1;
PORTB = ~count;
}
void main(void)
{ PORTB = ~count;
DDRB
= 0xFF;
TCCR1B = 0x0A;
OCR1A = 50000;
TCNT1 = 0;
TIMSK = 0x10;
#asm("sei")
while (1);
}

// initially LED0 is on
//
//
//
//

prescaler = 8, CTC-OCR1A mode


load OCR1A value for 0.1 sec
initialize TCNT1
enable Timer1 overflow interrupt

 #' '" #$""- " #$,$ #$" "( o *$&'%  #"$  ' '" *$, "
#"( &"  ' ' & '"( LED '  &,  0  6 sec,     0.1 sec.
(!"+& '"( *$, "(   '  ' !+'$ interrupt  0   1 (*$&"#"&' ' '
"-&). . ' ,'  *$" , &' 0 sec,  ' & '"( LED  &'',  
!  # ,% " *$&'% #'& '" "(# #"(  '&'"* &' -!& '"( *$" "&''"%.

  6

#include <mega16.h>
unsigned char count = 1;
unsigned char N = 1;
unsigned ovf_count = 0;
interrupt [EXT_INT0] void ext_int0_isr(void)
{ if (N<60) N++;
if (N==1) TCCR1B = TCCR1B | 0x02;
ovf_count = 0;
TCNT1 = 0;
}
interrupt [EXT_INT1] void ext_int1_isr(void)
{ if (N>0) N--;
if (N==0) TCCR1B = TCCR1B & 0b11111101;
ovf_count = 0;
TCNT1 = 0;
}

interrupt [TIM1_COMPA] void timer1_compa_isr(void)


{ if (++ovf_count == N)
{ if (count==128)
count = 1;
else
count = count<<1;
PORTB = ~count;
ovf_count = 0;
}
}
void main(void)
{ PORTB = ~count;
DDRB
= 0xFF;
TCCR1B = 0x0A;
OCR1A = 50000;
TCNT1 = 0;
TIMSK = 0x10;
GICR
MCUCR

// initially LED0 is on
//
//
//
//

prescaler = 8, CTC-OCR1A mode


load OCR1A value for 0.1 sec
initialize TCNT1
enable Timer1 overflow interrupt

= 0xC0;
= 0x0F;

#asm("sei")
while (1);
}

 6

 
! !"  "
 ! -  !

  " - 


AH 7
 " "
4+=-9136 '-%'836: + .) ' ,!'
5)'78,5.'/<6 91+5)#8,6: +"$*& ' ,!'
      "    :

 _
 ( $ ()/,$ )$* &()&$* *)$0 ":
1.

(. () %+ "!$ 1" ()."  ()$" ,&(/ )$*' ( -+  $&+.

2.

(. () !)$*& )$* ))&$% analog to digital (ADC) )$*  &$! )
Tmega16.

"  _


  ANALOG TO DIGITAL (ADC)
H $" ADC )$* ATmega16 %&, ))&$%' '/5&(+.'6 10-bit. M(. %$!*%!#' %$& "
,&() ,& 8 4,)$6 '1'03)./3= 7%'836 ( -+$&  (single-ended) (*"($!$. A*)' (*"$") ()$*'  &$ )' )' -=5'6 A  !"$") ( (,(  )$ *" / )$*  &$ ) GND.
O ADC )." AVR ,&($%$ ) $$ )' $, ' %&$((' (successive approximation
ADC)  $%$ ()&) ( 7%' 5303).3= :531.73=.  " ,$* ))&$%   & 10-bit %/
)$" ADC, )$ ( ,&$"($0 )$* %&% " , (*,"/))

50 kHz < FADC < 200 kHz


 )$ ( $%/ *)/ *%&, 2+:;5.78<6 prescaler $ $%$$' %&, )$ ( ,&$"($0  )$" ADC
%/ )$ &$!/ )$* (*())$'.  " %!#$* )$" )!!!$ &) (*,"/))' $ $%$$'  '
1( )" !0)& *") ),0)) !)$*&'  )$" ADC,  $!$*$0 )" +2%6 *.'*./'7&': (i)
&$0 ) (*,"/)) )$* (*())$' FCK  )$* 200 kHz, (ii) %!$* )$" (.' !0)&$ %/
)$ %$)!( ($ &) (*,"/))'  (iii) $&$* "!$ ) bits ADPS2:0 )$*
),.&) ADCSRA.
  !  ADC
$ bit DEN )$* ),.&) ADCSRA () "&/ )$" ADC  !$ / 1. H ))&$% # "  !$ / 1 ()$ bit ADSC )$* ADCSRA, )$ $%$$ ") 0 *)/)  )$ %&' )' ))&$%'.
"!! ) , " )$ bit ADATE )$* ADCSRA " 1,  ))&$% # "  "&,/"$ ).%$
%!$0 )' %' ()$/)(', /%.' *) $&) %/ ) bits ADTS2:0 )$* ),.&) SFIOR.
M&' +8'8534% '4'.8+& 13 4'03=6 )$*  ()$) &$!$$0 )$* ADC   $!$ !&.( )'
()$$)) *)/)  !$ / 1 ()$ bit ADIF )$* ADCSRA. " , "&$%$ )$ ")()$,$
interrupt ()$")' 1 )$ bit ADIE )$* ADCSRA),   )!()  ISR $!$ !&.(' )' ))&$%'.
T$ %$)!( )' ))&$%' !") (. )$* ADCW ()" CodeVision C  "

ADCW = 210VIN /VREF


/%$* VIN  )( ()$" %!"$  &$ ) (/$*  VREF  )( "+$&'. T$ ADCW=0x000
")()$, ()$ *" / )' .(' (GND)  )$ ADCW=0x3FF ")()$, () )( "+$&' VREF
$" )" )( %$* ")%&$(.%0 1 LSB.

  7

ADC Multiplexer Selection Register ADMUX


Bit
Read/Write
Initial Value

REFS1

REFS0

ADLAR

MUX4

MUX3

R/W

R/W

"*# n (0 <= n <= 7) ! &


 (" %# n %# +"# ).

MUX2

MUX1

MUX0

R/W

R/W

R/W

R/W

ADMUX

"% $% bits MUX0:2  " %   

% bits REFS1  REFS0 !%  ! %# %$# ' "# VREF )# #:
REFS1

REFS0

0
0
1
1

0
1
0
1

   


&* $%  " % AREF
&* $%  " % AVCC  )%"* !&)% $% AREF
( ("$ ! %)
E$)%" %$ ' "# 2.56 V  )%"* !&)% $% AREF

ADC Control and Status Register ADCSRA


Bit
Read/Write
Initial Value

ADEN

ADSC

ADATE

R/W

R/W

ADPS1

ADPS0

R/W

R/W

R/W

ADIF

ADIE

ADPS2

R/W

R/W

R/W

ADCSRA

 bit 7 (DEN) $% "* %  ADC   * 1.

H %%" !  "' %#  * 1 $% bit 6 (ADSC).

 interrupt  ")$# %# %%" !# " ! %   * 1 $% bit 3 (ADIE)  $%  %% $% bit 4 (ADIF).  ! %$ %# %%" !# (+" &# 10-bit) % $% CodeVision
C $) % & ADCW.

&% % !  " %# %%" !# !" ! %   * 0 $% bit 5 (ADATE).

% bits 0 )# 2  " & %  prescaler  % " * (" $ + % & ADC, )# #:
ADPS2

ADPS1

ADPS0

Prescaler N

0
0
0
0
1
1
1
1

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1

2
2
4
8
16
32
64
128


_
1

' + !"% ! $% % &# !"%% &# &!  $ +#, $&!",$% % %):
FCK = 4.00 MHz

!# $      #

$ prescaler   !$    


 ADC  10-bit   

= 32
(4000/200 = 20.0)

!# !$ ADC

FADC = FCK/N = 125 kHz

# !#   

TADC = 13/FCK = 104 sec

 # 10-bit %  VREF=5Volt

V0= VREF/1024 = 5/1024 = 4.88 mVolt



7

N $ "  "' (, "  #  


 
 (0-5 Volt).
#include <mega16.h>
interrupt [ADC_INT] void adc_isr(void)
unsigned char i, outb=0;
unsigned int lim, adc_data;
adc_data = ADCW;
ADCSRA |= 0x40;

// Read ADC Word


// ADC Start conversion

lim = 64;
for (i=7; i>=0; i--)
{
if (adc_data>lim)
outb = outb | (1<<i);
else
break;
lim = lim+128;
}
PORTB = ~outb;
}
void main(void)
PORTB = 0xFF;
DDRB = 0xFF;

PORTD = 0xFF;
DDRD = 0x00;
ACSR = 0x80;
SFIOR = 0x00;
ADMUX = 0x47;

// Analog comparator disable


// ADC Voltage Reference: AVCC pin
// select channel 7 (PORTA.7)

ADCSRA = 0x8D;
ADCSRA |= 0x40;

// ADC Start conversion

#asm("sei")
while (1);

// Global interrupt enable


}

  " ! !" " VCC  5Volt,    " ("" #  % 
 (' #!   8-!' "# "("#;

  "!"% ! 5/8 Volt = 0.625Volt

N #!" o #( "& "# ) !" !"  ' (.

# ("   " ( "" !" ADC " !' ( '
 ),  #( "& !" ( 
Fs = 1/TADC= 9.615 ksps (kilo samples per sec)



7

  &  !"%%# # $% !"*" !"  & % ! %$ %# %%" !# % &
ADC  ! %     $% 8 LED % & STK200.
!&% *% % ! %$ %# %%" !# ( +" # 10-bit,   !"! % $&! 
  
  8       bits !" ' "#.

// ADC interrupt service routine


interrupt [ADC_INT] void adc_isr(void)
unsigned char adc_data8;
ADCSRA |= 0x40;
adc_data8 = (ADCW >> 2);
PORTB = ~adc_data8;
}

//  

  
//   8  
  

   "% *%% ! & !"( &%  & ! $ % &  %*%" &;

5/28 = 19.53125 mVolt

  & !"%") # $% !"*" $# ,$%   $% LEDs  ,% 
 10
 -  ADC,   % %# #  !" +!% )# $ # *" # %) 10 &%,
%"$) (("$ ! ,%# %# !"# 10-bit %# %) %"$)).

#include <mega16.h>
#define MAXN 10
unsigned char n = 0;
unsigned int sumADC = 0;

// index variable
// 1023*10 fits in 16 bits!

// ADC interrupt service routine


interrupt [ADC_INT] void adc_isr(void)
unsigned char adc_data8;
sumADC += ADCW;
ADCSRA |= 0x40;

// start new conversion

if (++n == MAXN)
{
adc_data8 = ((unsigned int)(sumADC/MAXN)) >> 2;
PORTB = ~adc_data8;
n = 0;
sumADC = 0;
}
}


7

You might also like