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

📄 main.c

📁 本例程是描述了通过PIC33FXXX单片机的I2C接口实现对单片机外围EEPROM的访问
💻 C
字号:
/**********************************************************************
* ADDITIONAL NOTES: 
* This code is tested on Explorer 16 board with dsPIC33FJ256GP710 controller  
* The Processor starts with the Internal oscillator without PLL enabled and then the Clock is switched to PLL Mode.
**********************************************************************/
#if defined(__dsPIC33F__)
#include "p33Fxxxx.h"
#elif defined(__PIC24H__)
#include "p24Hxxxx.h"
#endif

#include "D:\Microchip(8.1)\dsp33xxcode demo\CE119_I2C_EEPROM\h\i2cEmem.h"

// Internal FRC Oscillator
_FOSCSEL(FNOSC_FRC);	   // FRC Oscillator 
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_XT); // 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
						   // (LPRC can be disabled by clearing SWDTEN bit in RCON register
_FPOR(FPWRT_PWR1);  				   // Turn off the power-up timers.
_FGS(GCP_OFF);            			   // Disable Code Protection
                                                   // Instantiate Drive and Data objects
I2CEMEM_DRV i2cmem= I2CSEMEM_DRV_DEFAULTS;                                  
I2CEMEM_DATA wData;
I2CEMEM_DATA rData;

unsigned int wBuff[10],rBuff[10];
unsigned int enable;

int main(void)
{
int i=0;

/**Configure Oscillator to operate the device at 40Mhz Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
***Fosc= 8M*40/(2*2)=80Mhz for 8M input clock **/
	PLLFBD=38;					// M=40
	CLKDIVbits.PLLPOST=0;		// N1=2
	CLKDIVbits.PLLPRE=0;		// N2=2
	OSCTUN=0;			// Tune FRC oscillator, if FRC is used

	RCONbits.SWDTEN=0;       // Disable Watch Dog Timer
	AD1PCFGL=0xFFFF;         // Make all the ANx pins as digital pins
	AD1PCFGH=0xFFFF;

// Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x03);		// Initiate Clock Switch to Primary
						// Oscillator with PLL (NOSC=0b011)
	__builtin_write_OSCCONL(0x01);		// Start clock switching
TRISFbits.TRISF4 = 0  ;
TRISFbits.TRISF5 = 0  ; 
PORTFbits.RF4 = 0     ;
PORTFbits.RF5 = 1     ;
	while (OSCCONbits.COSC != 0b011);	// Wait for Clock switch to occur	

// Wait for PLL to lock
	while(OSCCONbits.LOCK!=1) {};
//**********************************************
//**********************************************
TRISFbits.TRISF4 = 0  ;
TRISFbits.TRISF5 = 0  ; 
PORTFbits.RF4 = 1     ;
PORTFbits.RF4 = 1     ;
PORTFbits.RF5 = 0     ;

//**********************************************
//**********************************************
// Initialise I2C peripheral and Driver
    i2cmem.init(&i2cmem); 
  
// Initialise Data to be written to serial EEPROM         
    for(i=0;i<10;i++)                        
    	wBuff[i]=i+0x40;

// Initialise I2C Data object for Write operation   
    wData.buff=wBuff;
    wData.n=10;
    wData.addr=0x0040; 
    wData.csel=0x00;
                  

// Initialise I2C Data Object for Read operation            
    rData.buff=rBuff;
    rData.n=10;
    rData.addr=0x0040; 
    rData.csel=0x00;

// Enable data write to I2C serial EEPROM
	enable=1;

    while(1)
	{

		if(enable==1) 
		{
			enable=0;

			// Write Data
			i2cmem.oData=&wData;
			i2cmem.cmd = I2C_WRITE;	
		
			while(i2cmem.cmd!=I2C_IDLE )
    		{	
				i2cmem.tick(&i2cmem); 
			}

			// Read Data
			i2cmem.oData=&rData;
			i2cmem.cmd = I2C_READ;
			while(i2cmem.cmd!=I2C_IDLE)
    		{
				i2cmem.tick(&i2cmem); 
			}
//          while(1);
      }
	};

}


⌨️ 快捷键说明

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