Hier ist meine neue Code:
Code:
#include "p33EP512MU810.h"
#include "i2c.h"
#include <Initialisierung.h>
#define FCY 100000000UL     //declare FCY for a 100 MHz instruction rate
#include <libpic30.h>

//Schritt 1

/*Configuring the general features of the microcontroller*/

_FGS(GSS_OFF & GWRP_OFF & GSSK_OFF);
// External Oscillator
_FOSCSEL(FNOSC_PRIPLL);

_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_XT);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: XT Crystal

_FWDT(FWDTEN_OFF);
// Watchdog Timer Enabled/disabled by user software

_FICD(ICS_PGD3 &  JTAGEN_OFF & RSTPRI_PF); 

_FPOR( BOREN_ON & ALTI2C1_OFF & ALTI2C2_OFF );
// Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
// Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
// Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)


//Schritt 2

#define CFG_I2C2BRG  0x188
// (40MHz/100KHz)-2 = 0x188//I2C2=[(1/Fscl-PGD)*Fcy]-2


unsigned int get_srf08(void);


/*** Global functions for the I2C2******************************************/
 main(void)
{
     unsigned int range1=0;
     f_Initialisierung();

    unsigned int Config1; // ConfigI2c2 represents I2C2CON register.
         Config1 = (   I2C2_ON & I2C2_IDLE_CON & I2C2_CLK_HLD &
                       I2C2_IPMI_DIS & I2C2_7BIT_ADD & I2C2_SLW_DIS &
                       I2C2_SM_DIS & I2C2_GCALL_DIS & I2C2_STR_DIS &
                       I2C2_NACK & I2C2_ACK_DIS & I2C2_RCV_DIS &
                       I2C2_STOP_DIS & I2C2_RESTART_DIS & I2C2_START_DIS
                        );

    OpenI2C2(Config1,CFG_I2C2BRG);/*The following function initializes the I2C2 bus and
                                        set the I2C2 Bus Speed to 100KHz I2C2 used to communicate with the Ultrasonic sensors*/
       
    IdleI2C2();

/*End Configuration I2C2*/
    while(1)
    {
        range1=get_srf08();
    }
 }


//Schritt 3
 
// Start Condition
  unsigned int get_srf08 (void)
{
    unsigned int hi,lo,range=0; //high Byte,low Byte
    
 
     
      //StartI2C2();
        I2C2CONbits.SEN=1;         //send start bit
        while(I2C2CONbits.SEN);    //and wait fot it to clear
     //MasterWriteI2C2(SRF08_ADDR);
        MasterWriteI2C2(0xF0);     //SRF08 I2C address
        while(I2C2STATbits.TBF);
        while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
        while(I2C2STATbits.ACKSTAT);
     //MasterWriteI2C2(0x00);
        MasterWriteI2C2(0x00);    //Befehls-Register
        while(I2C2STATbits.TBF);
        while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
        while(I2C2STATbits.ACKSTAT);       //then clear it
     //MasterWriteI2C2(0x51);
        MasterWriteI2C2(0x51);     //Messung auslösen - Ergebnis in Zentimeter
        while(I2C2STATbits.TBF);
        while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
        while(I2C2STATbits.ACKSTAT);          //then clear it
     //StopI2C2();
         I2C2CONbits.PEN=1;         //send stop bit
         while(I2C2CONbits.PEN);
     
 //Schritt 4
     __delay_ms(80);
        


//Schritt 5

     //finally get the range form the SRF08     
        
     //RestartI2C2();
        I2C2CONbits.RSEN = 1;
        while(I2C2CONbits.RSEN);    
    //MasterWriteI2C2(SRF08_ADDR+1);
        MasterWriteI2C2(0xF0|0x01);     //SRF08 I2C address+Read
        while(I2C2STATbits.TBF);
        while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
        while(I2C2STATbits.ACKSTAT);    //then clear it   
     //
        hi=MasterReadI2C2();      //read Most Significant Bit
        AckI2C2();     //start acknowledge sequence
        while(I2C2CONbits.ACKEN);//wait for ack.sequence to end;
        
        lo=MasterReadI2C2();      //read Least significant Bit
        NotAckI2C2();     //not acknowledge for last byte
        while(I2C2CONbits.ACKEN);//wait for ack.sequence to end
     //StopI2C2();
        I2C2CONbits.PEN=1;         //send stop bit
        while(I2C2CONbits.PEN);

        range = ((hi<<8)+lo);
        
  }
Aber ich habe jetzt noch andere Frage. Wie man SRF08 mit Hyperterminal kommunizieren kann. Soll ich "Printf();"schreiben?

Viele Gruesse

Maximilian