工具与软件:
您好、我正在向 EEPROM 写入4字节(AT24C256)、但无法写入一个以上的字节。 一个字节可以写入读取、但多个字节无法写入。 请参考以下代码。请同时回复
void InitI2C (unsigned char EEPROM_i2c_address)
{
UCB1CTLW0 = UCSWRST;//启用软件复位
UCB1CTLW0 |= UCMODE_3 | UCMST | UCSSEL__SMCLK | UCSYNC;// I2C 主模式、SMCLK
UCB1BRW = 160;// fSCL = SMCLK/160 =~100kHz
UCB1I2CSA = SLAVE_ADDR;//从地址
UCB1CTLW0 &=~μ s UCSWRST;//清除软件复位、恢复操作
UCB1IE |= UCNACKIE
}
I2C_Mode I2C_ADC Master_Read (unsigned char dev_addr、unsigned char reg_addr、unsigned char 计数)
{
/*初始化状态机*/
MasterMode = TX_REG_ADDRESS_MODE;
// TransmitRegAddr = reg_addr;
//P4OUT |=(BIT3);
P3OUT |=(BIT0);
TransmitRegAddr_1 =(reg_addr >> 8);//MSB
TransmitRegAddr_2 =(reg_addr & 0xFF);// LSB
RXByteCtr = count;
TXByteCtr = 0;
ReceiveIndex = 0;
TransmitIndex = 0;
/*初始化从机地址和中断*/
UCB1I2CSA = dev_addr;
UCB1IFG 并且=~(UCTXIFG + UCRXIFG);//清除任何挂起的中断
UCB1IE &&~μ P UCRXIE;//禁用 RX 中断
UCB1IE |= UCTXIE;//启用 TX 中断
UCB1CTLW0 |= UCTR + UCTXSTT;// I2C TX、启动条件
___ enable_interrupt ();
__bis_SR_register (LPM0_bits + GIE);//随着中断进入 LPM0
返回 MasterMode;
}
void CopyArray (unsigned char *来源、unsigned char *目标、unsigned char 计数)
{
unsigned char copyIndex = 0;
对于(copyIndex = 0;copyIndex < count;copyIndex++)
{
dest[copyIndex]= source[copyIndex];
}
}
I2C_Mode I2C_ADC Master_Write (unsigned char dev_addr、unsigned int reg_addr、unsigned char * reg_data、unsigned char 计数)
//I2C_Mode I2C_LaunchPad Master_Write (uint8_t dev_addr、uint8_t reg_addr、uint8_t * reg_data、uint8_t count)
{
/*初始化状态机*/
MasterMode = TX_REG_ADDRESS_MODE;
// TransmitRegAddr = reg_addr;
// P4OUT &=~(BIT3);
P3OUT &=~Ω(BIT0);
TransmitRegAddr_1 =(reg_addr >> 8);//MSB
TransmitRegAddr_2 =(reg_addr & 0xFF);// LSB
//将寄存器数据复制到 TransmitBuffer
// unsigned char dt =88;
// putstr("\nstr");
CopyArray (reg_data、TransmitBuffer、count);
TXByteCtr = count;
RXByteCtr = 0;
ReceiveIndex = 0;
TransmitIndex = 0;
/*初始化从机地址和中断*/
UCB1I2CSA = dev_addr;
UCB1IFG 并且=~(UCTXIFG + UCRXIFG);//清除任何挂起的中断
UCB1IE &&~μ P UCRXIE;//禁用 RX 中断
UCB1IE |= UCTXIE;//启用 TX 中断
UCB1CTLW0 |= UCTR + UCTXSTT;// I2C TX、启动条件
___ enable_interrupt ();
__bis_SR_register (LPM0_bits + GIE);//随着中断进入 LPM0
返回 MasterMode;
}
#if defined (__TI_Compiler_version__)|| defined (__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_B1_VECTOR
_interrupt void USCI_B1_ISR (void)
#elif defined (_GNUC__)
void __attribute__(((interrupt (USCI_B0_vector))) USCI_B1_ISR (void)
#else
错误编译器不受支持!
#endif
必须从 UCB2RXBUF 读取
{
//putch ('B');
unsigned char rx_val = 0;
开关(__EVEN_IN_RANGE (UCB1IV、USCI_I2C_UCBIT9IFG))
{
case USCI_NONE:break;//向量0:无中断
case USCI_I2C_UCALIFG:break;// Vector 2:ALIFG
case USCI_I2C_UCNACKIFG:// Vector 4:NACKIFG
休息;
case USCI_I2C_UCSTTIFG:break;// Vector 6:STTIFG
case USCI_I2C_UCSTPIFG:break;// Vector 8:STPIFG
case USCI_I2C_UCRXIFG3:break;// Vector 10:RXIFG3
case USCI_I2C_UCTXIFG3:break;// Vector 12:TXIFG3
case USCI_I2C_UCRXIFG2:break;// Vector 14:RXIFG2
case USCI_I2C_UCTXIFG2:break;// Vector 16:TXIFG2
case USCI_I2C_UCRXIFG1:break;// Vector 18:RXIFG1
case USCI_I2C_UCTXIFG1:break;// Vector 20:TXIFG1
实例 USCI_I2C_UCRXIFG0://矢量22:RXIFG0
Rx_val = UCB1RXBUF;
IF (RXByteCtr)
{
ReceiveBuffer[ReceiveIndex++]= Rx_val;
;
RXByteCtr --;
}
if (RXByteCtr =1)
{
UCB1CTLW0 |= UCTXSTP;
}
否则为(RXByteCtr =0)
{
UCB1IE &=~UCRXIE;
MasterMode = IDLE_MODE;
_BIC_SR_REGISTER_ON_EXIT (CPUOFF);//退出 LPM0
}
休息;
case USCI_I2C_UCTXIFG0:// Vector 24:TXIFG0
开关(MasterMode)
{
实例 TX_REG_ADDRESS_MODE:
如果(UCB1I2CSA = 0x50)
{
UCB1TXBUF = TransmitRegAddr_1;//0x32 MSB
//UCB1TXBUF = TransmitRegAddr;
MasterMode = TX_LSB_MODE;
}
设计
{
UCB1TXBUF = TransmitRegAddr;
IF (RXByteCtr)
MasterMode = SWITCH_TO_RX_MODE;//现在需要开始接收
设计
MasterMode = TX_DATA_MODE;//继续传输发送缓冲器中的数据
}
休息;
案例 TX_LSB_MODE:
UCB1TXBUF = TransmitRegAddr_2;//LSB
//UCB1TXBUF = TransmitRegAddr_1;//LSB
IF (RXByteCtr)
MasterMode = SWITCH_TO_RX_MODE;//现在需要开始接收
设计
MasterMode = TX_DATA_MODE;//继续传输发送缓冲器中的数据
休息;
案例 SWITCH_TO_RX_MODE:
UCB1IE |= UCRXIE;//启用 RX 中断
UCB1IE &&~μ P UCTXIE;//禁用 TX 中断
UCB1CTLW0并且=~Ω UCTR;//切换到接收器
MasterMode = RX_DATA_MODE;//状态状态是接收数据
UCB1CTLW0 |= UCTXSTT;//发送重复启动
if (RXByteCtr =1)
{
因为这是 N-1字节,所以必须发送 STOP
while ((UCB1CTLW0和 UCTXSTT));
UCB1CTLW0 |= UCTXSTP;//发送停止条件
}
休息;
实例 TX_DATA_MODE:
IF (TXByteCtr)
{
UCB1TXBUF = TransmitBuffer[TransmitIndex+];// DATA=9
// UCB2CTLW0 |= UCTXSTP;
TXByteCtr --;
}
设计
{
//完成传输
UCB1CTLW0 |= UCTXSTP;//发送停止条件
MasterMode = IDLE_MODE;
UCB1IE &&~μ P UCTXIE;//禁用 TX 中断
_BIC_SR_REGISTER_ON_EXIT (CPUOFF);//退出 LPM0
}
休息;
默认值:
___ no_operation();
休息;
}
休息;
默认值:中断;
}
}