小弟求教 28335的I2C通信问题

2019-03-24 09:05发布

小弟现在课程实验需要从一个温度传感器读取温度数据到我的TI F28335的板子,使用的是I2C接口。传感器的硬件地址是0x40,使用手册说的是只要先发送一个0xE5的指令,传感器就可以发送一个16bit的温度数据。

说起来很简单,写0xE5进去然后读取就好了,可是小弟编写的程序运行后,在示波器上显示只能看到地址0x40的波形,根本读取不了温度数据,不知道编的程序哪里出了问题,希望各位大牛解答下!

  1. // TITLE:        DSP28335 I2C test
  2. //          temperature and humidity sensor HTU21D
  3. //          slave address 0x40
  4. //          I2C communication structure: send write register address (0x80) + command 0xF5 (for humidity measurement)
  5. //          and then sent read register address (0x81)
  6. //###########################################################################
  7. #include "DSP2833x_Device.h"

  8. #define SLAVE_Address                             0x40

  9. #define Write_humidity_hold         0xE5
  10. #define Write_humidity_unhold       0xF5
  11. #define Write_temperature_unhold    0xF3


  12. // external function prototypes
  13. extern void InitSysCtrl(void);
  14. extern void InitPieCtrl(void);
  15. extern void InitPieVectTable(void);
  16. extern void InitCpuTimers(void);
  17. extern void ConfigCpuTimer(struct CPUTIMER_VARS *, float, float);


  18. // Prototype statements for functions found within this file.
  19. void Gpio_select(void);
  20. void I2CA_Init(void);
  21. interrupt void cpu_timer0_isr(void);

  22. int Value_Read;

  23. //###########################################################################
  24. //                                                main code
  25. //###########################################################################
  26. void main(void)
  27. {
  28.         InitSysCtrl();                // Basic Core Init from DSP2833x_SysCtrl.c

  29.         EALLOW;
  30.            SysCtrlRegs.WDCR= 0x00AF;        // Re-enable the watchdog
  31.            EDIS;                        // 0x00AF  to NOT disable the Watchdog, Prescaler = 64

  32.         DINT;                        // Disable all interrupts

  33.         Gpio_select();        // GPI031-LED2, GPIO34-LED3 as output
  34.                                         // to 2 LEDs on F28335 control-card

  35.         I2CA_Init();         //        Initialize I2C device slave address is issued in I2CA_Init() 0x29

  36.         InitPieCtrl();                // basic setup of PIE table; from DSP2833x_PieCtrl.c
  37.         InitPieVectTable();        // default ISR's in PIE

  38.         EALLOW;
  39.         PieVectTable.TINT0 = &cpu_timer0_isr;
  40.         EDIS;

  41.         InitCpuTimers();        // basic setup CPU Timer0, 1 and 2
  42.         ConfigCpuTimer(&CpuTimer0,150,100000);

  43.         PieCtrlRegs.PIEIER1.bit.INTx7 = 1;

  44.         IER |=1;

  45.         EINT;
  46.         ERTM;

  47.         CpuTimer0Regs.TCR.bit.TSS = 0;        // start timer0

  48.         //step 2 : start read the sensor data including 1 byte pointer register==>2 byte sensor data (upper 8bit) + (lower 8bit)

  49.                 while(1)
  50.         {

  51.                   while(CpuTimer0.InterruptCount == 0);

  52.                     CpuTimer0.InterruptCount = 0;

  53.                         EALLOW;
  54.                         SysCtrlRegs.WDKEY = 0x55;        // service WD #1
  55.                         EDIS;

  56.                         //        Send START and set pointer to temperature - register
  57.                         I2caRegs.I2CCNT         = 1;                                // pointer to temperature register, count 1 bit only
  58.                         I2caRegs.I2CDXR                = Write_humidity_unhold;// address for reading
  59.                         I2caRegs.I2CMDR.all = 0x6620; //0110011000100000only change stop to '0', do not generate stop. all others are the same as 6E20
  60.                                         /*        Bit15 = 0;        no NACK in receiver mode
  61.                                                 Bit14 = 1;  FREE on emulation halt
  62.                                                 Bit13 = 1;  STT  generate START
  63.                                                 Bit12 = 0;        reserved
  64.                                                 Bit11 = 0;  STP  not generate STOP
  65.                                                 Bit10 = 1;  MST  master mode
  66.                                                 Bit9  = 1;  TRX  master - transmitter mode
  67.                                                 Bit8  = 0;        XA   7 bit address mode
  68.                                                 Bit7  = 0;  RM   nonrepeat mode, I2CCNT determines # of bytes
  69.                                                 Bit6  = 0;  DLB  no loopback mode ,it should be '0', corrected by alex.J
  70.                                                 Bit5  = 1;  IRS  I2C module enabled
  71.                                                 Bit4  = 0;  STB  no start byte mode
  72.                                                 Bit3  = 0;  FDF  no free data format
  73.                                                 Bit2-0: 0;  BC   8 bit per data byte,000        */

  74.                         while(I2caRegs.I2CSTR.bit.ARDY == 0);                        // 0=previous cycle has not completed, wait for access ready condition
  75.         //  while(I2caRegs.I2CSTR.bit.XRDY == 0);                // wait until first byte is out

  76.                         //while(I2caRegs.I2CSTR.bit.SCD == 0);                        // wait for STOP condition

  77.                     //I2caRegs.I2CSTR.bit.SCD = 1; //clear stop condition flag

  78.                         // Send start as master transmitter

  79.                         I2caRegs.I2CCNT                = 2;        // read 2 byte temperature
  80.                         I2caRegs.I2CMDR.all = 0x6C20; //0110010000100000 only change stop to '0', do not generate stop. all others are the same as 6E20
  81.                                         /*        Bit15 = 0;        no NACK in receiver mode
  82.                                                 Bit14 = 1;  FREE on emulation halt
  83.                                                 Bit13 = 1;  STT  generate START
  84.                                                 Bit12 = 0;        reserved
  85.                                                 Bit11 = 0;  STP  not generate STOP
  86.                                                 Bit10 = 1;  MST  master mode
  87.                                                 Bit9  = 0;  TRX  master - receiver mode
  88.                                                 Bit8  = 0;        XA   7 bit address mode
  89.                                                 Bit7  = 0;  RM   nonrepeat mode, I2CCNT determines # of bytes
  90.                                                 Bit6  = 0;  DLB  no loopback mode ,it should be '0', corrected by alex.J
  91.                                                 Bit5  = 1;  IRS  I2C module enabled
  92.                                                 Bit4  = 0;  STB  no start byte mode
  93.                                                 Bit3  = 0;  FDF  no free data format
  94.                                                 Bit2-0: 0;  BC   8 bit per data byte,000        */

  95.                         while(I2caRegs.I2CSTR.bit.RRDY == 1);        //0 means receive data NOT available in 12CDRR, wait for 1st byte
  96.                         Value_Read = I2caRegs.I2CDRR << 8; // read upper 8 Bit (integers)
  97.                         Value_Read += I2caRegs.I2CDRR;// RRDY is automatically cleared by read of I2CDRR

  98.                           GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;                // toggle red LED LD3 @ 28335CC
  99.                           GpioDataRegs.GPATOGGLE.bit.GPIO31 = 1;
  100.         }
  101. }

  102. void Gpio_select(void)
  103. {
  104.         EALLOW;
  105.         GpioCtrlRegs.GPAMUX1.all = 0;                        // GPIO15 ... GPIO0 = General Puropse I/O
  106.         GpioCtrlRegs.GPAMUX2.all = 0;                        // GPIO31 ... GPIO16 = General Purpose I/O

  107.         GpioCtrlRegs.GPBMUX1.all = 0;                        // GPIO47 ... GPIO32 = General Purpose I/O
  108.         GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1;        // GPIO32 = I2C - SDA
  109.         GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1;        // GPIO33 = I2C - SCL

  110.         GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0;    // Enable pull-up for GPIO32 (SDAA)
  111.         GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0;           // Enable pull-up for GPIO33 (SCLA)

  112.         GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3;  // Asynch input GPIO32 (SDAA)
  113.     GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3;  // Asynch input GPIO33 (SCLA)

  114.         GpioCtrlRegs.GPBMUX2.all = 0;                        // GPIO63 ... GPIO48 = General Purpose I/O

  115.         GpioCtrlRegs.GPCMUX1.all = 0;                        // GPIO79 ... GPIO64 = General Purpose I/O
  116.         GpioCtrlRegs.GPCMUX2.all = 0;                        // GPIO87 ... GPIO80 = General Purpose I/O


  117.         GpioCtrlRegs.GPADIR.all = 0;                        // GPIO0 to 31 as inputs
  118. //        GpioCtrlRegs.GPADIR.bit.GPIO9 = 1;                // GPIO9 = LED LD1
  119.         GpioCtrlRegs.GPADIR.bit.GPIO31 = 1;                // GpIO11 = LED LD2

  120.         GpioCtrlRegs.GPBDIR.all = 0;                        // GPIO63-32 as inputs
  121.         GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;                // peripheral explorer: LED LD3 at GPIO34
  122.         //GpioCtrlRegs.GPBDIR.bit.GPIO49 = 1;         // peripheral explorer: LED LD4 at GPIO49

  123.         GpioCtrlRegs.GPCDIR.all = 0;                        // GPIO87-64 as inputs
  124.         EDIS;
  125. }

  126. void I2CA_Init(void)
  127. {

  128.         I2caRegs.I2CMDR.bit.IRS = 0;        // Reset the I2C module
  129.         // I2C slave address register
  130.         I2caRegs.I2CSAR = SLAVE_Address;
  131.         // I2C Prescale Register
  132.         I2caRegs.I2CPSC.all = 14;                // Internal I2C module clock = SYSCLK/(PSC +1)
  133.                                                                     // = 10 MHz

  134.     I2caRegs.I2CCLKL = 95;                        // Tmaster = (PSC +1)[ICCL + 5 + ICCH + 5] / 150MHz
  135.         I2caRegs.I2CCLKH = 95;                        // Tmaster =  10 [ICCL + ICCH + 10] / 150 MHz
  136.                                                                         // d = 5  for IPSC >1

  137.                                                                         // for I2C 50 kHz:
  138.                                                                         // Tmaster = 20 μs *150 MHz / 10 = 200 = (ICCL + ICCH +10)
  139.                                                                         // ICCL + ICCH = 190
  140.                                                                         // ICCL = ICH = 190/2 = 95

  141. //        I2caRegs.I2CCLKL = 45;
  142. //        I2caRegs.I2CCLKH = 45;                        // for I2C 100 kHz:
  143.                                                                         // Tmaster = 10 μs *150 MHz / 10 = 100 = (ICCL + ICCH + 10)
  144.                                                                         // ICCL + ICCH = 90
  145.                                                                         // ICCL = ICH = 90/2 = 45

  146.         I2caRegs.I2CMDR.bit.IRS = 1;        // Take I2C out of reset
  147. }

  148. interrupt void cpu_timer0_isr(void)
  149. {
  150.         CpuTimer0.InterruptCount++;
  151.         EALLOW;
  152.         SysCtrlRegs.WDKEY = 0xAA;        // service WD #2
  153.         EDIS;
  154.         PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
  155. }
  156. //===========================================================================
  157. // End of SourceCode.
  158. //===========================================================================
复制代码

此帖出自小平头技术问答
0条回答

一周热门 更多>

相关问题

    相关文章