完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我把StellarisWareexamplesperipheralsi2c中的第一个例子移植到StellarisWareboardsek-lm3s9b92hello工程中,用I2C来访问另外一颗芯片的ID寄存器(slave地址0X68,ID寄存器地址0X02,此寄存器的值是芯片ID,是固定的),以下是main函数
int main(void) [ // // Set the clocking to run directly from the crystal. // ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Initialize the UART. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioInit(0); // // Hello! // UARTprintf(" 33[2JHello World!n"); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);// The I2C0 peripheral must be enabled before use. ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);// GPIO port B needs to be enabled so these pins can be used. GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // Configure the pin muxing for I2C0 functions on port B2 and B3. // This step is not necessary if your part does not support pin muxing. ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. ROM_I2CMasterEnable(I2C0_MASTER_BASE); ROM_I2CMasterInitExpClk(I2C0_MASTER_BASE, ROM_SysCtlClockGet(), false);//the data rate is set to 100kbps // 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. For this example we will use a data rate of 100kbps. ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x68, false);//I2C Master is initiating a writes to the slave // Tell the master module what address it will place on the bus when // communicating with the slave. Set the address to SLAVE_ADDRESS // (as set in the slave module). The receive parameter is set to false // which indicates the I2C Master is initiating a writes to the slave. If // true, that would indicate that the I2C Master is initiating reads from // the slave. ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x02); // Place the data to be sent in the data register ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); // Initiate send of data from the master. while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_RREQ))// Wait until the slave has received and acknowledged the data. [ ] ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x68, true);//I2C Master is initiating reads from the slave. // Modifiy the data direction to true, so that seeing the address will // indicate that the I2C Master is initiating a read from the slave. ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ)) [ ] // Dummy acknowledge and wait for the receive request from the master. // This is done to clear any flags that should not be set. UARTprintf("Receive:%dn",ROM_I2CMasterDataGet(I2C0_MASTER_BASE)); // // Finished. // while(1) [ ] ] 编译烧录后只有串口打印“[2JHello World!” 如果注释掉 while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_RREQ))// Wait until the slave has received and acknowledged the data. [ ]和 while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ)) [ ] 串口打印 [2JHello World! Receive:0 麻烦各位高手帮我分析,谢谢! |
|
相关推荐
4 个讨论
|
|
zcy615 发表于 2019-8-30 10:39 你好, 看起来楼主的I2C通信是没有问题了,我建议先检查一下被读取芯片的datasheet,确认读芯片ID的时序是否正确,其次可以用单步看一下ROM_I2CMasterDataGet(I2C0_MASTER_BASE)返回值是什么,最好是新建一个变量接收这个返回值。还有就是上拉电阻是否接好。在确认读取芯片ID时序没有错误的情况下,建议楼主也可以直接用示波器抓波形进行分析。 |
|
|
|
|
|
只有小组成员才能发言,加入小组>>
NA555DR VCC最低电压需要在5V供电,为什么用3.3V供电搭了个单稳态触发器也使用正常?
661 浏览 3 评论
MSP430F249TPMR出现高温存储后失效了的情况,怎么解决?
587 浏览 1 评论
对于多级放大电路板,在PCB布局中,电源摆放的位置应该注意什么?
1041 浏览 1 评论
720 浏览 0 评论
普中科技F28335开发板每次上电复位后数码管都会显示,如何熄灭它?
516 浏览 1 评论
请问下tpa3220实际测试引脚功能和官方资料不符,哪位大佬可以帮忙解答下
153浏览 20评论
请教下关于TAS5825PEVM评估模块原理图中不太明白的地方,寻求答疑
119浏览 14评论
在使用3254进行录音的时候出现一个奇怪的现象,右声道有吱吱声,请教一下,是否是什么寄存器设置存在问题?
122浏览 13评论
TLV320芯片内部自带数字滤波功能,请问linein进来的模拟信号是否是先经过ADC的超采样?
120浏览 12评论
TPA6304-Q1: TPA6304 两片公用一组I2C的话,其中一片配置不成功怎么办
163浏览 10评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-20 04:53 , Processed in 0.879273 second(s), Total 68, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号