完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好,专家,我遇到了使用PIC18F47 K42的I2C GPIO扩展器MC23017的问题。这是我的项目的最后一件事。我需要使用12位的12位ADC,所以我必须使用PIC18F K42设备。I2C是与旧PIC18的区别。我已经试过TB3191和TB3159了。但我只能读取温度传感器TC74。MC23017没有发生任何事情。我在SCL和SDA线路上有4K7上拉电阻。总线是好的,因为我可以从TC74获得实时值。但是,即使我已经尝试了MCC的I2C1功能,我的代码也在写/读MC23017上永远旋转。我有了新的MC23017来验证,并且它正在用我的PIC18F46K22的另一个板工作。有什么我仍然误入歧途吗?我压缩了我的代码并附加到这个线程上。请帮忙指点。谢谢你,谢谢你,BankKy。
以上来自于百度翻译 以下为原文 Hello Experts, I have the problem of using I2C GPIO expander MCP23017 with PIC18F47K42. This is the last thing that holding my project. I need to use 12bit ADC for 12 CH, so I have to use PIC18F K42 device. The I2C is the difference with the old PIC18. I tried the TB3191 and TB3159. already. But I just can read the Temp Sensor TC74. There is nothing happened with MCP23017. I have 4K7 pull-up resistors on SCL and SDA line. The BUS is good since I can get the real-time value from TC74. But my code is spinning forever on writing/reading MCP23017 even I have tried the I2C1 function by MCC. I have the new MCP23017 to verify, and it is working on another board with my old PIC18F46K22. Is there anything that I am still missing. I zipped my code and attached to this thread. Please help to advise. Thank you in advance. #include #include #include "mcc_generated_files/mcc.h" #include "LCD_16x4.h" #define MCP23017 0x20 //MCP23017A0 address (0x40/2) //(A0,A1,A2 pins are connected to GND) #define TC74 0x4F //TC74A7 (0x9E/2) address #define SetIO_A 0x00 #define SetIO_B 0x01 #define GPIOA 0x12 #define GPIOB 0x13 #define PORT_OUTOUT 0x00 #define PORT_INPUT 0xFF /* Main application */ void main(void) { // Initialize the device SYSTEM_Initialize(); char buf[16]; int temp=0; uint8_t fanSensor=0,error=0; __delay_ms(500); LcdInit(); LcdGoToXY(1,0); LCD_DisplayString("TEST I2C Commu"); i2c1_write2ByteRegister(MCP23017, SetIO_A, PORT_INPUT); fanSensor=i2c1_read1ByteRegister(MCP23017, GPIOA); while (1) { // Add your application code LED_Toggle(); __delay_ms(200); temp=i2c1_read1ByteRegister(TC74, 0); LcdGoToXY(0,1); LCD_DisplayString("Temp ="); sprintf(buf, " %02d", temp); LCD_DisplayString(buf); LCD_DisplayString(" C"); fanSensor = i2c1_read1ByteRegister(MCP23017, GPIOA); error=i2c1_getLastError(); if(error==I2C1_FAIL_TIMEOUT) { LcdGoToXY(0,2); LCD_DisplayString("MCP23017 TIMEOUT"); } else { LcdGoToXY(0,2); LCD_DisplayString("Comm is good"); } LcdGoToXY(0,3); LCD_DisplayString("Sensor = "); sprintf(buf, " %02d", fanSensor); LCD_DisplayString(buf); } } Thank you and Best Regard, BankKy Attached Image(s) Attachment(s) 47k42i2cSimple.zip (42.33 KB) - downloaded 50 times |
|
相关推荐
19个回答
|
|
看着你的面包板:我想你有一个MCP23017PDIP(不是MCP23 S17PDIP)。你没有连接Pin -13,由此我得出结论,你混淆了这两个芯片的图表。将SCL连接到引脚12和SDA到PIN 13,然后再试一次。有关详细信息,请参见MCP3017数据表。
以上来自于百度翻译 以下为原文 Looking at your bread-board: I think you have a MCP23017 PDIP (not the MCP23S17 PDIP). you did not connect Pin-13 Connect SCL to Pin 12 and SDA to pin 13 and try again. For details see the MCP23017 datasheet. |
|
|
|
例程I2C1A Read 1ByeReGistor将读取命令添加到设备地址吗?要读取,设备地址通常与0x01 IE FANSONTROR=I2C1YRead ByTeReGistor(MCP3017 17 0x01,GPIOA)进行处理;除非您没有显示的例程自动执行此操作。读和写例程也会改变发送时的设备地址吗?除此之外,看起来你的设置是可以的。
以上来自于百度翻译 以下为原文 Does the routine i2c1_read1ByteRegister add the READ command to the device address? To read, the device address is normally ORed with 0x01 i.e fanSensor=i2c1_read1ByteRegister(MCP23017 | 0x01, GPIOA); unless the routine, which you didn't show, does this automatically. Do the read and write routines also shift the device address left when sending? Other than that, looks like your setup is OK. |
|
|
|
除了RODIMS已经指出的不正确的引脚连接之外。
以上来自于百度翻译 以下为原文 Apart from the incorrect pin connections already noted by rodims. |
|
|
|
是的,MCC关心这个,ZIP包含代码。所以地址0x20是正确的,如果OP把地址位绑定到GND。
以上来自于百度翻译 以下为原文 Yes, MCC cares about that, the ZIP contains the code. So the address 0x20 is correct, if OP tied the address bits to GND. |
|
|
|
谢谢罗迪姆斯的每一个人的帮助。是的,我很抱歉,我做错事时,我把照片张贴。我的问题并不是那么简单,因为我已经删除了电路并不断地重新构建它。我已经修复了这条线,而且代码在I2C1Wrad(函数)中仍在继续旋转(见我的LCD图片)。直到我按住MCP23017的复位开关,代码才开始工作。我可以得到TC74的温度值25C和MC23017是不允许的。但是在我释放开关之后,温度值变成184C。这是代码中的一个错误。我也尝试从TB3159的简单代码,但是我的代码被击中& gt;& gt;而()!I2C1STAT1BIT.TXBE;你能帮我吗?谢谢你,班奇
以上来自于百度翻译 以下为原文 Thank you rodims every one for the help. Yes, I am sorry that I did it wrong when I took the picture to post. My problem is not that simple because I have removed the circuit and re-built it over and over. I have fixed the line and the code is still spinning forever in I2C1write(_) function (See my LCD pictures). Until I hold pressed the reset switch of the MCP23017 the code is working. I can get TC74 Temperature value 25C and MCP23017 is disable. but after I release the switch, the Temperature value is turn to 184C. It is something wrong in the code. #include #include #include "mcc_generated_files/mcc.h" #include "LCD_16x4.h" #define MCP23017 0x20 //MCP23017A0 address (0x40/2) //(A0,A1,A2 pins are connected to GND) #define TC74 0x4F //TC74A7 (0x9E/2) address #define SetIO_A 0x00 #define SetIO_B 0x01 #define GPIOA 0x12 #define GPIOB 0x13 #define PORT_OUTOUT 0x00 #define PORT_INPUT 0xFF /* Main application */ void main(void) { // Initialize the device SYSTEM_Initialize(); char buf[16]; int temp=0,displayCount=0; uint8_t fanSensor=0,error=0; __delay_ms(1000); LcdInit(); LcdGoToXY(1,0); LCD_DisplayString("TEST I2C"); i2c1_write2ByteRegister(MCP23017, SetIO_A, PORT_INPUT); // fanSensor=i2c1_read1ByteRegister(MCP23017, GPIOA); while (1) { // Add your application code LcdGoToXY(10,0); sprintf(buf, " %02d", displayCount); LCD_DisplayString(buf); displayCount++; if(displayCount>=99)displayCount=0; LED_Toggle(); __delay_ms(200); temp=i2c1_read1ByteRegister(TC74, 0); LcdGoToXY(0,1); LCD_DisplayString("Temp ="); sprintf(buf, " %03d", temp); LCD_DisplayString(buf); LCD_DisplayString(" C"); fanSensor = i2c1_read1ByteRegister(MCP23017, GPIOA); error=i2c1_getLastError(); if(error==I2C1_FAIL_TIMEOUT) { LcdGoToXY(0,2); LCD_DisplayString("MCP23017 TIMEOUT"); } else { LcdGoToXY(0,2); LCD_DisplayString("Comm is good"); } LcdGoToXY(0,3); LCD_DisplayString("Sensor = "); sprintf(buf, " %02d", fanSensor); LCD_DisplayString(buf); } } /** End of File */ I also try simple code from TB3159 but my code got struck in >> while(!I2C1STAT1bits.TXBE); void I2C1wr(void) { int8_t data[2]; I2C1ADB1 = (0x20<<1)|0; I2C1CNT = 2; data[0] = SetIO_B; data[1] = PORT_OUTOUT; for(int i=0; i< 2; i++) { //write to TXB starts communication I2C1TXB = data; // Wait until TXB is empty while(!I2C1STAT1bits.TXBE); } } Could you help me up? thank you again. BankKy Attached Image(s) Attachment(s) 47k42i2cSimple.zip (42.38 KB) - downloaded 42 times |
|
|
|
嗯,我不相信你的问题描述和测试结果总是匹配你发布的信息/图像/代码。也许是在某个时间点,但不久之后可能会发生变化。如果TB3159已经工作,为什么要更改它的工作MCC代码?我也有点惊讶,你已经连接了MC23017复位线与开关。停止测试两个I2C设备在同一时间。逐一测试(这意味着布线和代码)。如果两个工作都“独立”,然后再次尝试使用这两个。您似乎有问题的MC23017,而不是与TB3159如果你不工作与版本控制,制作备份副本,你现在的版本,你张贴。只有这样,你才能完全回到原来的代码,如果有一些变化。R测试应该恢复。[编辑:我假设你现在使用的MCC代码已经工作过了)a)从你的代码中注释出对MC23017的I2C访问(让线在这个测试中放置)-检查你的TB3159是否总是正常工作,而不管重置开关是否被按下。或者没有。如果TB3159仍然显示出错误的温度值(释放复位后),则MC23017要么没有正确连接,要么损坏。b)如果TB3159工作正常,请删除TB3159的SDA和SCL线,并注释代码以访问它。在MC23017代码中再次注释。-如果它仍然处于一个丢失的循环中,使用调试器来查看它在哪里(在MCC代码中),然后再次发布你的主函数。你的MC23017端口A在通电后自动设置为输入。对于测试,你应该先注释第一个写,然后在主while循环中使用读。
以上来自于百度翻译 以下为原文 Hm, I'm not confident then that your problem description and test results always matches the information/ images/ code you posted. May be it did at some point in time, but it might have changed soon afterwards. E.g. why do you change the working MCC code for TB3159, if it already worked ? Also I'm a bit astonished that you have connected the MCP23017 Reset line with a switch. Stop testing both i2c devices at the same time. Test them one by one (and this means both wiring and code). If both work "stand alone", then try again to use both. You seem to have a problem with the MCP23017, not with the TB3159 If you do not work with version control, make a backup copy of your current version, which you posted. Only that way you can return exactly to the original code, if some changes for testing should be reverted. [edit: and I assume you now use the MCC code, which already worked] a) Comment out the i2c access to the MCP23017 from your code (leave the wires in place for this test) -> check whether your TB3159 always works correctly, regardless of the Reset switch being pressed or not. If TB3159 still shows wrong temperature values (after releasing reset), then the MCP23017 is either not connected correctly or damaged. Possibly post a schmatic instead of your photos. b) if the TB3159 works correctly, remove the SDA and SCL wires for the TB3159 and also comment out the code to access it. Comment-in the MCP23017 code again. -> If it still is in an endloss loop, use the debugger to see where (in the MCC code) it stucks, and post your main function again. Your MCP23017 port A is automatically set to input after power-on. For testing you should comment out the first write and just use the read in the main while loop. |
|
|
|
TB3159代码没有设置“S”位,但我发现没有I2C不能工作。MCC代码确实设置了它。您的临时显示代码不适用于负温度。184是0xB8,它是-72C。这可能是TC74可以输出的最大负温度,这让我想知道MC23017是否拉低了VDD并导致TC74输出无效数据。因为它们在同一个电路板上,所以我怀疑硬件问题。张贴你的示意图,如果你没有一个,那么你需要创建一个。
以上来自于百度翻译 以下为原文 The TB3159 code doesn't set the "S" bit, but I found that i couldn't get I2C to work without it. The MCC code does set it. Your temp display code isn't working for negative temps. 184 is 0xB8 which is -72C. This might be the maximum negative temperature the TC74 can output, and it makes me wonder if somehow the MC23017 is pulling VDD down and causing the TC74 to output invalid data. Since they are on the same breadboard, I suspect a hardware problem. Post your schematic, and if you don't have one then you need to create one. |
|
|
|
这是一个用于MC23017 I2C端口扩展器的库,这些芯片使用I2C进行通信,2个引脚需要接口AdfRuIT投入时间和资源提供这个开源代码,请支持AdFuRuIT和开源硬件购买AdFuRIT的产品!由Limor Fray/Lajayad撰写的AdfuRIT产业。BSD许可证,检查LIPENSE.TXT的更多信息,所有文本必须包含在任何重新分配下载。点击右上角的下载按钮,重命名未压缩文件夹AdFuRuthMcP23017。检查AdFuRuthMcP23017文件夹包含AdFuRuthMcPl301717CPP和AdFuRuTITMCP23017.h放置AdFuRuthMcPl3017库文件夹显示框Kodi Lucky Patcher & lt;ARDuiSoCKChlase/Gt/ /文件夹。您可能需要创建库子文件夹,如果它是您的第一个库。重新启动IDE。
以上来自于百度翻译 以下为原文 This is a library for the MCP23017 I2c Port Expander These chips use I2C to communicate, 2 pins required to interface Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, check license.txt for more information All text above must be included in any redistribution To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_MCP23017. Check that the Adafruit_MCP23017 folder contains Adafruit_MCP23017.cpp and Adafruit_MCP23017.h Place the Adafruit_MCP23017 library folder your ShowBox Kodi Lucky Patcher |
|
|
|
我通过代码访问设备,但可以转换成一个库。这里是我创建的一个C头文件。所有的位都定义为掩码,ASM报头定义位。
以上来自于百度翻译 以下为原文 I access the device through code but may convert to a library. Here is a C header file I created. All the bits are defined as masks, the asm header defines bits. /* * File: MCP23017_Expander.h * Author: Gort1951 * * Created on 04 February 2015, 19:18 */ //---------------------------------------------------- #ifndef MCP23017_EXPANDER_H #define MCP23017_EXPANDER_H #ifdef __cplusplus extern "C" { #endif //---------------------------------------------------- //base ID #define MCP23017 0b0100000 //ID #define MCP23008 0b1000000 //ID //registers - bank0 #define b0_IODIRA 0x00 #define b0_IODIRB 0x01 #define b0_IPOLA 0x02 #define b0_IPOLB 0x03 #define b0_GPINTENA 0x04 #define b0_GPINTENB 0x05 #define b0_DEFVALA 0x06 #define b0_DEFVALB 0x07 #define b0_INTCONA 0x08 #define b0_INTCONB 0x09 #define b0_IOCONA 0x0A #define b0_IOCONB 0x0B #define b0_GPPUA 0x0C #define b0_GPPUB 0x0D #define b0_INTFA 0x0E #define b0_INTFB 0x0F #define b0_INTCAPA 0x10 #define b0_INTCAPB 0x11 #define b0_GPIOA 0x12 #define b0_GPIOB 0x13 #define b0_OLATA 0x14 #define b0_OLATB 0x15 //---------------------------------------------------- //registers - bank1 #define b1_IODIRA 0x00 #define b1_IPOLA 0x01 #define b1_GPINTENA 0x02 #define b1_DEFVALA 0x03 #define b1_IOCONA 0x04 #define b1_INTCONA 0x05 #define b1_GPPUA 0x06 #define b1_INTFA 0x07 #define b1_INTCAPA 0x08 #define b1_GPIOA 0x09 #define b1_OLATA 0x0a #define b1_IODIRB 0x10 #define b1_IPOLB 0x11 #define b1_GPINTENB 0x12 #define b1_DEFVALB 0x13 #define b1_INTCONB 0x14 #define b1_IOCONB 0x15 #define b1_GPPUB 0x16 #define b1_INTFB 0x17 #define b1_INTCAPB 0x18 #define b1_GPIOB 0x19 #define b1_OLATB 0x1a //---------------------------------------------------- //registers - single port #define b_IODIR 0x00 #define b_IPOL 0x01 #define b_GPINTEN 0x02 #define b_DEFVAL 0x03 #define b_IOCON 0x04 #define b_GPPU 0x05 #define b_INTF 0x06 #define b_INTCAP 0x07 #define b_GPIO 0x08 #define b_OLAT 0x09 //---------------------------------------------------- #define IOCON_INTPOL_MASK 0x02 #define IOCON_ODR_MASK 0x04 #define IOCON_HAEN_MASK 0x08 #define IOCON_DISSLW_MASK 0x10 #define IOCON_SEQOP_MASK 0x20 #define IOCON_MIRROR_MASK 0x40 #define IOCON_BANK_MASK 0x80 //---------------------------------------------------- #define IODIRA_IO0_MASK 0x01 #define IODIRA_IO1_MASK 0x02 #define IODIRA_IO2_MASK 0x04 #define IODIRA_IO3_MASK 0x08 #define IODIRA_IO4_MASK 0x10 #define IODIRA_IO5_MASK 0x20 #define IODIRA_IO6_MASK 0x40 #define IODIRA_IO7_MASK 0x80 //---------------------------------------------------- #define IODIRB_IO0_MASK 0x01 #define IODIRB_IO1_MASK 0x02 #define IODIRB_IO2_MASK 0x04 #define IODIRB_IO3_MASK 0x08 #define IODIRB_IO4_MASK 0x10 #define IODIRB_IO5_MASK 0x20 #define IODIRB_IO6_MASK 0x40 #define IODIRB_IO7_MASK 0x80 //---------------------------------------------------- #define IPOLA_IP0_MASK 0x01 #define IPOLA_IP1_MASK 0x02 #define IPOLA_IP2_MASK 0x04 #define IPOLA_IP3_MASK 0x08 #define IPOLA_IP4_MASK 0x10 #define IPOLA_IP5_MASK 0x20 #define IPOLA_IP6_MASK 0x40 #define IPOLA_IP7_MASK 0x80 //---------------------------------------------------- #define IPOLB_IP0_MASK 0x01 #define IPOLB_IP1_MASK 0x02 #define IPOLB_IP2_MASK 0x04 #define IPOLB_IP3_MASK 0x08 #define IPOLB_IP4_MASK 0x10 #define IPOLB_IP5_MASK 0x20 #define IPOLB_IP6_MASK 0x40 #define IPOLB_IP7_MASK 0x80 //---------------------------------------------------- #define GPINTENA_GPINT0_MASK 0x01 #define GPINTENA_GPINT1_MASK 0x02 #define GPINTENA_GPINT2_MASK 0x04 #define GPINTENA_GPINT3_MASK 0x08 #define GPINTENA_GPINT4_MASK 0x10 #define GPINTENA_GPINT5_MASK 0x20 #define GPINTENA_GPINT6_MASK 0x40 #define GPINTENA_GPINT7_MASK 0x80 //---------------------------------------------------- #define GPINTENB_GPINT0_MASK 0x01 #define GPINTENB_GPINT1_MASK 0x02 #define GPINTENB_GPINT2_MASK 0x04 #define GPINTENB_GPINT3_MASK 0x08 #define GPINTENB_GPINT4_MASK 0x10 #define GPINTENB_GPINT5_MASK 0x20 #define GPINTENB_GPINT6_MASK 0x40 #define GPINTENB_GPINT7_MASK 0x80 //---------------------------------------------------- #define GPPUA_PU0_MASK 0x01 #define GPPUA_PU1_MASK 0x02 #define GPPUA_PU2_MASK 0x04 #define GPPUA_PU3_MASK 0x08 #define GPPUA_PU4_MASK 0x10 #define GPPUA_PU5_MASK 0x20 #define GPPUA_PU6_MASK 0x40 #define GPPUA_PU7_MASK 0x80 //---------------------------------------------------- #define GPPUB_PU0_MASK 0x01 #define GPPUB_PU1_MASK 0x02 #define GPPUB_PU2_MASK 0x04 #define GPPUB_PU3_MASK 0x08 #define GPPUB_PU4_MASK 0x10 #define GPPUB_PU5_MASK 0x20 #define GPPUB_PU6_MASK 0x40 #define GPPUB_PU7_MASK 0x80 //---------------------------------------------------- #define GPIOA_GP0_MASK 0x01 #define GPIOA_GP1_MASK 0x02 #define GPIOA_GP2_MASK 0x04 #define GPIOA_GP3_MASK 0x08 #define GPIOA_GP4_MASK 0x10 #define GPIOA_GP5_MASK 0x20 #define GPIOA_GP6_MASK 0x40 #define GPIOA_GP7_MASK 0x80 //---------------------------------------------------- #define GPIOB_GP0_MASK 0x01 #define GPIOB_GP1_MASK 0x02 #define GPIOB_GP2_MASK 0x04 #define GPIOB_GP3_MASK 0x08 #define GPIOB_GP4_MASK 0x10 #define GPIOB_GP5_MASK 0x20 #define GPIOB_GP6_MASK 0x40 #define GPIOB_GP7_MASK 0x80 //---------------------------------------------------- #define OLATA_OL0_MASK 0x01 #define OLATA_OL1_MASK 0x02 #define OLATA_OL2_MASK 0x04 #define OLATA_OL3_MASK 0x08 #define OLATA_OL4_MASK 0x10 #define OLATA_OL5_MASK 0x20 #define OLATA_OL6_MASK 0x40 #define OLATA_OL7_MASK 0x80 //---------------------------------------------------- #define OLATB_OL0_MASK 0x01 #define OLATB_OL1_MASK 0x02 #define OLATB_OL2_MASK 0x04 #define OLATB_OL3_MASK 0x08 #define OLATB_OL4_MASK 0x10 #define OLATB_OL5_MASK 0x20 #define OLATB_OL6_MASK 0x40 #define OLATB_OL7_MASK 0x80 //---------------------------------------------------- #ifdef __cplusplus } #endif #endif /* MCP23017_EXPANDER_H */ //---------------------------------------------------- |
|
|
|
嗨,Zimou13,你的几篇文章看起来有点离题,这也是一个。假设你的意图是帮助:阿杜诺图书馆不会帮助OP解决他的问题,但只是增加了一个初学者像Bangky的混乱。你需要仔细阅读他报告的问题。
以上来自于百度翻译 以下为原文 Hi zimou13, your few posts appear to be a bit off-topic, so does this one. Assuming that your intention is to help: That Arduino library will not help OP to solve his problem, but simply adds confusion for a beginner like BankKy. You need to read carefully what problems he reported. |
|
|
|
|
|
|
|
嗨,罗迪姆和大家。谢谢你的帮助。我试着根据你们的建议评估我的计划。这是有帮助的,我可以对我的未来项目有一些想法。但我还是做不到这一点。我像个盲人,所以我必须尝试任何机会。由MCC产生的I2C在PIC18F46K22上为我工作,PIC18F46K22是旧PIC18核心,但对于这个设备来说。它是差分平台,我只能从TC74读取值。MC23017在两个读写功能上都没有发生。现在我遵照您的建议。我重新创建了硬件,并在MPLAB中进行了新项目,只验证了线路上的MC23017。请看下面的代码。我把注释放在每一行上,我还附上参考的示意图。我还重新制作了整个电路以确保通信工作正常。我在面包板上使用新的位置,以确保它没有电阻误差。我把所有的GPIOA连接到上拉电阻,GPIOA3除外,连接到GND。因此,GPIOA值必须被读取为0xF7或247。在编程如下代码后,LCD显示错误。所以我通过按压F7一步一步地调试我的代码。代码在工作,我可以得到247的值。(请参阅我所附图片)我猜应该是定时问题,所以我试图在等待功能中添加更多的延迟时间,但是代码仍然不能在编程模式下工作。我觉得它真的接近成功,但我不能得到它。你能帮忙吗?[编辑添加更多的细节]我把LCD打印放在函数上,看看哪个函数有错误。它是ReaveEnter();不确定是否用正确的方法调用它。请给我提些建议。谢谢!
以上来自于百度翻译 以下为原文 Hi rodims and everyone. Thank you for your help. I tried to evaluate my project follow you guys suggest. This is helpful and I can have some idea for my future project. But I still cannot get this done. I was like a blind so I have to try any chance. And the I2C generated by MCC is working for me on PIC18F46K22 that is old PIC18 core but for this device. It is difference platform and I can only read the value from TC74. Nothing happens with MCP23017 on the both read/write functions. Now I did follow your suggestion. I had re-created the hardware and made the new project in MPLAB to verify only MCP23017 on the lines. Look at my code as below. I put the comment on each line and I also attached the schematic for the reference. I also re-made the whole circuit to make sure the communication is working. I use the new place on the breadboard to make sure there is no resistance error on it. I connected all GPIOA with the pull-up resistors excepted GPIOA3 was connected to GND. So the GPIOA value must be read as 0xF7 or 247. After programming the code as below, the LCD shows error. So I did Debug my code with step by step running by pressing F7. The code is working, I can get the value 247. (See my attached picture) I guess it should be timing issue, so I tried to add more the delay time in the wait function but the code is still not working in programming mode. I feel like it is really close to success but I cannot get it. Could you help? #include #include #include "mcc_generated_files/mcc.h" #include "LCD_16x4.h" //#define TC74 0x4F //TC74A7 (0x9E/2) address #define MCP23017 0x20 //MCP23017A0 address (0x40/2) //(A0,A1,A2 pins are connected to GND) #define SetIO_A 0x00 #define GPIOA 0x12 #define SetIO_B 0x01 #define GPIOB 0x13 #define PORT_OUTOUT 0x00 #define PORT_INPUT 0xFF /* Main application */ void main(void) { // Initialize the device SYSTEM_Initialize(); uint8_t buf[16]; //Buffer array to use for LCD function uint8_t fanSensor,error; //Variable to store value getting from I2C __delay_ms(1000); //Delay for LCD module LcdInit(); // Initialize LCD module LcdGoToXY(1,0); // LCD Move Cursor LCD_DisplayString("MCP23017 Test"); //Test printing to LCD // MCP23017 GPIOA has default setting as input port // All GPIOA pull up to 5V except GPA3 connected to GND // GPIO value must be 0xF7 or 0b11110111 or 247 while (1) { // Add your application code fanSensor = i2c1_read1ByteRegister(MCP23017, GPIOA); //Get value of GPIOA (MCP23017) error=i2c1_getLastError(); // Check if any error occur if(error==I2C1_FAIL_TIMEOUT) { LcdGoToXY(0,2); LCD_DisplayString("MCP23017 TIMEOUT"); //display error occur } else { LcdGoToXY(0,2); LCD_DisplayString("Sensor = "); //no error, display the sensor value sprintf(buf, " %02d", fanSensor); LCD_DisplayString(buf); } LED1_Toggle(); // LED toggle for the status monitoring __delay_ms(200); } } /** End of File */ [EDIT to add more detail] I put LCD print on the function to see which function got an error. It was recieveByte(); Not sure if I called it in the correct method? Please give me some advice. thanks! static inline uint8_t receiveByte(void) { uint8_t delayCounter = 255; if(lastError == I2C1_GOOD) { while(--delayCounter) { if(I2C1STAT1bits.RXBF) { return I2C1RXB; } else { __delay_us(200);//change from 1 us to add more wait time } } lastError = I2C1_FAIL_TIMEOUT; LcdGoToXY(0,3); LCD_DisplayString("ReceiveByte err"); //display error occur } return 0; } Attached Image(s) |
|
|
|
|
|
|
|
从您的最后一篇文章中,我得出结论说,您正在使用MCC,并为PIC18F47 K42创建了一个新项目。请确保您使用了最近的MPLABX版本,因此MCC库符合我从您的帖子中读取的DATESOME语句:-您不能读取(I2C)MC23017,因为MCC函数接收字节。返回超时(I2C1yFultTimeOUT)-完全相同的代码和硬件读取MC23017正确,如果单步通过代码。同样的MCC为PIC18F47 K42代码确实与您的TB3159 I2C设备PIC18F47 K42(如果地址将被调整)正确工作-您的MC23017与MC的I2C读数C用于较老的PIC18F46K22(但在另一个板上)。(我们可以假设你使用的是同一个MC23017设备,你再次测试了吗?)如果所有这些陈述都是真的,那么它确实看起来像是一个定时问题,你的PIC18F47 K42,I2C MCC代码和MC23017的组合。我无法测试或验证。我还假设MCC代码是正确的,这些库有时包含缺陷,但我没有试图发现错误。我想你没有一个微型逻辑分析仪来观看I2C计时吗?既然你改变了这个问题,就如同通过代码一样解决问题吗?你也应该(作为一个实验)修改函数,使它不使用超时,例如通过这种方式替换,你也可以尝试“测量”RXBF需要设置的实际时间。这不是一个解决方案,只是看看“超时”是问题的根源还是仅仅是一个侧面。计算机断层扫描。
以上来自于百度翻译 以下为原文 From your last post I conclude that your are using MCC and created a new project for the PIC18F47K42. Be sure that you use a recent MpLabX version, so the MCC library is up to date Some statements which I read from your posts: - you cannot read (i2c) the MCP23017, because the MCC function receiveByte returns a timeout (I2C1_FAIL_TIMEOUT) - exactly same code and hardware reads the MCP23017 correctly, if single-stepping through the code. - same MCC for PIC18F47K42 code did work correctly with your TB3159 i2c Device PIC18F47K42 ( if addresses would be adjusted) - your i2c reading of MCP23017 with MCC works for the older PIC18F46K22 (but on another board). (can we assume you are using the same single MCP23017 device, and did you test it again?) If all these statements are true, then indeed it would look like a timing problem with the combination of your PIC18F47K42, i2c-MCC code and the MCP23017. I cannot test or verify that. I also assume that the MCC code is correct, these libraries sometimes contain flaws, but I did not try to spot an error there. I assume you do not have a tiny logic analyzer to watch the i2c timing ? Since you changed __delay_us(200);//change from 1 us to add more wait timedoes this resolve the problem in the same way as stepping through the code ? You should also (as an experiment) modify the function static inline uint8_t receiveByte(void) so that it does not use a timeout , e.g. replace while (--delayCounter) by while (1) This way you could also try to "measure" the actual time needed for RXBF to be set. This is not a solution, it's just to see whether the "timeout" is the origin of the problem or only a side-effect. |
|
|
|
我似乎有同样的问题与PIC 18LF27 K42。我不能让SPI接口工作。我把我的项目从PIC 18LF27 K40升级到18LF27 K42。在K40上,SPI接口工作正常,但在K42上,它似乎挂在“同时”(!我把两个PIC的I2C接口的信号与我的范围进行了比较,除了K42,在地址字节被发送后,从属器没有用A/ACK应答。K42(不工作):对我来说,信号看起来是相同的点,即AC/ACK是由奴隶发送的,但是用K40,从属用A/ACK(逻辑0)响应,并且用K42响应1。同一个板,只是一个不同的图片。欢迎任何想法…亚力山大
以上来自于百度翻译 以下为原文 I seem to have the same problem with the PIC 18LF27K42. I can't get the SPI interface to work. I upgraded my project from a PIC 18LF27K40 to the 18LF27K42. On the K40 the SPI interface was working fine, but on the K42 it seems to hang at the "while(!I2C1STAT1bits.TXBE); // wait for buffer empty" line. I compared the signals of the I2C interface of both PICs with my scope, and the timing seems identical except that with the K42 the slave does not reply with an /ACK after the address byte is sent. This is with the K40 (working): And this is with the K42 (not working): To me, the signals look the same up to the point where the /ACK is sent by the slave, but with the K40 the slave responds with an /ACK (a logic 0), and with the K42 it responds with a 1. Same board, just a different PIC. Any ideas are welcome... Alexander Attached Image(s) |
|
|
|
嗨,罗迪姆,现在我可以做这项工作。我修改了在MCC中添加一些功能到TB3159,它正在工作。但是我不能通过MCC修改I2C1来工作。是的,我使用最新的MPLABX V4.15和MCC V 3.55.1,是的,正确!是的,正确!修改后在MCC中添加一些功能。它对两个读/写函数都能正常工作。请参阅我的代码如下。它是TB3159的改进,在TCP3159中也修改了MCC中的RealEvEnter。我试图添加更多的延迟或不使用超时,但它仍然挂在I2C1STAT1BIT.RXBF中,请参阅下面的代码。现在我必须保持我的项目移动。因为我有I2C与这个设备一起工作,所以我必须完成我的其他项目。但如果你想让我尝试,让我知道。谢谢,BankKy。
以上来自于百度翻译 以下为原文 Hi rodims, Now I can have this work. I modified to add some function in MCC to TB3159 and it is working. But I cannot modify I2c1 by MCC to work. Yes, I use the latest MPLABX v4.15 and MCC v 3.55.1. Yes, correct! Yes, correct! After I modify to add some function in MCC. It is working correctly for the both read/write function. See my code is below. It is TB3159 modified. #include "i2c1_bank.h" void i2c1Init(void) { I2C1CON0 = 0x04; //MASTER 7 bit address mode I2C1CLK = 0x03; //MFINTOSC (500kHz) I2C1CON1 = 0x80; // ACKDT=ACK, ACKCNT=NACK I2C1CON2 = 0x21; // I2C1PIR = 0; //CLR all interrupt flag I2C1ERR = 0; //CLR all error flag I2C1CON0bits.EN = 1; //Enable I2C1 module } void i2c1_writeNBytes(uint8_t address, void* data, uint8_t len) { uint8_t *dataPointer = data; I2C1ADB1= (uint8_t)(address<<1); I2C1CNT=len; I2C1CON0bits.S=1; //Start while(len--) { sendByte(*dataPointer++); } wait4stop(); } uint8_t I2C1rd(uint8_t device, uint8_t regst) { uint8_t buffer; I2C1ADB1 = (device<<1)|0; I2C1CNT = 1; I2C1CON0bits.RSEN=1; I2C1CON0bits.S=1; //Start while (I2C1CON0bits.S); sendByte(regst); wait4MDRSetcount(1); device = (uint8_t)(device<<1); I2C1ADB1= (uint8_t)(device| 0x01); //Change the R/W bit for read I2C1CON0bits.S=1; //Start while(I2C1CON0bits.S); I2C1CON0bits.RSEN=0; while(!I2C1STAT1bits.RXBF); buffer = I2C1RXB; wait4stop(); return buffer; } void sendByte(uint8_t data) { uint8_t delayCounter = 255; while(--delayCounter) { if(I2C1STAT1bits.TXBE) { I2C1TXB = data; return; } else { __delay_us(1); } } } void wait4MDRSetcount(uint8_t count) { uint8_t waitCount=255; while(--waitCount) { if(I2C1CON0bits.MDR) { I2C1CNT = count; return; } else { __delay_us(1); } } } void wait4stop(void) { uint8_t waitCount=255; while(--waitCount) { if(I2C1PIRbits.PCIF) { break; } else { __delay_us(1); } } } I also modified recieveByte in MCC with the same code in working TB3159. I tried to add more delay or using no timeout but it was still hanging in I2C1STAT1bits.RXBF see the code as below. static inline uint8_t receiveByte(void) { // uint8_t delayCounter = 255; if(lastError == I2C1_GOOD) { // while(--delayCounter) // while(1) //Try to use no timeout // { // if(I2C1STAT1bits.RXBF) // { // return I2C1RXB; // } // else // { // __delay_us(10);//change from 1 us to add more wait time // } // } // lastError = I2C1_FAIL_TIMEOUT; // LcdGoToXY(0,3); // LCD_DisplayString("ReceiveByte err"); //display error occur while(!I2C1STAT1bits.RXBF); return I2C1RXB; } return 0; } Now I have to keep my project moving. As I have I2C work with this device, I have to do the rest of my project. But if you want me to try something let me know. Thanks, BankKy |
|
|
|
嗨,ApVENSTRA,请不要劫持另一个线程,你不会得到任何帮助,这样你也会打扰这个线程。在同一线程中支持两个开发人员是不可能的。如果你想得到帮助,请创建你自己的线程,如果你这样做,请修复你的SPI和I2C的混合。
以上来自于百度翻译 以下为原文 Hi apveenstra, Please don't hijack another thread, you won't get any help this way and you also disturb this thread. It is not possible to support two developers in the same thread. If you want to get help, please create your own thread, and if you do, please fix your mix of both SPI and i2c |
|
|
|
对不起,我不是想劫持这个线程,我想这可能是和BunkyRunin一样的问题。而且,我做了一个排版,我说SPI I的意思是I2C,但是网站不会让我编辑我的帖子。
以上来自于百度翻译 以下为原文 Sorry, I wasn't trying to hijack the thread, I thought this might be the same problem as BankKy ran into. Also, I made a typo, where I said SPI I meant I2C, but the website would not let me edit my post... |
|
|
|
也许会有所帮助…或不是!http://www. eTea技术…RK-in进步。15200
以上来自于百度翻译 以下为原文 May be of some help ... or not ! https://www.electro-tech-...rk-in-progress.152004/ |
|
|
|
只有小组成员才能发言,加入小组>>
5132 浏览 9 评论
1985 浏览 8 评论
1914 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3153 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2213 浏览 5 评论
702浏览 1评论
593浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
476浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
608浏览 0评论
499浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-5 09:14 , Processed in 2.033193 second(s), Total 112, Slave 95 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号