完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好!
我想用我的PSoC 4S先锋套件读出I2C芯片的ID号(TCS32 72)。所以我还是不太熟悉I2C总线和PSoC控制器。希望,有人能帮我还是能和我分享一个例子? 我所做的是: 1。将值0x1b写入寄存器使能 2。读出芯片的ID号 但我得到的不是ID号(见图像) 对于我的测试,我使用了这个代码: 包括“项目H” 定义I2CySavaEy ADDR(0x29)//TCS32 72地址 定义ID的RealStestDeVice ID(0x12)/RealStestAdvor 定义可注册寄存器的RealSab启用(0x00)/RealStCyt地址 定义PosiaAeNeNeWayAiEN(0x1b);/ /启用寄存器的值 定义缓冲区大小(2) 定义传递子CMPLT(0x00) 定义传递错误(0xFF) *标记标记的开始和结束*/ 定义PACETETHOSOP(0x01) 定义包PoEtEOP(0x17) /*分组位置*/ 定义PotoTeSopopyPOS(0) 定义包PayTeMCMDPOS(1) 定义PACETETSSTSIP-POS(PACETEY-CMDPY-POS) 定义包PayePopPoPOS(2) /*命令有效状态*/ 定义了StSyCMDYON(0x00) 定义了StsYCMDILIVER(0xFF) / ************* *宏指令 三、 *设置LED红色* *定义RGBBeLython OnRead 做{ LeDyRead写作(0); Ledi-GrimyLoad编写(1); }(0) *设置LED绿色* *定义RGBByDeloON-YONGRONE 做{ LeDyRead写作(1); Ledi-GrimyLoad编写(0); }(0) *设置LED关闭* 定义rgbyLeDyOff\ 做{ LeDyRead写作(1); Ledi-GrimyLoad编写(1); }(0) 三、 UIT32写入程序包() { UIT8缓冲区〔2〕; UIT32 32状态=0xFF;//TrimeLoistError=0xFF *用分组**初始化缓冲区/ 缓冲器〔0〕=可注册; 缓冲器〔1〕=PangyaNeNeWangayaEn; (空隙)I2CMYI2CMASTREST BUF(I2CySLaveEx ADDR,缓冲器,Buffelx大小,I2CMYI2CYMODEA完成EXEXEXXFER); /*等待直到主机完成写传输** 当(02= =(I2CMYI2CMASTSTATESUSER)和;I2CMYI2CYMSTATEA WRES-CMPLT)时 { } /*显示传输状态*/ 如果(0U= =(I2CMII2CYMSTATEYRIXXFER和AMP;I2CMYI2CMASTSTATUS())) { *检查所有字节是否写入*/ IF(I2CMII2CMASTReGETWEB BUFSIZE()= = Buffelx大小) { 绿色; 状态=Trimest-CMPLT; } } 其他的 { 红色; } (空)i2CMyi2cMistCaleStasUs(); 返回(状态); } UIT32读取状态包(无效) { UIT8缓冲区〔2〕; 缓冲器〔0〕=0x12;//RealStestDeVice ID; 缓冲器〔1〕=0x00;//虚拟字节 UIT32 32状态=0xFF;//TrimeLoistError=0xFF (空)I2CMYI2CMASTRADEBUF(I2CySavaEx ADDR,缓冲器,缓冲区大小,I2CMYI2CYMODEA完成EXEXEXXFER); /*等待直到主完成读转移*/ 当(02= =(I2CMII2CMASTSTATESUSER)和;I2CMYI2CYMSTATRO-RDY-CMPLT)时 { } /*显示传输状态*/ 如果(0U= =(I2CMII2CYMSTATEYRIXXFER和AMP;I2CMYI2CMASTSTATUS())) { *检查数据包结构*/ IF(I2CMJI2CMASTReTeRead Buffsisie()= = Buffelx大小) { *检查数据包状态* 如果(STSyCMDYONDO= =缓冲器[PACETESTSTSIPPOS)] { 绿色; 状态=Trimest-CMPLT; } } } 其他的 { 红色; 状态=传递错误; } (空)i2CMyi2cMistCaleStasUs(); 返回(状态); } 主(空) { I2CM*START(); CyGloalActuabess;/*启用全局中断。*/ (;) { *将数据包发送给从设备*/ IF(RealEcMoMdPoTeTe())=0) { *从从属*读取响应包 如果(RealStaseCubTo())=0) 绿色; 其他的 红色; } CyDelay(5);/* Delay之间的命令*/ } } 2017~11-02Y12H37 31.2 K 以上来自于百度翻译 以下为原文 Hi! I want to read out the ID number of an I2C chip (TCS3472) with my PSoC 4S Pioneer Kit. So i'm still not really familiar with I2C bus and PSoC Controller. Hope, somebode can help or can share an example with me? What i do is: 1. Write the value 0x1B to Register ENABLE 2. read out the ID number of the chip But what i got is not the ID number (see image) For my test, i used this code: #include "project.h" #define I2C_SLAVE_ADDR (0x29) // TCS3472 Address #define RegisterDeviceID (0x12) // Registeraddress of ID #define RegisterENABLE (0x00) // Registeraddress of Enable Register #define PON_AEN_WEN_AIEN (0x1B); // Value of Enable Register #define BUFFER_SIZE (2) #define TRANSFER_CMPLT (0x00) #define TRANSFER_ERROR (0xFF) /* Start and end of packet markers */ #define PACKET_SOP (0x01) #define PACKET_EOP (0x17) /* Packet positions */ #define PACKET_SOP_POS (0) #define PACKET_CMD_POS (1) #define PACKET_STS_POS (PACKET_CMD_POS) #define PACKET_EOP_POS (2) /* Command valid status */ #define STS_CMD_DONE (0x00) #define STS_CMD_FAIL (0xFF) /*************************************** * Macros ****************************************/ /* Set LED RED color */ #define RGB_LED_ON_RED do{ LED_RED_Write (0); LED_GREEN_Write(1); }while(0) /* Set LED GREEN color */ #define RGB_LED_ON_GREEN do{ LED_RED_Write (1); LED_GREEN_Write(0); }while(0) /* Set LED TURN OFF */ #define RGB_LED_OFF do{ LED_RED_Write (1); LED_GREEN_Write(1); }while(0) /****************************************/ uint32 WriteCommandPacket() { uint8 buffer[2]; uint32 status = 0xFF; // TRANSFER_ERROR = 0xFF /* Initialize buffer with packet */ buffer[0] = RegisterENABLE; buffer[1] = PON_AEN_WEN_AIEN; (void) I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR, buffer, BUFFER_SIZE, I2CM_I2C_MODE_COMPLETE_XFER); /* Waits until master completes write transfer */ while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT)) { } /* Displays transfer status */ if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus())) { /* Check if all bytes was written */ if (I2CM_I2CMasterGetWriteBufSize() == BUFFER_SIZE) { RGB_LED_ON_GREEN; status = TRANSFER_CMPLT; } } else { RGB_LED_ON_RED; } (void) I2CM_I2CMasterClearStatus(); return (status); } uint32 ReadStatusPacket(void) { uint8 buffer[2]; buffer[0]= 0x12; // RegisterDeviceID; buffer[1]= 0x00; // dummy byte uint32 status = 0xFF; // TRANSFER_ERROR = 0xFF (void) I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR, buffer, BUFFER_SIZE, I2CM_I2C_MODE_COMPLETE_XFER); /* Waits until master complete read transfer */ while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT)) { } /* Displays transfer status */ if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus())) { /* Check packet structure */ if (I2CM_I2CMasterGetReadBufSize() == BUFFER_SIZE) { /* Check packet status */ if (STS_CMD_DONE == buffer[PACKET_STS_POS]) { RGB_LED_ON_GREEN; status = TRANSFER_CMPLT; } } } else { RGB_LED_ON_RED; status = TRANSFER_ERROR; } (void) I2CM_I2CMasterClearStatus(); return (status); } int main(void) { I2CM_Start(); CyGlobalIntEnable; /* Enable global interrupts. */ for(;;) { /* Send packet to the slave */ if (WriteCommandPacket() == 0) { /* Read response packet from the slave */ if (ReadStatusPacket() == 0) RGB_LED_ON_GREEN; else RGB_LED_ON_RED; } CyDelay(5); /* Delay between commands */ } }
|
|
相关推荐
6个回答
|
|
如果要从指定地址(0x12)读取寄存器字节,必须按如下方式访问该设备。
1)将寄存器地址0x12作为命令写入设备。 2)从设备读取一个字节。 在步骤1)不需要数据字节。 之所以接收0x1b作为读取数据的原因是因为设备的内部地址指针仍然指向启用寄存器。如果使用命令重写地址指针,则将获得ID字段。 当做, 诺里基 以上来自于百度翻译 以下为原文 If you want to read a register byte from a specified address (0x12) you must access the device as follows. 1) Write the register address 0x12 to the device as a command. 2) Read one byte from the device. In the step 1) no data byte is required. The reason why you receive 0x1B as the read data is because the device's internal address pointer still points the ENABLE register. If you override the address pointer with the command, you will get the ID field. Regards, Noriaki |
|
|
|
numuwewr 发表于 2018-12-11 15:22 你好,Noriaki, 谢谢你的提示。我试着看书,但没用。 你能张贴一个小代码的例子来读出一个寄存器吗? 多谢 以上来自于百度翻译 以下为原文 Hello Noriaki, thanks for your hints. I tried to read out but it didn't work. Please can you post a little code example for reading out a register? Many thanks |
|
|
|
请告诉我你做了什么。 我不能提供一个工作示例,因为我没有目标设备。 当做, 诺里基 以上来自于百度翻译 以下为原文 Please let me know what did you do. I cannot provide a working example because I do not have a target device. Regards, Noriaki |
|
|
|
你好,Noriaki, 首先,我将值0x1b写入寄存器0x00中的寄存器使能: 定义I2CySavaEy ADDR(0x29)//TCS32 72地址 UIT8写入缓冲区〔2〕; 写入缓冲器〔0〕=0x00;/ /寄存器启用 写缓冲区[1 ]=0x1b;//值设置寄存器 (空)I2CMYI2CMASTRESWREST BUF(I2CYSLaveEXADDR,WrreBuffer-sisiOf(Read Effice),I2CMYI2CYMODEAXEXEXEXXFER); 在我的分析器上,我得到了:St*0x29 WR.AKAC0x00 所以这应该是好的! 然后我想从地址0x12读出芯片的ID号。 在这里,你写道,我必须首先发送命令0x12。 我试过这个: UIT8设备; UIT8读缓冲器〔1〕; 读缓冲区〔0〕=0x12;/ / ID号地址 (空)I2CMYI2CMASTRADEBUF(I2CYSLAVEYADDR,Read Buffic,SIZEOF(Read Buffic),I2CMYI2CYMODEAXEXEXEXXFER); /*等待直到主机完成读传输*/ 当(02= =(I2CMII2CMASTSTATESUSER)和;I2CMYI2CYMSTATRO-RDY-CMPLT)时 { } (空)i2CMyi2cMistCurrStaseUs();/ /清除I2C主状态 Deice ID=读缓冲器〔0〕; 在我的理解中,如果我使用这个函数,主启动一个读条件,写命令0x12+RD。 但是我在分析仪上得到的是:开始x0x29 rAk 0x1bnak停止。 在使用读函数之前,我还尝试编写命令0x12,但它不起作用。 所以我现在困惑了! 以上来自于百度翻译 以下为原文 Hello Noriaki, first, i write the value 0x1B into the Register Enable on Adress 0x00: #define I2C_SLAVE_ADDR (0x29) // TCS3472 Address uint8 writeBuffer[2]; writeBuffer[0] = 0x00; // Adress of Register Enable writeBuffer[1] = 0x1B; // Value to set the Register (void) I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR, writeBuffer, sizeof(writeBuffer), I2CM_I2C_MODE_COMPLETE_XFER); On my Analyzer i got: Start | 0x29WR | ACK | 0x00 | ACK | 0x1B | ACK | STOP So this should be ok! Then i want to read out the ID-Number of the chip from Address 0x12 Here you wrote, that i must first send the command 0x12. i tried this: uint8 deviceID; uint8 readBuffer[1]; readBuffer[0] = 0x12; // Address of ID number (void) I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR, readBuffer, sizeof(readBuffer), I2CM_I2C_MODE_COMPLETE_XFER); /* Waits until master completes read transfer */ while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT)) { } (void) I2CM_I2CMasterClearStatus(); //Clear I2C master status deviceID = readBuffer[0]; In my understanding, if i use this function, the Master initiate a read condition which write the command 0x12 + RD. But what i got on my Analyzer is: START | 0x29RD | ACK | 0x1B | NAK | STOP | I also tried to write the command 0x12 before i used the Read function, but it doesnt work. So im confused now! |
|
|
|
谢谢你的回复。 写操作需要两个I2C访问。 1)将寄存器地址0x12作为命令写入设备。 I2CMTX缓冲器〔0〕=TCS34 72ReGID; 状态=I2CMY2I2CMASTRESWORKE BUF(TCS34 72Advess,I2CMTXBuffic,1,I2CMYI2CYMODEAXEXEXEXXFER); /等待转移完成 而(!)(I2CMYI2CMASTSTATUS)(和;I2CMY2I2CYMSTATEA WRES-CMPLT); 2)从设备读取一个字节。 状态=I2CMY2I2CMASTRADEBUF(TCS34 72地址,I2CMRX缓冲器,1,I2CMYI2CYMODEA完成EXEXEXXFER); /等待转移完成 而(!)(I2CMYI2CMASTSTATUS)(和;I2CMY2I2CYMSTATUR-RDY-CMPLT); 请添加一个I2C写来指定寄存器地址。 我已经创建了一个示例项目使用CY8CKIT-044“PSoC 4 M系列先锋工具包”作为附件。 TCS32 72的接口协议类似于EZI2C组件。因此,我把一个I2CM组件作为主机和一个EZI2C组件作为一个PSoC 4M设备上的目标。连接每个组件的SDA和SCL引脚,并放置两个上拉电阻器。 当做, 诺里基 P4MII2CM.Cyrj.CaseVo.01.Zip 591.4 K 以上来自于百度翻译 以下为原文 Thank you for your reply. The WRITE operation requires two I2C accesses. 1) Write the register address 0x12 to the device as a command. i2cmTxBuffer[0] = tcs3472RegId; status = I2CM_I2CMasterWriteBuf(tcs3472Address, i2cmTxBuffer, 1, I2CM_I2C_MODE_COMPLETE_XFER); // Wait for transfer completion while (!(I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT)) ; 2) Read one byte from the device. status = I2CM_I2CMasterReadBuf(tcs3472Address, i2cmRxBuffer, 1, I2CM_I2C_MODE_COMPLETE_XFER); // Wait for transfer completion while (!(I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT)) ; Please add an I2C-WRITE to specify the register address. I have created an example project using CY8CKIT-044 "PSoC 4 M-Series Pioneer Kit" as attached. The interface protocol of the TCS3472 is similar to the EZI2C component. So, I put an I2CM component as a host and an EZI2C component as a target on one PSoC 4M device. Connect SDA and SCL pins of each components and put two pull-up resistors. Regards, Noriaki
|
|
|
|
字节I2C接口非常简单:在建立组件后,开始使用它 I2CyMasksEngESTEnter(DeVice地址,I2CX Read EXXFIX模式);/初始化事务处理 I2CJMistWrreEngEnter(寄存器);/ /指示要写入哪个寄存器 I2C.MistWrrestEngEnter字节(值);//写入寄存器 I/CyMasksEdStestStor(;)/ /事务结束 当您想从使用的设备读取时(读取两个字节的示例) I2CyMasksEngESTEnter(DeVice地址,I2CX Read EXXFIX模式);/初始化事务处理 I2C.MistMraveScript(寄存器);/ /指示要写入哪个寄存器 I2C.MaskestEntRead(DeVice地址,I2CX Read x Felx模式); I2C*MaultRead字节(I2CYACKYDATA);/从寄存器读取 I2C*MaultRead字节(I2CnNaKi数据);/ /从寄存器读取,最后字节是裸的 I/CyMasksEdStestStor(;)/ /事务结束 不太难。请记住,大多数API(除了读取字节之外)都返回一个状态字节,当非零指示错误条件时。 高级API必须以这种方式使用: 从从属字节计数写入 I2CJMistWrrestBuf(SaveAdvor,DATAPTR,CUT,I2CYMODEJORIGEXEXXFER); 从从属发送寄存器/命令字节读取: I2CJMistWrrestBuf(SLaveAdvor,RealAdvices,1,I2CyMoDEi NoY-STOP); I2C*MaultRead Bufff(SaveAdvor,DATAPTR,计数,I2CYMODEA重复启动); 鲍勃 以上来自于百度翻译 以下为原文 Byte I2C interface is quite simple: After setting up the component and starting it you use I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE); // Initialize a transaction for writing I2C_MasterWriteByte(Register); // Indicate which register you want to write to I2C_MasterWriteByte(Value); // Write to register I2C_MasterSendStop(); // End of transaction When you want to read from a device you use (example for reading two bytes I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE); // Initialize a transaction for writing I2C_MasterWrite(Register); // Indicate which register you want to write to I2C_MasterSendRestart(DeviceAddress,I2C_READ_XFER_MODE); I2C_MasterReadByte(I2C_ACK_DATA); // Read from register I2C_MasterReadByte(I2C_NAK_DATA); // Read from register, last byte is NAKed I2C_MasterSendStop(); // End of transaction Not too difficult. Keep in mind that most of the APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition. The high-level APIs must be used in this way: Writing to slave Count bytes I2C_MasterWriteBuf(SlaveAddress,DataPtr,Count,I2C_MODE_COMPLETE_XFER); Reading from Slave sending register/command byte first: I2C_MasterWriteBuf(SlaveAddress,&RegAddress,1,I2C_MODE_NO_STOP); I2C_MasterReadBuf(SlaveAddress,DataPtr,Count,I2C_MODE_REPEAT_START); Bob |
|
|
|
只有小组成员才能发言,加入小组>>
752个成员聚集在这个小组
加入小组2069 浏览 1 评论
1824 浏览 1 评论
3633 浏览 1 评论
请问可以直接使用来自FX2LP固件的端点向主机FIFO写入数据吗?
1760 浏览 6 评论
1509 浏览 1 评论
CY8C4025LQI在程序中调用函数,通过示波器观察SCL引脚波形,无法将pin0.4(SCL)下拉是什么原因导致?
507浏览 2评论
CYUSB3065焊接到USB3.0 TYPE-B口的焊接触点就无法使用是什么原因导致的?
357浏览 2评论
CX3连接Camera修改分辨率之后,播放器无法播出camera的画面怎么解决?
410浏览 2评论
357浏览 2评论
使用stm32+cyw43438 wifi驱动whd,WHD驱动固件加载失败的原因?
854浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 01:03 , Processed in 0.834060 second(s), Total 86, Slave 69 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号