服务热线400-888-0024
新拓PIC单片机论坛 新拓PIC论坛携手高奇微电子,强势推出买PIC原装工具,送PIC国产工具活动 新拓PIC开发工具商城

Microchip PIC单片机论坛MICROCHIP 单片机应用专区MICROCHIP 16/32位单片机应用 → [原创]PIC24FJ64GA002 IIC从模式SDA/SCL全锁死


  共有1857人关注过本帖树形打印

主题:[原创]PIC24FJ64GA002 IIC从模式SDA/SCL全锁死

帅哥哟,离线,有人找我吗?
liyang868
  1楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:新手上路 贴子:1 积分:18 威望:0 精华:0 注册:2017-2-25 14:21:00
[原创]PIC24FJ64GA002 IIC从模式SDA/SCL全锁死  发贴心情 Post By:2017-2-25 15:04:00

PIC24FJ64GA002 IIC从模式SDA/SCL全锁死 用的PIC24作为从器件,主器件是ARM内核的IIC调试器。IIC调试器主发送给我的PIC,一切正常,PIC可以接受到正常的数据。 但是IIC调试器主接受时,成功不了,现象是,一发送 START+addr+1(即发启动加读指令),则SDA、SCL全部被拉低为0;关闭PIC单片机,数据线 时钟线都恢复正常。 程序是从微芯官网下的IIC_SL*E,不过它貌似是用到dsPIC33上的,不知道会不会是这个有影响。 SDA/SCL锁死,都是什么原因造成的,大神们。解决定重谢!! #if defined(__dsPIC33F__) #include "p33Fxxxx.h" #elif defined(__PIC24H__) #include "p24Hxxxx.h" #endif #include "I2CSlaveDrv.h" unsigned char RAMBuffer[256]; //RAM area which will work as EEPROM for Master I2C device unsigned char *RAMPtr; //Pointer to RAM memory locations struct FlagType Flag; /***************************************************************** Init I2C1 Bus *****************************************************************/ void i2c1_init(void) { #if !defined(USE_I2C_Clock_Stretch) I2C1CON = 0x8000; //Enable I2C1 module #else I2C1CON = 0x9040; //Enable I2C1 module, enable clock stretching #endif I2C1ADD = 0x50; // 7-bit I2C slave address must be initialised here. IFS1=0; RAMPtr = &RAMBuffer[0]; //set the RAM pointer and points to beginning of RAMBuffer Flag.AddrFlag = 0; //Initlize AddFlag Flag.DataFlag = 0; //Initlize DataFlag _SI2C1IE = 1; } /* Function Name: SI2C1Interrupt Description : This is the ISR for I2C1 Slave interrupt. Arguments : None */ void __attribute__((interrupt,no_auto_psv)) _SI2C1Interrupt(void) { unsigned char Temp; //used for dummy read if((I2C1STATbits.R_W == 0)&&(I2C1STATbits.D_A == 0)) //Address matched { Temp = I2C1RCV; //dummy read Flag.AddrFlag = 1; //next byte will be address } else if((I2C1STATbits.R_W == 0)&&(I2C1STATbits.D_A == 1)) //check for data { if(Flag.AddrFlag) { Flag.AddrFlag = 0; Flag.DataFlag = 1; //next byte is data RAMPtr = RAMPtr + I2C1RCV; #if defined(USE_I2C_Clock_Stretch) I2C1CONbits.SCLREL = 1; //Release SCL1 line #endif } else if(Flag.DataFlag) { *RAMPtr = (unsigned char)I2C1RCV;// store data into RAM Flag.AddrFlag = 0;//end of tx Flag.DataFlag = 0; RAMPtr = &RAMBuffer[0]; //reset the RAM pointer #if defined(USE_I2C_Clock_Stretch) I2C1CONbits.SCLREL = 1; //Release SCL1 line #endif } } else if((I2C1STATbits.R_W == 1)&&(I2C1STATbits.D_A == 0)) { Temp = I2C1RCV; I2C1TRN = *RAMPtr; //Read data from RAM & send data to I2C master device I2C1CONbits.SCLREL = 1; //Release SCL1 line while(I2C1STATbits.TBF);//Wait till all RAMPtr = &RAMBuffer[0]; //reset the RAM pointer } _SI2C1IF = 0; //clear I2C1 Slave interrupt flag } #if defined(__dsPIC33F__) #include "p33Fxxxx.h" #elif defined(__PIC24H__) #include "p24Hxxxx.h" #endif #include "I2CSlaveDrv.h" //Include file for I2C1 Driver // Confiuration Register Settings // Internal FRC Oscillator _FOSCSEL(FNOSC_FRC); // FRC Oscillator _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE); // Clock Switching is enabled and Fail Safe Clock Monitor is disabled // OSC2 Pin Function: OSC2 is Clock Output // Primary Oscillator Mode: Disabled _FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software //Begin the Main Fucntion int main(void) { unsigned int i; // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 7.37*43/(2*2)=80Mhz for 7.37 input clock PLLFBD=41; // M=43 CLKDIVbits.PLLPOST=0; // N1=2 CLKDIVbits.PLLPRE=0; // N2=2 OSCTUN=0; // Tune FRC oscillator, if FRC is used // Disable Watch Dog Timer RCONbits.SWDTEN=0; // Clock switch to incorporate PLL __builtin_write_OSCCONH(0x01); // Initiate Clock Switch to // FRC with PLL (NOSC=0b001) __builtin_write_OSCCONL(0x01); // Start clock switching while (OSCCONbits.COSC != 0b001); // Wait for Clock switch to occur // Wait for PLL to lock while(OSCCONbits.LOCK!=1); //Now PLL is ready for(i = 0;i<256;i++) { RAMBuffer[i] = i; //Initlize RAMBuffer with some value //in case MasterI2C device wants to read //before it writes to it. } i2c1_init(); while(1) { //Do your application task } }

支持(0中立(0反对(0回到顶部