Suche fertiges RTC-Projekt (DS1307) für ATmega8 in C

#3362551
Lesenswert?

Hallo Simon,

den DS1307 erstmalig nach Inbetriebnahme initialisieren.

Beispiel:
1
void DS1307_Init( void )
2
{
3
    
4
    I2CSendAddr( DS1307_ADDR , WRITE );
5
    if( I2CCheckError() > 0 ){
6
        I2CSendStop();
7
        //Fehler
8
        return;
9
    }
10
    I2CSendByte( 0x00 );    //Adresse 0
11
    I2CSendByte( 0x00 );
12
    I2CSendByte( 0x00 );
13
    I2CSendByte( 0x00 );
14
    I2CSendByte( 0x00 );
15
    I2CSendByte( 0x00 );
16
    I2CSendByte( 0x00 );
17
    I2CSendByte( 0x00 );
18
    I2CSendByte( 0x10 );    //SQWE=1
19
    
20
    I2CSendStop();
21
}
Zeit auslesen:
1
struct ds1307 {    
2
                unsigned char sekunde;
3
                unsigned char minute;
4
                unsigned char stunde;
5
                unsigned char tag;
6
                unsigned char monat;
7
                unsigned int  jahr;    
8
                unsigned char wtag; //(0=Montag;...;6=Sonntag)
9
                unsigned char isdst;
10
                };
11

12
struct ds1307 rtc;
13

14
void DS1307GetTime ( void )
15
{   
16
    unsigned char __data rtc_temp;
17

18
    rtc.sekunde = DS1307ReadByte ( 0x00 );
19
    rtc.minute  = DS1307ReadByte ( 0x01 );
20
    rtc.stunde  = DS1307ReadByte ( 0x02 );
21
    rtc.wtag    = DS1307ReadByte ( 0x03 );
22
    rtc.tag     = DS1307ReadByte ( 0x04 );
23
    rtc.monat   = DS1307ReadByte ( 0x05 );
24
    rtc_temp    = DS1307ReadByte ( 0x06 );
25
    rtc.jahr    = (rtc_temp & 0xF0 );
26
    rtc.jahr    |= (unsigned char )(rtc_temp & 0x0F );
27
}
Zeit setzen:
1
void DS1307SetTime ( void )
2
{
3
    DS1307WriteByte( 0x00, (((rtc.sekunde / 10) << 4) + rtc.sekunde % 10));
4
    wait_ms(10);   //warte 10ms
5
    //Minuten
6
    DS1307WriteByte( 0x01, (((rtc.minute / 10) << 4) + rtc.minute % 10));
7
    wait_ms(10);
8
    //Stunden
9
    DS1307WriteByte( 0x02, (((rtc.stunde / 10) << 4) + rtc.stunde % 10));
10
    wait_ms(10);
11
    //Wochentag
12
    DS1307WriteByte( 0x03, (rtc.wtag & 0x07));
13
    wait_ms(10);
14
    //Tag
15
    DS1307WriteByte( 0x04, (((rtc.tag / 10) << 4) + rtc.tag % 10));
16
    wait_ms(10);
17
    //Monat
18
    DS1307WriteByte( 0x05, (((rtc.monat / 10) << 4) + rtc.monat % 10));
19
    wait_ms(10);
20
    //Jahr
21
    DS1307WriteByte( 0x06, (((rtc.jahr / 10) << 4) + rtc.jahr % 10));
22
    wait_ms(10);
23
}
24

25
void DS1307WriteByte (  unsigned char RTC_Adresse, unsigned char Value )
26
{
27
    I2CSendAddr( DS1307_ADDR , WRITE );
28
    if( I2CCheckError() > 0 ){
29
        I2CSendStop();
30
        //Fehler
31
        return;
32
    }
33
    I2CSendByte( RTC_Adresse );
34
    I2CSendByte( Value );
35
    I2CSendStop();
36
}
37

38
unsigned char DS1307ReadByte ( unsigned char RTC_Adresse )
39
{
40
    I2CSendAddr( DS1307_ADDR , WRITE );
41
    if( I2CCheckError() > 0 ){
42
        I2CSendStop();
43
        //Fehler
44
        return 0;
45
    }
46
    I2CSendByte( RTC_Adresse );
47

48
    I2CSendAddr( DS1307_ADDR, READ );
49

50
    temp = I2CGetLastByte();
51
    I2CSendStop();
52
    return temp;    
53
}

mfg erzfichte

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren