⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.c

📁 PIC to PIC communication using i2c bus. Multi-master implemented on pic24.
💻 C
字号:
/************************************************
*				initI2C_NCAP					*
*	Esta fun玢o inicia o i2c para o NCAP		*		
*	O NCAP tem sp o id 8						*
************************************************/

#include <p24fj128ga010.h>

// calculate baud rate of I2C
#define Fosc	(8000000) 
#define Fcy		(Fosc/2)	// no PLL
#define Fsck	100000
#define I2C_BRG	((Fcy/2/Fsck)-1)
	


void initI2C_NCAP(void)
{
	
	// o NCAP tem ID=8
	I2C1ADD=8;
	//enable I2C
	I2C1CON=0x8000;
	I2C1CONbits.STREN=1;
	I2C1CONbits.GCEN=1;
	I2C1BRG=I2C_BRG;

	//enable i2c slave interrupt
	IFS1bits.SI2C1IF=0;
	IPC4bits.SI2C1P=0b111;	
	IEC1bits.SI2C1IE=1;
}

/************************************************
*					initI2C						*
*	Esta fun玢o inicia o i2c, no inicio o stim	*
*	tem on id=9, isto para n鉶 causar problemas	*
* 	aquando da sua entrada na rede				* 
************************************************/
void initI2C_STIM(void)
{

	I2C1ADD=9;
	//enable I2C
	I2C1CON=0x8000;
	I2C1CONbits.STREN=1;
	I2C1CONbits.GCEN=1;
	I2C1BRG=I2C_BRG;

	//enable i2c slave interrupt
	IFS1bits.SI2C1IF=0;
	IPC4bits.SI2C1P=0b111;	
	IEC1bits.SI2C1IE=1;


}

void new_id(unsigned char id)
{
	I2C1CONbits.GCEN=1;
	I2C1ADD=id;
	//enable i2c slave interrupt
	IFS1bits.SI2C1IF=0;
	//IPC4bits.SI2C1P=0b111;	
	IEC1bits.SI2C1IE=1;
}

/************************************************
*					idlei2c1					*
*	Wait until I2C Bus is Inactive				*
************************************************/
void idlei2c1(void)
{
    /* Wait until I2C Bus is Inactive */
    while(I2C1CONbits.SEN || I2C1CONbits.PEN || I2C1CONbits.RCEN || I2C1CONbits.ACKEN || I2C1STATbits.TRSTAT);  
}

/************************************************
*					starti2c1					*
*	Esta fun玢o 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -