完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我已经挣扎了一段时间,不知道为什么它不工作。我想我有几个问题。首先,我认为,有一些问题是用。当这些被包含在编译的代码中时,微控制器上没有发生任何事情,但是当它们被注释出来时,代码按照预期运行(闪烁LED)。我正在使用一个实际的控制器,LED等来测试(不是模拟器)。(我的PracMMA注释)我使用了一个“DILIGITENIC模拟发现2”的协议分析器功能(主I2C),当我尝试发送和接收一个字节到微控制器时,它给了我如下:开始,H30[H18] WR]NAK,H01 NAK,STESTORT,H31(H18×RD)NAK,HFF NAK,StopWhen SSPCON寄存器设置为0x36(从,7位地址),主函数只运行(不断闪烁LED而不调用I2C功能)。然而,当这被设置为0x3e(从属,7位地址启用中断)它确实调用I2C函数,但被卡住等待BF标志被设置。它被困在这一点上,不管我是否设置RW标志是读还是写。有一件事让我感到困惑,就是在手册中,它说SSPIF是在地址匹配之后设置的,这是否意味着中断是在地址被匹配之后产生的,还是中断类型的。在启动条件下?
以上来自于百度翻译 以下为原文 Hi, I have been struggling for a while and can't work out why it's not working. I think I have several issues though. Firstly I think there are issues with the #pragma configs. When these are included in the compiled code, nothing happens on the microcontroller, however when they are commented out the code runs the main as expected (flashes the LEDs). I'm using an actual controller, LEDs etc to test (not a simulator). (#pragma comment out) I am using the protocol analyser function (master i2c) of a "Dilligent Analog Discovery 2" which gives me the following when I try and send or receive a byte to / from the microcontroller: Start, h30 [ h18 | WR ] NAK, h01 NAK, Stop Start, h31 [ h18 | RD ] NAK, hFF NAK, Stop When the SSPCON register is set to 0x36 (slave, 7 bit address) the main function just runs (constantly flashes the LEDs without calling the i2c function). However when this is set to 0x3E (slave, 7 bit address with interrupts enabled) it does call the i2c function but gets stuck waiting for the BF flag to be set. It get's stuck at this point, regardless of whether I am setting the RW flag to be Read or Write. One thing that does confuse me, is in the manual, it says the SSPIF is set after an address match, does this mean the interrupt is generated after the address has been matched, or is the interrupt generated on the start condition? #include //CONFIG1H #pragma config OSC = HS #pragma config FCMEN = OFF #pragma config IESO = OFF //CONFIG2L #pragma config PWRTEN = ON #pragma config BOREN = OFF #pragma config BORV = 45 //CONFIG2H #pragma config WDTEN = OFF #pragma config WDPS = 32768 #pragma config WINEN = OFF //CONFIG3L #pragma config PWMPIN = OFF #pragma config LPOL = HIGH #pragma config HPOL = HIGH #pragma config T1OSCMX = OFF //CONFIG3H #pragma config MCLRE = OFF //CONFIG4L #pragma config STVREN = ON #pragma config LVP = OFF #pragma config DEBUG = OFF //CONFIG5L #pragma config CP0 = OFF #pragma config CP1 = OFF //CONFIG5H #pragma config CPB = OFF #pragma config CPD = OFF //CONFIG6L #pragma config WRT0 = OFF #pragma config WRT1 = OFF //CONFIG6H #pragma config WRTC = OFF #pragma config WRTB = OFF #pragma config WRTD = OFF //CONFIG7L #pragma config EBTR0 = OFF #pragma config EBTR1 = OFF //CONFIG7H #pragma config EBTRB = OFF #define _XTAL_FREQ 8000000 #define slave_address 0x18 unsigned short cmd = 0x00; unsigned short buf_clr = 0x00; void init() { //Oscillator Control Register OSCCON = 0x76; //Pin Direction Register TRISA = 0x00; //0000 0000 (outputs) TRISB = 0x00; //0000 0000 (outputs) TRISC = 0x30; //0011 0000 (outputs, RC4, RC5 inputs for I2C) ANSEL0 = 0x00; //I2C SSPSTAT = 0x00; //00RR RRRR SSPCON = 0x36; //0011 1110 SSPADD = slave_address; //0001 100x (Slave 1, temp address) //Interrupt Enable Bits //RCONbits.IPEN = 1; INTCONbits.GIE = 1; INTCONbits.PEIE = 1; PIR1bits.SSPIF = 0; PIE1bits.SSPIE = 1; } int main(int argc, char** argv) { init(); while(1) { LATAbits.LA1 = 1; LATCbits.LC6 = 1; __delay_ms(500); LATAbits.LA1 = 0; LATCbits.LC6 = 0; __delay_ms(500); //main program loop... } return 0; } void interrupt slave_rec() { LATAbits.LA1 = 0; LATCbits.LC6 = 0; if(PIR1bits.SSPIF == 1) { if((SSPCONbits.SSPOV) || (SSPCONbits.WCOL)) { buf_clr = SSPBUF; SSPCONbits.SSPOV = 0; SSPCONbits.WCOL = 0; SSPCONbits.CKP = 1; } if(!SSPSTATbits.R_nW) { buf_clr = SSPBUF; while(!SSPSTATbits.BF){ LATAbits.LA1 = 1;} //wait for buffer to fill cmd = SSPBUF; LATAbits.LA1 = 0; } if(SSPSTATbits.R_nW) { SSPCONbits.CKP = 0; SSPBUF = 0x0a; SSPCONbits.CKP = 1; PIR1bits.SSPIF = 0; LATCbits.LC6 = 1; while(SSPSTATbits.BF){} } if(cmd == 0x01){LATAbits.LA0 = 1;} if(cmd == 0x02){LATAbits.LA0 = 0;} PIR1bits.SSPIF = 0; } return; } |
|
相关推荐
3个回答
|
|
我建议您将在您的SyrPracMaMigFig语句中的设置与如果不提供γ-语用值时使用的默认设置进行比较,这将显示PIC不与代码中的CONFIG语句一起运行的差异。您将时钟设置为外部晶体,这对您的安装是否正确?
以上来自于百度翻译 以下为原文 I suggest you compare the settings in your #pragma config statements to the default settings being used if you do not provide the #pragmas, this should show you the difference(s) that result in the pic not running with the config statements in the code. You are setting the clock to be an external crystal, is that correct for your setup? |
|
|
|
嗨,Stu,这是我在另一个被劫持的线程中给出的响应的副本。调试I2C从代码可能是棘手的,特别是如果你不能同时控制主控器。当你在调试器中停止奴隶时,它不会实时响应主,所以大师可能会认为。从属器没有响应,并在消息12中的代码中给出配置设置,即:如果真的有一个晶体恰好连接到PIC18F2231,如果您不确定,那么它是一个更简单和更安全的选择。即使在晶振没有连接或不工作的情况下,也可以进入调试器。在上面的代码中,MCLRE的设置是错误的,我不知道在进入调试器时MPLAB是否会重写它。应该尝试:MSSP硬件所产生的中断取决于SSPCON寄存器SSPM字段中选择的模式。如果选择普通的从属模式,SSPM=6,则当接收到匹配地址时将中断,然后当接收到每个数据字节时中断。如果模式SSPM=14,则为:EN选择时,当观察到开始和停止条件信号时,也会有中断。迈西尔
以上来自于百度翻译 以下为原文 Hi Stu, This is a duplicate of the response I have given in the other thread that was hijacked. Debugging I2C slave code may be tricky, especially if you cannot control the Master at the same time. When you stop the slave in debugger, it will not respond to the master in real time, so master may think slave is not responding, and give up Among configuration settings in code in message #12, there is: #pragma config OSC = HS This only make sense if there really is a Crystal properly connected to the PIC18F2331 If you are not sure, then #pragma config = IRCIO // Internal oscillator block, port function on RA6 and port function on RA7 is a much simpler and safer selection. Also: #pragma config FCMEN = ON // Fail-Safe Clock Monitor enabled #pragma config IESO = ON // Internal External Switchover mode enabled Then it might be possible to enter Debugger, even if Crystal oscillator were not connected or not working. Setting for MCLRE is wrong in code above, I don't know if MPLAB will override it when entering debugger. Use: #pragma config MCLRE = ON // MCLR Pin Enable bit, must be ON for Debugging to be possible Mode 36 The most common reason for I2C slave not responding to Address, is that the address do not match. You should try: SSPADD = slave_address << 1; Interrupts generated by the MSSP hardware depend on the mode selected in SSPCON register SSPM field. If ordinary slave mode is selected, SSPM = 6, there will be interrupt when a matching address have been received, and then for each data byte as they are received. If mode SSPM = 14, have been selected, then there will also be interrupts when Start and Stop condition signals are observed. Mysil |
|
|
|
MysI&杰克,谢谢你的回应,我在这方面做了一些进步。通过使用MPLAB对默认配置进行检查,我能够使用μ* PrimaMac配置使代码在微控制器上运行。正如你们提到的,我没有正确地理解振荡器配置,当我使用内部振荡器而不是外部晶体时,OSC=HS是不正确的设置。我也在I2C问题上取得了一些进展。我认为这个问题(再次被建议)是我在模拟发现2协议分析器中实际设置的地址是发现2设备(即主设备)而不是从微控制器:开始,H30[H18] WRNAK,H01 NAK,STOPI设置地址字段为H18认为这是从地址,所以留下的设置,因为他们在发现2和改变奴隶SSPADD到0x30在微控制器代码,产生一个地址匹配(和中断)正确时,SSPCON被设置为0x36。谢谢你的帮助,我应该能够得到它WOR国王从这里,只是想给出一个解释的解决方案,以防其他人有类似的问题。
以上来自于百度翻译 以下为原文 Mysil & Jack, Thank you for the response, I have made a bit more progress with this. I was able to get the code to run on the microcontroller using the #pragma config by checking it against the default configuration from MPLAB. As you both mentioned I hadn't understood the oscillator configuration correctly and as I was using an internal oscillator and not an external crystal the OSC = HS was the incorrect setting. I also made some more progress with the I2C issues. I think the problem (again as was suggested) was the address that I was actually setting in the Analog Discovery 2 protocol analyser was for the Discovery 2 device (i.e. the master) and not for the slave microcontroller: Start, h30 [ h18 | WR ] NAK, h01 NAK, Stop I was setting the Address field to be h18 thinking this was the Slave address so leaving the settings as they were in the Discovery 2 and changing the slave SSPADD to 0x30 in the microcontroller code, generated an address match (and interrupt) correctly when SSPCON was set to 0x36. Thank you for the help, I should be able to get it working from here, just wanted to give an explanation of the solutions, in case anyone else has similar problems. Stu |
|
|
|
只有小组成员才能发言,加入小组>>
5237 浏览 9 评论
2026 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3201 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
772浏览 1评论
662浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
590浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
672浏览 0评论
572浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 12:37 , Processed in 1.390492 second(s), Total 80, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号