SRF08超声波传感器和dsPIC33E I2C通信问题

[复制链接]
1970|0
 楼主| Maximilianliebe 发表于 2014-10-1 22:44 | 显示全部楼层 |阅读模式
以下是我的代码,实在不知道哪里出了错误,求求大哥们帮忙!!
  1. #include "p33EP512MU810.h"
  2. #include "i2c.h"
  3. #include <Initialisierung.h>
  4. #include "Timer.h"


  5. //Schritt 1


  6. /*Configuring the general features of the microcontroller*/

  7. _FGS(GSS_OFF & GWRP_OFF);
  8. // External Oscillator
  9. _FOSCSEL(FNOSC_PRIPLL);

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

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

  16. _FICD(ICS_PGD3 &  JTAGEN_OFF & RSTPRI_PF);

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


  21. //Schritt 2

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

  24. #define SONAR1 0xE0

  25. unsigned int get_srf08(unsigned int SONAR);


  26. /*** Global functions for the I2C2******************************************/
  27. main(void)
  28. {
  29.      unsigned int range1=0;
  30.      f_Initialisierung();

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

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

  41. /*End Configuration I2C2*/
  42.     while(1)
  43.     {
  44.         range1=get_srf08(SONAR1);
  45.     }
  46. }
  47. //Schritt 3
  48. // Start Condition

  49.   unsigned int get_srf08 (unsigned int SONAR)
  50. {

  51.     unsigned int range;  //range=Reichweiten
  52.     unsigned char hi,lo; //high Byte,low Byte
  53.    

  54.      //StartI2C2();
  55.         I2C2CONbits.SEN=1;         //send start bit
  56.         while(I2C2CONbits.SEN);    //and wait fot it to clear      
  57.      //MasterWriteI2C2(SRF08_ADDR);
  58.         MasterWriteI2C2(0xE0);     //SRF08 I2C address
  59.         while(I2C2STATbits.TBF);
  60.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  61.         while(I2C2STATbits.ACKSTAT);
  62.      //MasterWriteI2C2(0x01);
  63.         MasterWriteI2C2(0x01);    //Verstärkungsregister Adresse
  64.         while(I2C2STATbits.TBF);
  65.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  66.         while(I2C2STATbits.ACKSTAT);       //then clear it
  67.      //MasterWriteI2C2(0x00);
  68.         MasterWriteI2C2(0x00);     //Verstärkung
  69.         while(I2C2STATbits.TBF);
  70.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  71.         while(I2C2STATbits.ACKSTAT);          //then clear it
  72.      //StopI2C2();
  73.          I2C2CONbits.PEN=1;         //send stop bit
  74.          while(I2C2CONbits.PEN);




  75.      //StartI2C2();
  76.          I2C2CONbits.SEN=1;         //send start bit
  77.          while(I2C2CONbits.SEN);    //and wait fot it to clear
  78.      //MasterWriteI2C2(SRF08_ADDR);
  79.         MasterWriteI2C2(0xE0);     //SRF08 I2C address
  80.         while(I2C2STATbits.TBF);
  81.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  82.         while(I2C2STATbits.ACKSTAT);
  83.      //MasterWriteI2C2(0x02);
  84.         MasterWriteI2C2(0x02);    //Reichweiten-Register Adresse
  85.         while(I2C2STATbits.TBF);
  86.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  87.         while(I2C2STATbits.ACKSTAT);       //then clear it
  88.      //MasterWriteI2C2(0xA0);
  89.         MasterWriteI2C2(0xA0);     //Reichweiten=160    160*43mm+43mm = 6,9m
  90.         while(I2C2STATbits.TBF);
  91.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  92.         while(I2C2STATbits.ACKSTAT);          //then clear it
  93.      //StopI2C2();
  94.          I2C2CONbits.PEN=1;         //send stop bit
  95.          while(I2C2CONbits.PEN);




  96. //Schritt 4

  97.     /*now wait for the ranging to complete. This delay is 104ms*/
  98.      //Timer off; 16-bit mode; Prescaler 2; Input clk CPU/64

  99.      //delay_ms(80);
  100.         T6CON = 0x0000;
  101.         T6CONbits.TCKPS = 2;
  102.         // Timer6 period 419.424 ms = 2.384222171 Hz
  103.         PR6 = 0xFFFF-0x001;         //w:?
  104.         // 40MHz/64 = 625KHz , 625KHz/65535 = 9.537 Hz
  105.         //                                           = 104,855 ms
  106.         // later on 80ms required form this timer for the utrasonic
  107.         // initializing. the calculation as it shown below:
  108.         //
  109.         //  PeriodofChecking80ms      65535
  110.         // --------------------- = --------- ===> PeriodtoCheck
  111.         //          80ms           104,855ms

  112.         IFS2bits.T6IF = 0;                //clear old interrupt request.
  113.         IEC2bits.T6IE = 0;                //disable interrupt.
  114.         T6CONbits.TON = 0;                //switch timer off.


  115. //Schritt 5

  116.         //finally get the range form the SRF08

  117.      //StartI2C2();
  118.         I2C2CONbits.SEN=1;         //send start bit
  119.         while(I2C2CONbits.SEN);    //and wait fot it to clear            
  120.     //??????? //MasterWriteI2C2(SRF08_ADDR+1);
  121.         MasterWriteI2C2(0xE0|0x01);     //SRF08 I2C address+Read
  122.         while(I2C2STATbits.TBF);
  123.         while(!IFS3bits.MI2C2IF);       // Wait for 9th clock cycle.
  124.         while(I2C2STATbits.ACKSTAT);    //then clear it      
  125.      //??????????????????????
  126.         hi = MasterReadI2C2();      //read Most Significant Bit
  127.         AckI2C2();     //start acknowledge sequence
  128.         while(I2C2CONbits.ACKEN);//wait for ack.sequence to end;
  129.         
  130.         lo = MasterReadI2C2();      //read Least significant Bit      
  131.         NotAckI2C2();     //start acknowledge sequence
  132.         while(I2C2CONbits.ACKEN);//wait for ack.sequence to end

  133.      //StopI2C2();
  134.         I2C2CONbits.PEN=1;         //send stop bit
  135.         while(I2C2CONbits.PEN);

  136.         range = ((hi<<8)+lo);
  137.         
  138.   }

您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部