Professional Documents
Culture Documents
" "$ !$
" - "
_ _
( $ ()/,$ )." (. 1" *)1" ( (." ":
1.
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)* STK200 % !/&*( (SW0 - SW7) # )+##%
)*%+( !'%!*( *( 0'( D *%+ !'%"!*, #1 % ,.*%%% (LED0 LED7)
# )+##( )*%+( !'%!*( *( 0'( B.
_ _
CODEVISION C
!%"%+)* *# &'!*. !), # $% ! .* *%# &'%'* )/ *.# AVR )*%
#&*+$ !/ )0)* *%+ E')*'%+:
1.
2.
3.
4.
*# '1*) # "%+ # -') %&% )%+ *% +*/*% )0)* '0 )( '- !1#
&'*'.# (CodeWizard) & "%+ Yes.
5.
6.
!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.
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,-,,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;
DDRD = 0b00000000;
PORTD = 0b11111111;
while(1)
{
if (PIND.0 == 0)
PORTB.0 = 0;
else
PORTB.0 = 1;
}
1
2.
! & &! +"& &!'#$ &!' Board, ! !"!!$ #%& " %&! ,% &#!(!!%$.
3.
4.
"+ &! "%! +!' "!' (& %&! & After Make *& +&
##%! &! Program The Chip "&%& OK.
5.
"+ &! !, Project &!' CodeVisionAVR " & Make. & "+ !, (& "#'#! "#!(!#$ & % & % &!' compile &!' "#!#&!$.
)!' # %'&& , '"#), %& % &!' "#,#!' &! !'" Program.
&& "#"&*%, !#-!' & %&! - ")#!, &! Make.
6.
"!&$ &! !'" Program &! "#+# $ (!#&-& %&! #!& %* &$
"#$ ,#$ #) &% &!'.
1
*). " ,&($ )$" ")$%(. (+!)-" () !$ )$* 0 ', ) $%$ " %$&
-' " ")$%( $ compiler. #$$-)' $ %(' ()$ #$ . !,$ )$* %&$&)$'
((*%&+$& ( .!$*' )$*' *")$/' (*"*($/' )()(-" (.-"/#.-").
+") ).) $ 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
_ / _
1
_
- _
*%!&1() ()$" "/ ,1&$ )$" 1 %$* *!$%$ )$ )$0"$ )$* &.))$'.
1
!!, $
/%) SW2.
1 ' )$" $%$$ &-) %&$ "$* " " )$ LED3 /($ %) )$
( ()$" &/,$ )' while " &+$0" $ ")$!' PORTB = PIND. #() ) !)$*& )$*
%&$&)$' (%$&) " ")&#) () (!. 2 $!).
&+ %&/& )$ $%$$ " )$ LED0 %)1")' )$ /%) SW0 )$ ("
%)1")' )$ /%) SW1.
&+ %&/& )$ $%$$ " ) 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
" "
-
_
$*# $%*( # % & "$%" & &% + ')$ !"%") "+$ %#
& ! $# !, !" "%) ()"# % ("$ interrupts, !" & $% !*
"$%" & ! "# ' " ! $# ! & !'" ("$ %) interrupts.
_ _
1
//
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
: LED{0,1,2,3,4,5,6} %%& &! LED7 !% 0.25 sec
#!&& %&! "#+# %$ )#%!"!%& && flag &,"!' bit, !"!
#& & &#)!'% &%&% &!'#$ &!' #!&.
//
SW3
//
50ms, debounce
//
SW4
//
50ms, debounce
2
2
-
_
) !% )*/-% *%+ ')*'%+ +*%0 # :
1.
). )* !) )- )%0, +"%&%)( ! "-%+ ) &&% &'%)%%.)( ,'%1#
&%+ #* *.&%+# '" )* ! &'%"* &'%( &"+) *%# !'%"!*.
2.
') '%+* #1# &/ * ( *% ( " %!( &%+ &'-%#* *%# CodeVision C compiler.
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).
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;
PORTD = 0xFF;
DDRD = 0x00;
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
! !" "
! - !
_
&" &',*" '"( $&'$"( ('"- :
1.
#$"$-
2.
( &"" *"% &(&(. &+ '+ $. !+'$. "#. #"( #$* "
ATmega16.
# "#% #"( "( *$&"#"&"( $("( '% #('% #$'$"(%
'"($% ' +5/$64:7 +2+6)454&-8% '"(% "$" '% ' ' bits '+ ( +
'*+$'. #"( '&'"*"- & ('%.
2.
' &( * +2+6)454/4>+ 0'.41/0# 94 -;'2/8= 9<2 */'045?2, "#,' $"#""- '
, " " "#% #"( *"( $*"#" &'" #$""- " .
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
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
MCUCR=0x03;
:
MCUCR=0x02;
4
!& !)! $ '!# $ interrupt INT1 $ )% $%!:
$+$" $ ) $ % #%" #$ INT1, $%! $% !$
##$ # #$" " * $#$#":
5
#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;
{
//
//
//
//
//
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.
#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
# "
$"
!
! !" 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
-
_
( $ ().,$ )$* &()&$* *)$/ ":
1.
(- () ,& )&() !)$*&' )$* ,&$"() Timer0 )$* &$! ) Tmega16
2.
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 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
5
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
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
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
T0 = 256/F0
64 sec
512 sec
4,096 msec
16,384 msec
65,536 msec
2
};
FCK = 4 MHz
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);
}
...
if (++ovf_count == 250)
...
TCCR0 = 0x03;
...
5
...
unsigned long int ovf_count = 0;
...
if (++ovf_count == 2000*60)
...
5
$) #$,$ " '% *$& '+ Timer0 INT0, '" "#"" #' ' ,"(:
1.
'. '% '" ,#' #"( &( "% &'" 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;
{
}
{
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
-
_
&" &',*" '"( $&'$"( ('"- :
1.
&+ &' *$'$&' '"($% '"( *$" &' Timer1 '"( $"' Tmega16.
2.
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
6
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
1 TCNT1
t1 = 1/f1 = 2 sec
Timer1
,'&#&!*+ & (! 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 '"($% . ' +%:
// initially LED0 is on
//
//
//
//
#' '" #$""- " #$,$ #$" "( 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;
}
// initially LED0 is on
//
//
//
//
= 0xC0;
= 0x0F;
#asm("sei")
while (1);
}
6
! !" "
! - !
_
( $ ()/,$ )$* &()&$* *)$0 ":
1.
(. () %+ "!$ 1" ()." ()$" ,&(/ )$*' ( -+ $&+.
2.
(. () !)$*& )$* ))&$% analog to digital (ADC) )$* &$! )
Tmega16.
7
REFS1
REFS0
ADLAR
MUX4
MUX3
R/W
R/W
MUX2
MUX1
MUX0
R/W
R/W
R/W
R/W
ADMUX
% bits REFS1 REFS0 !% ! %# %$# ' "# VREF )# #:
REFS1
REFS0
0
0
1
1
0
1
0
1
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
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
= 32
(4000/200 = 20.0)
7
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;
ADCSRA = 0x8D;
ADCSRA |= 0x40;
#asm("sei")
while (1);
"
! !" " VCC 5Volt, " ("" # %
(' #!
8-!' "# "("#;
N #!" o #( "& "# ) !" !" ' (.
# (" " ( "" !" ADC " !' ( '
), #( "& !" (
Fs = 1/TADC= 9.615 ksps (kilo samples per sec)
7
& !"%%# # $% !"*" !" & % ! %$ %# %%" !# % &
ADC ! % $% 8 LED % & STK200.
!&% *% % ! %$ %# %%" !# ( +" # 10-bit, !"! % $&!
8
bits !" ' "#.
//
// 8
"% *%% ! & !"( &% & ! $ % & %*%" &;
& !"%") # $% !"*" $# ,$% $% 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!
if (++n == MAXN)
{
adc_data8 = ((unsigned int)(sumADC/MAXN)) >> 2;
PORTB = ~adc_data8;
n = 0;
sumADC = 0;
}
}
7