Hallo allerseits, ich hoffe ihr könnt mir helfen
Ich bin Neuer und möchte Ultraschallsensoren SRF08 (http://www.roboter-teile.de/datasheets/srf08.pdf) mit I2C Bus
am Mikrokotroller dsPIC33EP512MU810 (http://www.microchip.com/wwwproducts...IC33EP512MU810) anschließen.
Ich habe schon Code in C geschrieben.Aber es funktioniert leider nicht richtig.Die LED von Sensor leuchtet immer nicht.
Ich hoffe ihr könnt mir helfen.
Bin für jede Hilfe sehr dankbar!!
Mit freundlichen Grüßen
Maximilian
Hier ist die Code:
Code:
#include "p33EP512MU810.h"
#include "i2c.h"
#include <Initialisierung.h>
#include "Timer.h"
/*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)
#define CFG_I2C2BRG 0x188
// (40MHz/100KHz)-2 = 0x188//I2C2=[(1/Fscl-PGD)*Fcy]-2
#define SONAR1 0xE0
/*** Global functions for the I2C2******************************************/
main(void)
{
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);
}
// Start Condition
unsigned int get_srf08 (unsigned int SONAR)
{
unsigned int range; //range=Reichweiten
unsigned char hi,lo;
//This 1st part send the ranging command to the SRF08 to start it ranging
//StartI2C2();
I2C2CONbits.SEN=1; //send start bit
while(I2C2CONbits.SEN); //and wait fot it to clear
//MasterWriteI2C2(SRF08_ADDR);
MasterWriteI2C2(SONAR); //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(SONAR); //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(SONAR); //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);
//StartI2C2();
I2C2CONbits.SEN=1; //send start bit
while(I2C2CONbits.SEN); //and wait fot it to clear
//MasterWriteI2C2(SRF08_ADDR);
MasterWriteI2C2(SONAR); //SRF08 I2C address
while(I2C2STATbits.TBF);
while(!IFS3bits.MI2C2IF); // Wait for 9th clock cycle.
while(I2C2STATbits.ACKSTAT);
//MasterWriteI2C2(0x02);
MasterWriteI2C2(0x00); //SRF08 I2C address
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);
/*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 = 12500
// 80ms 104,855ms
IFS2bits.T6IF = 0; //clear old interrupt request.
IEC2bits.T6IE = 0; //disable interrupt.
T6CONbits.TON = 0; //switch timer off.
//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);
MasterWriteI2C2(SONAR); //SRF08 I2C address
while(I2C2STATbits.TBF);
while(!IFS3bits.MI2C2IF); // Wait for 9th clock cycle.
while(I2C2STATbits.ACKSTAT);
//MasterWriteI2C2(0x02);
MasterWriteI2C2(0x02); //SRF08 I2C address
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);
//MasterWriteI2C2(SRF08_ADDR+1);
MasterWriteI2C2(SONAR|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
I2C2CONbits.ACKEN = 1; //start acknowledge sequence
while(I2C2CONbits.ACKEN);//wait for ack.sequence to end;
lo = MasterReadI2C2(); //read Least significant Bit
I2C2CONbits.ACKDT=1; //not acknowledge for last byte
I2C2CONbits.ACKEN = 1; //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);
}
Lesezeichen