:
− , , , , , 2100 .;
− 56 ;
− TWI (I2C);
− ;
− ;
− 500 A 25 C;
− -40 C +85 C.
. 7.6.
. 7.6. DS1307
DS1307 8- DIP. DS1307 . 7.6.
TWI . 7- 1101000.
. 7.7.
. 7 0 . 0 7- 0.
7.6
DS1307
VCC | ||
X1, X2 | 1, 2 | 32,768 |
VBAT | (+3 ) | |
GND | ||
SDA | - | |
SCL | ||
SQW/OUT | / |
7.7
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |||
0x0 | CH | 0059 | ||||||||
0x1 | 0059 | |||||||||
0x2 | 12 24 | PM/AM | 112 + AM/PM 023 | |||||||
0x3 | 0107 | |||||||||
0x4 | 0131 | |||||||||
0x5 | - | - | 0112 | |||||||
0x6 | 0099 | |||||||||
0x7 | OUT | SQWE | RS1 | RS0 | - | |||||
0x8 0x3F | - | - | - | - | - | - | - | - | 568 | 0x00xFF |
7.6.
. 2. S1S12, . VT1, VT3, VT5 VT7, Q0Q3 DD3 HC138. PC.0PC.2. PC.5PC.7. PF.0PF.3, PE.2, PD.5 PD.7 , I2C PD.0, PD.1.
|
|
7.7.
1. TWI (I2C)?
2. , TWI?
3. ?
4. TWI .
5. TWI ?
6. .
7. DS1307?
8. DS1307?
9. TWI.
7.8.
31.100, :
1. .
2. , ( ).
3. , .
4. , 1.
5. , 1. .
6. , 10 1.
7. , 6 .
8. , 8 !.
9. .
10. .
11. , 6 1.
12. , 8 1.
13. 15 .
14. 15 .
15. . .
7.9.
.
.include "m128def.inc"
.cseg
.org 0000
jmp Init
Init: ;
ldi R16,0x00 ; , 0400
out SPL,R16
ldi R17,0x04
out SPH,R17
sbi DDRD,5; R/W
sbi DDRD,7; RS
sbi DDRE,2; E
ldi R16,0b00001111; DB7..DB4
sts DDRF,R16
cbi PortD,5; R/W = 0 (Write)
;
ldi R17,0
dec R17
brne PC-1
dec R18
brne PC-3
cbi PortD,7 ;RS = 0
ldi R16,0b0011
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
dec R17
brne PC-1
dec R18
brne PC-3
ldi R16,0b0011
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
dec R17
brne PC-1
ldi R16,0b0011
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
ldi R16,0b0010 ;4-
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
ldi R16,0b0010 ; 4-
sts PortF,R16
sbi PortE,2
|
|
call delay
cbi PortE,2
ldi R16,0b1000 ;N = 1, F = 0 (. 57 )
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
;
ldi R16,0b0000
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
ldi R16,0b1000 ;D = 0, C = 0, B = 0
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
;
ldi R16,0b0000
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
ldi R16,0b0110;I/D = 1, S = 0
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
;
ldi R16,0b0000
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
ldi R16,0b1111;D = 1, C = 1, B = 1
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
; TWI
clr r21
clr r22
clr r23
clr r25
;-
clr r17
clr r18
clr r19
; :
clr r16
; 2
ldi r25,5 ;
sts TWBR,r25
; .
ldi r25,(0<<TWPS1)|(0<<TWPS0)
; , 1
sts TWSR,r25
;
;
ldi r26,0x00
ldi r27,0x03
;
ldi r28,0x00
ldi r29,0x03
;
ldi r16,0x00
st x+,r16
andi r26,7
brne pc-3
clr r30
clr r31
rcall DS_Start ;
start:
; 0x80
cbi PortD,7 ;RS = 0
ldi R16,0x8 ;
sts PortF,R16 ;
sbi PortE,2 ;
call delay ;
cbi PortE,2 ;
ldi R16,0x0 ;
sts PortF,R16 ;
sbi PortE,2 ;
call delay ;
cbi PortE,2 ;
; "" 0x42
ldi r16,0x42
call lcd_print_symbol
; "" 0x70
ldi r16,70
call lcd_print_symbol
; "" 0x65
ldi r16,0x65
call lcd_print_symbol
; "" 0xBC
ldi r16,0xBC
call lcd_print_symbol
; "" 0xC7
ldi r16,0xC7
call lcd_print_symbol
; " " 0x20
ldi r16,0x20
call lcd_print_symbol
; " " 0x20
ldi r16,0x20
call lcd_print_symbol
rcallDS_get_time ;
;
ldi R16,0x3
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
mov r16,r23
andi r16,0xf0
swap r16
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
;
ldi R16,0x3
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
mov r16,r23
andi r16,0x0f
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
; ":" 0x3A
ldi r16,0x3A
call lcd_print_symbol
;
ldi R16,0x3
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
mov r16,r22
andi r16,0xf0
swap r16
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
;
ldi R16,0x3
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
mov r16,r22
andi r16,0x0f
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
; ":" 0x3A
ldi r16,0x3A
call lcd_print_symbol
;
ldi R16,0x3
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
mov r16,r21
andi r16,0xf0
swap r16
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
;
ldi R16,0x3
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
mov r16,r21
andi r16,0x0f
sts PortF,R16
sbi PortE,2
call delay
cbi PortE,2
; " " 0x20
ldi r16,0x20
|
|
call lcd_print_symbol
jmpstart ;
DS_Start: ;
call i2c_start ;
ldi r25,0b11010000 ;
call i2c_send ;
ldi r25,0x00 ;
call i2c_send ;
call i2c_start ;
ldi r25,0b11010001 ;
call i2c_send ;
call i2c_recive_last ;
cbr r25,0b10000000 ; 7-
push r25 ;
call i2c_stop ;
call i2c_start ;
ldi r25,0b11010000 ; +
call i2c_send ;
ldi r25,0 ;
call i2c_send ;
pop r25 ;
call i2c_send ;
call i2c_stop ;
ret
DS_get_time:
; DS1307
; R21, R22, R23
Call i2c_start ;
ldi r25,0b11010000 ;
call i2c_send ;
ldi r25,0x00 ;
call i2c_send ;
call i2c_start ;
ldi r25,0b11010001 ;
call i2c_send ;
call i2c_recive ;
cbr r25,0b10000000 ; 7-
mov r21,r25 ; R21
call i2c_recive ;
mov r22,r25 ; R22
rcall i2c_recive_last ;
mov r23,r25 ; R23
call i2c_stop ;
ret
i2c_stop: ;
ldi r25, (1<<TWINT)|(1<<TWEN)|(1<<TWSTO)
; , , ""
sts TWCR, r25 ; ;
ret
i2c_start: ;
ldi r25, (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)
; ., "" .
sts TWCR, r25
;
wait1: ;
lds r25,TWCR
;
sbrs r25,TWINT ;,
jmp wait1 ; , wait1, ,
;
ret
i2c_recive_last: ; R25
ldi r25, (1<<TWINT|1<<TWEN)
;
sts TWCR, r25
;
wait3: ;
lds r25,TWCR ; R25
;
sbrs r25,TWINT ;,
jmp wait3 ;
lds r25,TWDR ; , R25
ret ;
i2c_recive: ; R25
ldi r25, (1<<TWINT|1<<TWEN|1<<TWEA)
; ., . ""
sts TWCR, r25 ;
;
wait4: ;
lds r25,TWCR ;
sbrs r25,TWINT ;,
jmp wait4 ;
lds r25,TWDR ; R25
;
ret ;
i2c_send: ; R25
sts TWDR,r25 ; R25
ldi r25, (1<<TWINT)|(1<<TWEN)
;
Sts TWCR, r25
|
|
;
wait2: ;
lds r25,TWCR ;
;
sbrs r25,TWINT ; ,
;
rjmp wait2 ; ,
ret
lcd_print_symbol: ;
sbi PortD,7 ;RS = 1
mov r17,r16 ; R16
andi r16,0xF0 ; R16
swap r16 ;
sts PortF,R16 ;
sbi PortE,2 ;
call delay
cbi PortE,2 ;
mov r17,r16 ; R16
andi r16,0x0F ;
;
sts PortF,R16 ;
sbi PortE,2 ;
call delay
cbi PortE,2 ;
ret ;
delay: ;
clr r17 ;
dec r17 ;
brne pc-1 ;
ret ;
;
: , , , .
8
SPI
: SPI, , DATAFLASH , .
8.1. SPI
SPI . SPI I2C . Motorola, . Serial Peripheral Bus, . SPI -.
SPI , / . , SPI , , , . SPI . .
SPI, . , , . 8.1. MOSI SCLK, . . MISO, . , , , . , 3 4 . , , , SS . .
. 8.1. SPI
SPI () (. 8.2), () (. 8.3). , . . SPI- . , , , SS , . ( 3 + n, n ). , . . . , . 8.3. , , 4. , .
|
|
. 8.2. SPI
SPI , , , , , . . .
. 8.3. SPI
SPI . 8.1.
8.1
SPI
MOSI | DO SDO OUT | MOSI | DO SDO OUT | ||
MISO | DI SDI DIN | MISO | DI SDI DIN | ||
SCLK | DCLOCK CLK SCK | SCLK | DCLOCK CLK SCK | ||
SS | CS | SS | CS |
, SPI. SPI :
1. CPOL ( CPOL = 0, , (. . , ), , CPOL = 1 (. . , ));
2. CPHA . , ( CPHA = 0, , ; CPHA = 1, , ).
, SPI, , , . SPI, , SPI , SPI- ( ). , SPI .
8.2. SPI ATMega
SPI (. 8.4) ATmega128 :
, ;
;
;
;
;
;
(Idle);
() SPI (CK/2).
SPI SS , . ( ) , SCK. MOSI , MISO, , . SPI SS ( ).
. 8.4. SPI
SPI (), SS . . SPI . SPI , (SPIF). SPIE SPCR, SPI . , SPDR, SS. . SPI , MISO , SS . SPI (SPDR), SS. , , SPIF. SPI (SPIE) SPCR (. 8.2), SPIF . SPDR . .
, . , SPI , . , SPI , . .
SPI SCK. SPI fosc/4.
8.2
SPI SPCR
SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 | |
/ | / | / | / | / | / | / | / | / |
7 SPIE: SPI
SPIF SPSR (. 8.3) I SREG, SPI.
6 SPE: SPI
SPE 1, SPI. , SPI , .
5 DORD:
DORD = 1, . DORD = 0, .
4 MSTR: /
1, SPI (), (MSTR = 0) . SS , MSTR 1, MSTR SPIF SPSR. SPI MSTR.
3 CPOL:
1, SCK . CPOL = 0, SCK .
2 CPHA:
(CPHA) , SCK : .
1, 0 SPR1, SPR0: 1 0
(. 8.4) SCK . SPR1 SPR0 .
8.3
SPI SPSR
SPIF | WCOL | - | - | - | - | - | SPI2X | |
/ | / | |||||||
8.4
SPI2X | SPR1 | SPR0 | SCK |
fosc/4 | |||
fosc/16 | |||
fosc/64 | |||
fosc/128 | |||
fosc/2 | |||
fosc/8 | |||
fosc/32 | |||
fosc/64 |
7 SPIF: SPI
SPIF . , SPIE SPCR . SS , , SPI , SPIF. SPIF . , SPIF SPI SPIF, SPI (SPDR) (. 8.5).
6 WCOL:
WCOL , SPI (SPDR) . WCOL ( SPIF) SPI WCOL, SPI.
51 Res:
ATmega128 0.
0 SPI2X: SPI
1, SPI ( SCK) , SPI , , SCK . SPI , SPI fosc/4 .
8.5
SPI SPDR
/ | / | / | / | / | / | / | / | / |
SPI (R0R31) SPI. . .
8.3. Flash (DataFlash) AT45DB041B
AT45DB041B :
- 2,53,6 2,73,6 ;
- SPI;
- 20 ;
- ;
- ( );
- 2048 (264 );
- ;
- 264- SRAM, ;
- ;
- ;
- : 4 , 2 ;
- ;
- SI, SCK, CS ( ), RESET ( ) WP ( ) 5 .
. 8.5, . 8.6, . 8.6.
. 8.5. AT45DB041B
8.6
AT45DB041B
CS ( ) | |
SCK | |
SI | |
SO | |
WP ( ) | |
VCC | |
GND |
4 325 376 2048 264 . , SRAM 264 . , , . EEPROM ( ) 3- Read Modify Write. DataFlash . SPI 0 3, , 20 4 .
. 8.6. AT45DB041B
. 2,53,6 2,73,6 , .
AT45DB041B , , . , . . 8.78.9.
8.7
68h | |
52h | |
1 | 54h |
2 | 56h |
57h |
CS, 8- . , SCK SI ( ). , , . SPI0 3.
, .
8.8
1 | 84h |
2 | 87h |
1 | 83h |
2 | 86h |
1 | 88h |
2 | 89h |
81h | |
50h | |
1 | 82h |
2 | 85h |
8.9
1 | 53h |
2 | 55h |
1 | 60h |
2 | 61h |
1 | 58h |
2 | 59h |
. 8.10.
8.10
(HEX) | . | ||||||||||||||||||||||||
r | r | r | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | 4 | |
r | r | r | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | x | ||
x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | B | B | B | B | B | B | B | B | B | 1 | |
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | ||
x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | B | B | B | B | B | B | B | B | B | 1 | |
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | 4 | |
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | B | B | B | B | B | B | B | B | B | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | B | B | B | B | B | B | B | B | B | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | x | x | x | x | x | x | x | x | x | ||
D2 | r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | 4 |
D4 | x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | B | B | B | B | B | B | B | B | B | 1 |
D6 | x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | B | B | B | B | B | B | B | B | B | 1 |
D7 | |||||||||||||||||||||||||
E8 | r | r | r | P | P | P | P | P | P | P | P | P | P | P | P | B | B | B | B | B | B | B | B | B | 4 |
8.4.
-31.100 (. 8.7) .
. 8.7.
CS0CS7. DD4 74HC138 (15647). PB.0, PB.4 PB.5.
S1S12, . VT1, VT3, VT5 VT7, Q0Q3 DD3HC138. PC.0 PC.2, PC.5PC.7, PF.0PF.3, PE.2, PD.5 PD.7 .
8.5.
1. SPI.
2. SPI ?
3. SPI ATMega?
4. ?
5. AT45DB041B.
6. SPI?
7. SPI?
8.6.
31.100, :
1. FLASH- AT45DB041B , # FLASH- .
2. FLASH- AT45DB041B , * FLASH- .
3. 1- AT45DB041B , * FLASH- .
4. 1- AT45DB041B , # FLASH- .
5. 2- AT45DB041B , # FLASH- .
6. 2- AT45DB041B , * FLASH- .
7. , AT45DB041B.
8. , AT45DB041B.
9. , AT45DB041B.
10. , AT45DB041B.
11. , AT45DB041B.
12. , AT45DB041B.
13. , , AT45DB041B.
14. , , , AT45DB041B.
15. , , , AT45DB041B.
8.7.
-31.100, 100h 101h AT45DB041B.
.include "m128def.inc"
.cseg
.org 0000
jmp Init
Init:
;
ldi R16,0x00 ; , 0400
out SPL,R16
ldi R17,0x04
out sph,r17
jmp LCD_INIT ;
complete:
; SPI flash
sbi DDRB,0 ;
sbi DDRB,4
sbi DDRB,5
;
cbi PORTB,0
cbi PORTB,4
cbi PORTB,5
; MOSI SCK
ldi r17,(1<<DDB2)|(1<<DDB1) out ddrb,r17
; SPI , fck/16
ldi r17,(1<<SPE)|(1<<MSTR)|(1<<SPR0)
out SPCR,r17
start:
; FLASH- R21 R22
call dataflash_read
;
; 0x80
cbi PortD,7 ;RS = 0
ldi R16,0x8 ;
sts PortF,R16 ;
sbi PortE,2 ;
clr r14
dec r14
brne pc-1 ;
cbi PortE,2 ;
ldi R16,0x0 ;
sts PortF,R16 ;
sbi PortE,2 ;
clr r14
dec r14
brne pc-1 ;
cbi PortE,2 ;
sbi PortD,7 ;RS = 1
; "F" 0x46
ldi r20,0x46
call lcd_print_symbol
; "l" 0x6C
ldi r20,0x6C
call lcd_print_symbol
; "a" 0x61
ldi r20,0x61
call lcd_print_symbol
; "s" 0x73
ldi r20,0x73
call lcd_print_symbol
; "h" 0x68
ldi r20,0x68
call lcd_print_symbol
; " " 0x20
ldi r20,0x20
call lcd_print_symbol
; " " 0x20
ldi r20,0x20
call lcd_print_symbol
;
mov r20,r21
andi r20,0xF0
swap r20
call lcd_print_number
;
mov r20,r21
andir20,0x0f
call lcd_print_number
;
mov r20,r22
andir20,0xF0
swap r20
call lcd_print_number
;
mov r20,r22
andi r20,0x0f
call lcd_print_number
jmp start ;
;
dataflash_read: ; FLASH-
; SC,
sbi PORTB,0
ldi r20,0x52 ;
call spi_send
ldi r20,000 ;
call spi_send
ldi r20,0x01 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x00 ;
call spi_send
;
call spi_recieve
mov r21,r20 ;
; SC,
cbi PORTB,0
; SC,
sbi PORTB,0
ldi r20,0x52 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x01;
call spi_send
ldi r20,0x01 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x00 ;
call spi_send
ldi r20,0x00 ;
call spi_send
call spi_recieve ;
mov r22,r20 ;
; SC,
cbi PORTB,0
ret
SPI_send: ;
; SPI
out SPDR,r20
; SPI ,