PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : SRF08 mit I2C BUs am dsPIC33EP512MU810 anschließen,LED von Sensor lechtet nicht



Maximilian1990
30.09.2014, 08:34
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/Devices.aspx?product=dsPIC33EP512MU810) 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:

#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);


}

Maximilian1990
01.10.2014, 17:21
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


#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);

}

Maximilian1990
08.10.2014, 15:13
Hier ist meine neue 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

schorsch_76
08.10.2014, 15:23
Bist du sicher das hier Überall "Bitund" haben willst? Nicht "Bit Oder"? "|" anstatt "&"



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
);

Maximilian1990
08.10.2014, 22:42
Ja,Ich bin sicher. Was meisnst du dafuer?

witkatz
09.10.2014, 13:31
Sorry wenn ich dazwischen funke.
Bist du sicher das hier Überall "Bitund" haben willst? Nicht "Bit Oder"? "|" anstatt "&"
Das scheint zumindest von der Logik her in Ordnung so. Microchip verwendet im XC16 an der Stelle UND-basierte Bitmasken-Definitionen:
/* I2C2CON register Configuration bit definitions */

#define I2C2_ON 0xFFFF /*I2C module enabled */
#define I2C2_OFF 0x7FFF /*I2C module disabled */

#define I2C2_IDLE_STOP 0xFFFF /*stop I2C module in Idle mode */
#define I2C2_IDLE_CON 0xDFFF /*continue I2C module in Idle mode */

#define I2C2_CLK_REL 0xFFFF /*release clock */
#define I2C2_CLK_HLD 0xEFFF /*hold clock */

#define I2C2_IPMI_EN 0xFFFF /*IPMI mode enabled */
#define I2C2_IPMI_DIS 0xF7FF /*IPMI mode not enabled */
... Ob damit die I2C Schnittstelle richtig parametriert ist, kann ich nicht sagen. Aber zum Glück sind die Config Register dokumentiert. Der Firmware-Programmierer sollte in der Lage sein das Datenblatt der MCU mit Verständnis zu lesen und entspr. im Quellcode umzusetzen.

Die Frage mit dem Hyperterminal habe ich noch nicht verstanden. Wenn du "Printf();" schreibst, wird die ASCII Zeichenfolge "Printf();" also 0x50, 0x72, 0x69, ... über die serielle Schnittstelle (RS232) des Rechners gesendet. Und dann?

Maximilian1990
09.10.2014, 15:44
Ich werde weiter das Programm schreiben und probieren. Wenn alles in Ordnung ist, werde ich weite share