Forum: Mikrocontroller und Digitale Elektronik MCP3428 code


von irene (Gast)


Lesenswert?

Hello,
ich versuche seit einigen Tage der MCP3428 mit Atmega128 zu 
programmieren. Ich komme aber nicht recht, da ich Anfänger bin. Anbei 
sende ich mein Code. Vielleicht kann mit jemand helfen.

I2c
#include<inttypes.h>
#define F_CPU 8000000UL
#define I2c_FREQ 1000000L
#include<util/delay.h>
#include<stdlib.h>
#include <avr/io.h>


uint16_t ERROR=0xFF;
void I2c_init(void)
{
  TWSR=0x00;
    TWBR=((F_CPU/I2c_FREQ)-16)/2;
    TWCR=(1<<TWEN);
}

void I2c_start(void)
{
  TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  while((TWCR&(1<<TWINT))==0);
}
void I2c_stop()
{
  TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}
void I2c_Write(uint8_t u8data)
{
  TWDR=u8data;  // Schreiben eines Databyte im TWDR
  TWCR=(1<<TWINT)|(1<<TWEN);
  while((TWCR&(1<<TWINT))==0); // warten bis, der Schreibbefehl fertig 
ist
}
uint8_t I2c_Read_Ack(void)
{
  TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN);
  while((TWCR&(1<<TWINT))==0); // warten bis, der Lesebefehl fertig ist
  return TWDR;
}
uint8_t I2c_Read_NAck(void)
{
  TWCR=(1<<TWINT)|(1<<TWEN); // Kein ACK
  while((TWCR&(1<<TWINT))==0); // warten bis, der Lesebefehl fertig ist
  return TWDR;
}
uint8_t I2cGetStatus(void)
{
  uint8_t Status;
  Status= TWSR& 0xF8;
  return Status;
}



mcp3436

void mcp3426_ReadByte_Channel1(uint16_t u16addr, uint8_t (*u8data))
{
  I2c_start();
  if(I2cGetStatus()!=0x08)
  return ERROR;
  // device Adress Schreiben
  I2c_Write(208);
  if(I2cGetStatus()!=0x18)
  return ERROR;
  I2c_Write((uint8_t)(u16addr));
  if(I2cGetStatus()!=0x28)
  return ERROR;
  // Senden Von Start
  I2c_start();
  if(I2cGetStatus()!=0x10)
  return ERROR;
  // device Address and LeseBit
  I2c_Write(209);
  I2c_Write(136); // configurationsbit
  if(I2cGetStatus()!=0x40)
  return ERROR;
  *u8data=I2c_Read_Ack();
  I2c_stop();
}


void mcp3426_ReadByte_Channel2(uint16_t u16addr1, uint8_t *u8data1)
{
  I2c_start();
  if(I2cGetStatus()!=0x08)
  return ERROR;
  // device Adress Schreiben
  I2c_Write(208);
  if(I2cGetStatus()!=0x18)
  return ERROR;
  I2c_Write((uint8_t)(u16addr1));
  if(I2cGetStatus()!=0x28)
  return ERROR;
  // Senden Von Start
  I2c_start();
  if(I2cGetStatus()!=0x10)
  return ERROR;
  // device Address and LeseBit
  I2c_Write(209);
  I2c_Write(168); // configurationsbit
  if(I2cGetStatus()!=0x40)
  return ERROR;
  *u8data1=I2c_Read_NAck();
  I2c_stop();
}

void mcp3426_init_Channel2()
{
  I2c_start();
  I2c_Write(208);
  I2c_Write(168); // Configuration Bit
  I2c_stop();
}


main :

int main(void)
{

    while(1)
    {
    uint8_t u8edata;

    uint16_t u16eaddr = 0x0000;
    I2c_init();
    mcp3426_ReadByte_Channel1(u16eaddr, u8edata);

    mcp3426_ReadByte_Channel2( u16eaddr, &u8edata);


        //TODO:: Please write your application code
    }
}


LG irene

von Mampf F. (mampf) Benutzerseite


Lesenswert?

irene schrieb:
> #define F_CPU 8000000UL
> #define I2c_FREQ 1000000L

1MHz I2C-Frequenz ist zu hoch ... Das kann meines Wissens keine 
Peripherie. Vermutlich auch nicht das I2C-Interface im ATMega

Hast du da eine 0 zuviel getippt?

Probier es mal mit 100kHz

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.