MSP430-FR 6047模块在 I2C 协议中,我已经为写和读设置了一个特定的值。
写我已经设置22.555656在一个数组,它只需要22个我需要写和捕获的值后的小数点。
我需要执行任何转换过程吗?
如果是、建议我执行一些步骤和逻辑
#include <msp430.h> #include <stdint.h> #define ENABLE_PINS 0xFFFE #define DATA_SIZE 50 // Number of bytes to read //unsigned int addr = 0; unsigned int tx_byte_cnt = 0; unsigned int rx_byte_cnt = 0; float *g_buf = 0; char data_in[DATA_SIZE] = {0}; volatile uint8_t bytes_received = 0; volatile uint8_t eeprom_address = 0; // Starting EEPROM address void mem_write(unsigned int eeprom_addr, float *buf, int len) { //bytes_transmitted = 0; // addr = eeprom_addr; g_buf = buf; rx_byte_cnt = 0; tx_byte_cnt = len+1; UCB1I2CSA = (0xA0 >> 1); UCB1IE |= UCTXIE0; UCB1CTLW0 |= UCTXSTT; } void mem_read(uint16_t eeprom_addr, float *buf, int len) { // bytes_transmitted = 0; buf[0]=eeprom_addr>>8; buf[1]=eeprom_addr;//& 0xFF; g_buf = buf; rx_byte_cnt = len; tx_byte_cnt = 0; UCB1I2CSA = (0xA0 >> 1); UCB1IE |= UCTXIE0; UCB1CTLW0 |= UCTXSTT; } int main(void) { // char arr[5]="Hello"; float arr[15]={0x00, 0x50,22.555656}; unsigned int x = 0x0050; WDTCTL = WDTPW | WDTHOLD; // Stop the watchdog timer PM5CTL0 = ENABLE_PINS; // Unlock pins UCB1CTLW0 |= UCSWRST; UCB1CTLW0 |= UCSSEL_3; UCB1BRW = 80; // Set clock divider for desired SCL frequency UCB1CTLW0 |= UCMODE_3; UCB1CTLW0 |= UCMST | UCSYNC; UCB1CTLW0 |= UCTR; UCB1I2CSA = (0xA0 >> 1); // Configure I2C pins P8SEL1 &= ~(BIT5 | BIT6); P8SEL0 |= (BIT5 | BIT6); PM5CTL0 &= ~LOCKLPM5; UCB1CTLW0 &= ~UCSWRST; //UCB1IFG = 0; // UCB1IE |= UCTXIE0; __enable_interrupt(); mem_write(x, arr, 15); __delay_cycles(200000); mem_read(0x0050, &data_in, 3); while(1); return 0; } // I2C ISR #pragma vector = EUSCI_B1_VECTOR __interrupt void EUSCI_B1_I2C_ISR(void) { static uint8_t mode = 0; switch (__even_in_range(UCB1IV, USCI_I2C_UCBIT9IFG)) // Corrected to use UCB1IV { case USCI_I2C_UCNACKIFG: // NACK interrupt break; case USCI_I2C_UCALIFG: // Arbitration Lost interrupt break; case USCI_I2C_UCSTTIFG: // Start bit interrupt break; case USCI_I2C_UCSTPIFG: // Stop bit interrupt break; case USCI_I2C_UCRXIFG0: // Receive buffer full interrupt data_in[bytes_received++] = UCB1RXBUF; // Read received data rx_byte_cnt--; if(rx_byte_cnt==1) { // UCB1CTLW0 |= UCTXNACK; UCB1CTLW0 |= UCTXSTP; mode = 0; g_buf = 0; // break point for read } break; case USCI_I2C_UCTXIFG0: if(tx_byte_cnt /*&& (bytes_transmitted>1)*/) { UCB1TXBUF = *g_buf; g_buf++; tx_byte_cnt--; if(tx_byte_cnt==0) { UCB1CTLW0 |= UCTXSTP; //addr=0; g_buf = 0; // break point to see write } } if(rx_byte_cnt) { if(mode == 0 || mode == 1) { UCB1TXBUF = *g_buf; g_buf++; mode++; } else { // End of EEPROM address transmission, initiate read operation bytes_received = 0; UCB1CTLW0 &= ~UCTR; // Switch to RX mode UCB1IE &= ~UCTXIE0; UCB1IE |= UCRXIE0; UCB1CTLW0 |= UCTXSTT; // Initiate start condition for read operation } } break; default: break; } }