Hallo!
Ich habe neue code geschrieben. Aber leider funktioniert es immer nicht. kann ich mal ueber high Byte und low Byte read fragen, wie man code dafuer schreiben soll.
Bin für jede Hilfe sehr dankbar!!
Mit freundlichen Grüßen
Maximilian
Code:#include "p33EP512MU810.h" #include "i2c.h" #include <Initialisierung.h> #include "Timer.h" //Schritt 1 /*Configuring the general features of the microcontroller*/ _FGS(GSS_OFF & GWRP_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 #define SONAR1 0xE0 unsigned int get_srf08(unsigned int SONAR); /*** 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(SONAR1); } } //Schritt 3 // Start Condition unsigned int get_srf08 (unsigned int SONAR) { unsigned int range; //range=Reichweiten unsigned char hi,lo; //high Byte,low Byte //StartI2C2(); I2C2CONbits.SEN=1; //send start bit while(I2C2CONbits.SEN); //and wait fot it to clear //MasterWriteI2C2(SRF08_ADDR); MasterWriteI2C2(0xE0); //SRF08 I2C address while(I2C2STATbits.TBF); while(!IFS3bits.MI2C2IF); // Wait for 9th clock cycle. while(I2C2STATbits.ACKSTAT); //MasterWriteI2C2(0x01); MasterWriteI2C2(0x01); //Verstärkungsregister Adresse while(I2C2STATbits.TBF); while(!IFS3bits.MI2C2IF); // Wait for 9th clock cycle. while(I2C2STATbits.ACKSTAT); //then clear it //MasterWriteI2C2(0x00); MasterWriteI2C2(0x00); //Verstärkung 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); //StartI2C2(); I2C2CONbits.SEN=1; //send start bit while(I2C2CONbits.SEN); //and wait fot it to clear //MasterWriteI2C2(SRF08_ADDR); MasterWriteI2C2(0xE0); //SRF08 I2C address while(I2C2STATbits.TBF); while(!IFS3bits.MI2C2IF); // Wait for 9th clock cycle. while(I2C2STATbits.ACKSTAT); //MasterWriteI2C2(0x02); MasterWriteI2C2(0x02); //Reichweiten-Register Adresse while(I2C2STATbits.TBF); while(!IFS3bits.MI2C2IF); // Wait for 9th clock cycle. while(I2C2STATbits.ACKSTAT); //then clear it //MasterWriteI2C2(0xA0); MasterWriteI2C2(0xA0); //Reichweiten=160 160*43mm+43mm = 6,9m 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 /*now wait for the ranging to complete. This delay is 104ms*/ //Timer off; 16-bit mode; Prescaler 2; Input clk CPU/64 //delay_ms(80); T6CON = 0x0000; T6CONbits.TCKPS = 2; // Timer6 period 419.424 ms = 2.384222171 Hz PR6 = 0xFFFF-0x001; //w:? // 40MHz/64 = 625KHz , 625KHz/65535 = 9.537 Hz // = 104,855 ms // later on 80ms required form this timer for the utrasonic // initializing. the calculation as it shown below: // // PeriodofChecking80ms 65535 // --------------------- = --------- ===> PeriodtoCheck // 80ms 104,855ms IFS2bits.T6IF = 0; //clear old interrupt request. IEC2bits.T6IE = 0; //disable interrupt. T6CONbits.TON = 0; //switch timer off. //Schritt 5 //finally get the range form the SRF08 //StartI2C2(); I2C2CONbits.SEN=1; //send start bit while(I2C2CONbits.SEN); //and wait fot it to clear //??????? //MasterWriteI2C2(SRF08_ADDR+1); MasterWriteI2C2(0xE0|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(); //start acknowledge sequence while(I2C2CONbits.ACKEN);//wait for ack.sequence to end //StopI2C2(); I2C2CONbits.PEN=1; //send stop bit while(I2C2CONbits.PEN); range = ((hi<<8)+lo); }






Zitieren


Lesezeichen