;______________________________________ ; MODULE DS3231 ; Matériel : ATtiny 45/85 4MHz mini 5V ; ; (c) sammy76.free.fr ; V1.0 2016/06/01 Juin 2016 ;______________________________________ ; V1.0 ; ATtiny45 - 8MHz interne ; +-\/-+ ; PB5 1|- -|8 Vcc ; SCL PB3 2|- -|7 PB2 ; SDA PB4 3|- -|6 PB1 ; GND 4|- -|5 PB0 ; +----+ ;ATtiny85 - 16.5MHz Digispark ;P0 : ;P1 : ;P2 : ;P3 : SCL ;P4 : SDA ;P5 : ; .include "../includes/tn85def.inc" ; .define F_CPU 8000000 ;8MHz ; .def A=R16 ;.def DATA=R20 ;.def ADR=R19 ; .include "../utils/i2c_attiny.inc" ; .include "../utils/wait.inc" ; .dseg ; .org 0x100 ; now: .byte 0x12 ; date_txt: .byte 17 ;16 char ; temp_txt: .byte 6 ;5 char ; .cseg .equ DS3231 = 0x68 ; DS3231 address byte ;________________________________________________________________ ;DS3231 Init ;Registres utilisés : r16 (A),r19 (Adresse I²C=ADR), r20 (DATA),Z ;________________________________________________________________ ; DS3131_Init: ; ldi ADR,DS3231*2 ;Write Mode ; rcall I2CInit ; LDI DATA,0 ;set DS3231 register pointer to 00h ; rcall DATA_write_byte ; sets time and date data to DS3231 ; ldi ZL, LOW(date<<1) ; ldi ZH, HIGH(date<<1) ; rcall DS3231_write_string ; ret ; DS3231_write_string: ; lpm DATA, Z+ ;DATA=(Z) Z=Z+1 ; ser A ;0xFF ; cp DATA,A ;Si egal fin ; breq DS3231_end_write ; rcall DATA_write_byte ;Ecrire la donnée I2CWData ; ldi A, 2 ; rcall WaitMiliseconds ;2ms ; rjmp DS3231_write_string ; DS3231_end_write: ; ret ;________________________________________________________________ ;DS3231 Read ALL ;Registres utilisés : r16 (A),r19 (Adresse I²C=ADR), r20 (DATA),Z ;In : Nothing ;Out : now contient les données ;________________________________________________________________ DS3231_read_all: ; rcall I2CInit rcall I2CStart ldi DATA,DS3231*2 ;Write Mode rcall I2CSendByte ldi DATA,0 ;set DS3231 register pointer to 00h rcall I2CSendByte rcall I2CStop ; rcall I2CInit rcall I2CDelay rcall I2CStart ldi DATA,DS3231*2 | 1 ;Read rcall I2CSendByte ldi ZL, LOW(now<<1) ldi ZH, HIGH(now<<1) ldi R17,18 ;18 données recup: ldi DATA,0 ;read with ACK rcall I2CReadByte st Z+,A mov DATA,A dec R17 brne recup ldi DATA,1 rcall I2CReadByte ;read with NACK rcall I2CStop ;Stop I2C ;________________________________________________________________ ;Converting date for display ;In Z pointer data ;Out : 08.11.2016 06:16 in date_txt ;________________________________________________________________ ldi ZL, LOW((now<<1)+4) ;Day ldi ZH, HIGH((now<<1)+4) ldi XL, LOW(date_txt<<1) ldi XH, HIGH(date_txt<<1) rcall DCB2ASC ;Day ldi A,'.' st X+,A adiw ZL,1 rcall DCB2ASC ;Month ldi A,'.' st X+,A ldi A,'2' st X+,A ldi A,'0' st X+,A adiw ZL,1 rcall DCB2ASC ;Year ldi A,' ' st X+,A sbiw ZL,4 rcall DCB2ASC ;Hour ldi A,':' st X+,A sbiw ZL,1 rcall DCB2ASC ;MINUTE ser A st X,A ;0xFF en fin de chaine ;ret ;________________________________________________________________ ;Converting temp for display ; ;________________________________________________________________ conv_temp: adiw ZL,0x10 ;Temp ldi XL, LOW(temp_txt<<1) ldi XH, HIGH(temp_txt<<1) ld DATA,Z+ ldi A,'-' sbrs DATA,7 ldi A,'+' st X+,A andi DATA,0x7F mov A,DATA rcall bin2dcb ldi A,',' ;partie decimal st X+,A ldi r17,25 ld DATA,Z+ clr A sbrc DATA,7 ldi A,50 sbrc DATA,6 add A,r17 rcall bin2dcb ldi A,1 ;° st X+,A ser A ;End of string st X+,A ret DCB2ASC: ;DCB to ASC ld A,Z ;A=0x08, 0x11,0x16... DCB2ASC_A: mov DATA,A andi DATA,0xF0 ;HIGH swap DATA ori DATA,0x30 ;NUM st X+,DATA mov DATA,A andi DATA,0x0F ;LOW ori DATA,0x30 ;NUM st X+,DATA ret bin2dcb: ; maxi dec 99 clr DATA ; digit count is zero bin2dcba: cpi A,0x0A ; bigger or equal decimal brcs bin2dcbb ; smaller than decimal (A < 10 ) inc DATA ; Increment digit count subi A,10 ; Subtract LSB decimal -10 rjmp bin2dcba ; Next loop bin2dcbb: swap DATA or A,DATA rcall DCB2ASC_A ret ;date: .db 0,%MINUTE%,%HOUR%,0x6,%DAY%,%MONTH%,(%YEAR%-2000),0xFF