TI中文支持网
TI专业的中文技术问题搜集分享网站

TM4c123x I2C配置信息解析

I2C的初始化

//initialize I2C module 0
//Slightly modified version of TI's example code
void InitI2C0(void)
{//enable I2C module 0SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);//reset moduleSysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);//enable GPIO peripheral that contains I2C 0SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);// Configure the pin muxing for I2C0 functions on port B2 and B3.GPIOPinConfigure(GPIO_PB2_I2C0SCL);GPIOPinConfigure(GPIO_PB3_I2C0SDA);// Select the I2C function for these pins.GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);// Enable and initialize the I2C0 master module.  Use the system clock for// the I2C0 module.  The last parameter sets the I2C data transfer rate.// If false the data rate is set to 100kbps and if true the data rate will// be set to 400kbps.I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);//clear I2C FIFOsHWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000;
}

I2c发送数据

//sends an I2C command to the specified slave
void I2CSend(uint8_t slave_addr, uint8_t num_of_args, ...)
{// Tell the master module what address it will place on the bus when// communicating with the slave.I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);//stores list of variable number of argumentsva_list vargs;//specifies the va_list to "open" and the last fixed argument//so vargs knows where to start lookingva_start(vargs, num_of_args);//put data to be sent into FIFOI2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint32_t));//if there is only one argument, we only need to use the//single send I2C functionif(num_of_args == 1){//Initiate send of data from the MCUI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));//"close" variable argument listva_end(vargs);}//otherwise, we start transmission of multiple bytes on the//I2C buselse{//Initiate send of data from the MCUI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));//send num_of_args-2 pieces of data, using the//BURST_SEND_CONT command of the I2C modulefor(uint8_t i = 1; i < (num_of_args - 1); i++){//put next piece of data into I2C FIFOI2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint32_t));//send next data that was just placed into FIFOI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));}//put last piece of data into I2C FIFOI2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint32_t));//send next data that was just placed into FIFOI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));//"close" variable args listva_end(vargs);}
}
xyz549040622:

I2C发送字符串数组

//sends an array of data via I2C to the specified slave
void I2CSendString(uint32_t slave_addr, char array[])
{// Tell the master module what address it will place on the bus when// communicating with the slave.I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);//put data to be sent into FIFOI2CMasterDataPut(I2C0_BASE, array[0]);//if there is only one argument, we only need to use the//single send I2C functionif(array[1] == '\0'){//Initiate send of data from the MCUI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));}//otherwise, we start transmission of multiple bytes on the//I2C buselse{//Initiate send of data from the MCUI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));//initialize index into arrayuint8_t i = 1;//send num_of_args-2 pieces of data, using the//BURST_SEND_CONT command of the I2C modulewhile(array[i + 1] != '\0'){//put next piece of data into I2C FIFOI2CMasterDataPut(I2C0_BASE, array[i++]);//send next data that was just placed into FIFOI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));}//put last piece of data into I2C FIFOI2CMasterDataPut(I2C0_BASE, array[i]);//send next data that was just placed into FIFOI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);// Wait until MCU is done transferring.while(I2CMasterBusy(I2C0_BASE));}
}

I2C接收数据

//read specified register on slave device
uint32_t I2CReceive(uint32_t slave_addr, uint8_t reg)
{//specify that we are writing (a register address) to the//slave deviceI2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);//specify register to be readI2CMasterDataPut(I2C0_BASE, reg);//send control byte and register address byte to slave deviceI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);//wait for MCU to finish transactionwhile(I2CMasterBusy(I2C0_BASE));//specify that we are going to read from slave deviceI2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, true);//send control byte and read from the register we//specifiedI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);//wait for MCU to finish transactionwhile(I2CMasterBusy(I2C0_BASE));//return data pulled from the specified registerreturn I2CMasterDataGet(I2C0_BASE);
}

赞(0)
未经允许不得转载:TI中文支持网 » TM4c123x I2C配置信息解析
分享到: 更多 (0)