I²C-Software RTC (DS1307)

OP #3119050
Lesenswert?

Hallo und gutem Middach,

ich habe für meinen PIC16F721 der kein MSSP hat, versucht eine 
Softwareimplementierung des I²C-Masters für den DS1307 zu programmieren. 
Bisher funktioniert sie jedoch nicht so, wie ich will, die RTC scheint 
überhaupt nicht zu reagieren.

Kann jemand von euch vielleicht kurz über den Code schauen, ob es 
irgendwelche Fehler gibt?

Danke im Voraus,
  Frederik

Anhang:
1
#include <htc.h>
2

3
#define SDA PORTBbits.RB4
4
#define SCL PORTBbits.RB6
5
#define SDA_OUT TRISBbits.TRISB4 = 0
6
#define SDA_IN TRISBbits.TRISB4 = 1
7
#define rtc_w 0b11010000
8
#define rtc_r 0b11010001
9

10
void wait(void);
11
void i2c_start(void);
12
void i2c_stop(void);
13
void i2c_readbit(void);
14
void i2c_writebit(void);
15
unsigned char i2c_read_ack(void);
16
unsigned char i2c_read_nack(void);
17
void i2c_write(unsigned char byte);
18
unsigned char rtc_read(unsigned char adress);
19
void rtc_write(unsigned char adress, unsigned char byte);
20

21
typedef struct
22
{
23
    unsigned temp1 :1;
24
    unsigned temp2 :1;
25
}Bit;
26

27
Bit temp;
28

29
void wait(void)
30
{
31
    char i;
32
    for (i = 0; i < 100; i++)
33
    {
34
        // NOP
35
    }
36
}
37

38
void i2c_start(void)
39
{
40
    SCL = 1;
41
    wait();
42
    SDA = 1;
43
    wait();
44
    SDA = 0;
45
    wait();
46
    SCL = 0;
47
}
48

49
void i2c_stop(void)
50
{
51
    SDA = 0;
52
    wait();
53
    SCL = 1;
54
    wait();
55
    SDA = 1;
56
}
57

58
void i2c_readbit(void)
59
{
60
    SDA_IN;
61
    SCL = 1;
62
    wait();
63
    temp.temp1 = SDA;
64
    wait();
65
    SCL = 0;
66
}
67

68
void i2c_writebit(void)
69
{
70
    SDA_OUT;
71
    SDA = temp.temp2;
72
    SCL = 1;
73
    wait();
74
    SCL = 0;
75
}
76

77
unsigned char i2c_read_ack(void)
78
{
79
    unsigned char byte = 0x00;
80
    unsigned char k;
81

82
    for (k = 0; k < 8; k++)
83
    {
84
        i2c_readbit();
85
        byte << 1;
86
        if (temp.temp1 == 0b1)
87
        {
88
            byte |= 0x01;
89
        }
90
        else
91
        {
92
            byte |= 0x00;
93
        }
94
    }
95

96
    temp.temp2 = 0;
97
    i2c_writebit();
98

99
    return byte;
100
}
101

102
unsigned char i2c_read_nack(void)
103
{
104
    unsigned char byte = 0x00;
105
    unsigned char k;
106

107
    for (k = 0; k < 8; k++)
108
    {
109
        i2c_readbit();
110
        byte << 1;
111
        if (temp.temp1 == 0b1)
112
        {
113
            byte |= 0x01;
114
        }
115
        else
116
        {
117
            byte |= 0x00;
118
        }
119
    }
120

121
    temp.temp2 = 1;
122
    i2c_writebit();
123

124
    return byte;
125
}
126

127
void i2c_write(unsigned char byte)
128
{
129
    unsigned char k;
130

131
    for (k = 0; k < 8; k++)
132
    {
133
        temp.temp2 = byte & 0x01;
134
        i2c_writebit();
135
        byte >> 1;
136
    }
137

138
    i2c_readbit();
139
}
140

141
unsigned char rtc_read(unsigned char adress)
142
{
143
    unsigned char byte;
144
    
145
    i2c_start();
146
    i2c_write(rtc_w);
147
    i2c_write(adress);
148
    i2c_stop();
149
    i2c_start();
150
    i2c_write(rtc_r);
151
    byte = i2c_read_nack();
152
    i2c_stop();
153

154
    return byte;
155
}
156

157
void rtc_write(unsigned char adress, unsigned char byte)
158
{
159
    i2c_start();
160
    i2c_write(rtc_w);
161
    i2c_write(adress);
162
    i2c_write(byte);
163
    i2c_stop();
164
}

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