Forum: Mikrocontroller und Digitale Elektronik SRF08


von clemens (Gast)


Lesenswert?

hat dieses modul schon mal wer verwendet?

falls ja eventuell auch mit nem ATMEGA?

hätt da nämlich meinen code nur funzt der net so wie er sollte!

wär cool wenn mir da wer helfen könnte!

/*
  Two wire serial Interface for the ATMEGA 16 microcontroller.
  It is used to communicate with our own EEPROM, which purpose is it
  to store the measured data from the amp.

  programmed by Christoph Lex
*/
//********************************************************************** 
*****



#include<io.h>
#include<iom32.h>
#include<interrupt.h>

#define START   0x08
#define SLA_ACK 0x18
#define DAT_ACK 0x28

#define R_START 0x10
#define R_SLA_ACK 0x40
#define DTA_REC 0x50


void TWI_send(unsigned char ucDatabyte, unsigned char ucSLAVE_W, 
unsigned char ucByteaddress);
unsigned char TWI_rec(unsigned char ucSLAVE_R, unsigned char 
ucByteaddress, unsigned char ucSLAVE_W);
void Timer_1ms(int Time);
void Timer_10us(int Time);


void main()
{
  unsigned char ucByteaddress;
  unsigned char ucSLAVE_W;

  int i=0;

  DDRB = 0xFF;

  TWBR = 0x01;

  ucSLAVE_W = 0xE0;
  ucByteaddress = 0x00;


   TWI_send(0x51,ucSLAVE_W,ucByteaddress);

  Timer_1ms(50);

//   PORTB = ~TWI_rec(0xE1,0x03,0xE0);

}



void TWI_send(unsigned char ucDatabyte, unsigned char ucSLAVE_W, 
unsigned char ucByteaddress)
{


  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // clear interrupt flag and 
initiate START condition


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) != START)    // check if the value from the ack 
matches the START cond.
    PORTB = ~0xFE;


  TWDR = ucSLAVE_W;        // Slave - address + Write command are 
written in the TWI data register
  TWCR = (1<<TWINT)|(1<<TWEN);  // clear flag and enable TWI


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) != SLA_ACK)    // check if the value from the ack 
matches the SLAVE_address cond.
    PORTB = ~0xFD;



  TWDR = ucByteaddress;      // Byteaddress is written into the data 
register
  TWCR = (1<<TWINT)|(1<<TWEN);  // clear flag and enable TWI


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) != DAT_ACK)
    PORTB = ~0xFC;



  TWDR = ucDatabyte;        // Databyte is written into the data 
register
  TWCR = (1<<TWINT)|(1<<TWEN);  // clear flag and enable TWI


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) != DAT_ACK)    // check if the value from the ack 
matches the DATA_send cond.
    PORTB = ~0xFB;



  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // transmit STOP condition

    PORTB = ~0xFA;



}

unsigned char TWI_rec(unsigned char ucSLAVE_R, unsigned char 
ucByteaddress, unsigned char ucSLAVE_W)
{


  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // clear interrupt flag and 
initiate START condition


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) == START)    // check if the value from the ack 
matches the START cond.
    PORTB = ~0x0E;


  TWDR = ucSLAVE_W;        // Slave - address + Write command are 
written in the TWI data register
  TWCR = (1<<TWINT)|(1<<TWEN);  // clear flag and enable TWI


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;



  if ((TWSR & 0xF8) == SLA_ACK)    // check if the value from the ack 
matches the SLAVE_address cond.
    PORTB = ~0x0D;


  TWDR = ucByteaddress;      // Byteaddress is written into the data 
register
  TWCR = (1<<TWINT)|(1<<TWEN);  // clear flag and enable TWI


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) == DAT_ACK)
    PORTB = ~0x0A;


  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // clear interrupt flag and 
initiate START condition


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) == R_START)    // check if the value from the ack 
matches the START cond.
    PORTB = ~0x09;

  TWDR = ucSLAVE_R;        // Slave - address + READ command are written 
in the TWI data register
  TWCR = (1<<TWINT)|(1<<TWEN);  // clear flag and enable TWI


  while(!(TWCR & (1<<TWINT)))    // wait for interrupt flag

    ;


  if ((TWSR & 0xF8) == R_SLA_ACK)    // check if the value from the ack 
matches the SLAVE_address cond.
    PORTB = ~0x08;

  TWCR = (1<<TWINT)|(1<<TWEN);

  while(!(TWCR & (1<<TWINT)))
    ;

  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);


  return TWDR;
}

void Timer_1ms(int Time)
{
  int i;

  for(i=0;i<Time;i++)
    Timer_10us(100);
}

void Timer_10us(int Time)
{
  int i;

  for(i=0;i<Time*3;i++)
    ;
}


thx a lot!

von atomino (Gast)


Lesenswert?

ohne deinen code jetzt zu lesen

meine erfahrung:

I2C  100khz nicht höher
min 6..7 messungen  1..2 messeungen immer sinnlos
range runtersetzen sonst grössere gefahr fuer störung (4m)

bei mir funzt es
muss mal den code raussuchen


ach so ldr spinnt bei mir manchmal


gruesse

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.