Bahman Molaee
BASCOM AVR
10 "
() * " 'SPI # $ % & 'AV VR
$ / 0.1 .+ !
76 . /
www.ebm21.blogfa.com
! 1 ? /
1
-$
+,
A
. / 0.1 2345 . $ = .
; <0
89 :
! @9
" #$ 6 .9 0; $
B
ECA &' () * +, 4 5 7 9 9 11 14 15
.............................. @C ............................... ............................... ............................... .............................. 9 E9 ............................... ............................... ............................... .............................. - $ ; H <I9 '# G ) 1. ............................... ............................... .............................. AVR 1 ? / @ 9 '* G ) 2. ............................... ............................... .............................. avr 1 ? / @ 9 ;) N9 .1-2 ............................... ............................... .............................. atmega16 ? / @ 9 .1-1-2 ............................... ............................... .............................. ATMEGA8 ? / @ 9 .2-2 ............................... ............................... ............................. ............................... bascom avr ;- ." 9 " O P9 '*.$ G ) 3.
15 ...................... ............................... bascom ;- ." 9 " O P9
1./9 ;) N9 .1-3
16 ............................ ............................... ............................... file ./9 .1-1-3 16 .......................... ............................... ............................... edit
./9 .2-1-3
.............................. program ............................... ............................... 18 .............................. tools ............................... ............................... 18 .............................. option ............................... ............................... 18 ............................. ............................... (simulator) T $ &0 O
./9 .3-1-3
17
./9 .4-1-3 ./9 .5-1-3 P9 ;) N9 .2-3
.............................. @ 9 ( 9 " O P9 ;) N9 .3-3 ............................... ............................... 23 .............................. stk 200/300 9 , $ .4-3 ............................... ............................... 25 ........................... ............................... SPI % & - $ () <$ '* 8C G ) 4. 21
.............................. SPI# $ % & .1-4 ............................... ............................... 26 .............................. ............................... B. 9 1 - U SPI % & .1-1-4 25
29 ........................ ............................... BASCOM O P9 31 31 34
SPI
/
@ .2-1-4
.............................. () <$ H ( 8V / @ .2-4 ............................... ............................... .............................. lcd / @ 4-2-1. ............................... ............................... .............................. - $ () * " ' V/ G ) 5. ............................... ...............................
www.ebm21.blogfa.com
2
ECA .............................. - $ ; 5 9 " .1-5 ............................... ............................... 54 .............................. 9T X W/ .2-5 ............................... ............................... 0 .3-5 61 .............................. ............................... ............................... 9 34
62 ............... ............................... ............................... ............................... Y /9 .6
www.ebm21.blogfa.com
3
www.ebm21.blogfa.com
ECA 10
$ / 0.1 .+
"
!
;& N9
76
-"#
Z39
-$6
I H " @9 !
0
+ $ 1.11.7.4 ]
+$
&? . /
I H " @9
- $;5
= .
I ;"
6
() * " T
( " avr
1?
"
.9 0;
89 : /
.
/
)\
@9 (
9"
() * " .9
;" & I
avr
1?
/
@9T
B
$
/@ 9 ;N$ 1
;- ." 9 "
-! ./7
A
; <0
I
bascom
T
+,
. / 0.1 2345 . $
[ N9 6
8
, ;9 H .5 basic
* "T 0
Y)
"T $
-$
/
$T /
.
@9*
P
1 () . $
avr
1 @9;$
* G )
0 ;) N9 ;- ." 9 " 0 0." ;
/
@
0
0
$
() * " *.$ G )
-$
1 9" ( "
.
G )
()
0
^ \.
.
! U.
<$ H ( 8V * 8C G ) . $
0 ;) N9 8"
^ \. ( " H . $ T
-$;
N
.9
. $
@ 7# G ) 8" H <I9
0 +,
%. 9 H . $ 0 _
-
0 ;$ T . $
www.ebm21.blogfa.com
4
H " @9 spi
1./9 %&
1 @9 0 _ ("
0 9
ECA 6
$ 9 W"
6
. $
)I #
1 . ;9 , ( " 9 .
&
T
/ ;9 ;B
+$ a
/ -1 1 ?
/
6
.U +,
H " @9 T
. / -1 ` $ @9T
$ Z N!
h W) ' (cpu) ( . /7
9h
i
"
;j -
&I 6 "T
.U 1 ?
@9
"
.
@9
$ ()
l 8/
! 6
g 9"
/" 9 ?
<$
1 9
- $ ()
1
?
/
$ 8"
@9 6
!
<$ ]1 . @9
.
$
/
$
$
0&9 .
T ." 9 H !
6
;1 , / 9T 6 ;
&7
! %&
$ ; .I9; ' basic ' c
BT
N .
B
.U
.
T
1
b9
k
/" 9 . I 9
?
1 () * " + $
@ 9 c."
@ 9;5
5
]1
81
@
/ 9T 6
" @?
T
"T $
7
. 6
1
@9
;j - 6 U.
'
(
0 O$. ? " $
/
g 9"
/
9" @9
b9 T Zn.5 .
5
.VI"
/" 9 .C ?
@9
T
- " m. 49
+ $ ]" /
1
" @? T
". 5
//
" $ ;- ." 9 "
( " ;+ <9 ;- ." 9 "
/
8.
0
m gU . b9
1 .P"
8
. b9
/ /@ 9 G 7 . b9
C
G
.U ; 5
. 0
$
@9
h W) ' (rom) 9 "
1?
( " j m.
. /@ 9 ;/ 0
/
@9h 0
'
g 9"
B
?
h "T $
8 -$6 $
/
(ram)
1
1 A
/ 0.1 H ! . b9
1
., 6
d <9 c ."
. 9 ; N/5 ; 7
@9
(" j
.U ) I
1 ;,e
. ;E+
;!
&I 0
;U /
$ /0 ; " T 9
; . H " @9 T ;@
)I
' . b9
%&
" @?
7
d <9 H " @9
/
- *. 7 T
.VI"
+$
&?
-,
] T
" @?
. $ ? $
$
.N5 $ I 1
. b9
$ 6@ 9 f 1 ? /
.C
T
.
` $
@9 $
("T
f
assebly
ECA $*
?
/
@96 8 A
$ ;
B
$ 6 # .$ $ + 6
o $* (" T " C
$
k
C
; + <9 c ." . /@ 9 ' atmel
9
0 avr c."
;- ." 9 "
W" .9 2 B 1T " 8
1 @ 9 ' 8051
$
c."
T
/ 9T 6
;
.!
1
T h !37
G
9 [" .U ;$
b9 6 0
0
8"
; /0 ;- ." 9 "
1 @ 9 /" 9
.U
Tr .
/0 & 9 avr
7
(
1?
j9
!
.VI" h + $
9"
1?
/
@ 9 .9
) , ; 7 [? U h A
0 2 49
H .5 avr
/
@9T
pic
.
c 0
8" T
1 @9
1 @ 9 8" m. c ." T ;@
.
6
+,
b9 *
. j c ." 8" T) hll
T6 8
;j - 6
T
#
1?
; 1 ;1 ,
9
$ A
/
.9 (n ^4$
@9
6
+,
/&9
?
(
1
ECA
. . . /7 0 .
/ ;) N9 "
/ ;9 G 7 H .5 6 ) , W"
$
T
W"
$
;"
;9
. T u"T *.$ .
6+
.
"
W)
V"
G9
("
9"
. .0 ;9 1
0
%. 9 #
* : 22 . ;9 G
$
. $ ". " W" .9 * : ;9 :
9T X
T
B H .5 6
! t<0 , . .I 9
= 7
- $ @/
.0 ;9
!
(, * : &?
$
-!
/
.
0 ) , W"
I" lcd
lcd
7
;" 9T T
G9 ] " 9T X
) xG $
9T X
eeprom
;/ &9 ;9 :
. ;9 delete
20 6 T
0 ) , W"
0 ),
NU 9
.
@ 9T *: 6
.0 ;9
$
7$
7 ,
T
1
, 9 / ;9 T
76
14'
0 ) , W"
0
9( 1 ".0 ;9
enter
$
NU 9 t<0 ` $ 6
96
&0
". " *
! ; <0 # a9
6 %&
! 14 " . ;9 a
v T
//
-$
0 &" #(/9
0;
512 ' mega16 @ 9 eeprom W)
$
W" .9 6+
. 0
&l T r
.0;9w
T
$
. ;9 .0
.0 ;9 0." eeprom W) eeprom
$
T N
-$
u"T
gj t<I9 X
6 v
$
B. 9
0 " . ;9 t<0
( " delete
P+5
5
NU 9 t< 0 #( /9 u"T
$ /0 . /7
s.)
1 I9 lcd
' /
-! 6
10
;9 $ " . ;9 u"T
9. . B
B
1 . ;9
NU 9
1
(9 ,.
//
@ 7
/1 ] " ( " ;) B T
P+ 5
".
9;
'
(lcd)
<I9 0 #(/9
!
0;
P+5
0 ) , W" 0
8
0 9
%. 9 H 73B .0
g9
0 ) , W" H 73B
1 :9 @9
@9;
%&
20 * : N
( " W) enter
B. 9
V/
6
$
9 T 6 20
/ 0
1
ECA W)
; 73B
? 6
. .0i
-! 6 T
exit
T
". 9 0
0 "
. .0 ; " $
0 0."
X
9T
. $
0
1 ;9
9"
6
//
1 ;9 ] " lcd
W/ spi
+$
9T X
%& T ( " @ 9
I"
9 " H 73B
W/
%& . #
. 1 ;9
. $ @9
I"
G 9 ;@ 9T X
(V9
0G5
89 : G
W) T
@9
. 9
20
$
0
9 1
1
. .I 9 ;! +
C
*: T r
led
8 . +$
down
6 T*
1
] " lcd $;
$
N
7
$
@9
led
B. 9 u"T
j T
1 u"T ] " =
89 : ] "
$ H .5 6
1 I9
^ P5 H .5
N * : 22
0
.0 ;9
eeprom
".
.0 ;9 2 49 # .$
V/ o
6 H .5
.0 ;9 * : 6 eeprom
W)
;
! 6 (j U *:
N *: '*: 6 -
N
. 1 ;/N '
0G @ T r
8
) ;9 +,
;9 9 H .5 6 1
. ".0 ;9 j @ 6 (j U 89 : ;I C H .5
www.ebm21.blogfa.com
/
7 $("
. *
T(" T
; -0
lcd
7 10 T V9 H .5
up
@96
9T ;- 0 X
0 ) , W"
. .0 ;9 60 $
W/ ]E" *
=
.U
" =
AVR
ECA
2
AVR % !"#" $ "
.
avr % !"# " $ "
9"
T
0G & #
; - ." 9 " U ;?. P Y!
.B
hll (high level languages)
7$
8" T . / -1 ; & 8
C.
atmel
1 @ 9 ; - ."
1
0.
"
1 @9;
E9 T $ / 8 ]1
12 4
0 y7
// ;9
/
1 n ^4$ @9
9"
37
(reduced risc insteruction set computer) ' risc
N $
1?
"' . &
" $ ;- ." avr
1?
/
8/
H
1 - U 32 T
. /0 ;"./ h + $ ),
!
+$
.9 1 avr (
1 avr . / -1 (isp) ( 7 $
//
?.
9" G !
"
-$;
0
) fz 9
eeprom
9G
.I 9 y7
-E { 1 . .I 9 ;@
-$ 6
0
atmel
9"
0;
v3
flash
V "
$
. 3 $ v3 ' H . $ G@ $
* V"
V
" .U
12 4 ( " 8" z 9
// ;9
1 @9
7 * V"
7
( "l
. 1 9
I" 1 @ 9 j
9
A.?./@
n
j
H
h W)
$
T
(1:1 & -") avr G@ $
1 @9T
.
. 6
. $ r" ) [$ / 9 . z 9 avr
.9
1 @ 9 ./7
- $ 7 $ ]1 y7 &-"
7
/1 9 * V"
1 W)
)H .$
/ ' .0;9
1 @9
@9
rics
B
. 3 $ v3 1 @ 9 a . / v3 T ) z3
$
basic
I
T h "
h .W/9
c
+ $6
N9 O $. G@ $ v3
+$ ;
8" T
.$
avr
G
-E 1:12 1:4 &-" 12 4 1 avr
N $ 0
+$
cmos
A.?./@
. 9) mips ] () T 1:12 1:4
8 &-"
. "
- E9
AVR
ECA
2
!"#$$ " $ ! avr 1:1
%"&'$$ ()
mips/power $* consumption avr +!&
1:
1 : 12
power
# a9 . B '
+9 hll
. 0
d N 9" 1 avr . $
.W/9
1 8/
;9 1 -U 6 T
U
ram
h +$ " $
;
/ -1 ; +$ ; eeprom
1 G@
.
tiny avr
". I
avr
1 @9
h W)
.
1 @9
H " @9 H . $
0
$ ' ".I 9
) I c. " . /0 ;9 ; &!
?. d <9
8? 9
1 @9T
+$
@9
6 T A
$
;.
. / -1
$
T b
6
10
;$ $ 1 - U 32 ;$ $
1 @ 9 89 H 5.
j T
# V
1 @ 9 c ." $ /C
I H " @9
! 9
8" T
/" .
1 A
8
c
! 1 /
';
$ &-"
1 :9
),
0 \7 T
+$
$6 . "
<I9 ' ; ) N9
./7
avr
B atmel z 1
OE) H .5 6
1 . // ;9
!' "
"
9T
/ -1 .W/9
avr
H " @9 6
U
; P9
Z E -9
1 -U
1 @ 9 ? c ." N
]1
16
%&
N9 ;
. 9 basic
T
8 1 -U 6
. / ;9 s ) ; " 1 ;,e !
.
; P9 : 9
T "'
0 G 9 alu
"
1
h W) T ;" @9 9 "
1 -U
+U $ . ".0
;&" U H " @9
$ $ :9 :9
G@ $ v3
6 0 '
T
TG
. .0 ;9 # :0
6
T 1 ;& $
consumption
!
}.? " # &9 &? avr
/0 ;
1 @9 1
/ -1 at90s / -1 megaavr c." avr
8 G ! ;? 7 H " @9 atmega8
j
atmega16
0 1 @9
AVR
ECA
2
atmega16 "# " $ "
; 5 .
1 () /
<$
$
0
+$
@96 T A
@ 9 c." 6 H 5.
;5
! V/
. "
-!
. . ".0 ;9 U G@ $ v3
T Z a
+$
v
6 0."
10000
!
(
6 0."
100000
!
(
N9 T • •
- U 32
•
;7$
•
16 mips
r" )
sram
•
9 " ' W) flash
9" G !;
9" G !;
avr risc
G N? . $ 131
; v
-$
n ;
) fh .
B
@96
;) 9 .
n ;
16 mhz
$ ;
0 G5 /@ 9
. & &
16kb
•
1024
•
512
•
h 9 " G+!
•
h W)
h W)
eeprom
h W) .
eerpom
h
flash
x+
jtag (ieee std.) jtag
%& ~ BT
lock bits ' fues bits ' eeprom ' flash
%&
h 9"
(
-. / compare compare
1 9
•9 (V9 prescaler ;
( V9 prescaler
! 9"
•
*+ , .)&)&)&( •
8 (timer/counter) "
•
16 (timer/counter) "
;
•
capture
#" 4
•
}.? " # &9 # " 8
•
pwm
;
10
# V
single-ended
200x 10x
11
tqfp
/
1x
6,# /
-
#"
•
;\+ #" 7
•
;\+ #"
•
AVR
ECA
2
;
master
slave
$
%&
!
•
%&
!
•
9 " G ! # $ usarT
•
% & G@
spi (serial peripheral interface)
H .5
(
"# " $ "
9 " G ! brown_out
(
•
9 " G ! watchdog
( (two-wire)
•
}.? " , - E9
0 12
*+ , . & & & •
power_ on reset circuit rc
.3 $
•
sleep ( adc noise reduction , extended standby , standby , power-save , idle
? 6
•
0 &? ;
down) , power
;U
+! Y /9
•
@ 7
•
7$ 6
;) 9 .
•
256c ' 3v ' 1mhz
;) 9 .
•
# N) ?
•
?
•
(interrupts)
;
l Z39 cmos
A.?./@ O$. n
atmega16l
1.1 ma (active mode) 0.35 ma (idle mode)
@
: power-down 1ua
(
);
7
?
1A ?
•
atmega16 l
5.5 v
2.7 v
•
atmega16
5.5 v
4.5 v
•
8-" ) atmega16l atmega16
www.ebm21.blogfa.com
12
8mhz
0mhz
•
16mhz
0mhz
•
A AVR
ECA
2
/ 9 " G ! (i/o) ;U
(
mlf
: $ 0 PORTD
0
I" T
PORTc ' PORTB ' PORTA
. $
@96
89 "
0 t<I9 T G@0
$;
1
MEGA16 @ 9
1
1
/
tqfp
44
0
•
40
•
44 ' pdip 1
. /1 9
[
1
- $ v3
.9
1
T ; €N
@9
0v
(erase)
; 9 * V" :
;5 +,
@9 ." j
.
@/
- $ v3
8H. 4
@96
@ 9;
0 . ".0 ;9
"
T. ). ...
W/ /" 9
* V" 8" O$.
G+! B. 9 G+!
1
T. ) 6 T *
$ 6@ 9 8"
9"
8 .9 ;
/
j
.
(
.5&)&)&(
/ -1 flash h W) T ; -! 8
@ 7 6 N ' ;U 1
[
; " @9
!
@9
%.4
O 32
atmega16 % 3 4 6+
H.56
i/o
- c ."
+$ ;
1 () 8 1
<$ % &
O$.
8"
$
T. )
. ;9 ;
T$
$." T
T. ) W/
0
EC
@ 9 H W/ ' . ;9 ;
".0 ; " v mega16
@9
T. ) . 9 bascom () * " O P9 . O$.
/1
@$
T. )
13
.C
, H .5 G9 ;1 ,
8
A AVR
ECA i
8 W/
0 8
2
T. ) G9 ^ \. . 0 T
" 8
T. ) (
T. )
@ 9 6 .$
W/
/N9
". 9 . 6
(
( " ;) B
I
T " Y! .9 I
9 " T .W W/9 . / ;9
. 1 H .5 6
f
$
1T "
0 (
(data sheet)
}.?
9 " ;/N 0 0 ; T. ) ,
" . ;9 I H 73B
8
9T
) , H .5 8" ‚ ) ]
. $ .
9"
T $ 8"
I" (
T. ) .9
9"
/N9
I ^ \. .
/
NU 9 @ 9
ATMEGA8 "# " $ "
H
<I9 .
$
/@ 9
H. . $; . $
OE) .
$
$
$ ;@
0
; I" T
1
MEGA16
@9
8 .
FLASH
$
7'CH.
MEGA16
8'D
B
8 .
&0 8KB
W) $
PORTD
14
1
"
T. )
. &
- H 8U T ( " @ 9 6
.9 = T V/
@96 T ; 0. $
MEGA8 @ 9
T
0
. / ;9 s )
@ 96 ;5
1H +
PORT TC ' PORTB
89 "
(" @96
BASCOM AVR
ECA
$
-
T
2+.
;9 1.11.7.4 ]
bascom
;- ." 9 "
simulator
$; [ " ;j".jC
1
g, ;1 j"
9T
;
; 7
6
$T /
P+ 5 '
/" 9
.
eeprom
+5
'O
r? h W)
".
O H .5 . j H " @9
!
;" 0 () * " 6
&0 () * " 6 [ " G ("v
&?
9
.0 ;9 [ "
I H (U h
V ' }.? " h //
6 0." ' 9"
H .5
U' -
T [ "G
;- ." 9 " h V/
15
~4/9 2+.
U "
- E9 ' adc
9
}.? "
V ' lcd ' 4*4
1 +! *
$ $ ; P9
.<?
bascom
. .0 ;9 1 x T H .5
,
1
; . H " @9 () * " 6 T $ &0 O P9
@9T
' sram
() * " 6 -
1./9
v
!
]< 6
m.
- ." 9 "
/ 9T 6
.7
1 @9h
1 () * " j
("
/@ 9
1 ;9
" H " @9
P
/ . .0 U ; $ .
# /j $
" ;$
+
b9 ;$
() * " 6 h "T
. /@ 9 avr
! V/
."4 8 9
avr
/ ;9 ;" & I
, G P .U
. ;
) *- . 3
bascom avr . T T.
!')
1 :9
1 -U *
: ' led O$. 1
.
."4 8 9 % !+$
"' . &7
[ "
@/ T r
9"
BASCOM AVR
ECA
!')
) *- . 3
file %+$ . & &7
T ' (file new) ]
U G ) V G &! T ; " @9
" ' (file save) G ) h (file exit)
.U T /
P
= '(file close) G ) 6 - ' (file open)
G )T i
(file print)
G ) /
1 9" h 1 0
./9
= G&! T G )
' (file print preview) G ) / . 1 9
!
edit %+$ . & &7
: / -1 T H .5
./9 6
1 / (, edit undo
.
6 T
9"
@$
. 9 / (, 6 edit redo
.
" ,
.
6 T
@$
" . 9 / (, 6 edit cut
.
1 #E "
U GP9
0m<" 69
" . 9 0 / (, 6 edit copy
.
;b
U GP9
0 m < " 6 9 . 9 / (, 6 edit past
16
BASCOM AVR
ECA .
" &-k W" .9 GP9
!')
) *- . 3
past
.
copy
Z3&!
;/ 9
" . ;9 0 / (, 6 edit find
.
/ .V -U
9"
;/ 9
/ . ;9 0 / (, 6 edit replace
.
/
9"
W" .9 6 9 6 (j U ;/ 9
" . 9 0 / (, 6 edit goto
.
;1 .<? O
7$
Z E -9
" . ;9 0 / (, 6 edit toggle bookmark
( , " I"
$ 0 W" .9
9 " T ;5 .
1U
"w
8"
" . 9 0 / (, 6 edit goto bookmark
.$
program %+$ .7& &7
:
; 89
1 / (, ./9 6
program compiLE
G b9 T G &! . ;9
?.
1 9 * V"
6 1 ( " f7
.
0m<"
-
option compiler setting
G b9 T
8 )
9"
. ;9 / (, 6
.0 ;9
= 9"
0 . ".0
. .0 program ?
/
@9
" . ;9
/ G )
xx . bin
. $ T " .9 bascom T $ &0
debug
G ) xx . dbg
. $ T " .9 avr studio () * "
object
G ) xx . obj
. ;0 (, G ) xx . rpt . $ T " .9 1 9 , 1. $
) â&#x20AC;&#x17E;
P9 4
4 *: 9 " T ;4
G / # $ (j1 G ) xx . hex
c ." T ;â&#x201A;ŹN
. ;9
0 0
9"
; 4
,
0
g9
. 0 1 . d!. 9 .
b9 w
program syntax check
17
BASCOM AVR
ECA ;9 0
.U ; 4
, . .0 ;9
!')
) *- . 3
C ; 39
4 60 "
0h 9"
/ (, 6 h $.
. 0 1 .<" V ; ) { 1 0 program show result
.
+$
. ;9 G b9 h V "
/ (, 6 T program simulator
&0T
". 9 G
H .5
. .0 ;9
+$ ; .
f2
T $ &0 / (, 6
/
+$
avr studio
I)
W" ( "
j
1T$
send to chip
". 9
0
V/ 6
8"
.0 ;9 1 x (
;$ $
9 " O P9 h V/
@ 9 ] ) h W)
8
T. )
f4
/ (, 6 O$.
' @9 .
( .
9" /
37 (
9"
tools %+$ .:& &7 terminal emulator
O P9 6 T .
/
" . ; 9 terminal emulator + $ . b9
n
rs â&#x20AC;&#x201C; 232
@9 6
ctrl+t
%&
1
/ (, 6 O$.
;)
;? $ h
] "
lcd designer
1 ] " lcd
.
;
B
.
W" .9
1
" . ;9 / (, 6 O$. graphic converter
] " ;@ ) , lcd
.
W" .9
1
1 r@7 . ;9 V/ 6 O$.
option %+$ .;& &7
h V/
.
/
8" ; .
/ m<"
" . ;9 .
j 9 * V" H W/
programmer
c."
(simulator) %6
. .0 ;9 1 x T V/
www.ebm21.blogfa.com
18
$
./9 6
" . ;9 option programmer -< 8 9
"' . &7
T $ &0 O P9
BASCOM AVR
ECA
!')
) *- . 3
: T
;9 O P9 6
1./9 ;) N9
9 run
. .0 ;9 T f
T $ &0
6
I) pause
. .0 ;9 T $ &0 !.9 d!. y7
6 stop
. .0 ;9 T $ &0 G9 d!. y7
6
step into code
Y . ;" .
) * j/1 . ;9
UO
O .
9" ;$
. 9 f4 8"
6 UG
9
I)
) 8" G run to
.
9 pause ?
rb$
1 ;9 * V"
0m<" O
T $ &0
(the hardware simulator)
. .0 ;9 T 19
()
T h V/
6
<$ T $ &0
BASCOM AVR
ECA
;9
+$ 1 led T
H. h .
lcd
;? $
1
0 ;9 T V9 lcd
. 1 9 led
. 60 getkbd()
". G ! 9 "
# 7 G ! adc d <9
) *- . 3
I"
1 h ?(/9
$
!')
8? "
1
I"
1 H . ;U
.0 ;9 `.@N9 . $
$
}.? " h //
V/ ; n
1 led ." . .0
6 N\
/ -1
0 &N ( " - E9
-!
./7 P+5
1 ( " }.? "
. $ E9 6 \ . $ registeres
r31
r0
1 -U
E9 . 9
1 ;9 ] " ; N)
E9 .
1 - U h V/
6
1 I9 # $ (j1 H .5 i/o registeres
. .0 ;9
+$
i/o
1 -U ] " vaeiables
enter
h
E9
" . ;9 6 /k 1 .
I) .
0 ;9 variables . $ 1.
9"
:9
:9m<" U
E9 h 1 I9 .
1
:
! 0
! 9" value
U * j/1
O$.
:9 1 local
1 :9
". ; " 0 . 1 9
I"
function
sub
.U.9 ; P9
1 : 9 local V/ .
/
)\ watch
20
BASCOM AVR
ECA !
+ $
.9 .0 ; T
. ) , 1.
! pause ?
!')
T $ &0 #3
) *- . 3
$
T $ &0 0 ^ P
!
; N\
W" .9
/ (, 6
N\
;9 j/1
, ;9 up
check
O$.
1 flag . 1 ;9
. ;9
I"
N\
-U
N\ / (, 6
.
N\
: 1 box interrupts
;
;U
;
1 +!
.&"
. # N) . 1 ;9
"
I) .
, ;9 1 x .0 ;9 m < "
programmer
1 x T h V/
G
9 @ 9 ". "
T ;@ H .5
0
; .5
+! Y /9 / (, 6
. $
1 I9 G ! V/ 6
%=2>
run programmer
b9
I"
."4 8 9
;9 j/1 @ 9 .
"' .7&7
9 " # $ h V/
6 1 . ;9 ( " f4
0 G@I9 programmer .
@/
/ ; $ /0 : .I 9
21
BASCOM AVR
ECA
!')
. $
) *- . 3
0
I" (
9 " O P9 h V/ G@0
: T / &7 O P9 6 ; 5
1./9
file
$ ;" 9T
OE) / (, 6 .
lpt
/ .
H.
/ ;9
1
+$
./9 : test
" . ;9 0 / (, 6
sample electronic programmer
T
buffer
0 ./9
buffer clear
. / ;9 v
)
/ (, 6
load from file
.
(
9"
@ 9 h W)
"
; )
)
. ;9 / (, 6 save to file
.
=; )
)
. ;9 / (, 6 O$. chip
./9
chip identify
22
BASCOM AVR
ECA
.
!')
) *- . 3
; $ /0
9,
G 9 @ 9 . ;9 / (, 6 write buffer to chip
.
(
@ 9 eeprom
9"
rom
h W)
)
. P9 . ;9 / (, 6 O$. raed clipcode into buffer
. ".
@9
h W)
. ;9 / (, 6 blank check
. / ;9 t<I9
@ 9 h W)
. ;? erase
eeprom
. / ;9 v
9"
W)
. P9 / (, 6 verify
- H.5
/ ;9 - E9
$
0 (
9"
@9
k"
)
. P9 / (, 6
] " verify ok * :
. .0 ;9
auto program
. /@ 9 program
@9 reset
. / ;9 $ . ;N$ . 6@ 9 H
(U
V"
( $
9" ( "
H .5 1
.0
. 0 1.
G 9 @ 9 / (, 6
T. ) . ;9 (
0 W/) 5 ( " 1 V/
. 0 ^ I ( ) * " ; O P9
- () * "6
8
9,
1./9 T
9 " O P9 6 1
N ^ \. T
^ \. ; 5
1 V/
U. 9 () * "
+,
1./9 $
9T
0
.
I
. $ stk 200/300 " "@ "A ?,
0
() ' .0 (
m. ;9
19, +$
<$ h $
9"
@9
T ;@
@9 (
9"
$ ;9 W"
" $ G@
.0 (
~ BT
.0
/ ., ;9 (programmer) 9 , spi
" $ %& T
9"
!
$
@ 9 ;E B
. b9
1 ;9 * V" stk 200/300
.:&7
@ 9%& 6
9,
$ / 9T 6 . /
23
BASCOM SCOM AVR
ECA option
. /9
/ ;9 .
!')
) *- . 3
+ $ 9,
/ m<"
stk200/300
. ". ; 9 .
8 .U
9 . .0 ; 9 G5 AVR
;0T.99
8
6 T ,
. b9
lpt
$w
24
/C
H.
I G
.9 6
89 @"
c." programmer m < " h / (,
9,
STK 200/300 PROGRAMER
www.ebm21.blogfa.com
$6
9
9
. 9
programmer
$ T
9T
+$ w
T-
74H H C 2 44
) T
6
+$
SPI 0 12%"
ECA
%"# " 3
4
SPI B -C>
>= ?
>3
.:
SPI 2" B -C> . &:
% & SPI
1
.P"
@9
% & G@
T
6
@/
$
.
0
$
+$
@9
T @/
" $ ;B & G@
U. T "6
A / //
. $ @/$ # $ ;B & G@
SPI (SERIAL PERIPHERAL ICTERFACE)
!
j @
j
1
" $ %& .
/@ 9
$
AVR
1 @ 9%&
1 6 ( " ;) B T . .0 ;9
+ $
T
1 - U ;) N9
* , T
1;
. $
AVR
-@
&7 1 @ 9 c ." MEGA16
.
# $%&
c." 6
(
+$
$n
G@
@9 .
!
0
". 9
/ -1
6
7$ %&
9"
%& 6
$
%. 9
1 -U
SPI
Ga9 @ 9 ". "
%. 9
H 5. (3-WIRE)
$ $ (SYCCHROCOUS)
9( 1 h
SLAVE MASTER,
8 .5
LSB
MSB
(
9" G !
7$
#$ * @ MASTER
h 6
// # $ ),
SCK
v3 ; U . /7 ! SPI h
h
@ 9 +, . 9 -U G
MOSI (MATER OUTPUT SLAVE
h
#$
MASTER
25
. $
W" .9 h SPI
T 1
. .0 ;9
W" .9 ?
SLAVE
•
#$
•
1
•
h
?.
•
C
•
0
1
U.
SLAVE
v3
? 6
v3
MOSI
%&
h +!
? T
' SPI % &
•
FULL-DUPLEX
#$
/ ;9 G 7 c 0 CPU 0i
9T
ICPUT)
SPI 0 12%"
ECA (SPIF)
. ;9
#$ SLAVE
. 0 ;9 d? <9 1
+!
T G@0 8U SPI
;
8 -U
c.\.9 6 . ) , W"
#
1
4
Y4! SPI v3 'MASTER O$.
C
MASTER
%"# " 3
+0 ;
H.56
. .0 ;9 U +! h 9 "
16 ;I C - U SLAVE
. 9 ' .0 ;9 # $
v3 8 #. B
.
# E"
8
SLAVE/ lsb
master
BIT
#
MISO MISO
SHIFT
8
;" 9T. SLAVE
MASTER
slave
BIT
./7
T
MASTER
T B msb
.0 ;9
+0
MASTER
. .0 ;9 ‚.7 1
msb
G9 # E " T N
T SLAVE
lsb
SHIFT
MOSI MOSI
SCK
SPI
SCK
CLOCK SS
SS
VCC
/> SPI B -C> . & &:
D+4" % !"
SPI #" $ "
. . 0 ;9
/0 SPCR * " . $
/ ;9 t<I9 0 t<I9
7
6
SPIE
SPE
5
8 4
SPI
T* 3
% & ;? / d x G5 1
T 2
. ; 1
0
DORD MSTR CPOL CPHA SPR1 SPR0
26
/> . & & &:
-U 6 8 -U 6
SPI 0 12%"
ECA 0 # N)
$ $ h +!
;5 h 9"
%"# " 3
4
,. $
SPI
. 0 1. 1 "., 1 * V"
;
&7
0 1
6
SPI
6 ;
h +!
# N) T N
SPI
?
wT
$ 6 h // t<I9
LSB
$)
0 1
6 ,
. .0 ;9 6 ,. $ . . 1.
SLAVE
. 1. n
SPI
@9 SCK
SPI
?
h
%& %&
0 1
0
6 ,
+5 , SPI
'
$)
MASTER
.
@
SLAVE
0
^ \. 9
6
wT
MASTER
MSB
0
+5 ,
SPI
?
%&
SCK
h
SPI
h 6
h
# E" N \ O E) /
d <9 "
U. 89 h @" . ".0
/
;IE" CPOL
V/ @
POLARITY
1 9 . ".0 ;9 t<I9 CPOL
# $ h .P"
H .5 6
27
PHASE
CPHA
. $
f
SLAVE
. & & &:
[
;? /
O$.
I" T # U @
?
c." 8C 8
0
. / ;9 t<I9 SPI MASTER
9
- CPOL
(CLOCK PHASE) – CPHA
6
U.
.I 9
0 1
SPI (DATA MODE) B -C> % "4 C EFD % !
0 ;9 .U.9 T # U ~&B # $ h
7
6 – MSTR
(CLOCK POLARITY)
?
H
.0 # $
h // t<I9
. . 1. 6 . $
SPI
6
(DATA ORDER) – DORD
6
H .5 6
0 – SPE
# N)
. 0 1 6
6 -SPIE
h +! h // # N)
? 1 @9
SCK
$
SPI 0 12%"
ECA
%"# " 3
4
SPI MODE
CPOL
CPHA
SHIFT SCK EDGE
CAPTURESCK EDGE
0 1 2
0 0 1
0 1 0
FALLI5G RISI5G RISI5G
RISING FALLING FALLING
3
1 1 SPI ; 73B ;B &
FALLING 1 9• m < " # U
MASTER
6 . // ;9 6 N
SCK
. 3 $ v3 r" )
SPI
6 %&
RISING
6 – SPR0
v3 r" )
" " SLAVE
T# U
@ 7 .
/ ;9
SPR1
SPR0
SCK FREQUENCY
0 0 0 0 1 1 1
0 0 1 1 0 0 1
0 1 0 1 0 1 0
Fosc/4 Fosc/16 Fosc/64 Fosc/128 Fosc/2 Fosc/8 Fosc/32
1
1
1
Fosc/64
U.
8
.0 ;9
1 I9
spi v3 r" ) m < " # U
SPI ? 'G "
. ;
8 l
SPI2X
. 3 $ r" )
@ 7
SPR1
/> .7& & &:
/0SPSR (SPI STATUS REGISTER) * " :
$ T H .5
7
6
5
4
3
2
1
0
SPIF
WCOL
-
-
-
-
-
SPI2X
28
-U 6 8
SPI 0 12%"
ECA SPIE
-U
; .5
%"# " 3
.0 ;9
6
. 0
0." SPDR y7
-U
N\
# E"
-U T
(SPDR)
-U
0 # N)
". 6 ?
6
6
.0 ;9
! SLAVE
•9
@9
T ; €N
6
$ $ +!
.
9T
8
SPI
h
)
-U 6 T
;" 9T . .
.
, ;9 * V"
8
FOSC/4
SPI
$ ;/ 0."/;" " . SPI
`
. .0 ;9
v3 r" ) 6 I
/k 1
.U
AVR
1 @9
"
/> .:& & &:
-U
. ;
-U 6 ".
-U
7$
" . ;9 SPI v3
- $ v3 d "
/0 SPDR (SPI DATA REGISTER) * "
.0 ;9
/ ;9 # $
0 +5
( SPI v3 r" )) v3
•9
] ()
$
# $ ".
.0
6 –1...5
SPI 0
/" 9 ( " - U 6
FLAG)
WCOL
h(U 8
.
j
- U ) SPCR
WCOL
. .0 ;9 6 /k 1
0 T
MASTER
+,
( SPI ;? /
.4" 1 – SPI2X (DOUBLE SPI SPEED BIT)
6 *"T
$ ;/N9 6
@ " 9T – SPIF
#$
.0 ;9 SPIE
; $ $ rb $
$ t<I9
0*
, – WCOL (WRITE COLLISIOC
9T
. / -1 0
4
-U 6 8 1 -U
6 0." . .0 ;9
+0;)
)
+$
.U.9 h
BASCOM 8 9 > SPI % $4 " A . & &:
/
@
()
‚ )] 1
T
<$ 1 1 . ;9
SPI
() * " H .5 1 .0 ;9 () * "
/ /
29
@
1
()
@ O$. 9 .
. ;9 () * " 6 O P9 <$ H .5 :
%& 8"
;" 9T . . ; "
" 9
SPI 0 12%"
ECA ()
SPI
<$ •9
%&
9 A
%"# " 3
6
. .0 ;9
. /
4
SPI
d N SPI
@
%&
1
./7
@9
T . $ O$.
/
@
COCFIG SPI=HARD , ICTERRUPT=OC|OFF , DATA ORDER =LSB|MSB , MASTER=YES|CO , POLARITY=HIGH|LOW , PHASE=0|1 , CLOCK RATE = 4|16|64|128 , COSS=0|1
. $ . .0 ;9
+$
1. # $ YES
/(
0
()
<$
/
OC
/ (, T
SPI
%&
MSB
rb $
LSB
LSB
rb$
MASTER
-1
@
.$ +! T
BASCOM
COCFIG
: DATA ORDER=LSB|MSB
MSB
.0 ;9 # $
.B
: ICTERRUPT=OC|OFF
+ $ H .5
' LSB m < " H .5
.$ ;
MSB
.0 m < "
,
0
; @ 9 , : MASTER=YES|CO
;- ." 9 " #
CO
. .0 ;9 m < "
SLAVE
/ (, 0
,
. .0 ;9 5. +5 m < " : PHASE=0|1 0 n v3
$
@
SPI
?
1 .< , : POLARITY=HIGH|LOW
;" 9T
. .0 ;9 m < " r"
)
1/12 ,1/64 , 1/16 , 1/4
". 9
SPI
v3 r"
)
HIGH
// t<I9 : CLOCK RATE . 0 ;9
.
/ m <"
1 .0 V
SS
1 . ; " MASTER ?
# /j $
W" .9 SLAVE
. / 6
-$
;/ 9T : COSS=0|1
() * " H .5
: 0." ( " H .5 6
/ (,
()
? 6 <$
/
@
. ;9
COCFIG SPI=HARD
'
POLARITY =HIGH
.0;9 .
$)
MSB
# ‚ )]
H.5
? 6
clockrate=4 ' Phase =0 ' MASTER =YES
".0 ;9 ) , W"
spiinit >+
@ T
N . $ 6 . ".0 ;9 . .0
initil, spi % &
0." [$ /9 8U 30
0 0
+$
,
. & & &:
1
. $ 6 O$.
!
;-
spi
/
SPI 0 12%"
ECA
%"# " 3
4
spiin >+
. & & &:
SPIIC VAR , BYTES
;9
!
0d N
:9G
spI
0 )
byte
` T
N
. $ 6 O$. .
SPIOUT >+
,
.7& & &:
SPIOUT VAR , BYTES .
.0 ;9 # $
SPI
`
BYTES
W" .9
%> = ?
N
. $ 6 O$.
= 3HC % $4 " A . &: lcd % $4 " A . & &:
1
.
/
/
@
()
<$ … P? T " . ".0 ;9
/ G5
/
@
lcd
@9
T . $ ~&B @ 9
@" lcd
#
config lcdpin=pin , db4=portd.4 , db5=portd.5 , db6=portd.6 , db7=_ portd.7 , e=portd.1 , rs=portd.0
;
4H.5
H.
T
1 . ;9
1 lcd c. " T lcd
$ "
! @9
$
8 . ( " G E9
c. " T
1
$ . $ (" 0
+$
Lcd
4$
7
$ ;/N9 6 .
$
G 9 lcd
T 9 9" 6
db7
8 .
$ t<I9 .
lcd
lcd
db4
0
+$
@ 9 T ;1 .<? 0
g9
V/
/ -1 16*2 c." T t<I9
/ (, 6
1
. ;9 T . $ ~&B config lcd=16*2
; 9 ; ) N9
H . $ 6 6
89 9
$
0 ) , W"
; .$
lcd
.
/ lcd
. $ 6 ;
* ) . 1 ;9 ] " lcd
:9
l H &7 /C
.$ 6 . $ T H .5
: 9 ] " LCD
www.ebm21.blogfa.com
31
lcd x
SPI 0 12%"
ECA
%"# " 3
4
/C
lcd “salam”
] "
cls
. / ;9 v
] " P+5 G . $ 6 cursor
' (on) 6 0 H . 5
" . ;9 " . 0
@9 .
lcd
W/
(no blink)
"
@9 . ;9 . $ 6 O$.
IC ' (blink) T
("
IC ' (off) w.9
cursor on/off/blink/no blink home
. 1 ;9
!#
.$#
4$
"
@9 ; 8/
.$ 6 locate
.
]
"
W" .9 : 9
lcd
)
T ;". $
4$ 1
. ;9 . $ 6 O$.
: $ T H .5
.$ 6 ; *) locate x,y
// t<I9 y
. $ .$
4$
// t<I9 x shiftlcd
: 1 ;9 # E "
$
†C
] " P+5 G . $ 6 shiftlcd left/right lowerline
.
()
<$
$
./7 :
/ ;9 G5
;9
6
+ $ 4 *4 @9
T G@0 ~&B
32
4$
"
4*4
P+5
P+5 P+5
@9 . $ 6
/
T 9 A . .0
@ 6
/
@
SP PI 0 12%"
ECA
@9
:
/ ;9
%"# " 3
;-
9
4
P+5 #
+$ T .$ T
.P"
P+5
()
<$
/
@
#
COCFIG KBD = PORTX , DEBOUCCE = VALUE , DELAY = VALUE DEBOUCCE
9
. .0 ;9 G5
( " DELAY .
O $.
".
0
0
9T
P+5 255 $
E9
$ ( 9
// t<I9 PORTX
@9T ; . ". ; 9
$ 20 ‚ ) ] H .5
" l ; 9 [-
// t<I9 . .0 ;9 V
$
GETKBD ()
.$ :# a9
COCFIG KBD = PORTA , DEB BOUCE=20 , DELAY=100
: X
;\)
T j"
:9
0
I)
&9 : 9 . " j 9
16
.
G5
@9
x/ 9 7 0
7 I"
P+5
".
33
T H .5
".
.$
GETKBD()
P+5 @ 9 . $ 6 O$.
I)
; &! G@0
: . /1 .
P+5 T
U.
;" 9T . $ 6 . 1 ;9
!
BYTE
c."
# . .0 d N
GETKBD()
.$
x/ 9
1 7
%"# " 8 ) 5
ECA
>=
".
H$A
.;
*
V/
.
87
. :
! mega16 @ 9 G
; 5 ]E" @ 9 6 1
.I 9 ;) N9 @ 9
- $;5 9"
<9 ^ \. 9 " 6 bascom
$ H .5 6
."4 . &;
.9
1 . ;9
9"
1; * )
O P9
$regfile = “mega16def.dat”
. . " 6 (j U ( "
j
I9 @ 9 1 . 9
: $# -
r" )
1
.I 9 ;) N9 @ 9 c." . $ 6 ~&B
.I 9 t<I9
- $ v3 r" ) 9
$crystal = 8000000
6 (j U
j
" $ r" ) 1 . 9
$
(hertz)
( 1 [-
0 0." r" ) . . "
spi
% & rb$ .
lcd
P+5
/" 9
/
@
()
<$ H ( 8V 9 . $
mega16
@9 . 9
@9 $
0
t< I9
$
0i
V" T
+$
@9;
9T T r
0 ) , W"
watchdog
W/ : .0
T
/@ 9 ) /
'
,
v3 ;
@
@ +,
() * " reset
.I 9 y7
6
/
exit
P+5
-1 9 " T ; -! 1
0
#
-$
7
", U .3 $ T
watchdog
. .I 9 $
0
9T
?
?
watchdog
-$
0
config watchdog = 16
& N9
E9
$
" l ; 9 16
0 $
' 64 ' 128 ' 256 ' 512 '1024 ' 2048 : T / &7
E9 6
b$
E9 . $ 6 ~&B
. " 6 (j U . ;9 ( "
j
. " l ; 9 32 c 0 . $
/@ "
stop watchdog
.$
c 0
0
/
@
watchdog
.0 0." start
d!.
watchdog
. .I 9 $ 34
OE) . $ 6 .$
+, watchdog
reset watchdog
%"# " 8 ) 5
ECA t<I9 H .
8U . :
0
6
/ . ;U
* V"
6
", U( "
pinb.2
/ -1
. 9 @9
H .5
8/
@9
8 . T
1
8 .
/
@
/
U. ,
'
d N
config porta=input
1
config pinb.2=output
;U
./7
;U
. /7
6
/ G5
/" 9 @ 9 led
@9
8/ T
3a9
1 . 9
1 .< 9 ; !
$
. .0 d N "
"
bascom
87
9 " ]E" 6
O P9
:9
;j - 6
89
;) N9
/0
/ -1
. .I 9
1 : 9 9"
W/ 8" ` $
= eeprom W) T ;5 `
.
"
/&9
" 9
.4"
9"
1 . ;9
0 ; + <9 c ." /" . 9 1 : 9
. string ' single ' long ' integer ' word ' byte ' bit /" 9
;
1;5
.0 ;9
j9 .
dim
+$
.$ T
:9
E9
: 9 . 9 ( " eeprom
d N
byte dim a as dim u(22) as eram word at &h1bd 44
22) W)
#. B
.
$
N
u(22)
7 0
:9
$ (j1) h1bd `
(#
$
$
0 ;) N9
c." T
0 ;) N9 eeprom . $
gosub
. $ O$.
//@ 9
: /" 9
@+ 1 T d <9 ;9 $
.U
return
. $ ( " 9"
8&-C O$.
I,
w
TT
:9
( word = eeprom
0
", U
a
1 9" 8"
T . 9
gosub eprom_read
@9
V/
$ 9 " ; 5 [-C
1. < 9 . C
1 T G &!
!a :9G
j9 + $
T* :9 1
b
* "
1
T 6 E9
.U
j
76 :9T 6
/ . .I 9 " 9 . .I 9
35
W/
(lable) again
.0 /
1 `$
] " lcd
[-C
P+5 T :
7
P+5 0
I)
9" $ W /9
1 [$ / 9
@ 7 E9
0 * " 9 " ~&B
%"# " 8 ) 5
ECA .
+5
E9
9"
1 :9
;/N 6
. " 0 + $ c*"
:9T 1
) , W" .
u"T $
: .<?
eeprom
89
.I9 * : 6
p
0 ; "
0 T
//
. + $ 6
wait
) , W"
0
/@ 9 V 9
"l
8&-C .I 9 $
$ @/ T N
enter payam
*:
.B 9 " . /
.
1 : 9 O$. H W/ lcd
/ ;9 t<I9 :96
. 9
9( 1
u"T
W/
E9
. 9 bascom O P9
$ .$ T
.$ 6 ; *)
16
$
W) ; N) `
] " lcd
:96
N
next1
j 9 H .5 p1 , p2 , d
.
8j"T
(" eeprom
$
"
next10
0 T B. 9
.I 9
V/
: " .<? .B 7
G
9. .B
E9 ! 14 T ]
9"
0 T
0
!
"
t<I9
] " lcd
: 9 . .I 9
E9 @/
; 1 : 9 (U
N
9T
P+5 T
NU 9 t<0 T
W/ ;P P 5 H . 5
8j"T
0 +5
-!
E9
0
9;$
( ,*:
1 .< 9
8 .
0 ? ;1
-!
W)
1 : 9 T ;@ . .0
1
(, * :
. 9("
0." @ 9 ; 9"
T
$ +5 .0 ;1
.$("z :9
0 * V" 9"
6
V 1
.0
V
" l ; 9 ' " l @ 9 [-
; 1 : $ H .5
waitus 10 waitms 10 wait 10
. $ end if
if
1
10s
0 V 0
+$
if
10ms ' 10us
.$ T . 0
spi
% & O$. 9T X
9 " T [-C 6 H 73B
rb$ . ".I 9 0." eerpom G
mega8
rb$
.I 9 w @ 9 T ;? $ " j9 36
I 0
1
# a9 ./7
9 " ;B 0 H . $
V/ V
.U ;B 0 . $ 6
spi_inter
8
! B. 9
[-C
enter
$
/
0
1 :9G
./7 T
@ G&! T 0 )
"
%"# " 8 ) 5
ECA .I9 .I9;b
) , W"
*
u(22)
22
;
0‡+
H 73B
".I 9
= eeprom
@ G !
-$
eeprom
`
6
T
~&B
g: Y4! 6 89 :
/
.
$
@/
eeprom
37
( "r :9
. I 9 y7
6 0."
".I 9 .U
W/ `
;@
T
W) `
V/
=`
6 T N
E9
6 U
.$
U.
+,
.I 9
8j"
. /0 ;$ $
89 :
V"
.$
( " 8$
. t<I9
( " 9"
6
. /@ 9 eprom_read
.0 w eeprom
@9;
1 9 ^ \. # a9
6
= W) 6 1
- $ g: ; !
:9
E
. c." T
0
gosub eprom_read r
case
+$
.I 9 y7 G 7 6
.U ; 5 . $ :
0
eeprom
W)
^ P 5 H .5
. I 9 Y 4! ;" 8, " . B "
$
!p :9G
G
. .I 9 W)
-
g: Y 4! T r ;
@/ 9"
1
.
W)
".
j
$
b , p writeeeprom readeeprom b , p eeprom b
+, . 7 .
" . 0 U. 9
[" .U 9
:9 8" $
"
. P9
." 9 9 "
!
W) T
eeprom
. 9 9 " ;$
0 ;N$ … P? 1 T ;? . 9# 6 . $
0
37
p
W) T
.U 9 " $ G9
5
T
p
: 9 6 0."
`
"
("
j H (U
5 9"
. .0 ) , W" -
b
`
".
9" 6
d <9 O 0
- $;5 9"
.0
N P+5
ECA
%"# " 8 ) 5
$regfile = "m16def.dat" $crystal = 8000000 Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = _ Portd.7 , E = Portd.1 , Rs = Portd.0 Config Lcd = 16 * 2 Cls Cursor Off Config Kbd = Porta , Debounce = 20 , Delay = 100 Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = _ Co , Polarity = Low , Phase = 0 , Clockrate = 16 Spiinit Config Watchdog = 16 Stop Watchdog Config Pinb.0 = Output Config Pinb.1 = Output Config Pinb.2 = Output Config Pinb.3 = Output Config Pind.2 = Output Config Pind.3 = Output Config Portc = Output Dim A As Byte , B As Byte , C As Byte , Z As Byte Dim X As Bit Dim Hour As Byte , Minuit As Byte , Second As Byte Dim Year As Byte , Mounth As Byte , Day As Byte Dim U(22) As Eram Word At &H1BD Dim Ep(6) As Byte Dim C As Byte , D As Byte Dim R As Eram Byte At &H1F4 Dim L1 As Word , L2 As Word Dim P As Word , P2 As Word , P1 As Word Lcd "BE APARTEMAC " Lowerline Lcd "KHOSH AMADID" Z = 16 B = 10 Gosub Eprom_read Again:
38
ECA
%"# " 8 ) 5
A = Getkbd() If A > 15 Then Goto Again If A = 0 Then B=7 If C = 3 Then Gosub Cext7 If X = 0 Then C=0 Cls Home Lcd "ROOSTAMI" Lowerline Lcd "vahed 7" End If End If If A = 1 Then B=8 If C = 3 Then Gosub Cext8 If X = 0 Then C=0 Cls Home Lcd "BARATI" Lowerline Lcd "vahed 8" End If End If If A = 2 Then B=9 If C = 3 Then Gosub Cext9 If X = 0 Then C=0 Cls Home Lcd "GHOMAYSHI" Lowerline Lcd "vahed 9" End If End If
39
ECA
%"# " 8 ) 5
If A = 4 Then B=4 If C = 3 Then Gosub Cext4 If X = 0 Then C=0 Cls Home Lcd "REZAEI" Lowerline Lcd "vahed 4" End If End If If A = 5 Then B=5 If C = 3 Then Gosub Cext5 If X = 0 Then C=0 Cls Home Lcd "SAHARDOOST" Lowerline Lcd "vahed 5" End If End If If A = 6 Then B=6 If C = 3 Then Gosub Cext6 If X = 0 Then C=0 Cls Home Lcd "AMJADI" Lowerline Lcd "vahed 6" End If End If
40
ECA
%"# " 8 ) 5
If A = 8 Then B=1 If C = 3 Then Gosub Cext1 If X = 0 Then C=0 Cls Home Lcd "ASHORI" Lowerline Lcd "vahed 1" End If End If If A = 9 Then B=2 If C = 3 Then Gosub Cext2 If X = 0 Then C=0 Cls Home Lcd "HOSAICI" Lowerline Lcd "vahed 2" End If End If If A = 10 Then B=3 If C = 3 Then Gosub Cext3 If X = 0 Then C=0 Cls Home Lcd "CADERI" Lowerline Lcd "vahed 3" End If End If If A = 13 Then
www.ebm21.blogfa.com
41
ECA
%"# " 8 ) 5
B=0 If C = 3 Then Gosub Cext10 If X = 0 Then C=0 Cls Home Lcd "AMIRI" Lowerline Lcd "vahed 10" End If End If If A = 15 Then If X <> 1 Then If B = 1 Then Set Portb.0 Waitms 500 Reset Portb.0 Incr C End If If B = 2 Then Set Portb.1 Waitms 500 Reset Portb.1 Incr C End If If B = 3 Then Set Portb.2 Waitms 500 Reset Portb.2 Incr C End If If B = 4 Then Set Portb.3 Waitms 500 Reset Portb.3
42
ECA
%"# " 8 ) 5
Incr C End If If B = 5 Then Set Portd.3 Waitms 500 Reset Portd.3 Incr C End If If B = 6 Then Set Portc.0 Waitms 500 Reset Portc.0 Incr C End If If B = 7 Then Set Portc.1 Waitms 500 Reset Portc.1 Incr C End If If B = 8 Then Set Portd.2 Waitms 500 Reset Portd.2 Incr C End If If B = 9 Then Set Portc.6 Waitms 500 Reset Portc.6 Incr C End If If B = 0 Then
43
ECA
%"# " 8 ) 5
Set Portc.7 Waitms 500 Reset Portc.7 Incr C End If If C = 3 Then X=1 P1 = P Cls Home Lcd "enter payam" Goto Again End If End If End If If A = 14 Then If D > 0 Then Shiftlcd Right Waitms 100 Decr P Decr Z Decr D End If End If If A = 12 Then If D > 0 Then B = 10 D=0 X=0 C=0 Cls Z = 16 Gosub Spi_inter Waitms 150 End If End If
44
ECA
%"# " 8 ) 5
If A = 7 Then If X <> 1 Then Incr C If C = 23 Then C = 1 If C > 0 Then If C < 23 Then Gosub Ready_eprom End If End If End If End If If A = 11 Then If X <> 1 Then Decr C If C = 0 Then C = 22 If C = -1 Then C = 22 If C > 0 Then If C < 23 Then Gosub Ready_eprom End If End If End If End If If A = 3 Then If D = 0 Then Reset Watchdog Start Watchdog End If End If Goto Again End
Cext7:
45
ECA
%"# " 8 ) 5
X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext8: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext9: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z
46
ECA
%"# " 8 ) 5
Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext4: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext5: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext6:
47
ECA
%"# " 8 ) 5
X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext1: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext2: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z
48
ECA
%"# " 8 ) 5
Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext3: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Cext10: X=1 P2 = P - P1 If P2 = 14 Then Return Incr D If P = P1 Then Cls Shiftlcd Left Waitms 100 Incr Z Locate 1 , Z Lcd B Incr P Writeeeprom B , P Waitms 4 Return Spi_inter:
49
ECA
%"# " 8 ) 5
Spiin Year , 1 Spiin Mounth , 1 Spiin Day , 1 Spiin Hour , 1 Spiin Minuit , 1 Spiin Second , 1 Select Case P Case 1 To 20: R=1 U(1) = P Case 21 To 40: R=2 U(2) = P Case 41 To 60: R=3 U(3) = P Case 61 To 80: R=4 U(4) = P Case 81 To 100: R=5 U(5) = P Case 101 To 120: R=6 U(6) = P Case 121 To 140: R=7 U(7) = P Case 141 To 160: R=8 U(8) = P Case 161 To 180: R=9 U(9) = P Case 181 To 200: R = 10 U(10) = P Case 201 To 220: R = 11 U(11) = P
50
ECA
%"# " 8 ) 5
Case 221 To 240: R = 12 U(12) = P Case 241 To 260: R = 13 U(13) = P Case 261 To 280: R = 14 U(14) = P Case 281 To 300: R = 15 U(15) = P Case 301 To 320: R = 16 U(16) = P Case 321 To 340: R = 17 U(17) = P Case 341 To 360: R = 18 U(18) = P Case 361 To 380: R = 19 U(19) = P Case 381 To 400: R = 20 U(20) = P Case 401 To 420: R = 21 U(21) = P Case 421 To 440: R = 22 U(22) = P End Select Incr P Writeeeprom Mounth , P Waitms 4 Incr P Writeeeprom Day , P Waitms 4
51
ECA
%"# " 8 ) 5
Incr P Writeeeprom Hour , P Waitms 4 Incr P Writeeeprom Minuit , P Waitms 4 Incr P Writeeeprom Second , P Waitms 4 Incr P Writeeeprom Year , P Waitms 4 Cls Home Lcd "OK" Lowerline Lcd "payam saved" Wait 2 Cls Lcd "BE APARTEMAC " Lowerline Lcd "KHOSH AMADID" Gosub Eprom_read
Eprom_read: If R = 1 Then P = 20 If R = 2 Then P = 40 If R = 3 Then P = 60 If R = 4 Then P = 80 If R = 5 Then P = 100 If R = 6 Then P = 120 If R = 7 Then P = 140 If R = 8 Then P = 160 If R = 9 Then P = 180 If R = 10 Then P = 200 If R = 11 Then P = 220 If R = 12 Then P = 240 If R = 13 Then P = 260 If R = 14 Then P = 280
52
ECA
%"# " 8 ) 5
If R = 15 Then P = 300 If R = 16 Then P = 320 If R = 17 Then P = 340 If R = 18 Then P = 360 If R = 19 Then P = 380 If R = 20 Then P = 400 If R = 21 Then P = 420 If R = 22 Then P = 0 Return Ready_eprom: Cls B = 10 C=0 L1 = U(n) + 1 L2 = L1 + 5 Z=0 For P2 = L1 To L2 Incr Z Readeeprom Ep(z) , P2 Cext Locate 2 , 9 Lcd Ep(4) ; ":" ; Ep(5) ; ":" ; Ep(6) If Ep(5) < 10 Then Cls Locate 2 , 9 Lcd Ep(4) ; ":" ; "0" ; Ep(5) ; ":" ; Ep(6) If Ep(6) < 10 Then Cls Locate 2 , 9 Lcd Ep(4) ; ":" ; "0" ; Ep(5) ; ":" ; "0" ; Ep(6) End If End If If Ep(6) < 10 Then Cls Locate 2 , 9 Lcd Ep(4) ; ":" ; Ep(5) ; ":" ; "0" ; Ep(6) If Ep(5) < 10 Then Cls
53
%"# " 8 ) 5
ECA
Locate 2 , 9 Lcd Ep(4) ; ":" ; "0" ; Ep(5) ; ":" ; "0" ; Ep(6) End If End If Locate 2 , 1 Lcd Ep(1) ; "/" ; Ep(2) ; "/" ; Ep(3) Z=0 L1 = C - 1 L2 = L1 * 20 Incr L2 For P2 = L2 To U(n) Readeeprom Ep(1) , P2 Incr Z Locate 1 , Z Lcd Ep(1) Cext Waitms 150 Z = 16 Return
K 6 J2> C I$C . &;
w . . /7
"l
@/ $ H .5 . /
W/
~! " . 9 avr
tosc1
) , W" .
( $ C power save
1 @9T
?
;
?
N
0."
Tr
E9
), * V"
rtc
T !
T
" /
. !
@9# 128
; .5
9"
power save
B. 9 isr
9T X
7$# -
9T rtc ;N!
-E ) prescaler =
+! 9 "
9"
+5
.E
32.768 khz
1
(isr)
0
"
+ $ ;U
(r" )
/ ;9T
@9G
9T
power save
tosc2
69
mega8
. "* ,
. H .5 6 /
?. /&9 /
U ;? -
? 6
j9 !
0 ( $ 1s
9T T r
active
v3 .0
@ 9 ' g: z 9 ]1 ?
power save
.
54
9T
7 $ ./7
.
? )
T
W/
â&#x20AC;˘9 T j9
%"# " 8 ) 5
ECA bascom
$ •9
O$. .
/
@ 6
.B
$
0
.$ 6 . $
config clock
+$
0;
: / ;9
B
. $ T 9" 6
@/$ •9 1s
?.
9T
/
@
@/$
config clock = soft , gosub sectic
[ -C 6 T
+ $ ' .I9
U
sectic
9"
T "l
T N
( $T N . $
.
$
#
0 ) , W"
0 G 9 pind.2
~&B
+5
"l
E9 ; -0
T
E ! ' 7 $ 'T ' 9 ' # $ pind.1 ' pind.0
8/
E9
; -0
0 & 9 1 ;E4/9 8/ 6 T
1
: 9 ]0 9 "
7 3 ( " ; $ H W/ E9
7 ? .
# $ H 73B
spi
] E" spi % & 9T X
"
("
9( 1
@96 .
H W/ ]<
9( 1 ( " H ]
H.
9T X
: 6 I,
j"
, ;9 H .5
"l
pind.0=0
87
] () 9"
,.
j"
1 [-C 6
;E4/9
w . .I 9
// # $ ;/N
pind.2=0
<$
E9 show [-C
1 . 9 pind.1
1 :9T 7 "
j9 .
] " lcd
".I 9
()
0 +5
, . ".I 9
master
.I 9
] " lcd . ".0 ;9
. $
www.ebm21.blogfa.com
55
0
--
T
9" 6
N P+5
ECA
%"# " 8 ) 5
$regfile = "m8def.dat" $crystal = 8000000 Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = _ Portc.0 , E = Portc.4 , Rs = Portc.5 Cls : Home : Cursor Off Config Clock = Soft , Gosub = Sectic Disable Interrupts Config Portd = &B11111000 Dim Flag As Bit , S As Byte , M As Byte , H As Byte , Y As Word Dim Mo As Byte , D As Byte , Slc As Byte , C As Byte Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = _ Yes , Polarity = Low , Phase = 0 , Clockrate = 16 Spiinit Slc = 0 Y = 1385 Mo = 1 D=1 H = 12 Flag = 0 Dim Ge As Byte Scan: If Pind.0 = 0 Then Slc = Slc + 1 If Slc = 5 Then Slc = 0 If Pind.2 = 0 Then Flag = 0 Goto Waitls End If On Slc Goto Hour , Minute , Year , Month , Day Goto Scan Waitls: Enable Interrupts Do Ge = 10 If Pind.0 = 0 Then Flag = 1 Goto Scan
56
ECA
%"# " 8 ) 5
End If Loop Sectic: If Flag = 0 Then Gosub Show Incr S If S > 59 Then S=0 Incr M Cursor Off Shiftcursor Left , 2 If M > 59 Then Incr H M=0 If H > 23 Then H=1 Incr D End If End If End If If Mo < 7 Then Goto First If Mo => 7 Then Goto Second First: If D > 31 Then D=1 Incr Mo If Mo > 12 Then Mo = 1 End If Second: If D > 30 Then D=1 Incr Mo If Mo > 12 Then Mo = 1 End If Return Show: C = Y - 1300
57
ECA
%"# " 8 ) 5
Spiout C , 1 Spiout Mo , 1 Spiout D , 1 Spiout H , 1 Spiout M , 1 Spiout S , 1 Cls Locate 2 , 1 Lcd "TIME:" Locate 2 , 7 Lcd H ; ":" ; M ; ":" ; S If M < 10 Then Cls Locate 2 , 1 Lcd "TIME:" Locate 2 , 7 Lcd H ; ":" ; "0" ; M ; ":" ; S If S < 10 Then Cls Locate 2 , 1 Lcd "TIME:" Locate 2 , 7 Lcd H ; ":" ; "0" ; M ; ":" ; "0" ; S End If End If If S < 10 Then Cls Locate 2 , 1 Lcd "TIME:" Locate 2 , 7 Lcd H ; ":" ; M ; ":" ; "0" ; S If M < 10 Then Cls Locate 2 , 1 Lcd "TIME:" Locate 2 , 7 Lcd H ; ":" ; "0" ; M ; ":" ; "0" ; S End If End If Locate 1 , 1 Lcd "DATE:"
58
ECA
%"# " 8 ) 5
Locate 1 , 7 Lcd Y ; "/" ; Mo ; "/" ; D Return End
Hour: If Pind.1 = 0 Then H = H + 1 If H => 24 Then H = 0 Lcd "HOUR =" ; H Spiout H , 1 Waitms 200 Cls : Home Goto Scan Minute: If Pind.1 = 0 Then M = M + 1 If M => 60 Then M = 0 Lcd "MICUTE =" ; M Spiout M , 1 Waitms 200 Cls : Home Goto Scan Year: If Pind.1 = 0 Then Y = Y + 1 If Y = 1400 Then Y = 1385 C = Y - 1300 Lcd "YEAR =" ; Y Spiout C , 1 Waitms 200 Cls : Home Goto Scan Month: If Pind.1 = 0 Then Mo = Mo + 1 If Mo = 13 Then Mo = 1 Lcd "MOUCTH =" ; Mo
www.ebm21.blogfa.com
59
ECA
%"# " 8 ) 5
Spiout Mo , 1 Waitms 200 Cls : Home Goto Scan Day: If Pind.1 = 0 Then D = D + 1 If D = 31 Then D = 1 Lcd "DAY =" ; D Spiout D , 1 Waitms 200 Cls : Home Goto Scan
60
ECA
%"# " 8 ) 5
>
LCD1 , LCD2 = 2 * 16 OHM R1 . . . . . . . R10 = 120 R11 , R12 , R13 = 10 KOHM U1= ATMEGA16 U2 = ATMEGA8 X1 = 32.768 KHZ D1 . . . . . . . D10 = LED VCC = +5V
www.ebm21.blogfa.com
61
L C M< .7&;
9*
ECA
O4 $ .N
1385 ' T )
1384 ' 1 ; 7 ` /89 ' avr
1?
/
@ 9 [1]
avr
1?
/
@ 9 [2]
9 ` /89 ' 8"
1
www.ebm21.blogfa.com www.ebm21.blogfa.com
BAHMAN MOLAEE www.ebm21.blogfa.com
62