|
小弟现在课程实验需要从一个温度传感器读取温度数据到我的TI F28335的板子,使用的是I2C接口。传感器的硬件地址是0x40,使用手册说的是只要先发送一个0xE5的指令,传感器就可以发送一个16bit的温度数据。
说起来很简单,写0xE5进去然后读取就好了,可是小弟编写的程序运行后,在示波器上显示只能看到地址0x40的波形,根本读取不了温度数据,不知道编的程序哪里出了问题,希望各位大牛解答下!
- // TITLE: DSP28335 I2C test
- // temperature and humidity sensor HTU21D
- // slave address 0x40
- // I2C communication structure: send write register address (0x80) + command 0xF5 (for humidity measurement)
- // and then sent read register address (0x81)
- //###########################################################################
- #include "DSP2833x_Device.h"
- #define SLAVE_Address 0x40
- #define Write_humidity_hold 0xE5
- #define Write_humidity_unhold 0xF5
- #define Write_temperature_unhold 0xF3
- // external function prototypes
- extern void InitSysCtrl(void);
- extern void InitPieCtrl(void);
- extern void InitPieVectTable(void);
- extern void InitCpuTimers(void);
- extern void ConfigCpuTimer(struct CPUTIMER_VARS *, float, float);
- // Prototype statements for functions found within this file.
- void Gpio_select(void);
- void I2CA_Init(void);
- interrupt void cpu_timer0_isr(void);
- int Value_Read;
- //###########################################################################
- // main code
- //###########################################################################
- void main(void)
- {
- InitSysCtrl(); // Basic Core Init from DSP2833x_SysCtrl.c
- EALLOW;
- SysCtrlRegs.WDCR= 0x00AF; // Re-enable the watchdog
- EDIS; // 0x00AF to NOT disable the Watchdog, Prescaler = 64
- DINT; // Disable all interrupts
- Gpio_select(); // GPI031-LED2, GPIO34-LED3 as output
- // to 2 LEDs on F28335 control-card
- I2CA_Init(); // Initialize I2C device slave address is issued in I2CA_Init() 0x29
- InitPieCtrl(); // basic setup of PIE table; from DSP2833x_PieCtrl.c
- InitPieVectTable(); // default ISR's in PIE
- EALLOW;
- PieVectTable.TINT0 = &cpu_timer0_isr;
- EDIS;
- InitCpuTimers(); // basic setup CPU Timer0, 1 and 2
- ConfigCpuTimer(&CpuTimer0,150,100000);
- PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
- IER |=1;
- EINT;
- ERTM;
- CpuTimer0Regs.TCR.bit.TSS = 0; // start timer0
- //step 2 : start read the sensor data including 1 byte pointer register==>2 byte sensor data (upper 8bit) + (lower 8bit)
- while(1)
- {
- while(CpuTimer0.InterruptCount == 0);
- CpuTimer0.InterruptCount = 0;
- EALLOW;
- SysCtrlRegs.WDKEY = 0x55; // service WD #1
- EDIS;
- // Send START and set pointer to temperature - register
- I2caRegs.I2CCNT = 1; // pointer to temperature register, count 1 bit only
- I2caRegs.I2CDXR = Write_humidity_unhold;// address for reading
- I2caRegs.I2CMDR.all = 0x6620; //0110011000100000only change stop to '0', do not generate stop. all others are the same as 6E20
- /* Bit15 = 0; no NACK in receiver mode
- Bit14 = 1; FREE on emulation halt
- Bit13 = 1; STT generate START
- Bit12 = 0; reserved
- Bit11 = 0; STP not generate STOP
- Bit10 = 1; MST master mode
- Bit9 = 1; TRX master - transmitter mode
- Bit8 = 0; XA 7 bit address mode
- Bit7 = 0; RM nonrepeat mode, I2CCNT determines # of bytes
- Bit6 = 0; DLB no loopback mode ,it should be '0', corrected by alex.J
- Bit5 = 1; IRS I2C module enabled
- Bit4 = 0; STB no start byte mode
- Bit3 = 0; FDF no free data format
- Bit2-0: 0; BC 8 bit per data byte,000 */
- while(I2caRegs.I2CSTR.bit.ARDY == 0); // 0=previous cycle has not completed, wait for access ready condition
- // while(I2caRegs.I2CSTR.bit.XRDY == 0); // wait until first byte is out
- //while(I2caRegs.I2CSTR.bit.SCD == 0); // wait for STOP condition
- //I2caRegs.I2CSTR.bit.SCD = 1; //clear stop condition flag
- // Send start as master transmitter
- I2caRegs.I2CCNT = 2; // read 2 byte temperature
- I2caRegs.I2CMDR.all = 0x6C20; //0110010000100000 only change stop to '0', do not generate stop. all others are the same as 6E20
- /* Bit15 = 0; no NACK in receiver mode
- Bit14 = 1; FREE on emulation halt
- Bit13 = 1; STT generate START
- Bit12 = 0; reserved
- Bit11 = 0; STP not generate STOP
- Bit10 = 1; MST master mode
- Bit9 = 0; TRX master - receiver mode
- Bit8 = 0; XA 7 bit address mode
- Bit7 = 0; RM nonrepeat mode, I2CCNT determines # of bytes
- Bit6 = 0; DLB no loopback mode ,it should be '0', corrected by alex.J
- Bit5 = 1; IRS I2C module enabled
- Bit4 = 0; STB no start byte mode
- Bit3 = 0; FDF no free data format
- Bit2-0: 0; BC 8 bit per data byte,000 */
- while(I2caRegs.I2CSTR.bit.RRDY == 1); //0 means receive data NOT available in 12CDRR, wait for 1st byte
- Value_Read = I2caRegs.I2CDRR << 8; // read upper 8 Bit (integers)
- Value_Read += I2caRegs.I2CDRR;// RRDY is automatically cleared by read of I2CDRR
- GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1; // toggle red LED LD3 @ 28335CC
- GpioDataRegs.GPATOGGLE.bit.GPIO31 = 1;
- }
- }
- void Gpio_select(void)
- {
- EALLOW;
- GpioCtrlRegs.GPAMUX1.all = 0; // GPIO15 ... GPIO0 = General Puropse I/O
- GpioCtrlRegs.GPAMUX2.all = 0; // GPIO31 ... GPIO16 = General Purpose I/O
- GpioCtrlRegs.GPBMUX1.all = 0; // GPIO47 ... GPIO32 = General Purpose I/O
- GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // GPIO32 = I2C - SDA
- GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // GPIO33 = I2C - SCL
- GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA)
- GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO33 (SCLA)
- GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA)
- GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO33 (SCLA)
- GpioCtrlRegs.GPBMUX2.all = 0; // GPIO63 ... GPIO48 = General Purpose I/O
- GpioCtrlRegs.GPCMUX1.all = 0; // GPIO79 ... GPIO64 = General Purpose I/O
- GpioCtrlRegs.GPCMUX2.all = 0; // GPIO87 ... GPIO80 = General Purpose I/O
- GpioCtrlRegs.GPADIR.all = 0; // GPIO0 to 31 as inputs
- // GpioCtrlRegs.GPADIR.bit.GPIO9 = 1; // GPIO9 = LED LD1
- GpioCtrlRegs.GPADIR.bit.GPIO31 = 1; // GpIO11 = LED LD2
- GpioCtrlRegs.GPBDIR.all = 0; // GPIO63-32 as inputs
- GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1; // peripheral explorer: LED LD3 at GPIO34
- //GpioCtrlRegs.GPBDIR.bit.GPIO49 = 1; // peripheral explorer: LED LD4 at GPIO49
- GpioCtrlRegs.GPCDIR.all = 0; // GPIO87-64 as inputs
- EDIS;
- }
- void I2CA_Init(void)
- {
- I2caRegs.I2CMDR.bit.IRS = 0; // Reset the I2C module
- // I2C slave address register
- I2caRegs.I2CSAR = SLAVE_Address;
- // I2C Prescale Register
- I2caRegs.I2CPSC.all = 14; // Internal I2C module clock = SYSCLK/(PSC +1)
- // = 10 MHz
- I2caRegs.I2CCLKL = 95; // Tmaster = (PSC +1)[ICCL + 5 + ICCH + 5] / 150MHz
- I2caRegs.I2CCLKH = 95; // Tmaster = 10 [ICCL + ICCH + 10] / 150 MHz
- // d = 5 for IPSC >1
- // for I2C 50 kHz:
- // Tmaster = 20 μs *150 MHz / 10 = 200 = (ICCL + ICCH +10)
- // ICCL + ICCH = 190
- // ICCL = ICH = 190/2 = 95
- // I2caRegs.I2CCLKL = 45;
- // I2caRegs.I2CCLKH = 45; // for I2C 100 kHz:
- // Tmaster = 10 μs *150 MHz / 10 = 100 = (ICCL + ICCH + 10)
- // ICCL + ICCH = 90
- // ICCL = ICH = 90/2 = 45
- I2caRegs.I2CMDR.bit.IRS = 1; // Take I2C out of reset
- }
- interrupt void cpu_timer0_isr(void)
- {
- CpuTimer0.InterruptCount++;
- EALLOW;
- SysCtrlRegs.WDKEY = 0xAA; // service WD #2
- EDIS;
- PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
- }
- //===========================================================================
- // End of SourceCode.
- //===========================================================================
复制代码
|
|