This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
各位前輩與先進您好,
目前使用MSP430G2553來進行I2C通訊,
程式代碼:
if ( TI_USCI_I2C_slave_present(0x2a) ) ;// 這行回傳值為"1"
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmitinit(0x2a,ISPEED);
array[0] = 0x08; array[1]=0x12; array[2]= 0x34; // RCOUNT = ffff
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array
其中黃色為SCLK,藍色為SDA,下圖為示波器上看到的值。
可以看到一開始為高電位,但是結束後卻沒有拉回到高電位。
還請各位先進與高手幫忙解答,感謝
Susan 您好,
MSP43-G2553 (EXP430G2) with the FDC2212(Captative to digital IC).
BCSCTL1 = CALBC1_8MHZ; //
DCOCTL = CALDCO_8MHZ; //
array[0] = 0x08; array[1]=0xff; array[2]= 0xff; // RCOUNT = ffff
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmitinit(0x2a,ISPEED);
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array
++ 傳遞之後,波型如下:
i2C 外掛 10k 歐姆,有pull high.
想要跟您請教:
1. 如何通常pull high電阻會用多少?
2. 如何增加延遲時間以利i2c能夠順利傳送資料(避免佔線問題)
謝謝您
lin
/*** USCI master library ************************************************************ In this file the usage of the USCI I2C master library without DMA support is shown. This library uses pointers to specify what data is to be sent. When calling the TI_USCI_I2C_receive or TI_USCI_I2C_transmit routines the number of bytes, which are to be transmitted or received have to be passed as well as a pointer to a data field, that contains(or stores) the data. This code checks if there is a slave with address 0x50 is connected to the I2C bus and if the slave device is present, bytes are received and transmitted. Uli Kretzschmar MSP430 Systems Freising Added FDC2212-Q1 code for TIDA-01409 Capacitive Kick-to-Open Reference Design *******************************************************************************/ #include "msp430g2553.h" #include "TI_USCI_I2C_master.h" #define DATA_CH0_MSB_REG 0x00 #define DATA_CH0_LSB_REG 0x01 #define DATA_CH1_MSB_REG 0x02 #define DATA_CH1_LSB_REG 0x03 ///#define ISPEED 0x3F #define ISPEED 63 //#define ISPEED 12 #define MARGIN 500 signed char byteCtr; signed char Alive=0; unsigned char *TI_receive_field; unsigned char *TI_transmit_field; unsigned char timercounter; unsigned char array[3] = {0x00, 0x00, 0x00 }; unsigned char store[10] = { 0xff, 0xff }; unsigned char LSB_Channel_0[2] = { 0x0, 0x0}; unsigned char MSB_Channel_0[2] = { 0x0, 0x1}; unsigned char LSB_Channel_1[2] = { 0x0, 0x2}; unsigned char MSB_Channel_1[2] = { 0x0, 0x3}; unsigned char MUX_CNFG[2] = {0x00,0x00}; unsigned char RCOUNT_0[2] = {'R','0'}; unsigned char RCOUNT_1[2] = {'R','1'}; unsigned char SCOUNT_0[2] = {'S','0'}; unsigned char SCOUNT_1[2] = {'S','0'}; unsigned char IDRIVE_0[3] = {0x1E,0x78,0x00}; unsigned char IDRIVE_1[3] = {0x1F,0x78,0x00}; unsigned char CK_DIV_0[2] = {'C','D'}; unsigned char CK_DIV_1[2] = {'c','d'}; unsigned char ERR_CNFG[2] = {'E','C'}; unsigned char CONFIG[2] = {'C','F'}; unsigned short DataChannel_0_MSB ; unsigned short DataChannel_0_LSB ; unsigned long DataChannel_0 ; unsigned short DataChannel_1_MSB=0 ; unsigned short DataChannel_1_LSB=0; unsigned long DataChannel_1 ; unsigned long Measurement1; unsigned long Measurement2; unsigned long Measurement3; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WatchDogTimer // Set up the GPIO pins for the TIDA-01409 board P1DIR |= BIT1; // P1.1 is an output (unused pin) P1DIR |= BIT4; // P1.4 is an output (Shutdown) -> FDC2212 Pin6 SD P1OUT &= ~BIT4; // Set P1.4 low so Shutdown is LOW P1DIR |= BIT2; // P1.2 is an output (ADDR) P1OUT &= ~BIT2; // Set P1.2 low so ADDR is LOW and FDC2212 address is 0x2a, -> FDC2212 Pin4 ADDR // P1OUT |= BIT2; // Set P1.2 high so ADDR is HIGH and FDC2212 address is 0x2b P2DIR |= BIT1; // Set P2.1 as an output, Green Led P2DIR |= BIT2; // Set P2.2 as an output, Red Led // P2DIR |= BIT5; // Set P2.5 as an output, // P2OUT |= BIT5; // Set P2.5 high to pull up SDA and SCL BCSCTL1 = CALBC1_8MHZ; // DCOCTL = CALDCO_8MHZ; // // BCSCTL1 = CALBC1_1MHZ; // // DCOCTL = CALDCO_1MHZ; // _EINT(); // Enable interrupts // Prepare to write to the FDC2212-Q1 registers while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED); // init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz Alive=TI_USCI_I2C_slave_present(0x2a); // Write to the RCOUNT registers for Channel 0 and Channel 1 array[0] = 0x08; array[1]=0xff; array[2]= 0xff; // RCOUNT = ffff while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array __delay_cycles(2000); array[0] = 0x09; array[1]=0xff; array[2]= 0xff; // RCOUNT = ffff, register address for RCOUNT_CH1 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array, consisting of the register address plus two data bytes __delay_cycles(2000); // Write to the ERROR_CONFIG register array[0] = 0x19; array[1]=0x00; array[2]= 0x01; // disable all the error reports, enable the data flag interr while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array __delay_cycles(2000); // Write to the SETTLECOUNT registers array[0] = 0x10; array[1]=0x00; array[2]= 0x0A; // SETTLECOUNT = 000A while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array __delay_cycles(2000); array[0] = 0x11; array[1]=0x00; array[2]= 0x0A; // SETTLECOUNT = 000A, register address for SETTLECOUNT_CH1 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array (register address plus two data bytes) __delay_cycles(2000); // Write to the CLOCK_DIVIDERS registers array[0] = 0x14; array[1]=0x20; array[2]= 0x01; // CLOCK_DIVIDERS FIN_SEL=2, FREF_DIVIDER=1 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array __delay_cycles(2000); array[0] = 0x15; array[1]=0x20; array[2]= 0x01; // CLOCK_DIVIDERS FIN_SEL=2, FREF_DIVIDER=1,register address for CLOCK_DIVIDERS_CH1 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array (register address plus two data bytes) __delay_cycles(2000); // Write to the DRIVE_CURRENT registers array[0] = 0x1E; array[1]=0x78; array[2]= 0x00; // see current drive table p.36 of FDC2212-Q1 datasheet while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,IDRIVE_0); // transmit the first three bytes from array __delay_cycles(2000); array[0] = 0x1F; array[1]=0x78; array[2]= 0x00; // register address for DRIVE_CURRENT_CH1 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,IDRIVE_1); // transmit the first three bytes from array (register address plus two data bytes) __delay_cycles(2000); // Write to the MUX_CONFIG register array[0] = 0x1B; array[1]=0x82; array[2]= 0x0D; // see p.35 of FDC2212-Q1 datasheet while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array __delay_cycles(2000); // Write to the CONFIG register // Last of the set-up write commands array[0] = 0x1A; array[1]=0x5e; array[2]= 0x01; // see p.34 of FDC2212-Q1 datasheet while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(3,array); // transmit the first three bytes from array __delay_cycles(2000); // Read the CONFIG_MUX register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x1B; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MUX_CNFG); // receive 2 bytes from slave // Read the RCOUNT_0 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x08; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,RCOUNT_0); // receive 2 bytes from slave // Read the RCOUNT_1 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x09; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,RCOUNT_1); // receive 2 bytes from slave // Read the SCOUNT_0 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x10; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,SCOUNT_0); // receive 2 bytes from slave // Read the SCOUNT_1 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x11; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,SCOUNT_1); // receive 2 bytes from slave // Read the IDRIVE_0 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x1E; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,&IDRIVE_0[1]); // receive 2 bytes from slave // Read the IDRIVE_1 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x1F; // register for Device ID while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,&IDRIVE_1[1]); // receive 2 bytes from slave // Read the CLOCK_DIVIDERS_CH0 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x14; // register for CLOCK_DIVIDERS_CH0 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,CK_DIV_0); // receive 2 bytes from slave // Read the CLOCK_DIVIDERS_CH1 register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x15; // register for CLOCK_DIVIDERS_CH0 while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,CK_DIV_1); // receive 2 bytes from slave // Read the ERROR_CONFIG register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x19; // register for ERROR_CONFIG while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,ERR_CNFG); // receive 2 bytes from slave // Read the CONFIG register while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a array[0] = 0x1a; // register for CONFIG while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,CONFIG); // receive 2 bytes from slave // begin the looping for(;;){ // Measurement 1 ************ P2OUT &= ~BIT1; // Turn off the Green LED P2OUT |= BIT2; // Turn on the RED LED __delay_cycles(10000000); // Read the MSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x02; // register for DATA_CH1 MSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1]; // Read the LSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x03; // register for DATA_CH1_LSB LSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1]; // DataChannel_0 = DataChannel_0_MSB * 65536 + DataChannel_0_LSB ; // DataChannel_1 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; Measurement1 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; // Read the MSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x02; // register for DATA_CH1 MSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1]; // Read the LSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x03; // register for DATA_CH1_LSB LSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1]; // DataChannel_0 = DataChannel_0_MSB * 65536 + DataChannel_0_LSB ; // DataChannel_1 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; Measurement1 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; // Measurement 2 ************ P2OUT &= ~BIT2; // Turn off the Red LED __delay_cycles(10000000); P2OUT |= BIT1; // Turn on the Green LED __delay_cycles(10000000); // Read the MSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x02; // register for DATA_CH0 MSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1]; // Read the LSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x03; // register for DATA_CH0_LSB LSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1]; // DataChannel_0 = DataChannel_0_MSB * 65536 + DataChannel_0_LSB ; Measurement2 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; // Measurement2 = DataChannel_1; // Read the MSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x02; // register for DATA_CH0 MSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1]; // Read the LSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x03; // register for DATA_CH0_LSB LSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1]; // DataChannel_0 = DataChannel_0_MSB * 65536 + DataChannel_0_LSB ; Measurement2 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; // Measurement2 = DataChannel_1; P2OUT &= ~BIT1; P2OUT &= ~BIT2; __delay_cycles(10000000); // Measurement 3 ************ P2OUT |= BIT1; // Turn on the Green LED P2OUT |= BIT2; // Turn on the RED LED __delay_cycles(10000000); // Read the MSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x02; // register for DATA_CH0 MSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1]; // Read the LSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x03; // register for DATA_CH0_LSB LSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1]; // DataChannel_0 = DataChannel_0_MSB * 65536 + DataChannel_0_LSB ; Measurement3 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; // Measurement3 = DataChannel_1; P2OUT |= BIT1; // Turn on the Green LED P2OUT |= BIT2; // Turn on the RED LED // __delay_cycles(10000000); // Read the MSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x02; // register for DATA_CH0 MSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1]; // Read the LSB of Channel 1 Data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz array[0] = 0x03; // register for DATA_CH0_LSB LSB of channel 1 data while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_transmit(1,array); // transmit the first byte from array while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI while ( TI_USCI_I2C_notready() ); // wait for bus to be free TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1]; // DataChannel_0 = DataChannel_0_MSB * 65536 + DataChannel_0_LSB ; Measurement3 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB ; // Measurement3 = DataChannel_1; P2OUT &= ~BIT1; // turn off Green LED P2OUT &= ~BIT2; // turn off Red LED __delay_cycles(5000000); if((Measurement1 > (Measurement2 + MARGIN)) && (Measurement3 > (Measurement2 + MARGIN))) { P2OUT |= BIT1;//red __delay_cycles(2000000); P2OUT &= ~BIT1; __delay_cycles(2000000); P2OUT |= BIT1; __delay_cycles(2000000); P2OUT &= ~BIT1; __delay_cycles(2000000); P2OUT |= BIT1; __delay_cycles(2000000); P2OUT &= ~BIT1; __delay_cycles(2000000); P2OUT |= BIT1; __delay_cycles(2000000); P2OUT &= ~BIT1; __delay_cycles(2000000); } else { P2OUT |= BIT2;//green __delay_cycles(2000000); P2OUT &= ~BIT2; __delay_cycles(2000000); P2OUT |= BIT2; __delay_cycles(2000000); P2OUT &= ~BIT2; __delay_cycles(2000000); P2OUT |= BIT2; __delay_cycles(2000000); P2OUT &= ~BIT2; __delay_cycles(2000000); P2OUT |= BIT2; __delay_cycles(2000000); P2OUT &= ~BIT2; __delay_cycles(2000000); } // end of if-else loop __delay_cycles(3000000); } // end of for loop LPM3; // low power mode 3 }
目前看起來I2c已經能夠正常傳遞資料,不過對於FDC2212的量測,仍然不清楚,
// Read the MSB of Channel 1 Data
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz
array[0] = 0x02; // register for DATA_CH1 MSB of channel 1 data
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmit(1,array); // transmit the first byte from array
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_receive(2,MSB_Channel_1); // receive 2 bytes from FDC
DataChannel_1_MSB = MSB_Channel_1[0] * 256 + MSB_Channel_1[1];
// Read the LSB of Channel 1 Data
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmitinit(0x2a,ISPEED);// init transmitting to slave 2a with baud rate SMCLK / 3f (3f = 63) that is, 8 MHz / 63 = 126 kHz
array[0] = 0x03; // register for DATA_CH1_LSB LSB of channel 1 data
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_transmit(1,array); // transmit the first byte from array
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_receiveinit(0x2a,ISPEED); // init receiving with USCI
while ( TI_USCI_I2C_notready() ); // wait for bus to be free
TI_USCI_I2C_receive(2,LSB_Channel_1); // receive 2 bytes from FDC
DataChannel_1_LSB = LSB_Channel_1[0] * 256 + LSB_Channel_1[1];
Measurement1 = DataChannel_1_MSB * 65536 + DataChannel_1_LSB;
程式中,重複兩次(Measurement1),接下來還有Measurement2&3.
最後利用這個判斷式,判斷電路是否有效觸發:(Margin 設為500)
if((Measurement1 > (Measurement2 + MARGIN)) && (Measurement3 > (Measurement2 + MARGIN)))
能否請您指導該如何做實驗,來提升判斷的準確度,謝謝您!