Вы находитесь на странице: 1из 107

-

..

ATMEGA128 68HC908

-
2007

681.32

.. ATMega128
68hc908. : : , 2007, 107 .

ATMega128 68hc908
.
,

02 2006., 2.

2007
20072008 .



-
,
, 2007
.. , 2007


ATMega128.................. 5
1. ................................................................................................... 5
2. .............................................................................. 5
3. ATMega128...................................... 5
3.1 ............................................................................................... 5
3.2 /......................................... 8

4. ,
, ................. 10
4.1 /.................................................................. 10
4.2 /............... 12
4.3
.................. 12
4.4 ............................................. 17
4.4.1 ............................................. 19
4.4.2 ................................................................................ 22

5. ........................................................... 23
5.1 ...................................................................... 23
5.2 ............................................................... 25
5.2.1 / ............................................... 26
5.2.2 0 .................................................................................. 27
5.2.3 .......................................... 29
5.2.4 8- . .................. 31
5.2.5 ................................................................................ 35
5.2.6 / EEPROM ...................................................................... 41
5.2.7 ......................................................... 41
5.2.8 ................................................................................... 47

68HC908..................... 48
6. ................................................................................................. 48
7. ............................................................................ 48
8. ATMega128.................................... 49
8.1 ............................................................................................. 49
8.2 /....................................... 50

9. ,
, ................. 54
9.1 /.................................................................. 54
9.2
....................................................................................................................... 56
9.3 .................................................................................................................. 59
9.4 ............................. 62
9.5 1 .................................................................................................... 63
9.6 1 ....... 67
9.6.1 -
............................................................................................................................................... 67
9.6.2 ..................................... 69
3

9.7
DS1722S ................................................................................................................................... 70
9.7.1 ........... 71
9.7.2 DS1722.............................................. 73
9.7.3 SPI ................................... 74
9.7.4
.................................................................................... 74
9.8 4- ........ 75
9.8.1 ................... 76
9.8.2 ............................................................................... 79

10. ......................................................... 81
10.1 ............................................................. 81
10.2 ....... 83
10.2.1 /......................................................... 83
10.2.2 .................................. 85
10.2.3 ................................................................ 86
10.2.4 (LCD) ................................................ 90
10.2.5 ( ) ........................ 92
10.2.6 ............................................................................. 94
10.2.7 .......................................................... 95
10.2.8 ...................................... 97
10.3 .............................................................. 97
11. ............................................................................................ 104
........................................................................................................ 105

ATMega128
1.
/
:
1. 8- ,
2. ,
3. , ,
4. 0
32768,
5. 4x3,
6. / / EEPROM.

2.
1.
.
2. ( )
8- .
3. .
4. : * # .
* , #
.
5. .

3. ATMega128
3.1
.1.1
. .
ATmega128
:
- 8- - ( );
- - 128
;
- 32 ;
- EEPROM 4 ;
5

- 4 ;
- 6 8- ;
- 4 -;
- 10- 8- ;
- UART0, UART0, TWI SPI;
- ( ).

.1.1. ATmega128.
Port A (PA7..PA). 8- .
(
). 20
.
,

.
/.
Port B (PB7..PB0). 8-
. 20 .

6

,
.
.
Port C (PC7..PC0). 8- .
20 . C
.
Port D (PD7..PD0). 8-
. 20 .

,
. D
.
Port (P7..P0). 8-
. 20 .

,
. E
.
Port F (PF7..PF0). 8- .
- .
#RESET. .
50 .
XTAL1, XTAL2.
.
TOSC1, TOSC2.
/.
#WR, #RD. .
ALE. . ALE
AD0-AD7
.
AD0-AD7 .
AVCC. - .
VCC .
AREF. - .
AGND AVCC.
AGND. ,
. .
#PEN.
.
,
.
V, GND. .

3.2 /
AVR
( ).
ATMega128 . 1.2.

.1.2. ATmega128
AVR
:
-.
16-
, SPI;
16-

(50 FOSC=20 );
8- . 32
, 64
-,
4096 .
60 ;
EEPROM 4
,
-.
. 1.2 1.3, 32 ()

. ,
.
.
. ,
,
.
16- , , ,
.
8

.1.3. ATmega128
64
- ( / ).
,
-.
IN OUT - $00 $3F.
-
. - $20.
.
- $00 ($20) $1F ($3F)
. SBI CBI,
SBIS SBIC. B1
-.
1.1. -
ATmega128 (
).

PORTG
DDRG
PING
PORTF
DDRF
SREG
SPH
SPL
TIMSK
TIFR
MCUCR

G
G
G
F
F

,
,
/
/

9

MCUCSR
TCCR0
TCNT0
OCR0
ASSR
TCCR1A
PORTA
DDRA
PINA
PORTB
DDRB
PINB
PORTC
DDRC
PINC
PORTD
DDRD
PIND
SPDR
SPSR
SPCR
ACSR
ADMUX
ADCSRA
ADCH
ADCL
PORTE
DDRE
PINE
PINF


/ 0
/ 0
/ 0

/ 1









D
D
D
SPI
SPI
SPI



,
,



F

4. ,
,

4.1 /
(, , 8 , .)
10

, ..
.

.1.5.
#RESET
(
DDRx ),
.
DDRx .
.1.5 1.5.1 , PC 8-
, DDRC (Data Direction
Register C) FF.

.1.5.1. 8-
PE (. 1.5.1)
8- , 4
DDRE .. . 4
PD 4
11

(3 PD6..4 ),
PD7 , .. 3..0 7
DDRD . PF ()
,
DDRF .
4.2 /
Pascal
:
procedure Init_Ports; //== ,
begin
//==
DDRD:=%10001111; //== PD0..PD3 " 0",
//== PD4..PD6 -
//== PORTD.7 ,
//==
DDRC:=$FF; //== C 8-
DDRE:=%11110000; //== 7 E (LED) ( RESET'
)
//== 6,5,4 ,
//==
PORTE:=%11111111; //== ,
//== (.. )
end;

4.3

(.
).
8- 0,
CLKi/o,
, (TOSC1,2)
32768 (215).
CLKi/o, ..
. 0 (/ 0)
.1.6.

12

.1.6. 0
0 ,
/ TCCR0
(.1.6.1.).
7
6
5
4
3
2
1
0

FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00

0
0
0
0
0
0
0
0

.1.6.1. / - TCCR0

#RESET
.
WGM01 WGM00 /

0
1
2
3

0
0
1
1

0
1
0
1

.1.6.2. WGM00,WGM01 /.
13

COM01
0
0
1
1

COM00

0
/ OC0
1
OC0

0
OC0 ( 0)
1
OC0 (

1)

.1.6.3. COM01, COM00 .

CS02
0
0
0
0
1
1
1
1

CS01

CS00

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1


AS0 ASSR
AS0=0
AS0=1
/ T/C0

TOSC1
/ 8
TOSC1/8
/ 32
TOSC1/32
/ 64
TOSC1/64
/ 128
TOSC1/128
/ 256
TOSC1/256
/ 1024
TOSC1/1024

.1.6.4. CS02, CS01, CS00 - .


FOC0 - OC0.

TCCR0, .
32768
, 128 256
(32768/128/256=1). CS02 CS00 .. 1
(prescaler) .1.6. clkT0 256.
:
(WGM00=WGM01=0) 256(8 )
OCR0 ,
. , ..
, 256.
FF. /
0 FF, .. 256.
, CS02=CS00=1, CS01=0, WGM00=0 WGM01=1.
7
6
5
4
3
2
1
0
AS0 TCN0UB OCR0UB TCR0UB
.1.6.5. . / T/C0 - ASSR

14

AS0: / T/C0.
(= 1) / 0

.
OCR0
TCNT0 (.1.6). 1 OCIE0
TIMSK
7
6
5
4
3
2
1
0
OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0
.1.6.6. / TIMSK.

0
:
procedure Init_Timer0Async; //== 0
//== 32768
begin
TCCR0:=$0D;//== CS00=CS02=1(=128), WGM01=1(
.)
OCR0:=$FF; //== (256-1): 32768/128/256= 1
//==
//== ., .. FF
ASSR.3:=1; //== AS0=1 - Timer0
//== 32768
TIMSK.1:=1;//== OIE0:=1 -
//== 0 OCR0
end;


() TIMER0COMP:
var b: boolean;
var i,t0: byte;
interrupt TIMER0COMP; //==
//== 0 ( TCNT0) OCR0
begin
b:=not b; //== b
PORTE.7:= b; //==
for t0:=0 to 6 do //== ""
sound:=not sound; //== _|`|_|`|_|`| ( sound)
PORTD.7:=sound; //==
for i:=0 to 255 do endfor; //== ""
endfor;
15

end;
1.2
ATMega128
0 1E
TIMER0COMP.
1.2

TCNT0 OCR0
(Control Logic) .1.6
TOV0 .
1E, ( )
JMP TIMER0COMP
TIMER0COMP.
interrupt TIMER0COMP.... (. ),

.
?
,
,
(TIMER0COMP) .
, (
) .
() .
1.7
, , 1E
JMP 03A9h
TIMER0COMP,

0003A9.

16

, (
2A).

.1.7. ,
4.4
ATmega128 10-
(.1.8). 8-
. 8
F.
. (ADC1,
ADC0 ADC3, ADC2)
: 0 (1x), 20 (10x), 46 (200x).
10x, 8- , 200x,
7-.
10-.
.
GND (0), AREF 2,56
.

MUX ADMUX.

ADC0ADC7, GND
1,22 .
ADEN ADCSRA.

ADEN.
10- ,
ADCH ADCL.
10- 16-.
17

( ), 10-
( ) ADLAR ADMUX.

, 8- , ..
ADCH.
ADCL, ADCH,
,
. ADCL
. , ADCL
ADCH,
.
ADCH ADCH ADCL
.

.1.8.
. 1
ADSC.

18

.
,
, .

.
. 1 ADFR ADCSRA.
. 1 ADSC
ADCSRA.
,
ADIF.
4.4.1
() ADCSRA
.1.9.

/
.

ADEN ADSC ADFR ADIF

ADIE ADPS2 ADPS1 ADPS0

.1.9. ADCSRA
ADEN: . . 1
. . 0, ,
.
ADSC: .
.

, .
,
ADSC, 25
, 13 .
.
ADFR: .
. 1,
.

. . 0
.
ADIF: .
.
19

ADIE I ( SREG),
. ADIF
. ADIF
. 1 . ,
"--" ADCSRA
.
SBI CBI.
ADIE: . . 1 ,
, I SREG,
.
ADPS2..0: (Prescaler) .

.
1.3.
(10 ),
50200 . 10 ,
,
200 .
1.3
ADPS2
0
0
0
0
1
1
1
1

ADPS1
0
0
1
1
0
0
1
1

ADPS0
0
1
0
1
0
1
0
1


2
2
4
8
16
32
64
128

REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0

./. ./. ./. ./. ./. ./. ./. ./. ./.


.1.10. ADMUX
REFS1..0: .
,
20

. -2
( REFS0 REFS1 0).
1.4
REFS REF

1
S0
0
0 AREF,
AVCC
0
1
AREF
1
0

1
1
2.56 AREF
ADLAR: .
ADLAR
. ADLAR = 1,
(
, ..
), - .
ADLAR ,
.
MUX4..0: .
,
. ,
(. . 1.5).
,
(
ADIF ADCSRA).
1.5
MUX4..0

00000

00111


ADC0

ADC7

ADCL ADCH

ADLAR = 0:
21

15
ADC7
7

14

13

12

11

10

ADC6 ADC5
6
5

ADC4
4

ADC3
3

ADC2
2

ADC9
ADC1
1

ADC8 ADCH
ADC0 ADCL
0

14

12

11

10

ADLAR = 1:
15

13

ADC9

ADC8 ADC7

ADC6

ADC5 ADC4

ADC3 ADC2

ADCH

ADC1

ADC0 -

ADCL


.
ADLAR=1
, 8 . 8-
ADCH , ,
ADCL .
ADCL, ADCH.
4.4.2

, .. ADEN ADIE . 10-
.
.. 50..200,
6
64 (6000000/64 = 93750) ADSP2, ADSP1,
ADSP0=110. ,
ADCSRA=8E.
.1.5 , ( )
ADC0 ( PF0), MUX4..0
ADMUX .
ADLAR=1, .. .
ADMUX 20h.
1 ADSC ADCSRA
.

I () SREG.

22

7
I

6
T

5
H

4
S

3
V

2
N

1
Z

0
C

.1.11. SREG.
I .
(I=1).
EIMSK TIMSK.
(I=0), .
(
) RETI.
Pascale EnableInts;

:
procedure Init_ADC; //==
begin
ADCSRA:=$8E; //== = 64
//== (6000000/64=93750 - 50..100000)
ADMUX:=$20;//== ADLAR=1-
//== ADCH
end;
interrupt ADCRDY; //==
//==
begin
adccode:= word(ADCH); //== .
//== (.. 8- )
ADCSRA.6:=1; //== ( ADSC=1)
end;

5.
5.1
E-LAB PED32

AVR.
. "File | New Project".
"New-Edit-Account" (
gr8888 ),
OK "Save" "Exit" (.1.12).
23

. 1.12.

" (.1.13).

"

.1.13.
"mega128",
"System
Tick --> none. "next"
.
"next 16-

Store Exit.

24

.1.14.
E-LAB PED32
"Wizard'" "Pascal" (.1.14).


.
5.2
- . ,
E-LAB PED32
.
. "Project | Load Project".
25

"Project load/delete"
"myATMega128" "Load" (.1.15) .

.1.15.
.
, . "File | Open MainFile".
.
5.2.1 /
,
/ (.1.16).

.1.16.
, 1.16,
.
, ,
.
E-LAB PED32 Make Project
,

26

. !

.
.
5.2.2 0
, 0
1.
.

.1.16. 0
,
1 .
TIMER0COMP (.1.17).

.1.17.
27

,
:

program myATMega128;
{ $BOOTRST $0F000}
{Reset Jump to $0F000}
{$NOSHADOW}
{ $W+ Warnings}
{Warnings off}
Device = mega128, VCC=5;
Import ;
From System Import ;
Define
ProcClock = 6000000; //== . - 6
StackSize = $0064, iData; //==
.
FrameSize = $0064, iData; //==
//==
Implementation
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
{--------------------------------------------------------------}
{ Const Declarations }
{--------------------------------------------------------------}
{ Var Declarations }
{$IDATA}
{--------------------------------------------------------------}
{ functions }
{--------------------------------------------------------------}
procedure Init_Ports; //== ,
begin
//==
DDRD:=%10001111;//== PD0..PD3 " 0", PD4..PD6

//== PORTD.7 ,

DDRC:=$FF; //== C 8-
DDRE:=%11110000;//== 7 E (LED) ( RESET'
)
//== 6,5,4 ,

PORTE:=%11111111;//== ,

end;

28

procedure Init_Timer0Async;//== 0
32768
begin
TCCR0:=$0D;//== CS00=CS02=1(=128),WGM01=1(
.)
OCR0:=$FF; //== (256-1): 32768/128/256= 1
!
//==
, .. FF
//==
ASSR.3:=1; //== AS0=1 - Timer0
32768
TIMSK.1:=1;//== OIE0:=1 -

end; //== 0 OCR0


var b: boolean;
interrupt TIMER0COMP;//==
0
//== 1 TOSC=32768

begin
b:=not b;
//==
PORTE.7:= b; //==
end;
{ Main Program }
{$IDATA}
begin
Init_Ports;
Init_Timer0Async;
EnableInts;//==
loop
endloop;
end myATMega128.
5.2.3
,
ATMega128.

29

.1.18. AVRprog
E-LAB PED32
HEX *.hex ( - atm128.hex).

.
AVRprog (.1.18).

. Browse..
C:\EMUL\Work\E-LAB\gr8888\atm128.hex
Flash -> Program.
(.1.19).
Flash ,
. .

.1.19.
30

5.2.4 8- .

8-
. , -, ASCII
(019) 8- .
var,
(.1.20).

ScanKeyOnce (. 1.20-1)

8-
(. 1.20-2).

.1.20.

.1.20-1 .

31

.1.22.
,

program myATMega128;
{ $BOOTRST $0F000}
{Reset Jump to $0F000}
{$NOSHADOW}
{ $W+ Warnings}
{Warnings off}
Device = mega128, VCC=5;
Import ;
From System Import ;
Define
ProcClock = 6000000; //== . - 6
StackSize = $0064, iData; //== . .
FrameSize = $0064, iData; //==
//==
Implementation
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
{--------------------------------------------------------------}
{ Const Declarations }
{--------------------------------------------------------------}
const keyASCII: string = '123456789*0#';//== ASCII /
const key8segm: string = #$F9 + #$A4 + #$B0 + #$99 + #$92 + #$82 + #$F8 +
#$80 + #$90 + #$7F + #$C0 + #$B6; //== 8- .
{ Var Declarations }
{$IDATA}
var sound :boolean;
s:string[5];//== 8- .
maskind,j,key,kn,e2temp: byte;
adccode:word;
32

{--------------------------------------------------------------}
{ functions }
{--------------------------------------------------------------}
function ScanKeyOnce (var kn: byte):boolean;//== .
var row,col: byte; //== var kn -
var delay:word;
begin
for col:=0 to 3 do //==
PORTD:=not(1 shl col);//== 11111110,11111101,11111011,11110111
//== )
for row:=0 to 2 do //==
if ((PIND and ($10 shl row))=0) then //== ,
kn := col*3+row;//== (0..11)
for delay:=0 to 5000 do endfor;//==
return (true); //== " "
endif;
endfor;
endfor;
return (false); //== " "
end;
procedure Init_Ports; //== ,
begin
//==
DDRD:=%10001111; //== PD0..PD3 " 0", PD4..PD6
//==
//== PORTD.7 ,
//==
DDRC:=$FF; //== C 8-
DDRE:=%11110000; //== 7 E (LED) ( RESET'
//== )
//== 6,5,4 ,
//==
PORTE:=%11111111; //== ,
//==
end;
procedure Init_Timer0Async; //== 0
//== 32768
begin
TCCR0:=$0D; //== CS00=CS02=1(=128),WGM01=1(
//== .)
OCR0:=$FF; //== (256-1): 32768/128/256= 1
//== , ..
//== FF
ASSR.3:=1; //== AS0=1 - Timer0
//== 32768
33

TIMSK.1:=1;//== OIE0:=1 -
//== 0 OCR0
end;
var b: boolean;
interrupt TIMER0COMP; //==
//== 0
//== 1 TOSC=32768
begin
b:=not b; //==
PORTE.7:= b; //==
end;
{ Main Program }
{$IDATA}
begin
Init_Ports;
Init_Timer0Async;
EnableInts; //==
loop //==
while not ScanKeyOnce(kn) do endwhile;//==
key:=byte(keyASCII[kn+1]); //== +1, .. 0- -
//== !
while ScanKeyOnce(kn) do endwhile;//==
case key of //== -
//== (#,*,0..9)
'0'..'9':
PORTE:=%10111111; //== 8- . -
//== -
PORTC:=byte(key8segm[kn+1]); //== 8- .
//== LED
| //== |
endcase;
endloop;
end myATMega128.


Flash ->
Program . ,
0,1,29 ,
LED .
.

34

5.2.5
,
(.1.22).
,
,
, .
,
.

.1.22.
(. 1.23):

.1.23. Display8seg
35

Dec2_8seg_Str 8-
, Display8seg 8-
.

.1.24. Dec2_8seg_Str

(.1.25).

.1.25.
.
, .
36

program myATMega128;
{ $BOOTRST $0F000}
{Reset Jump to $0F000}
{$NOSHADOW}
{ $W+ Warnings}
{Warnings off}
Device = mega128, VCC=5;
Import ;
From System Import ;
Define
ProcClock = 6000000; //== . - 6
StackSize = $0064, iData; //== . .
FrameSize = $0064, iData; //==
//==
Implementation
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
{--------------------------------------------------------------}
{ Const Declarations }
{--------------------------------------------------------------}
const keyASCII: string = '123456789*0#';//== ASCII /
const key8segm: string =
#$F9+#$A4+#$B0+#$99+#$92+#$82+#$F8+#$80+#$90+#$7F+#$C0+#$B6;
{ Var Declarations }
{$IDATA}
var sound :boolean;
s:string[5];//== 8- .
maskind,j,key,kn,e2temp: byte;
adccode:word;
{--------------------------------------------------------------}
{ functions }
{--------------------------------------------------------------}
function ScanKeyOnce (var kn: byte):boolean;//== .
var row,col: byte; //== var kn -
var delay:word;
begin
for col:=0 to 3 do //==
PORTD:=not(1 shl col);//== 11111110,11111101,11111011,11110111
)
for row:=0 to 2 do //==
if ((PIND and ($10 shl row))=0) then //== ,
kn := col*3+row;//== (0..11)
for delay:=0 to 5000 do endfor;//==
return (true); //== " "
37

endif;
endfor;
endfor;
return (false); //== " "
end;
procedure Init_Ports; //== ,
begin
//==
DDRD:=%10001111;//== PD0..PD3 " 0", PD4..PD6
//==
//== PORTD.7 ,
//==
DDRC:=$FF; //== C 8-
DDRE:=%11110000;//== 7 E (LED) ( RESET'
//== )
//== 6,5,4 ,
//==
PORTE:=%11111111;//== ,
//==
end;
procedure Init_Timer0Async;//== 0
32768
begin
TCCR0:=$0D;//== CS00=CS02=1(=128),WGM01=1(
//== .)
OCR0:=$FF; //== (256-1): 32768/128/256= 1
//== . , .. FF
//==
ASSR.3:=1; //== AS0=1 - Timer0
//== 32768
TIMSK.1:=1;//== OIE0:=1 -
//== 0 OCR0
end;
var b: boolean;
interrupt TIMER0COMP;//==
//== 0, 1 TOSC=32768
//==
begin
b:=not b; //==
PORTE.7:= b; //==
end;
procedure Init_ADC; //==
begin
ADMUX:=$20;//== ADLAR=1-
//== ADCH
ADCSRA:=$8E; //== = 64
38

//== (6000000/64=93750 - 50..100000)


end;
interrupt ADCRDY; //==
//==
begin
adccode:= word(ADCH);//== . (.. 8-
//== )
ADCSRA.6:=1;//==
end;
function Dec2_8seg_Str (decn:word):string[5];//== 10 = 1023max
var s:string[5];
temp:char;
remn:word;
begin
s:='';
repeat
remn:=decn mod 10; //==

decn:=decn div 10; //== 10


if remn<>0 then
temp:=key8segm[remn]; //== 1 9
else
temp:=key8segm[remn+11]; //== 0 ""
//==
endif;
s:=s+temp; //== 's' ( E//==LAB)
until decn<1;
return(s); //== ( "123" 321)
end;
procedure Display8seg; //== (8-
//==. )
var i,j:byte;
begin
maskind:=%11110111; //== -
for i:=2 downto 0 do //== 3 E (PORTE.6 .. PORTE.4)
maskind:=maskind rol 1; //==
if not b then maskind:=maskind and $7F;endif;//== LED (PORTE.7)
//=="",
PORTC:=byte(s[i+1]); //== 8-
//==
PORTE:=maskind; //== 8- . -
//=="
for j:=0 to 255 do endfor; //==
endfor;
39

end;
{ Main Program }
{$IDATA}
begin
Init_Ports; //== /
Init_Timer0Async; //== 0
Init_ADC; //==
EnableInts; //==
loop //==
while not ScanKeyOnce(kn) do endwhile;//==
key:=byte(keyASCII[kn+1]);//== +1, .. 0- -
//==!
while ScanKeyOnce(kn) do endwhile;//==
case key of //== - (*,0..9)
'*': //== ( )
ADCSRA.6:=1; //== (
//== )
loop //==
s:=Dec2_8seg_Str(adccode);//== 8 ADLAR=1
s:=s+#$FF+#$FF+#$FF; //== FF - . ,
//== "__8" "_48"
Display8seg; //==
if ScanKeyOnce(kn)then //== ,
ADCSRA.6:=0; //==
exitloop; //==
endif;
endloop;
| //==
'0'..'9':
PORTE:=%10111111;//== 8- . -
//==-
PORTC:=byte(key8segm[kn+1]);//== 8- .
//== LED
| //==
endcase;
endloop;
end myATMega128.

Flash ->
Program
. , *
,
. .
40

5.2.6 / EEPROM

/ EEPROM. EEPROM
0..9 endcase, . 1.26.

.1.26. EEPROM
5.2.7

TIMER0COMP
.

41


.
program myATMega128;
//== ATMega128,
:
//== 1. 8- . , 2. , 3.
,
//== 4. 0 32768,
//== 5. , 6./ / EEPROM
{$NOSHADOW}
{ $W+ Warnings}
{Warnings off}
Device = mega128, VCC=5;
Import ;
From System Import ;
Define
ProcClock = 6000000; //== . - 6
StackSize = $0064, iData; //== . .
FrameSize = $0064, iData; //==
//==
Implementation
{$EEPROM} //==
var
e2prom: array[0..9]of byte; //== . (>10)
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
{--------------------------------------------------------------}
{ Const Declarations }
{--------------------------------------------------------------}
const keyASCII: string = '123456789*0#';//== ASCII /
42

const key8segm: string =


#$F9+#$A4+#$B0+#$99+#$92+#$82+#$F8+#$80+#$90+#$7F+#$C0+#$B6;
{ Var Declarations } //== 8- /
{$IDATA}
var sound :boolean;
s:string[5];//== 8- .
maskind,j,key,kn,e2temp: byte;
adccode:word;
{--------------------------------------------------------------}
{ functions }
{--------------------------------------------------------------}
function ScanKeyOnce (var kn: byte):boolean;//== . . -
var row,col: byte; //== var kn: -
var delay:word;
begin
for col:=0 to 3 do //==
PORTD:=not(1 shl col);//== 11111110,11111101,11111011,11110111
)
for row:=0 to 2 do //==
if ((PIND and ($10 shl row))=0) then //== ,
kn := col*3+row;//== (0..11)
for delay:=0 to 5000 do endfor;//==
return (true); //== " "
endif;
endfor;
endfor;
return (false); //== " "
end;
procedure Init_Ports; //== ,
begin
//==
DDRD:=%10001111;//== PD0..PD3 " 0", PD4..PD6

//== PORTD.7 ,

DDRC:=$FF; //== C 8-
DDRE:=%11110000;//== 7 E (LED) ( RESET'
)
//== 6,5,4 ,

PORTE:=%11111111;//== ,

end;
procedure Init_Timer0Async;//== 0
32768
begin
43

TCCR0:=$0D;//== CS00=CS02=1(=128),WGM01=1(
.)
OCR0:=$FF; //== (256-1): 32768/128/256= 1
!
//== , ..
FF
//==
ASSR.3:=1; //== AS0=1 - Timer0
32768
TIMSK.1:=1;//== OIE0:=1 -

end;
//== 0 OCR0
var b: boolean;
var i,t0: byte;//== v.4.09 !!
interrupt TIMER0COMP;//==
0
//== 1 TOSC=32768
begin
b:=not b; //==
PORTE.7:= b; //==
for t0:=0 to 6 do //== ""
sound:=not sound; //== _|`|_|`|_|`|
PORTD.7:=sound;
for i:=0 to 255 do endfor; //== ""
endfor;
end;
procedure Init_ADC; //==
begin
ADCSRA:=$8E; //== () =
64
//== (6000000/64=93750 - 50..100000)
ADMUX:=$20;//== ADLAR=1-
ADCH
end;
interrupt ADCRDY; //==

begin
adccode:= word(ADCH);//== . (.. 8-
)
ADCSRA.6:=1;//==
end;
function Dec2_8seg_Str (decn:word):string[5];//== 10 = 1023max
var s:string[5];
temp:char;
remn:word;
44

begin
s:='';
repeat
remn:=decn mod 10; //==

decn:=decn div 10; //== 10


if remn<>0 then
temp:=key8segm[remn]; //== 1 9
else
temp:=key8segm[remn+11]; //== 0 ""

endif;
s:=s+temp; //== 's' ( ELAB)
until decn<1;
return(s); //== ( "123" 321)
end;
procedure Display8seg; //== (8-
. )
var i,j:byte;
begin
maskind:=%11110111; //==

for i:=2 downto 0 do //== 3 E (PORTE.6 .. PORTE.4)


maskind:=maskind rol 1; //==
if not b then maskind:=maskind and $7F;endif;//== LED (PORTE.7)
"",
//==
PORTC:=byte(s[i+1]); //== 8-

PORTE:=maskind; //== 8- . -
"
for j:=0 to 255 do endfor; //==
endfor;
end;
{ Main Program }
{$IDATA}
begin //==
Init_Ports; //== /
Init_Timer0Async; //== 0
Init_ADC; //==
EnableInts; //==
loop //==
while not ScanKeyOnce(kn) do endwhile;//==
45

key:=byte(keyASCII[kn+1]);//== +1, .. 0- -
!
while ScanKeyOnce(kn) do endwhile;//==
case key of //== -
(#,*,0..9)
'#': //== ( #)
PORTE:=%11101111; //== 8- . -
-
PORTC:=byte(key8segm[11+1]);//== 8- . '#'
//== LED
loop //==
sound:=not sound; //== ""
PORTD.7:=sound; //==
for j:=0 to 255 do endfor;//== .
if ScanKeyOnce(kn)then exitloop;endif; //== ,
endloop;
//==
| //==
'*': //== ( )
ADCSRA.6:=1; //== (
]
//== )
loop //==
s:=Dec2_8seg_Str(adccode);//== 8 ADLAR=1
s:=s+#$FF+#$FF+#$FF; //== FF - .
//== "__8" "_48"
Display8seg; //==
if ScanKeyOnce(kn)then //== ,
ADCSRA.6:=0; //==
exitloop; //==
endif;
endloop;
| //==
'0'..'9': //== (0..9)
e2prom[key-'0']:=byte(key8segm[kn+1]); //== EEPROM
(E2PROM)
e2temp:=e2prom[key-'0'];//== EEPROM
//== AVRPROG EEPROM ,
//== .
PORTE:=%10111111;//== 8- . -
-
PORTC:=e2temp;//== 8- . LED

| //==
endcase;
endloop;
46

end myATMega128.

5.2.8
1.
2.
3.
4.
5.
6.
7.
8.

.
ATMega128 ( 1.1).
ATMega128 (.1.3).
(.1.5).
/0 ( TCNT0, OCR0, TCCR0).
( ADCSRA, ADMUX, ADCH/ADCL).
8- .

47

68HC908
6.

:
1. - (LCD) 4-
( 4 8- ),
2. SPI
() DS1722,
3. - - (PWM)
-
,
4. .
5. ( , ..
).
6. . 1-4

.

7.
1. .
2. ( )
.
3.
.
4. ,
.
5.
ASCII .

48

8. ATMega128
8.1

MC68HC908GP32 8-
CPU08, Flash- 32 , 512
.
CPU08 8-
90 . -
: 8- A () CCR,
16- H:X, SP
PC. :
- CGM08,
- SIM08,
- LVI08,
- BREAK08,
- IRQ08,
- COP08,
- TBM08.
CGM08
, SIM08
. CGM08
.
SIM08 ,
.
: CGM08, LVI08,
IRQ08, BREAK08, COP08, ,
, .
BREAK08

.
IRQ08
, IRQ#,
.
COP08
.
LVI08
.
TBM08
.

49


,
.

MC68HC908GP32

:
A, B, C, D, E - ,
SCI08,
SPI08,
KBI08,
8- - ADC08,
TIM08.
A, B, C, D, E
. A, B 8
-, E - 2 , C, D - 5 8
, .
A, B, D, E
- KBI08, ADC08, TIM08-1, TIM08-2, SPI08, SCI08.

,
,
- .
SCI08, SPI08

.
TIM08 ,
,
,
.
- ADC08
, 8
, 8- .
KBI08

,
.
8.2 /
68HC08/908 64
( $0000-FFFF).

,
. 2.1
MC68HC908GP32.
50

$0000
$003F
$0040
$023F
$0080
$7FFF
$8000
$FDFF
$FE00
$FE01
$FE02
$FE03
$FE04
$FE05
$FE06
$FE07
$FE08
$FE09
$FE0A
$FE0B
$FE0C
$FE0D
$FE1F
$FE20
$FE52
$FE53
$FF7D
$FF7E
$FF7F
$FFDB
$FFDC
$FFFF

2.1


(64 )

(512 )

(32 192 )
Flash-
(32 256 )
SBSR ( BREAK08)
SRSR (
)

SBFCR (
BREAK08)
INT1 (
)
INT2 (
)
INT3 (
)

FLCR ( Flash)
BRKh ( BREAK08)
BRKl ( BREAK08)
BRKSCR (
BREAK08)
LVISR ( LVI08)

(19 )

(307 )

(43 )
FLBPR ( Flash)

(93 )

(36 )
51

,
,
.
.
64 ( $000-$003F)
( . 2.2
). , 16-
TCN, TMOD, TCHx
: l ( L
), h.
.
2.2

$0000
PTA

A, B, C, D
$0001
PTB
$0002
PTC
$0003
PTD
$0004
DDRA

A, B, C, D
$0005
DDRB
$0006
DDRC
$0007
DDRD
$0008
PTE
E
$000D
PTAPUE

A
$0010
SPCR
SPI08
$0011
SPSR
$0012
SPDR
$001A
INTKBSCR
KBI08
$001B
INTKBIER
$001C
TBCR
TBM08
$001D
INTSCR

$001E
CONFIG2

$001F
CONFIG1
$0020
T1SC

TIM08-1
$0021-22
T1CNTh-l
$0023-24
T1MODh-l
$0025
T1SC0
$0026-27
T1CH0h-l
$0028
T1SC1
$0029-2A
T1CH1h-l
$003B
PMDS
$003C
ADSCR

ADC08
$003D
ADR
$003E
ADCLK
52

,
SP.
() SP $00FF,
.
TXS,
SP H:X,
1. SP 1,
. ,
. (
) H:X
TSX.
MC68HC908GP32 Flash-,

.
10000 -,
10 .
,
.
Flash-.

.
512
, $0040-$023F.
.
,
-,
,
. ,
.

.
68HC08/908
. 2.3
.

53

2.3
ADC08
$FFDEDF
KBI08
$FFE0E1
0 TIM08-2 $FFF0-F1
TIM08-1 $FFF2-F3
1 TIM08-1 $FFF4-F5
0 TIM08-1 $FFF6-F7
$FFFAIRQ#
FB
SWI (
$FFFC)
FD

$FFFE(Reset)
FF

.
,
.
(. 2.3) -
, .

9. ,
,

9.1 /

(, ,
, .)
, ..
(.2.1).

54

. 2.1.
#RESET
( DDRx
),
.
DDRx . . 2.1
, PTC ,
DDRC (Data Direction Register C) 7F.
4 5 PTD
, . DDRD ..
. 4 PTA
. 3 PTA3..1 ,
DDRA 0E.
PTD3..1
( SPI) .
DDRD #SS.

55

9.2


. 12-
(4 3 ) PTA1-PTA7 A.
.
PTA
.2.2.
(PTAi/ KBDi),

. ,
,
. ,
(
) ,
. .

.2.2.

KBD3..1 KBD7..4.
, PTA7..4
( F).
( 1-->0) PTA7..4
KBI08 . 2.3.
.5, PTA7..4 1101=13.

PTA3..1 , ( PTA5)
.
keynum=col+row*3 (0..11) key ASCII (char
key[ ] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#',''};).
col=1 row=1, keynum=4
5 ( , .. key
).
56

(. . 13 15)
PTA7..4
, ,
( PullUp) .2.3.

.2.3. x
Vdd
PTAPUE
PTA7..4 (PTAPUE=0xF0).
DDRA
, 1 3 (
), 2 PTA7..4 ,
.


INTKBIER (. 2.4).

. 2.4. - INTKBIER

( MODEK=0
) ( IMASKK=0). ..
RESET , INTKBSCR
.

57

. 2.5. INTKBSCR
( KBDinit)
:
KBDinit(){//==

DDRA=0x0E;//== 4 . - ( ), 3 PTA1..3
-
PTAPUE=0xF0;//== 4 (PullUp)
PTA4..7
//== ,
PTA=0xF1;//== PA1..PA3 (
)
INTKBIER=0xF0;//== - . (``|__)
PTA4..7
}
KBD_int (. )
ACKK=1,
.
,

PTA7..4
(r=(PTA&0xF0)>>4)) rown (row=rown[r]).
F0 , .. ,
.
PTA3..1 (PTA|=0x08>>i)
PTA7..4.

,
..
4

1111=F
(if((PTA&0xF0)==0xF0)), (col=2-i)
(keynum=col+row*3).
.
char key[]={'1','2','3','4','5','6','7','8','9','*','0','#',''};//== ASCII
char keynum, keypressed=0; //== "
"
char rown[]={0,0,0,0,0,0,0,3,0,0,0, 2, 0, 1, 0, 0};//==

7
11 13 14
58

void KBD_int(void){//==
char row,col,temp,r,i; //== col - , row -
//==
INTKBSCR|=(1<<ACKK); //==
Delay(800); //==
if((r=(PTA&0xF0)>>4)==0x0F)goto exit; //== r=0111(7), 1011(11),
1101(13), 1110(14)
//== "goto exit"

//== r = 1111 0111..1110


row=rown[r]; //== = (0,1,2,3)
for(i=0;i<3;i++){//==

PTA|=0x08>>i; //== ""

//== PTA3..1(col=2..0)
if((PTA&0xF0)==0xF0){col=2-i;break;}//== 1 0
//==
}
keynum=col+row*3; //== (0..11)
//== key
exit:
PTA=0b11110001; //==

INTKBSCR|=(1<<ACKK); //==
//== . PTA
keypressed=1; //== ()
(
//== - main())
}
9.3
8- - ADC08
(. 2.6), MC68HC908GP32,
,
, ,
- ADSCR, ADCLK
ADR. AD0-AD7 ADC08
PTB0-PTB7 B.
,
B -.

59

. 2.6 B

ADSCR, :
7
COCO

6
AIEN

5
4
3
ADCO ADCH4 ADCH3

2
1
0
ADCH2 ADCH1 ADCH0

COCO - ,
, AIEN = 0;
COCO = 1 ,
AIEN =1
;
ADCO - :
ADCO=0,
ADCO = 1,
ADCH4-ADCH0 -
2.4.

60

ADCH4ADCH0
00000
00001
00111
1000-11100
11101
11110
11111

2.4

AD0 (PTB0)
AD1 (PTB1)
AD7 (PTB7)

Vrf


,
ADCH4-ADCH0 ADSCR.
ADR
U, U
$FF, - $00.
U 0 < U < Uo, Uo ,
. Uo = 5, ,
-1
20 , 10 .
ADC08 ,
ADCLK,

:
7
6
5
4
ADIV2 ADIV1 ADIV0 ADICLK

3
0

2
0

1
0

0
0

ADICLK - :
Ft ADICLK = 1
Fq ADICLK = 0;
ADIV2-ADIV0 -
Kadc (. 2.5).
ADIV2-ADIV0
000
001
010
011
1xx
61

2.5
Kadc
1
2
4
8
16

-
Fa 1.
Ft (
ADICLK = 1) Fq (
ADICLK = 0). Kadc = Ft/Fadc
Kadc = Fq/Fadc ADIV2-ADIV0
ADCLK 2.5.
17 , Fa = 1
17 , T = 17 / Fadc.
- ADR:
7
AD7

6
AD6

5
AD5

4
AD4

3
AD3

2
AD2

1
AD1

0
AD0

9.4
U (
) 7- (. 2.7),
ADCH4..0 ADSCR 00111(BIN)=7(DEC).

. 2.7. 7-
( )
,
AIEN 1.
( ) ADCO=0.
ADSCR 47(HEX).
Fadc (. 2.6)
1.
Fq ( CGMXCLK). Fq=4.915.200,
Kadc 8-. ADIV2..0 = 011(BIN),
ADICLK=0. .. ADCLK 60(HEX).

:
InitDevices(){//==
................................
ADCLK=0x60; //== - ADIV1,0=1(Fq/8),
62

//== ADICLK=0, Fq = 4.915.200 / 8 ~= 0.6


ADSCR=0x47; //== AIEN=1(..),
ADCH4..ADCH0=00111(
//== AD7(PTB7) )
................................
}
char ad;//== 8-
void ADC_int(void){//==
//==
ad=ADR; //== )
ADSCR|=0x07; //== ( ADCH4..ADCH0
//== . )
}
9.5 1
. 2.8 .

. 2.8.

,
63

( ),
-
() .
MC68HC908GP32
TIM08, TIM1 TIM2. TIM08 (. 2.8)
/ 2 ,

.
/ 16-
TxCNT, 16- TxMOD 8-
- TxSC. x
1 2 TIM1
TIM2. 16-
TxCHi 8- - TxSCi,
i : 0 1.
- T1CH0 T1CH1 TIM1
PTD4 PTD5 D ,
T2CH0 T2CH1 TIM2 PTD6
PTD7.

. 2.9.
/ TIM08
TxSC.
.
7
TOF

6
TOIE

5
TSTOP

4
TRST

3
64

2
PS2

1
PS1

0
PS0

TOF - , ;
TOF=1, TxCNT
, TxMOD;
TOIE - TOIE = 1
( TOF = 1);
TSTOP - TxCNT
TSTOP = 1;
TRST - TRST = 1
TxCNT $0000 PS2-PS0 =
000; 0
TxCNT;
PS2-PS0 - Kd=Ft/Fc
TxCNT (. 2.8). 2.6
Kd TIM08.
PS2-0
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

2.6
Kd
1
2
4
8
16
32
64

TxCNT
,
Fc = Ft/Kd, Kd
PS2-PS0 TxSC (. 2.6).
TxCNT , TxMOD,
.
TOF = 1, ,
TxSC TOIE = 1. Mc,
TxMOD, 2 65535
( TxMOD=0xFFFF). TxMOD
1.
TSTOP TxSC.
i-
- TxSCi.
.
7
CHiF

6
CHiIE

5
MSiB

4
MSiA

3
ELSiB
65

2
ELSiA

1
TOVi

0
CHiMAX

CHiF - i- ,
; CHiF = 1,
;
CHiIE - CHiIE = 1
i- ( CHiF = 1);
MSiB, MSiA - i- (.1.7);
ELSiB, ELSiA -
(.8.3);
TOVi - TCHi ,
, /:
TOVi = 0,
TOVi = 1;
CHiMAX - Km
-: CHiMAX=0 Km
i- TxCHi,
CHiMAX=1 Km= 1.
,
- MSiBMSiA ELSiB-ELSiA TxSCi (.2.7
).
2.7. TIM08.

MSiB-A ELSiB-A
01
01


TxCHi
-

( )
01
10
0
TxCHi
01
11
1
TxCHi
1X
01


TxCHi
-

( )
1X
10
0
TxCHi
1X
11
1
TxCHi
( MSiB-MSiA = 01)
TxCHi
Kc, . TxCNT
, TxCHi
, TxSCi
CHiF = 1. TxCHi
66

( 0, 1
) ELSiB-ELSiA (. 1.7).
CHiF = 1
, CHiIE = 1.
9.6
1
9.6.1 -

() ,
1 1 PTD4/T1CH1 (. 2.10).

.2.10.
- Tc,
() Tp (. 2.11).

. 2.11. -
-
.
(), RC .

Tp / Tc.
,
1/Tc
Tc.

1 T1SC. ,
Kd
67

, ..
.
(CGMOUT) Kd=Ft/Fc (. 2.8
2.9) 64.
(prescalera) PS2,PS1,PS0=110. ,
1 4.915.200/2/64 =
38400.
(.1.5-1).

TOIE=1. 1
T1MOD=400.
38400/400=96 (
). , T1SC
0x46.

- T1SC1 1.

T1CNT T1CH1
(.1.5). CH1IE 1.
1 PTD5/T1CH1
. MS1B=0, MS1A=1; ELS1B=ELS1A=1.
TOV1
1.
Tp.

. 2.12.
68

T1SC1 : T1SC1 = 0x40 |


0x10 | 0xC | 0x2 = 0x5E.
T1CH1=1.
200.
1

.
2.12.
: ()
TxCHi, .
TxCHi Kc ,
TxCNT ,
(
). ,
TxCHi ,
- .

.

TxSCi MSiB = 1 (.1.7).
TxCHi
, 0-1.
0 ,
1 Kc.
TxSC0 . ,
-
TxCH0.
T1CH1,
.
9.6.2
,
38400/400=96.
96
1. 60
sec ( min).

1 (TIM1_ovf)
1 (TIM1_cmp) . 2.13:

69

. 2.13.

9.7
DS1722S
SPI
, (master),
(slave) . 2.14.

. 2.14. SPI
SPI ,
:
"
SPSCK - SCLK
;
"
MOSI - SDI
;
"
MISO - SDO
;
"
SS# - :
SS#=0, -
SS#=1. : DS1722S 70

CE (Chip Enable, ChipSelect),



. MC68HC908GP32 SPI08
PTD3..0 D.
9.7.1
SPI08 ,
SPDR.
.
.
SPI08 SPCR
- SPSCR,
2.15 2.16.
SPCR
7
SPRIE

6
0

5
SPMSTR

4
CPOL

3
CPHA

2
SPWOM

1
SPE

0
SPTIE

. 2.15. SPI
SPSCR
7
SPCRF

6
ERRIE

5
VRF

4
MODF

3
SPTE

2
1
MODFEN SPR1

0
SPR0

. 2.16. SPI
SPCR :
SPRIE - SPRIE=1
( SPSCR SPRF=1);
SPMSTR - :
SPMSTR=1 SPMSTR=0;
CPOL, CPHA - ;
SPWOM -
SPSCK, MOSI, MISO:
SPWOM=0, " " SPWOM=1;
SPE - SPE=1 SPE=0 SPI08;
SPTIE - SPTIE=1
, (
SPSCR SPTE=1).
SPSCR:
SPCRF - , SPCRF=1

;

71

OVRF - , OVRF=1,
,
;
MODF - , MODF=1,
SS#=0,
SS#=1;
SPTE - ,
SPTE=1,
.
SPSCR ,
-:
ERRIE - EERIE=1
OVRF=1 MODF=1;
MODFEN - MODFEN=1
SPI08 MODF;
SPR1..0 - Kd,

SPR1-0
00
01
10
11

Kd
2
8
32
128


.

.
8- SPSR
SPCRF=1. ,
SPCR SPRIE=1.
SPSCK SPI
0, SPCR
CPOL=0, 1, CPOL=1.
8- ,
SPDR SPI08.
SPSCK ,

.

CPHA, .

72

9.7.2 DS1722
DS1722
(), SPI
. DS1722
. ,

.
DS1722 ,
.
8- ( 1 o), 9-, 10-, 11 12- ( 0.0625 o ) . ,
.

DS1722 $01 $02 .
:
, - .
,

DS1722. $00 $80 .
2.17:
7
1

6
1

5
4
3
2
1
1
1SHOT
R2
R1
R0
. 2.17.

0
SD

1SHOT - SD = 1, 1SHOT = 1,
, 1SHOT 0.
R0-R2 -
2.8.
2.8
R2 R1 R0 ,

,
0
0
0
8
0.075
0
0
1
9
0.15
0
1
0
10
0.3
0
1
1
11
0.6
1

12
1.2
SD - DS1722
. SD = 0,
, SD = 1,
DS1722 .
D7..5 1.
73

9.7.3 SPI
InitDevices 0
D #SS,
DDRD 1 (
). PTD0 = CE = 0, ..
. SPMSTR
SPE SPCR 1,
SPI.
InitDevices (.
2.18):

. 2.18. SPI
9.7.4


, 1SHOT = SD = 1.
1 o ( R2,R1,R0 = 0).
11110001 = F1.
SPIctrl() (. 2.19-1),
Send_SPI_Byte() (.
2.19-2):

. 2.19-1.
74

. 2.19-2.

SPIdata(),
main().

....................................................................................................................................

. 2.20.

9.8 4-

RS(~C/D),
RW(R/~W) E (PTC6..4). 8-
4-
(. 2.21).

75

. 2.21. 4- .
9.8.1
ASCII .
5x8 5x10.
: DDRAM (Data Display RAM) ,
, , CGROM
(Character Generator ROM) ,
CGRAM (Character Generator RAM) ,
,
( CGROM) .
(DDRAM) 80 (80
). 16- 20-
() 00h,
= 40h ( 2.9).
0
0
0
4
0

2.9 ( DDRAM )
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0f 1 1 1
0 1 2
1 2 3 4 5 6 7 8 9 a b c d e
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4f 5 5 5
0 1 2
1 2 3 4 5 6 7 8 9 a b c d e

1
9
1
3
5
3

2.10 5 x 8 ,
(CGROM) , .
8 e (CGRAM) ,
. 8 CGRAM,
8 . CGRAMe
16 .

76

2.10

CGROM 5 x 8 5 x 10
8- . , ,
,
ASCII , . ,
DDRAM F

(ASCII = LHLL LHHL = 0100 0110 = 46h).


.
CGROM = HLHL LLLL = 1010 0000 = A0h,
DOS WIN(CP-1251) .
WIN = c1h.
,
,
CGROM. ,
,
, CGROM \xa0 (
).

77

CGROM ,
8- CGRAM.
(. 1.9.1) 8
( 5
5x8) CGRAM (0..7)
44780 (Hitachi).
( ) 2.11.
8-
: RS(~Control/Data), RW(Read/~Write) E, D7..D0.
E -. RS ,
: RS=0
RS=1. RW R/~W, ,
: .
2.11
RS(~C/D) R/~W D7 D6 D5 D4 D3 D2 D1 D0
0
0
0
0
0
0
0
0
0
1
0

ID

SC RL

DL

BF

78


/
.

/


CGRAM


A..A

.


""
,

ID -
S -
D - / (1/0)
C - / (1/0)
B - / (1/0)
SC - / (1/0)
RL - : / (1/0)
DL - : 8/4 (1/0)
N - : 1/2 (0/1)
F - : 5x10/5x8 (1/0)
BF - : 1

A
H
9.8.2

(. 2.22):

. 2.22.
4- (, )
PTC3..0. 4 (. 2.23).

. 2.23.
LCD
( 2.11),
(. 2.24):
79

. 2.24.

( 2.25):

. 2.25.
LCDNctrl(0x80+(x+((y)?0x40:0)));
,
1AAAAAAA (. 2.11) AAAAAAA =
1000000 = 40h , AAAAAAA =
0000000 = 0 ( . 2.9).

80

10.
10.1
ImageCraft IDE icc08
.
HC08.
. "Project | New ".
C:\EMUL\Work\ ( )
68hc08_grXXXX, 68hc08_gr
. XXXX , 8888.
, myHC908.
,
(. 2.26).

. 2.26.
. File | New.
Untitled-1. . File | Save As
68hc08_gr8888 , c,
my08.c (. 2.27).

. 2.27
,
Files Project,
(Add File(s)) my08.c.
81

, View Project File


Window ..

(. 2.28),
.

Project | Options.
Target Compiler
. GP32 68HC08.
sprintf()
(),
float.
HEX
*.S19.

. 2.28.
OK .
82

10.2


/ iogp32.h
hc08def.h.
main.
(. 2.29).

. 2.29.


.
, ,

, .

!
. ,
.
10.2.1 /
InitDevices(),
/ . 2.1,

68HC908GP32. .
, 2.30 .

83

. 2.30.

(while(8888)),
..

- main()

. ,
, . ,
84

,
.
#include <float.h> #include <stdio.h>
sprintf()
:
, ,
- .


.
.
(main) ,
.
10.2.2

. 2.31.

85

- - (PWM)
. 2.31
.


. ,
16- myHC908.s19 S Record.
HEX- (. . 10)
.
COM .
( SA1).
10.2.3
IDEicc08 HC08
/,
- WinIDE
,
S Record, .
,
Programmer - (.2.32).

.2.32. Win IDE


(. 2.33),
.
Contact.

. 2.33.
86

,
2.34.

. 2.34.

(. 2.24-1).

Done

. 2.24-1. -
16- ()
(). *.s19 -
HEX- *.hex (. 2.35).

87

. 2.35. 16-
.

-.
Module has been erased (. 2.36).

. 2.36. -
88


Programmed
(. 2.37).

. 2.37. -
,
(
COM ).

,
.
89


. .
.
Reset
COM ,
(. 2.38),
(Reset),
(Halt). ,
.

. 2.38.

10.2.4 (LCD)
LCD ,
.. .
,
LCDNibble(...) .2.1
(, )
. LCD
. 0xF0(11110000) 0x0F(00001111)
/
( - &) ( - |).

2.10 2.11.
4- ,
,
.
LCDNstrn(), LCDNclr() LCDNxy()
, ,
.
90

Delay() .
.
, 2.39.

91

. 2.39.
,


(. . 10.2.3).

( ..

main())..
10.2.5 ( )

(. 2.40). 7-
(ADC7 . 2.1). ,
, ,
,
.

. 2.40.

92


FFDE,
ADC_int(). ,
( ADCO ADSCR 0),
,
,
...
, main()
(. 2.41): (:
: ,
!)

. 2.41.
sprintf() 2
(float) ad.
()
. ,
.
ASCII
sa. .
,


(. . 10.2.3).
.

( , . 2.42).

93

. 2.42.
10.2.6
.
SPI
.

(. 2.43).

main() (. 2.44).

94

. 2.44. main()
: , ,
.
:

. 2.45.
10.2.7

(. 2.46).

95

. 2.46.

.
LCD .

. 2.46.

(
).
96

10.2.8
(
, . . 2.47).

. 2.47.

, . 2.48:

. 2.48.
, ,
;-(

10.3
97

#include <iogp32.h>
#include <hc08def.h>
#include <float.h> //== sprintf
#include <stdio.h> //== sprintf
#define LCD_CTRL PTC //== PORTC
#define LCD_DATA PTC
#define LCD_RS 6 //== RS(~C/D) 6-
#define LCD_E 4 //== E (4- )
#define LED 5 //== 5- D ()
#define SS 0 //== 0- D ( #SS SlaveSelect
ChipSelect)
#define SPCRF 7 //== 7- SPSCR ( SPI)
#define TOF 7 //== 7- T1SC ( 1)
#define CH1F 7 //== 7- T1SC1 (
1)
#define Tc 400 //== 1 (38400/Tc - .. )
#define ACKK 2 //==
#define IMASKK 1 //== IMASKK=0,

//==
#pragma interrupt_handler TIM1_ovf
unsigned int d;//==
char sec=0,min=0,m=0;
void TIM1_ovf(void){//== 1
T1SC&=~(1<<TOF); //== TOF
1
d=T1CH1;//== Tp
d++;//==
if (d==201)d=1;//=
Tp
T1CH1=d;//== - T1CH1

if(++m==(38400/Tc)){m=0;//== ( )
if(++sec==60){sec=0;if(++min==60)min=0;}//==
}
}
#pragma interrupt_handler TIM1_cmp
void TIM1_cmp(void){//==
1
T1SC1&=~(1<<CH1F);//== CH1F
1
}
#pragma interrupt_handler ADC_int
char ad;//== - 8-
void ADC_int(void){//==
98


ad=ADR;//== ADR (
)
ADSCR|=0x07;//== (7- ), ADCO=0
}
#pragma interrupt_handler KBD_int
char key[]={'1','2','3','4','5','6','7','8','9','*','0','#',''};//== ASCII
char keynum, keypressed=0;//== "

char rown[]={0,0,0,0,0,0,0,3,0,0,0, 2, 0, 1, 0, 0};//==


//==
7
11 13 14 //==
void KBD_int(void){//== (
!)
char row,col,temp,r,i;//== col - , row -
INTKBSCR|=(1<<ACKK);//==
Delay(800);//==
if((r=(PTA&0xF0)>>4)==0x0F)goto exit;//==
r=0111(7),1011(11),1101(13),1110(14)
//== "goto exit"

//== r = 1111 0111..1110


row=r[rown];//== = (0,1,2,3)
for(i=0;i<3;i++){//==
PTA|=0x08>>i;//== ""
PTA3..1(col=2..0)
if((PTA&0xF0)==0xF0){col=2-i;break;}//== 1 0
}
keynum=col+row*3;//== (0..11)
key
exit:
PTA=0b11110001;//==
INTKBSCR|=(1<<ACKK);//==
. PTA
keypressed=1;//== () (

}
//== - main())
//== .
( 1.3)
#pragma abs_address:0xFFE0
void (*_vec_keyboard)(void) = KBD_int;
#pragma end_abs_address
#pragma abs_address:0xFFDE
void (*_vec_adc)(void) = ADC_int;
99

#pragma end_abs_address
#pragma abs_address:0xFFF2
void (*_vec_tim1_ovf)(void) = TIM1_ovf;
#pragma end_abs_address
#pragma abs_address:0xFFF4
void (*_vec_tim1_ch1)(void) = TIM1_cmp;
#pragma end_abs_address
InitDevices(){//==
CONFIG1|=1;//== COP ( COPD=1)
//== CONFIG ,
//== ( . )
//=========
ADCLK=0x60;//== ADIV1,0=1(Fq/8),ADICLK=0
ADSCR=0x47;//== AIEN=1(..), ADCH4..ADCH0=00111(
AD7(PTB7) )
//========= 1
T1SC1=0x40;//== CH1IE=1 - .
1 1
T1SC1|=0x10|0x0C;//== MS1B=0,MS1A=1; ELS1B=ELS1A=1 (
T1CH1=1 -)
T1SC1|=0x02;//== TOV1=1
T1CH1( 0)
T1CH1=1;//== - (Tp)
//========= 1
T1SC=0x46;//== TOIE=1(. .), 4.915.200/2/64 = 38400 (PS2..PS0
= 110(bin)->(64))
//== 1 Fosc/2=4.915.200/2
T1MOD=Tc;//== (Tc=400)
//========= D
DDRD=1<<LED;//== 5- ,
//========= SPI
( D)
DDRD|=1<<SS;//== #SS
(ChipEnable)
PTD&=~(1<<SS);//== #SS=CE=0 ( - CE(CS)
. .)
SPCR=0x2A;//== SPMSTR=1(MCU - ()), SPE=1(
SPI)
//========= C
DDRC=0x7F;//== 0..6 C LCD
PTC=0;//==
}
Delay(unsigned char time){//==
asm( //== ( time=1 100)
100

"Var_Delay:\n"
//== Var_Delay
"lda #10\n"
//== 10
"loop:\n"
//== loop
"deca\n"
//== 1
"bne loop\n" //== - 0, loop
"dec %time\n" //== time 1
"bne Var_Delay\n" //== - 0,
Var_Delay
); //== . . time Delay
}
LCDNibble(nib){//== nib ( LCD
)
PTC&=0xF0; //== 3 4

PTC|=nib & 0x0F; //== 4


PTC|=1<<LCD_E; //==
_|`|_
PTC&=~(1<<LCD_E);//== _|`|_
Delay(1);//== 100us ( >= 40)
}
LCDNchar(char ch){//==
LCD_DATA|=1<<LCD_RS;//== ( RS=~C/D=1)
LCDNibble(ch>>4);//== (nibble)
LCDNibble(ch&0x0F);//==
}
LCDNctrl(char ch){//==
LCD_CTRL&=~(1<<LCD_RS);//== . (
RS=~C/D=0)
LCDNibble(ch>>4);//== ,
LCDNibble(ch&0x0F);//==
}
LCDNinit(){//==
Delay(150); //== 15 ms
LCDNibble(3);
Delay(41); //== 4.1 ms
LCDNibble(3);
Delay(1); //== 0.1 ms
LCDNibble(3);
LCDNibble(2);
LCDNctrl(0x28);//== 4- ( DL=0,N=1)
LCDNctrl(0x0C);//== ( D=1)
LCDNctrl(0x01);//==
Delay(200); //== 20 ms
LCDNctrl(0x06);//== ( ID=1)
}
LCDNstrn(char *s){ //==
101

while(*s!=0)LCDNchar(*s++);//== , *s 0
}
LCDNclr(){LCDNctrl(1);Delay(200);}//==
LCDNxy(char x,char y){//==
//== x = 0..15 , y=0/1 - ./.
LCDNctrl(0x80+(x+((y)?0x40:0)));
}
Send_SPI_Byte(char c){//==
SPDR=c;//== SPDR, MOSI
//== ()
while((SPSCR & (1<<SPCRF))==0)continue;//==
c=SPDR;//== !!!! ( )
//== .. SPI -

}
LCDN_ReadSPIValue(){//==
char c,s[10];
c=SPDR; //== . SPI
sprintf(s,"%d",(signed char)c);//==
ASCII
LCDNstrn(s);//==
}
SPIctrl(){//==
DS1722
PTD|=1<<SS;//== () CE (
ChipSelect)
Send_SPI_Byte(0x80);//== 80- - . (00-
)
Send_SPI_Byte(0xF1);//==
1SHOT=SD=1(.),R2=R1=R0=0( t- 8 )
//== |1|1|1|1SHOT|R2|R1|R0|SD| - .
PTD&=~(1<<SS);//== CE
Delay(150);//==
}
SPIdata(){//==
PTD|=(1<<SS);//== () CE
Send_SPI_Byte(0x02);//== t-
Send_SPI_Byte(0x02);//== 8- t- .
//== 9..12- t- 0x01(.), 0x02(.)
PTD&=~(1<<SS);//== CE
}
KBDinit(){//==
DDRA=0x0E;//== 4 . - ( ),3 PTA1..3 -

PTAPUE=0xF0;//== 4 (PullUp)
102

PTA4..7
//== , -

PTA=0xF1;//== PA1..PA3 ( )
INTKBIER=0xF0;//== PTA4..7
}
void main(void){
char sa[8];//==
InitDevices();//==
LCDNinit();//==
KBDinit();//==
keynum=12;//== ( -
'')
CLI();//== (asm("cli\n")),(SEI()(asm("sei\n"))
//== : . cli - , sei(sti) )
LCDNinit();//== (
)
LCDNstrn("U(Vt) = ");//== "() = ...."
LCDNxy(0,1);//== LCD
LCDNstrn("T(oC)=");//== " = "
SPIctrl();//==
while(0x8888){//==
LCDNxy(11,1);//== 11
sprintf(sa,"%02d:%02d",min,sec);//== '0' -

LCDNstrn(sa);//== LCD :
LCDNxy(9,1);//==
sprintf(sa,"%c",key[keynum]);//== sa ASCII

LCDNstrn(sa);//== ASCII
LCDNxy(8,0);//== 8-

sprintf(sa,"%#1.2f %03d",((float)ad*(float)5)/(float)256,ad);//== #-

//== U=0 . 0, -
.!
LCDNstrn(sa);//== .
SPIdata();//==
LCDNxy(6,1); //== LCD,

LCDN_ReadSPIValue();//== t- LCD
}
}
103

11.
1.
2.
3.
4.
5.
6.
7.
8.

.
.
.
(. 2.1).
/1 ().
().
LCD .
.

104


1. .. . .
(http://faculty.ifmo.ru/electron), 2003.
2. .. .
(http://faculty.ifmo.ru/electron), 2004.
3. A.B.Ecee. Mpoopoep AVR ceec iny Mega p
Atmel. -o "oea-XXI", M, 2005.
4. M.C.oyo, A.B.Kpeoa. Mpoopoep AVR: o
pocoo cooy. -o "COOH-pecc", M, 2004.
5. A.B.Ecee. Mpoopoep AVR ceeca Classic p
Atmel. -o "oea-XXI", M, 2004.
6. B.H.apao. peee poopoepo AVR: cxe,
aop, popa. -o "oea-XXI", M, 2004.
7. B..po, A.B.Ka. Cce a poopoepax C
popapyeo o. -o "KOM", M., 2002.
8. ..ayp. Copeee poopoep popoeccop
Motorola. -o "opa -eeo", M., 2004.
9. B.B.Ca p. poepoae pox ycpoc a
oopcax poopoepax. -o "epoaoa",M.,
1990.
10. Oopcae poBM. Cpao. -o "o", 1994.
11. Ma peo. Cpao o PIC-poopoepa. -o
"oea", M, 2002.
12. Kpca aepe. PIC-poopoep.paa pee.
-o "MK", M, 2003.
13. B.C.eo. Mpoopoep MicroCHIP. -o "opa eeo", M., 2005.
14. A.B.eo. Kocpypoae ycpoc a MK. -o "H", C,
2005.
15. A.B.pye. Mpoopoep? o e poco! -o "CKMEH",
M, 2003.
16. ..Pe. peoe cce copa ax ceeca
MSC12xx. -o "oea-XXI", M, 2006.

105

2007
20072008 .




: ..., . .. .
( )
1945 .
..
,
, ,
, 1938 .,
- (1946 .).
.. c 1945 1978 .
50
, .
, 1978 . 1985 . ...,
.. , .. .
1985 2006 ...,
.. ,
.
2006 . ..., .. .

,
,
.
..
:
;
106


;

, ;

;
;

;
;

;
.

( .. ):

;
600 ;
18

:
;
;
;

.

107