完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我开始用PIC18F25K42工作,似乎无法让I2C模块工作正常。我最初运行的PIC作为奴隶,我可以看到它中断的条件下,我启用中断(启动条件,ADDR接收等),它也正确地读取地址从主人。但是,我无法得到它的地址。我玩PPS寄存器,检查所有的错误标志,还是什么都没有。因为我似乎只有发送数据的问题,我试图把问题归结为一个简单的主实现发送一个字节。在我最初的尝试不起作用之后,我实现了TB3159的代码,这似乎也不起作用。我最初用I2C2在引脚RB1和2上工作,这个例子是在RC3和4上的I2C1。然而,这仍然没有在公共汽车上放任何东西。此外,代码挂在“当”(!“I2C1STAT1BIT.TXBE”;“永远”,所以似乎没有数据实际上是时钟。我正在运行一个外部振荡器,把它变成一个内部振荡器也没有改变任何东西。我基本上没什么可尝试的了。代码是
以上来自于百度翻译 以下为原文 Hi, I started working with a PIC18F25K42 and can't seem to get the I2C module to work correctly. I originally ran the PIC as a slave and I can see that it interrupts on the conditions I enable interrupts for (start condition, addr received, etc.) It also correctly reads in the address from the master. However, I couldn't get it to acknowledge the reception of the address. I played around with the PPS registers and checked all the error flags, still nothing. Since I only seem to have issues with sending data I tried to condense the problem down to a simple master implementation sending a byte. After my initial attempts didn't work I implemented the code from TB3159 which also didn't seem to work. I originally worked with I2C2 on pins RB1 and 2, the example is I2C1 on RC3 and 4. However, this still doesn't put anything on the bus. Additionally, the code hangs in "while(!I2C1STAT1bits.TXBE);" forever, so it seems no data is actually clocked out. I'm running off an external oscillator, changing this to an internal one didn't change anything either. I'm basically running out of things to try. Code is below. Thanks Erik #pragma config RSTOSC = 2, FEXTOSC = 7 #pragma config WDTE = OFF, XINST = 1, DEBUG = 1; #include #include void main() { int i; RC4PPS = 0x22; I2C1SDAPPS = 0x14; // Set RC3 for SCL RC3PPS = 0x21; I2C1SCLPPS = 0x13; // PPS Lock Sequence PPSLOCK = 0x55; PPSLOCK = 0xAA; PPSLOCKbits.PPSLOCKED = 0x01; // Configure the pins as Open-drain ODCONCbits.ODCC3 = 1; ODCONCbits.ODCC4 = 1; // Set the I2C levels and pullups RC3I2Cbits.TH = 1; RC4I2Cbits.TH = 1; RC3I2Cbits.PU = 1; RC4I2Cbits.PU = 1; // Configure the pins as Outputs TRISCbits.TRISC3 = 1; TRISCbits.TRISC4 = 1; // 7bit Master Mode (MODE = 4) I2C1CON0 = 0x04; // I2C Clock = MFINTOSC (500kHz) I2C1CLK = 0x03; // ACK for every valid byte (ACKDT = 0) // NACKs to end a Read (ACKCNT = 1) I2C1CON1 = 0x80; // Auto-count disabled (ACNT = 0) // General Call disabled (GCEN = 0) // Fast mode enabled // (FME = 1; SCL = I2CCLK/4) // ADB1 address buffer used (ADB = 0) // SDA Hold time of 300 ns (SDAHT = 0) // Bus free time of 16 I2C Clock pulses // (BFRET = 1) I2C1CON2 = 0x21; // Enable I2C module I2C1CON0bits.EN = 1; I2C1ADB1 = 0x8; I2C1CNT = 2; I2C1TXB = 0xa5; //I2C1CON0bits.S = 1; // Wait until TXB is empty while(!I2C1STAT1bits.TXBE); // this is never reached for (i = 0; i < 20; i++); } |
|
相关推荐
6个回答
|
|
没关系,我发现了这个问题。端口需要指定为输出,而不是输入。这和我过去工作的大多数照片不同。
以上来自于百度翻译 以下为原文 Never mind, I found the issue. The ports need to be specified as outputs, not inputs. That's different to most pics I worked with in the past. |
|
|
|
你是说TIS寄存器设置吗?我从来没有使用过这个设备,但是数据表说:
以上来自于百度翻译 以下为原文 Are you just talking about the TRIS register setting? I've never used that device, but the datasheet says: |
|
|
|
谢谢你的报道。我有一个56K42项目,我已经启动了,需要I2C。我正在等待代码,而我在原型机上等待,但我还没有得到我想要的。竞争的优先级太多。
以上来自于百度翻译 以下为原文 Thanks for reporting that. I have a 56K42 project I've started that will need I2C. I'm working on code while I'm waiting on the prototype board, but I haven't gotten as far as I'd like. Too many competing priorities. |
|
|
|
是的,我就是这么说的。想一想:一个开放排放的投入是没有意义的。还有很多其他奇怪的现象。数据表中的PPS模块描述称,例如,端口RB1和RB2默认设置为I2C2模块,但是数据表中的位值是错误的,它们看起来像I2C2SDApp被设置为使用RA2作为输入,而不是RB2。因此,无论是输入还是输出,都必须在PPS模块中明确设置通道,即使默认值应该是正确的。我花了两天的时间来处理这个问题,然后最终放弃并张贴在这个论坛上,30分钟后它开始工作。真有趣,埃里克
以上来自于百度翻译 以下为原文 Yes, that is what I was talking about. Thinking about it: an open drain input doesn't really make sense. There are also a bunch of other oddities. The PPS module description in the datasheet says that for instance port RB1 and RB2 are setup by default for the I2C2 module, but the bit values in the datasheet are wrong, they look like I2C2SDAPPS is set up to use RA2 as an input, not RB2. So both, in and out, channels have to be explicitly set in the PPS module even though the defaults should be correct. Anyway, everything works now as expected. I spent two days messing around with this, then finally gave up and posted on this forum, and 30 min later it started working. Funny how that works. Erik |
|
|
|
PIC18FXXK42是其他东西…http://www. eTeaTeo…RK-in进步。15200
以上来自于百度翻译 以下为原文 The PIC18FxxK42 is something else... http://www.electro-tech-o...rk-in-progress.152004/ |
|
|
|
由于某种原因,当PPS在开始时没有设置为指定的默认值时,这是非常常见的。不管是否默认,我总是设置PPS。
以上来自于百度翻译 以下为原文 For some reason, this is very common when PPS is not set to specified default values at the start. I always set PPS regardless of whether it is default or not. |
|
|
|
只有小组成员才能发言,加入小组>>
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 13:27 , Processed in 1.334365 second(s), Total 57, Slave 50 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号