SRF08

Gast #501839
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!
Gast #791825
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

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.