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

1. ..................................................................................................

2
1.1. .................................................................. 2
1.2 AVR ATmega8535
. 1............................................................................................ 2
1.3. ................................................................. 3
1.4. ............................................ 5
2. ........................................................... 9
1. . ................................................... 9
AVR .................................. 9
1.2. 8- AVR ....... 30
1.2. 8-
AVR ..................................................................................................22
1.3.
...........................................................................................................30
1.4. AVR...........................................34
1.5. 16-
...........................................................................................................39
1.6. -
...........................................................................................................44
1.7. UART .................................................................48
3. .......................................................................................57
2.1.
AVR ..........................................................................................................57
2.2. / ....................................66
2.3.
AVR .............................................................................................71
2.4. AVR...........................................74
2.5. -
AVR ..................................................................................................81
2.6. -
AVR ..................................................................................................94
2.7.
.........................................................................................................101
................................................................................................................................115
1. AVR ..............................116
2. AVR..................................127
3. .........................................135
4. / ATmega8535 .............................158
5.
ATmega8535 ..............................................................................................160

1.
1.1.

ATmega8535

AVR,

Atmel.

()

USB- .
-
(, , , , ,
, ).
1.2 AVR ATmega8535
. 1.
1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


(Flash ROM),
(Static RAM),
(EEPROM),
8535/8535L,
8535/8535L,
/
8-
16-
,

10- ,
UART
SPI
I2C
RC-



8
512
512
06/08
4,55,5/2,75,5
32
2
1
4
+
8
1
1
1
+
+
+
130
PDIP40

1.3.
. 1 , . 2
.

1.

2.
,

:
;
32 / , Port A,
Port B, Port C, Port D 7, 6, 0.

390 ;
( );
.
RESET .
13;
S1, S2 S3S8
0 5.
0 5
3

390 .

0)

,
( 1)
;
RP1 RP2
05;
G 50 ( 0-5);
( );
;

.
T=10 ,
1;
VD7VD0 ;

;
Rx Tx UART
RS232.

, MAX232;
;
A, B, H HG1HG4
.
(AH)
(HG1HG4).
(. . 2,):
1 0,25;
2 ~ 220;

3 DB9
USB ( USB-DB9).

SPI . ,
RESET .
,
.

PB5,
SPI.

PB6

PB7


/.
,
, , ,
. ,
.
1.4.

,
.


UART ;

RX TX 0 1 D, ;

pk&mk.exe ;

COM2,
- COM ,
;
5

,
(
);

, ,
.
,
,
,
.
(
) ,
;

.
. .

.
. ,
(
) (
).
;

,
, , ,
;

,
() ,
;

,
.


.

.

,
.

,
.

, .
(X0X17)
(Y0Y17) .

.

PK&MK .

Y i Xi
.
.
.

. .

COM ,
COM .
9600
.
COM ,
.

2.
1. .
AVR

1. ,
ICCavr .
2. / .

1.
:
,

AVR

ATmega8535 (, ,
..) ;
"ImageCraft " (ICCavr),
;
AVR Studio,
;

USB

prog,

AVR.
2. () (. 1):
AVR ATmega8535-8PI,
. 1 ( );
;
0... 7 : Port A, Port , Port Port D
/ ;
SA1...SA6 SB1, SB2
;

RP1, RP2
;
G 50 ( 0-5);
;
;
VD1...VD8
(+5- , 0 - ;
, );
...
HG1...HG4 ( i HGj
i HGj);
;

;
;
;
Rx UART
RS232.

, 232.

10

1.

~220,

SPI.
ATmega8535
1


(FLASH),
(RAM),
EEPROM,
/
:

: 8-
16-

-

RISC
130
8
512
512
32
17
3
2
1
4
8 , 10
SPI, TWI, UART

11

,
,

,
, 6 SLEEP

,
,

16/8 (L)
4,5...5,5/2,7...5,5(L)

3. :
;
;
;
.
4.
"ImageCraft " ( ICCavr)
AVR . ,
,
.
"*." ( "*.prj"),
"*.hex",
.
4.1. ICCavr

(ImageCraft Development ToolsICC AVR).


(. 2), :
1 - ;
2 - ;
3 - ;
4 - .

12

2. ICCavr

4.2.
: Project New
( ProjectNew).
, ,
- , , tmp .
.
4.3.

, .
(. 3).

13

3. ICCavr


( Target CPU 8535)
(Xtal) - 8 .
, , ,
UART SPI, , .
Options
main, Preview - .
, Ok.
"Untitled - 0"

14

4.

: FileSave ,
"*.", , "test.".
:
) ;
) - ;
) ..
4.4.
, ,
, ,
, .. ,
main()
, , :
...
void

main(void)

{
15

while (1)
;
}

4.5.
ProjectAdd Files
, , "test.c".
"Files"
.
4.6.
ProjectOptions
. (. 5).

5.


: Target Device Configuration
ATMega8535.

16

4.7.

(Build Project),

, ,
,
"*.hex" ( test.hex).
, .
, .
(
).
5.
5.1. ,

, .
AVR Studio,
Atmel.
, /, /, ,
. .
5.2. AVR Studio

(Atmel AVR ToolsAVR Studio 4).

17

6. AVR Studio

Open
*.hex, , test.hex.
*.aps (
test_hex.aps).
, Ok.
Debug platform Device.

18

7.

Debug platform AVR Simulator, Device


, ATmega8535
Finish.
(. 8),
:

19

8.

I/O View
/;
Disassembler,
- ,
(. 8);
Message,
.
.
5.3.
(. 9) :

20

9.

;

, ;
;
;
, ,
.
, FFFF, .

.
5.3. /
.
+. , :
, .. , ,
/ : PORTA,
DDRA PINA (. 10).

21

. "0" "1".
.

10. /

5.4.
AVR Studio
. Debug
, :
Run

,
;

Break ,
;
22

Reset ;
Step Over ,
, ;
AutoStep
.

AutoStep,

.
.
Break,
/, .. . ,

AutoStep,

(. 11).

23

11.

5.5.

.
USB prog.
, ,
,
,
.
hex- Intel (Intel Standard Intel
Extended).
USB prog,
(. 12).

24

2
3
4
6
8

. 12.

.

(AVR 1).
*.hex
( ).
( 4).
:
( 8);
( 7);
;
( 5);
( 9).
.

25


.

, 2
.
ATMega8535

Atmel

(http://www.atmel.com)

.

.
7.
:

, , ..
.
.
(
, ..)

.
,

.
, .

26

8.
1. AVR,
D 2
. .
#include <iom8535v.h>
#include <macros.h>
void port_init(void)
{
PORTA = OxFF;
DDRA = 0x00;
PORTD = 0x00;
DDRD = OxFF;
}

//
// mega8535
//
// /
// (DDRA=0000 0000)
// D
// D (DDRD=1111 1111)

void init_devices(void)//
{
CLI();
//
port_init();
//
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00;
//
SEI();
//
void main(void)
{
init_devices();

//
//
//
//

while(1)
{
if((PINA&4)!=0) // 2
// (PINA=0000 0100)
PORTD=0xF0;
// PORTD=llll 0000
else
//
PORTD=0x0F;
// PORTD=0000 1111
}
}

2. ,
(
).
#include <iom8535v.h>
#include <macros.h>
void port_init(void)
{
PORTD = Ox01;
DDRD = 0xFF;
}

//
// mega8535
//
// /
// 0 D (PORTD=0000 0001)
// D
27

void init_devices(void)
{
CLI();
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00;
SEI();
void main(void)
{
unsigned int I;
//
init_devices();
while(1)
{
for(i=1;i;i++) //
;
PORTD=PORTD<<1;
if(PORTD==0)
//
PORTD=1; //
}
}


// D
0,
1


1. 1
. .
2. : PB0,
C. PB0=0 C
, PB0=1
.
3. 2 :
2 ;
2 .
4. , :
PD0, PD1 .. ,
D, .
5. ,
.
6. , RS-:
0 ( 0, 1)
0 ( 1).
28

7. , ,
( S1-S8), D.
8. , ,
, .
9. ,
- -.

29

1.2. 8- AVR

ATmega8535. 0.
8-
- .

1. ATmega8535 :
0 2, 1.
:
) ;
) ( 2);
) -
();
) (
2).
2.
/ (..
)
/. .

, /
. . 1.

/.
.
,
1


0
0
0
PB0
0
0
PB3
1
1
1
PB1
1
1
PD5
1
1
PD4
2

OSC1
OSC2
2

1
2
2
2
2

PC6
PC7
PD7

:
. ,
0 0
3 : DDB3 1.
1.


TIMSK .
TIMSK .2, - . 3
30

TIMSK

7
OCIE2

6
TOIE2

5
TICIE1

4
OCIE1A

3
OCIE1B

2
TOIE1

R/W
0

R/W
0

R/W
0

R/W
0

R/W
0

R/W
0

T2

2
1
0
OCIE0 TOIE0
R/W
0

T1

R/W
0
T0

TIMSK
3

7
6
5
4
3
2
1
0

OCIE2
TOIE2
TICIE1
OCIE1A
OCIE1B
TOIE1
OCIE0
TOIE0

2
2
T1
A T1
B T1
T1
T0
T0


TIFR . . 4,
- . 5.
TIFR
4

7
6
5
4
3
2
1
0

OCF2
TOV2
ICF1
OCF1A
OCF1B
TOV1
OCF0
TOV0

/
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W

0
0
0
0
0
0
0
0

T2
T1
T0
TIFR

7
6
5
4
3
2

OCIE2
T2
TOIE2

T2
TICIE1
T1
OCIE1A A
T1
OCIE1B B
T1
TOIE1

TIMER2_COM
TIMER2_OVF

4
5

TIMER1_CAPT
TIMER1_COMPA

6
7

TIMER1_COMPB

TIMER1_OVF

9
31

1
0

OCIE0
TOIE0

T1
T0

T0

TIMER0_COMP
TIMER0_OVF

20
10

TIFR
.
0.
2. T0
2.1. T0
T0 VR
:
- TCNT0 (Timer CouNT);
- OCR0 (Output Compare Register);
- TCCR (Timer/Count Control Register).
2.2. T0
T0 :
- ;
- ;
- - .
2.3. T0
T0 :
- TCNT0;
- TCNT0 OCR0.
TIFR,
TIMSK.
2.4. TCNT0
TCNT0 .
, ,
( 1) ( 1)
. ,
. TCNT0
.
0..255.
255
TOV0 TIFR
(. . 5). 1 TOIE0
TIMSK.
2.5. OCR0
OCR0 , (
) TCNT0.
OCF0
TIFR ( TIMSK). ,
OC0 . ,
T0 OC0 (
TCCR0) PB3 (DDB3=1).
2.6. TCCR0

32

TCCR0 T0.
.6, .7.
TCCR0
6

7
6
5
4
3
2
1
0

FOC0 WGM00 COM01 COM00


WGM01
SC02
SC01
SC00

/
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W

0
0
0
0
0
0
0
0

TCCR0
7

FOC0

3,6

WGM01:
WGM00

5,4

COM01:COM00

2..0

SC02..SC00

OC0 ( Normal
CTC)

WGM01
WGM00

0
0
Normal
0
1

1
0
( )
1
1

OC0
COM01
COM00
OC0
0
0
OC0
0
1

1
0
0 0
1
1
0 1

SC02
SC01
SC00

0
0
0

0
0
1
=1
0
1
0
=8
0
1
1
=64
1
0
0
=256
1
0
1
=1024
1
1
0
T0 (. )
1
1
1
T0 (. )

3.
1. 0 .
0.
-------------------------------------------------------------------// 0
//:
//: Port D
#include <iom8535v.h>
#include <macros.h>
int i=0
// i
33

void port_init(void)
{
PORTD = 0x00;
DDRD
= 0xFF;
}
void timer0_init(void)
{
TCCR0 = 0x00;
TCNT0 = 0x64;

TCCR0 = 0x05;
0000 0101

// D
// 0
//
//
// TCCR=
//SC02:SC01:SC00=101
> Kdel=1024
//
COM01:COM00=00
> OC0
//
WGM 01:WGM00=00 >

Normal
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void) //
//
{
TCNT0 = 0x64;
//
i++;
// i
}
void init_device(void)
{
CLI();
port_init();
timer0_init();
TIMSK = 0x01;
// TIMSK = 0000 0001 >

// 0
SEI();
}
void main (void)
{
init_devices();
while(1)
PORTD=1;
//, : PORTD=i/50;
}
------------------------------------------------------------------ :
) I, i=0;
) D, port_onit () ;
) timer0_init T0.
, .
TCCR0 = 0x00;

TCNT0 = 0x64;
Kdel, .

TCCR0 = 0x05;
//TCCR0=0000 0101
1024 Normal.
34

)
#pragma interrupt handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x64;
//
i++;
// i
}
T0, 10
(. .5), timer0_ovf_isr ,
. :
TCNT = 0x01,
i. ,
i . ?
:
f=fCLK/KDEL/(ET0-N),
fCLK - fCLK =8;
KDEL - , KDEL =1024;
ET0- T0, ET0 =255;
N- , N= 0x64=100.

f = 8 000 000 / 1024 / (255-100) = 50,
, 50 T0
i;
) init_ devices ()
TIMSK = 0x01;
T0.
TIMSK = 0000 0001 . 3 TOIE=1;
e) main() (..
T0) PORTD = i;
D i.
2. 0 -
----------------------------------------------------------------// T0
//: PA0,PA1
//: PB3 ( )
#include <iom853v.h>
#include <macros.h>
void port_init (void)
{
PORTA = 0xFF;
DDRA = 0x00; //
PORTB = 0xFF;
DDRB = 0xFF; // (
// PB3)
}
void init_devices(void)
35

{
port_init();
timer0_init();
}
void main(void)
}
init_devices();
while(1)
{
switch(PINA&3)
{
case 0:
OCR0=0x00;
.. =0
break;
case 1:
OCR0=0x40;
.. =0,25
break;
case 2:
OCR0=0x80;
.. =0,5
break;
case 3:
OCR0=0xFF;
.. =1
}
}
}
------------------------------------------------------------------
1. 1 ,
D? .
2. , D
1.
3. , ( PA0..PA2)
: =0; 0,125; 0,25; 0,375; 0,5; 0,675; 0,75; 1.
4. T2
5. 8-
: 1 PA0 T0 (
) 0 1 5 (
T2,
). PA0 1 0
.

2.
2.1.

36


. () , ... (. 6,).
(. 6, , ). , , , ( )
,
. "2" (. 6, )
, , D, G.

6.
- , - ,
, - "2"
2.2.
4 HG1...HG4 ( 2, )
. HG1... 4
.
. , ,
HG2 "0" "1" ,
, ... "1" HG2.

, ,
, "123" HG1..-HG3? ,
,
,... . ,
"3" "1" HGI, HG2 1 ,
.

2.

, , .
"123" HGI-HG3 :
HG1 "1", HG2 "2",
HG3 "3".
25 , . ,
25*3=75 .
4.
,
"123". . -
37

: 0-7, HGI-HG3 D: PD0-PD2.


1.
//-----------------------------------------------------------------//

7-

//
"123"
//
:

//
:
PORT

//
PORT
D HG1,
G2,
HG3
#include <iom8535.v.h>
#include <macros.h>
#define NUM l000
//---------------void main(void)
{
unsigned int i;
DDRC =0xff;
// C
PORTC=0x00;
//
DDRD =0x07;
// C
PORTD=0x00;
//
for (;;)
{
// 1 HG1
PORTC=0x06;
// b c: 1
PORTD=0x01;
// HG1
for (i=0; i<NUM; i++);
// 5
// 2 HG2
PORTC=0x5b;
// a, b, g, e, d:
2
PORTD=0x02;
// HG2
for (i=0; i<NUM; i++);
// 5
// 3 HG3
PORTC=0x4f;
// a, b, c, d, g:
3
PORTD=0x04;
// HG3
for (i=0; i<NUM; i++);
// 5
}
}
----------------------------------------------------------------3. ,
.
,
( ).
,
, ,
.
, .
123,
T0.
2.
-------------------------------------------------------// 7-
// T0
38

// 123
// :
// : PORT C
//
PORT D HG1, HG2, HG3
#include <iom8535v.h>
#include <macros.h>
unsigned char c1=0x06, // 1
c2=0x5B, // 2
c3=0x4F; // 3
unsigned char n;
//
//------- T0------#pragma interrupt_handler indic:10
void indic(void)
{
PORTD&0xF8;
// HG1-HG3
switch (n)
{
case 0:
PORTC=c1;
//
PORTD=0x01;
//
break;
case 1:
PORTC=c2;
//
PORTD=0x02;
//
break;
case 2:
PORTC=c3;
//
PORTD=0x04;
//
break;
}
n++;
//
n%=3;
}
//-------- -----------------------------------------void main(void)
{
DDRC =0xFF; // C
PORTC=0x00; //
DDRD =0x07; // D
PORTD=0x00; //
TIMSK=0x01; // 0
_SEI();
// 7

SREG
TCCR0=0x04; // =0000 0100, 256,
// 8000000/256/256=122 ,
// (3 ): 122/3=40
for(;;);
}
//------------------------------------

39

1. ,
, , 2007.
2. , 0
9. , , A:
PA0PA3.
3. ,
2 A: PA0PA3,
PA4PA7.
4. 0,5
0 200. .
2. ,
( 0 9- ), .

4. AVR


ATmega8535.
.

1.
-
. , , ( )
, -
. ,
.
.

,
40

.
PC
. , ,
.
RETI,
.
, ,
, .
2. AVR.
AVR, Mega
. ,
$0000, .
,
.
: , .
1
AVR.
RJMP.
1 I ( 7) SREG (. . 4),
.
ATMega
.
, .

.

GICR (. . 4): 18(1- ) IVCE (0- ).
IVSEL . ,
,
1 .
BOOTSZ1 BOOTSZ0.
IVCE IVSEL.
IVCE 1
IVSEL.
IVCE , IVCE
,
IVSEL.
IVCE. I
SREG .
.
ATmega8535 . 3, . 1.
, ,
.

1
2
3
.
19

1. ATmega8535

()

RESET
$0000
0
INT0
$0001
0
INT1
$0002

0
INT2
$0012
41

I SREG ,
.
1
. (
RETI) I . ,
SREG .

( )
RETI ( ).
3. ATmega
3.1. . 1 ,
( INT0, INT1, INT2).
. 2.
2.

INTO
INT1

PD2
PD3

INT2

,
. ,
INT1 :
PORTD = 0x08;
// PD3(INT1) DDRD
// D (
// 3 DDRD)

= 0x00;

:
) ( GICR);
),
, .. ( MCUCR
MCUCSR);
) .
3.2 3
GICR (. 3). GICR 1,
.
3. GICR -

7
INT1
INT1
6
INTO
INTO
5
INT2
INT2
, 7 GICR 1,
INT1:
GICR

= 0x80;

//1000 0000 - INT1

3.3. MCUCR
MCUCSR. . 4 MCUCR

42

INTO INT1, . 5 - 6 MCUCSR


INT2.
4. MCUCR


ISC01 ISC00

1,0

ISC01
ISC00

INT0

INT0

INT0
INT0

INT0

ISC11 ISC10

3,2

ISC11
ISC10

INT1

INT1

INT1
INT1
INT1

5. 6 MCUCSR

ISC2

ISC2


INT2

INT2

INT2

, INT1 ()
3 2 ISC11 ISC10 MCUCR
1 0:
MCUCR = 0x08;
// 0000 1000 - INTO
// D

3.4. ,
, . .
.
.
GIFR . 6.
6. GIFR -

7
INTF1
INT1
6
INTFO
INTO
5
INTF2
INT2
, ,
INT1, 7 GIFR ( INTF1) 1. INTF1
43

,
1. INTF1 ,
INT1
.
:
#pragma . , ,
INT1
;
#pragma interrupt_handler intl_isr:3 //
// INT1 ( - 3)
void int0_isr(void)
//
{
//
}

4.
INTO,
, .
50
( ), PD2 (

INTO).

.
//-------------------------------------#include <iom8535v.h>
#include <macros.h>
unsigned char count=0,
pri2_on=0;
// -------------void port_init(void)
{
PORTB = 0x00; //

DDRE = 0x01; //

PORTD = 0x04; //
PD2(INT0)
DDRD = 0x00; //
}
// ---------------// INT0 2
#pragma interrupt_handler int0_isr:2
void into isr(void)
{
count++;
if (count==50) //
{
count=0;
//
priz_on=!priz_on; //
if (priz_on) PORTB|=1;
else
PORTB&=0xfe;
}
// ---------------void init_devices(void)
{
CLI();

44

port_init 0;
MCUCR = 0x03; // 0000 0011 - INTO -
//
GICR = 0x40; // 0100 0000 - INTO
TIMSK = 0x00; //
SEI();
}
// ---------------void main(void)
{
init_devices() //

for {;;) ;
}
// ---------


1. 1 , .
2. 1 :
) INT1 ;
)

INT2

.
0,5 ;
)

INTO

.
- 1 .
3. INT1 ,
( ) .
. INT1
50 .
,
1 ( 1 ).
INT1, .
,
50 , , , 1
.
4.

7-

.
, .
5. I .
7- 1 .
.

45

5. 16-

16- ATmega8535.
.

1. 1
16- 1
ATmega8535. / 1
. ,
. -
.
1 . 1.

1. 1

2. / 1
46

/ .
, / , ,
/,
. ATmega8535
:
PB1 (T1) 1;
PD6 (ICP) 1;
PD4 (OC1B), PD5 (OC1A) 1.
/ ,
,
.
3.
1
( 1), .
, 1.
1 . 2.

2.


SFIOR ( ).
4. 1
/ 1 /:
16- TCNT1;
16- ICR1;
16- OCRIA, OCR1B;
8- TCCR1A, TCCR1B.
16-
/,
( ) L ( ). TCNT1, ,
TCNT1H:TCNT1L.
4.1. TCNT1 OCR1A OCR1B
1 ,
( ,
1) TCNT1 .
,
. TCNT1
TIMER0_OVF 1.
OCR1A OCR1B .
OCR1A OCR1B
OC1A OC1B .
TIMER1_COMPA TIMER1_COMPA.
4.2. TCCR1A TCCR1B
47

/ : TCCR1A,
TCCR1B. . 13.
1. TCCR1A

7, 6
COM1A1:COM1A0
A B.
OC1A OC1B
5, 4
COM1B1:COM1B0
.

/
3, 2
FOC1A:FOC1B

OC1A OC1B
1, 0
WGM11:WGM10
/.
WGM13:WGM12 TCCR1B
/ 1

7
ICNC1

6
ICES1

5
-

2. TCCRB1
4
3
2
1
0
WGM13 WGM12 CS12
CS11
CS10

: WGM13WGM10
. COM1n1:COM1n0 0
1 OC1x.
CS12..CS10
1. .
3.
3. 1
TCCR1B
1
CS12 CS11 CS10
0
0
0
/
0
0
1
clkI/O
0
1
0
clkI/O/8
0
1
1
clkI/O/64
1
0
0
clkI/O/256
1
0
1
clkI/O/1024
1
1
0
1,
1
1
1
1,
4.3. ICR1
ICR1 ,
/ ICR1.
ICP
, .
ICF1 TIFR
. 1 TICIE1
TIMSK. TCCR1B
ICNC1 ( 7) ICES1 ( 6). ICNC1 .
0,
. 1,
4 , .
, ,
( 1
0 ). , .. ,

48

ICES1/ 0, .
1, .
ICP, (
DDRx, ICP, 0).
, ,
. ,
/
. ,
. 2,5..3,5 .
4 .
5. TIMSK TIFR
/ /
TIMSK (Timer/Counter Interrupt MaSK Register
/). T1 :
TICIE1 ( 5) /
1;
OCIE1A ( 4) A
/ 1;
OCIE1 ( 3)
/ 1;
TOIE1 ( 2) / 1.
TIFR
(Timer/Counter Interrupt Flag Register /).
1 :
ICF1 ( 5) / 1;
OCF1A ( 4) / 1;
OCF1 ( 3) / 1;
TOV1 ( 2) / 1.
- TIFR
1.
0. 0
1.
6.
1. , ,
C ICP(PD6). ..
1.
T=1024*65535/8*10 6=8,38 .
.
//-----------------------------------------------// 16-
//: ICP(PD6)
//:
#include <iom8535v.h>
#include <macros.h>
//---------------------------------void port_init(void)
{
PORTC=0x00;
DDRC=0xFF;
// C
}
//---------------------------------void timer1_init(void)
// 1
{
49

TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x01;
TCCR1A=0x00;
TCCR1B=0xC5;

//
//
//
// Normal,
// 1024,
// ICP,
//

}
//------ T1
#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
CLI();
TCCR1B=0;
//
v=ICR1L
//
v|=(int)ICR1H<<8;
PORTC=v/7812;
//
//
TCNT1H=0x00;
//
TCNT1L=0x01;
//
TCCR1B=0xC5;
//
SEI();
}
//---------------void init_devices(void)
{
CLI();
port_init();
timer1_init();
TIMSK=0x20;
//

T1
SEI();
}
//----------void main(void)
{
init_devices();
for (;;);
}
//----------------------------------------------------- 2. T1 - .
OCR1A OCR1B T1,
A.
//-------------------------------// 1 -
//: PA0-PA3
//PA4-PA7 .
// 16
//: OCR1A (PD5) -
//OCR1B (PD4) -
#include <iom8535v.h>
#include <macros.h>
//--------------------------void port_init(void)
50

{
PORTA=0x00;
DDRA=0x00;
//
PORTD=0x00;
DDRD=0x30;
// D
}
//_____________________________________
void timer1_init(void)
// 1
{
TCCR1B=0x00;
// 10-
TCCR1A=0xA3;
//, 1, OC1A OC1B

TCCR1B=0x09;
// 0
}
//----------------------------------void init_devices(void)
{
CLI();
port_init();
timer1_init();
SEI();
}
//------------------void main(void)
{
init_devices();
for(;;)
{
OCR1AH=(PINA&0x0f)>>2;
//
OCR1AL=(PINA&0x0f)<<6;
// PA0-PA3
OCR1BH=(PINA&0xf0)>>6;
//
OCR1BL=(PINA&0xf0)<<2;
// PA4-PA7
}
}
//-----------------------------------------
1.
( 50 ) .
:
;
.
2. ,
0,1 . 999,9 . .
3. , .
.
,
.
6. -

ATmega8535.
( ).
51


1. ATmega8535
ATmega8535 8
. 10-
. :
: 2 ;
: 0.5 ;
: 65...260 . . 1.

1.
:
- 8- ;
- 2,56 ;
- ;
- .
ADC0...ADC7
, 0...7 (. 1). ,
, , ,
,
.
. 1, ADC0...7,
1,22 (Bandgap Reference) .
52


, (2,56) .
(ADC
Conversion Complete IRQ).
2.
4 :
) ADMUX -
;
) ADCSRA -
) ADCH, ADCL - ( 10, 2 ).
2.1. ADMUX

ADMUX (. 1).
1. ADMUX

7,6
REFS1:REFS0
5
ADLAR

3...0
MUX3...MUX0

(. 2). ,
( 10- - 1023). 4
(. 3). 8-
. (10-
) 5 ADLAR ADMUX. 1,
16- ,
.
2.
6,7 ADMUX

REFS1 REFSO
, AREF;
0
0
AVcc
0
1
2,56, AREF
1
1

3.
3..0 ADMUX

MUX3...MUX0
0000
ADC0
53

0001
0010
0011
0100
0101
0110
0111
1110
1111

ADC1
ADC2
ADC3
ADC4
ADC5
ADC6
ADC7
1.22
OB(GND)

2.2. ADCSRA
ADCSRA ,
( ), ,
.
. 4.

7
6
5
4
3
2...0

ADEN
ADSC
ADFR
ADIF
ADIE
ADPS2:ADPS0

4. ADCSRA

(1 - , 0 - )
(1 - )



ADEN ADCSRA
1.
1 ADSC ADSRA.
:
,
;

.
ADFR. 1,
.
.
ADFR ,
.
25
, 14 - , 14 -
.

.
ADPS2..ADPS0 ADCSRA.
ADCSRA . 5, . 2
.
5.
ADPS2 ADPS1 ADPSO
54

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

,
50...200 .
,
. 10 ,
, .
ADCHM ( 4) SFIOR. 1
.
.

1 ADIE ADCSRA 1 ( 7) SREG.
, ADIF
1.
2.3. ADCL ADCH
, 16-
ADC. , 10
8- /: ADCH ADCL, .
ADCL.
, ADCH.
3.
;
;
;
-
- ( 1 ADSC).
.
( 1 ADIF ADCSRA)
.
4.
, ADC3 (3-
- ), 10-, 8-
D.
//----------------------------------#include <iom8535v.h>
#include <macros.h>
//--------------void port_init(void)
{
PORTA = 0xFF;
//

DDRA = 0x00;
//
55

PORTD = 0x00;
DDRD = 0xFF;

// D
}
//-------------Void adc_init(void) //
{
ADMUX = 0x03;
// 3 (3),
// - AREF,

ACSR
= 0x80;
//
ADCSRA = 0;
// 1100 1110
// ||| | | =64
// ||| |

// |||

// ||

}
// |

//

// =15
#pragma interrupt_handler adc_isr:15
void adc_isr(void)
{
int v;
CLI();
v=ADCL;
//
,
v|=(int)ADCH 8; //
v>>=2;
// 2
// 8-
PORTD=v;
// D
ADCSR|=0x40;
//
SEI();
}
// ---------------------void init_devices(void) //
{
CLI() ;
port_init();
//
adc_init();
//
SEI() ;
}
// ----------void main(void)
{
init_devices() ;

56

for (;;);
}
//-------
1. ! , 10
: D - 8 , - 2 .
2. :

.
3. 0,5
ADC1 0,1 .
2,56,
.
7. UART

UART ATmega8535.
.

1.

(USART) ATmega8535.

(UART).
2. .
,
. -
( 1). ,
-.
1
. . 1,
1.
/.
(, 38400, 19200, 9600, 4800, 2400 ..) () >.

FRAME

(IDLE) St

[ P] Sp1 [Sp2] (St/IDLE)

St Star bit, always low


(n) Data bit (0 to 8)
P Parity bit. Can be odd or even.
Sp Stop bit, always high.
IDLE No transfers on the communication line (RxD or TxD).
An IDLE line must be high.
1. UART
57

, , RS-232,
RS-485, RS-422 . RS-232.
3. US ART (UART)
: , .
ATmega8535:
PD0 - RXD UART (),
PD1 - TXD UART ().

/ UDR (Universal Data Register).
8 .
UDR , -
.
4. USART (UART) : UCSRA,
UCSRB UCSRC. . 1...3.
1. UCSRA

. 1
( UDR).
RXC ( UART -
). RXCIE UCSRB ,

. 1
, ,
UDR . TXCIE

UCSRB ,
.
, . 1
. >
( UDR
). ,
UDRE
. UDR1E UCSRB
, .
,
. 1
, .. - 0.
FE -, >

. USART >,
-
, ( ). UART
3 DOR(OR) >,
,
.

58

. 1, ,
, .
0

. >,
16 8,
U2X
. USART U2X
.
.
. 1,
, . ,
,

2. UCSRB

RXCIE

.
1, RXC UCSRA
( I SREG 1)

.
1, UCSRA
TXCIE
( I SREG
1)
UART.
1, UDRE UCSRA
UDRIE
( I SREG
1)
. 1
USART/UART
RXEN RXD. RXEN ,
. , DOR FE

. 1
- UART TXD.
TXEN 0 ,
,

. ,
. USART
UCSZ2(C
UCSZl:0 UCSRC.
HR9)
UART, CHR9 1,
9- , - 8-
8- . 9-
.
RXB8
USART
UDR
59

8- . 9-
,
0
8
.
UDR
5. ,
, ,

. UBRR.
, -
, (2, 8 16)
USART/UART.

URSEL

UMSEL

UPM1

UPMO

USBS

UCSZ1

UCSZO

UCPOL

3. UCSRC

. ,
. 1,
UCSRC. 0,
UBRRH.
USART. 0, USART
. 1, USART

.
.
UPM1:UPM0:
00
- ,
10
- ,
11
- .
1 -. -,
. 0, 1
-, 1, 2 -.
.
. UCSZ2
( ).
UCSZ2:UCSZ0:
000-5 , 001-6 , 010-7 , 011-8 , 111-9 .
.
.
.
0. 0 -
TXD ,
RXD ; 1 -
TXD , RXD
.

60

UERR 12-
/ UBRRH:UBRRL. UBRRH ,
UCSRC.
.
URSEL.
, UBRRH.
1, UCSRC.
.
UBRRH.
UCSRC.
.
UBRR,
U2X UCSRA. 1,
,
.
.
, , BAUD -
, fck - , UBRR -
(0...4095):
(, U2X=0)
BAUD=fck/(16(UBRR+l));
(, U2X=1)
BAUD=fck/(8(UBRR+l));

AUD=fck/(2(UBRR+1)).
, , :
150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200 ..

, , .
2%, - 0,5%.
. 4 UBRR
8 .
UBBR ( , UBBRL), ,
.
4. UBBR
U2X=0 U2X=1
[]
UBRR *[%] UBRR *[%]
2400
207 0.2
4800
103 0.2 207 0.2
9600
51
0.2 103 0.2
19200
25
0.2
51
0,2
38400
12
0.2
25
0.2
57600
8
-3.5 16
2.1
76800
6
-7.0 12
0.2
115200
3
8.5
8
-3.5
250000
1
0
3
0

500
1
6. UART ,
( , , -)
UCSRA-UCSRC,
UBRR.
61

UCSRB
I SREG.
RXC UCSRA.
1, , , UDR.
UDR UCSRA
. UART UDRE
UCSRA. 1, .
,
UDR ( ),
UCSRA 1.
UDR. ( ),
UART.
ATmega8535 .
UCSRA UCSRB,
5-9 ,
, ,
UART.
7.
,
UART. .
1.
// -------------------------------------------------------------#include <iom8535v.h>
#include <macros.h>
//--------------void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
// -
PORTC = 0x00;
DDRC = 0xFF;
// -
}
//--------------void uart0_init(void)
// UART
{
UCSRA = 0x00;
UCSRC = BIT{URSEL)|0x06; // - ,
UBRRL = 0x33;
// - 8,
,
UBRRH = 0x00;
//-, 9600 ,
UCSRB = 0x80;
//

}
// -------

#pragma interrupt_handler uart0_rx_isr:12


void uart0_rx_isr(void)
{
PORTC=UDR;
//
UDR=PINA;
//
UART
UCSRB|= 0x08;
//
62

UCSRB|= 0x10;
SEI();

//

}
// -------------void init_devices(void)
{
CLI();
port_init();
uart0_init();
SEI();
}
//-------------void main(void)
{
init_devices(); //
UART
UDR=PINA;
//
UCSRB|= 0x08;
//
UCSRB|= 0x10;
for(;;);
}
//-------------------------------------------
1. 1 . UART
, UART.
.
2. .
: "Master" "Slave",
. UART
.
3.
.
4. (8 ) -
4 , . 2 , ,
. .

63

Оценить