最近尝试使用ti的ccs的driver lib 库函数来写代码,
#define SLAVE_ADDRESS 0x48
uint8_t transmitData;
void main(void)
{
//Stop WDT
WDT_A_hold(WDT_A_BASE);
//Assign I2C pins to USCI_B0
GPIO_setAsPeripheralModuleFunctionInputPin(
GPIO_PORT_P3,
GPIO_PIN1 + GPIO_PIN2
);
//Initialize transmit data packet
transmitData = 0x01;
//Initialize Master
USCI_B_I2C_initMasterParam param = {0};
param.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK;
param.i2cClk = UCS_getSMCLK();
param.dataRate = USCI_B_I2C_SET_DATA_RATE_400KBPS;
USCI_B_I2C_initMaster(USCI_B0_BASE, ¶m);
//Specify slave address
USCI_B_I2C_setSlaveAddress(USCI_B0_BASE,
SLAVE_ADDRESS
);
//Set in transmit mode
USCI_B_I2C_setMode(USCI_B0_BASE,
USCI_B_I2C_TRANSMIT_MODE
);
//Enable I2C Module to start operations
USCI_B_I2C_enable(USCI_B0_BASE);
//Enable TX interrupt
USCI_B_I2C_clearInterrupt(USCI_B0_BASE,
USCI_B_I2C_TRANSMIT_INTERRUPT
);
USCI_B_I2C_enableInterrupt(USCI_B0_BASE,
USCI_B_I2C_TRANSMIT_INTERRUPT
);
while(1)
{
//Send single byte data.
USCI_B_I2C_masterSendSingleByte(USCI_B0_BASE,
transmitData
);
//Delay until transmission completes
while(USCI_B_I2C_isBusBusy(USCI_B0_BASE))
{
;
}
//Delay between each transaction
__delay_cycles(50);
//Increment transmit data counter
transmitData++;
}
}
//******************************************************************************
//
//This is the USCI_B0 interrupt vector service routine.
//
//******************************************************************************
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_B0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(USCI_B0_VECTOR)))
#endif
void USCI_B0_ISR(void)
{
switch(__even_in_range(UCB0IV,12))
{
//Vector 12: Transmit buffer empty – TXIF
case USCI_I2C_UCTXIFG:
{
__no_operation();
break;
}
default: break;
}
}
这是官方的代码,刚开始学习,我想知道iic当我发送给从设备地址+读命令,然后在发送一个读的寄存器地址,我怎么来接收从设备给我的数据呢,代码怎么操作呢,我看了官方的参考,都是单独发或者单独收,我怎么才能才发完读取命令后,切换到接收来接收数据呢?
不知道有没有实际用库函数操作从设备获取设备数据的参考,我想借鉴一下。
thanks a lot!
zhanfeng dong:
回复 灰小子:
thanks,
iic 协议,我理解,刚开始理解差了,想多了,只要把把接收中断打开,硬件iic就会处理了,我只要把读出buf就没问题了,唉,看来是多想了。