Forum: Compiler & IDEs I2C Anfänger-Probleme


von Till H. (mohman)


Angehängte Dateien:

Lesenswert?

Hallo zusammen, ich versuche den i2c-bus zum laufen zu bringen! ich 
hoffe ihr könnt mir weiter helfen! ich seh das problem nicht...seh den 
wald vor lauter bäumen nicht...:(

hier einmal der code für den master!





MfG
Till

von Till H. (mohman)


Angehängte Dateien:

Lesenswert?

und hier und die funktionen vom master!

von Till H. (mohman)


Angehängte Dateien:

Lesenswert?

und das gleiche für den slave

von Till H. (mohman)


Angehängte Dateien:

Lesenswert?

hmm

von Peter (Gast)


Lesenswert?

[c]
//-------------------------------------------------------------
//TWI.h : header file for the master Two-wire Serial Interface
//-------------------------------------------------------------

#ifndef TWI_H
#define TWI_H

//-------------------------------------------
// Definition of global includes
//-------------------------------------------

#include "MyTypeDefs.h"

//-------------------------------------------
// Definition of global constants
//-------------------------------------------

#define F_SCL  400000UL     // 400 kHz for SCL

//-------------------------------------------
// Declaration of global functions
//-------------------------------------------

void TWI_init(void);

u08 TWI_read (u08 TWI_adr, u08 *val, u08 count);
u08 TWI_write(u08 TWI_adr, u08 *val, u08 count);

#endif
[\c]

[c]
//--------------------------------------------------------
//TWI.c : source file for the master Two-wire Serial Interface
//--------------------------------------------------------

//-------------------------------------------
// Definition of local includes
//-------------------------------------------

#include <util/twi.h>
#include <util/delay.h>
#include "TWI.h"

//-------------------------------------------
// Definition of local constants
//-------------------------------------------

#define TW_MT_ACK (u08)0x08  // master transfer acknowledge for address 
or data

//-------------------------------------------
// Declaration of local variables
//-------------------------------------------

static volatile u16 I2C_timer;

//-------------------------------------------
// Declaration of local functions
//-------------------------------------------

//-------------------------------------------
// Code for functions
//-------------------------------------------

void delay_us(u08 delay)
{
  while (delay--) _delay_us(1);
}

//--------------------------------------------------------
// Intialize the I2C-Bus Interface
// as Master with SCL 100kHz, non interrupt driven
//--------------------------------------------------------


void TWI_init(void)
{
  //--------------------------------------------------------
  // SCL = F_CPU/(16+2*TWBR*4^TWSR)
  // TWSR=0 => TWBR = F_CPU/2/F_SCL-8
  //--------------------------------------------------------
  TWBR = (u08)(F_CPU/2/F_SCL-7.5);
  TWSR = 0x00;                     // TWI-clock prescaler = (TWSR+1) = 1
  TWCR = (1<<TWEA)|(1<<TWEN);      // no interrupt mode
}

u08 TWI_read(u08 TWI_adr, u08 *val, u08 count)
//------------------------------------------------
// Read n=count bytes from I2C-Bus in polled mode
// needs about 200us for reading addr + one byte
//------------------------------------------------
{
  u08 i=0, rc=0;
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);     // send START condition
  while (!(TWCR & (1<<TWINT)));               // wait for response
  if ((TWSR & 0xF8) == TW_START)              // if START was successful
  {
    TWDR = (TWI_adr | 0x01);                  // put slave address+READ 
to TWDR
    TWCR = (1<<TWINT)|(1<<TWEN);              // and start transmission
    while (!(TWCR & (1<<TWINT)));             // wait for response
    if ((TWSR & 0xF8) == TW_MR_SLA_ACK)       // if acknowledge ok
    {
      while (--count>0)                       // while more than one 
byte to read
      {
        TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);  // then start transfer 
with acknowledge
        while (!(TWCR & (1<<TWINT)));         // wait for response
        val[i++]=TWDR;                        // read databyte from TWDR
      }
      TWCR=(1<<TWINT)|(1<<TWEN);              // start last byte 
transfer without acknowledge
      while (!(TWCR & (1<<TWINT)));           // wait for response
      val[i]=TWDR;                            // read databyte from TWDR
    }
    else
    {
      //printl_I("Error: I2C[R] Addr");
      rc = 0x10;
    }
  }
  else
  {
    //printl_I("Error: I2C[R] Start");
    rc = 0x20;
  }
  TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);      // transmit STOP 
condition
  delay_us(10);                                // wait until STOP is 
transmitted
  return rc;
}

u08 TWI_write(u08 TWI_adr, u08 *val, u08 count)
//------------------------------------------------
// write n=count bytes to I2C-Bus in polled mode
// needs about 200us for writing Addr + one byte
//------------------------------------------------
{
  u08 rc=0;
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);   // send START condition
  while (!(TWCR & (1<<TWINT)));             // wait for response
  if ((TWSR & 0xF8) == TW_START)            // if START was successful
  {
    TWDR = (TWI_adr & 0xFE);                // put slave address+WRITE 
to TWDR
    TWCR = (1<<TWINT)|(1<<TWEN);            // and start transmission
    for (u08 i=0; i<count; i++)
    {
      while (!(TWCR & (1<<TWINT)));         // wait for response
      if ((TWSR & 0xC8) == TW_MT_ACK)       // if acknowledge ok
      {
        TWDR = val[i];                      // put databyte  to TWDR
        TWCR = (1<<TWINT)|(1<<TWEN);        // and start transmission
      }
      else
      {
        //printl_I("Error: I2C[W] Data");
        rc=0x10;
      }
    }
  }
  else
  {
    //printl_I("Error: I2C[W] Start");
    rc=0x20;
  }
  while (!(TWCR & (1<<TWINT)));             // wait for response
  TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);   // transmit STOP condition
  delay_us(10);                             // wait until STOP is 
transmitted
  return rc;
}
[\c]

von Peter (Gast)


Lesenswert?

1
//-------------------------------------------------------------
2
//TWI.h : header file for the master Two-wire Serial Interface
3
//-------------------------------------------------------------
4
5
#ifndef TWI_H
6
#define TWI_H
7
8
//-------------------------------------------
9
// Definition of global includes
10
//-------------------------------------------
11
12
#include "MyTypeDefs.h"
13
14
//-------------------------------------------
15
// Definition of global constants
16
//-------------------------------------------
17
18
#define F_SCL  400000UL     // 400 kHz for SCL
19
20
//-------------------------------------------
21
// Declaration of global functions
22
//-------------------------------------------
23
24
void TWI_init(void);
25
26
u08 TWI_read (u08 TWI_adr, u08 *val, u08 count);
27
u08 TWI_write(u08 TWI_adr, u08 *val, u08 count);
28
29
#endif
1
//--------------------------------------------------------
2
//TWI.c : source file for the master Two-wire Serial Interface
3
//--------------------------------------------------------
4
5
//-------------------------------------------
6
// Definition of local includes
7
//-------------------------------------------
8
9
#include <util/twi.h>
10
#include <util/delay.h>
11
#include "TWI.h"
12
13
//-------------------------------------------
14
// Definition of local constants
15
//-------------------------------------------
16
17
#define TW_MT_ACK (u08)0x08  // master transfer acknowledge for address
18
or data
19
20
//-------------------------------------------
21
// Declaration of local variables
22
//-------------------------------------------
23
24
static volatile u16 I2C_timer;
25
26
//-------------------------------------------
27
// Declaration of local functions
28
//-------------------------------------------
29
30
//-------------------------------------------
31
// Code for functions
32
//-------------------------------------------
33
34
void delay_us(u08 delay)
35
{
36
  while (delay--) _delay_us(1);
37
}
38
39
//--------------------------------------------------------
40
// Intialize the I2C-Bus Interface
41
// as Master with SCL 100kHz, non interrupt driven
42
//--------------------------------------------------------
43
44
45
void TWI_init(void)
46
{
47
  //--------------------------------------------------------
48
  // SCL = F_CPU/(16+2*TWBR*4^TWSR)
49
  // TWSR=0 => TWBR = F_CPU/2/F_SCL-8
50
  //--------------------------------------------------------
51
  TWBR = (u08)(F_CPU/2/F_SCL-7.5);
52
  TWSR = 0x00;                     // TWI-clock prescaler = (TWSR+1) = 1
53
  TWCR = (1<<TWEA)|(1<<TWEN);      // no interrupt mode
54
}
55
56
u08 TWI_read(u08 TWI_adr, u08 *val, u08 count)
57
//------------------------------------------------
58
// Read n=count bytes from I2C-Bus in polled mode
59
// needs about 200us for reading addr + one byte
60
//------------------------------------------------
61
{
62
  u08 i=0, rc=0;
63
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);     // send START condition
64
  while (!(TWCR & (1<<TWINT)));               // wait for response
65
  if ((TWSR & 0xF8) == TW_START)              // if START was successful
66
  {
67
    TWDR = (TWI_adr | 0x01);                  // put slave address+READ
68
to TWDR
69
    TWCR = (1<<TWINT)|(1<<TWEN);              // and start transmission
70
    while (!(TWCR & (1<<TWINT)));             // wait for response
71
    if ((TWSR & 0xF8) == TW_MR_SLA_ACK)       // if acknowledge ok
72
    {
73
      while (--count>0)                       // while more than one
74
byte to read
75
      {
76
        TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);  // then start transfer
77
with acknowledge
78
        while (!(TWCR & (1<<TWINT)));         // wait for response
79
        val[i++]=TWDR;                        // read databyte from TWDR
80
      }
81
      TWCR=(1<<TWINT)|(1<<TWEN);              // start last byte
82
transfer without acknowledge
83
      while (!(TWCR & (1<<TWINT)));           // wait for response
84
      val[i]=TWDR;                            // read databyte from TWDR
85
    }
86
    else
87
    {
88
      //printl_I("Error: I2C[R] Addr");
89
      rc = 0x10;
90
    }
91
  }
92
  else
93
  {
94
    //printl_I("Error: I2C[R] Start");
95
    rc = 0x20;
96
  }
97
  TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);      // transmit STOP
98
condition
99
  delay_us(10);                                // wait until STOP is
100
transmitted
101
  return rc;
102
}
103
104
u08 TWI_write(u08 TWI_adr, u08 *val, u08 count)
105
//------------------------------------------------
106
// write n=count bytes to I2C-Bus in polled mode
107
// needs about 200us for writing Addr + one byte
108
//------------------------------------------------
109
{
110
  u08 rc=0;
111
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);   // send START condition
112
  while (!(TWCR & (1<<TWINT)));             // wait for response
113
  if ((TWSR & 0xF8) == TW_START)            // if START was successful
114
  {
115
    TWDR = (TWI_adr & 0xFE);                // put slave address+WRITE
116
to TWDR
117
    TWCR = (1<<TWINT)|(1<<TWEN);            // and start transmission
118
    for (u08 i=0; i<count; i++)
119
    {
120
      while (!(TWCR & (1<<TWINT)));         // wait for response
121
      if ((TWSR & 0xC8) == TW_MT_ACK)       // if acknowledge ok
122
      {
123
        TWDR = val[i];                      // put databyte  to TWDR
124
        TWCR = (1<<TWINT)|(1<<TWEN);        // and start transmission
125
      }
126
      else
127
      {
128
        //printl_I("Error: I2C[W] Data");
129
        rc=0x10;
130
      }
131
    }
132
  }
133
  else
134
  {
135
    //printl_I("Error: I2C[W] Start");
136
    rc=0x20;
137
  }
138
  while (!(TWCR & (1<<TWINT)));             // wait for response
139
  TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);   // transmit STOP condition
140
  delay_us(10);                             // wait until STOP is
141
transmitted
142
  return rc;
143
}

von Peter (Gast)


Lesenswert?

@ADMINS

Hallo Admins! Ich finde es zum kotzen: Wie wär es, wenn ihr es endlich 
mal auf die Reihe kriegt, dass man mehr als 80 Zeichen auf eine Zeile 
bringt, ohne dass automatisch Zeilenumbrüche eingefügt werden. 120 
Zeichen wäre das Minimum. Oder viel besser: beliebig lang bis zum 
gewollten Zeilenumbruch, ansonsten dynamische Zeilenlängen (je nach 
Fensterbreite). Sowas ist heutzutage doch State-of-the-Art!!!

von was-willst-du (Gast)


Lesenswert?

wenn was zum kotzen ist, dann der ton mancher user hier im forum

tststst

von Till H. (mohman)


Lesenswert?

hey danke für die schnelle antwort, guck mir das mal genauer an :) aber 
ist mein code total für die katz ?:(

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.