PIC:读写24LCxx系列的EEPROM的实例C语言程序
void isr_high_direct(void) //************************************************ void isr_high(void) //*********************************************** void Initialize_Timer2(void) RCONbits.IPEN=1; // Enable Interrupt Priority bit OpenTimer2 (TIMER_INT_ON // Turn On the Timer2 with Interrupt PR2 = 99; //*********************************************** WriteI2C(ctrl); // Write Control+Write to EEPROM Check BF flag WriteI2C(addr); // Write Address to EEPROM WriteI2C(data); // Write Data to EEPROM StopI2C(); // Stop condition //*********************************************** WriteI2C(addr); // Write Address to EEPROM while (length!=0) // Check write completed ? IdleI2C(); // ensure module is idle WriteI2C(ctrl); // Write Control to E
{
_asm //begin in-line assembly
goto isr_high //go to isr_high function
_endasm //end in-line assembly
}
#pragma code
//* Function: isr_high(void) *
//* High priority interrupt for Timer2 *
//************************************************
#pragma interrupt isr_high
{
PIR1bits.TMR2IF=0; // Clear Timer2 interrupt Flag
if (Debounce==0)
{
if (!SW2)
{ P_SW2=1; Debounce =Bounce_Time; }
if (!SW6)
{ P_SW6=1; Debounce =Bounce_Time; }
}
else if (SW2 SW6)Debounce--;
else Debounce =Bounce_Time;
}
#pragma code
//* Write a Byte to EEPROM
//* - ctrl : Control Byte of EEPROM
//* - addr : Location of EEPROM
//* - data : Data Byte of EEPROM
//***********************************************
{
IPR1bits.TMR2IP=1; // Set Timer2 for High Priority
INTCONbits.GIEH=1; // Enable High Priority Interrupt
T2_PS_1_4 // (4Mhz/4) [4*10*(99+1)] = 4mS */
T2_POST_1_10);
}
//* Write a Byte to EEPROM *
//* - ctrl : Control Byte of EEPROM *
//* - addr : Location of EEPROM *
//* - data : Data Byte of EEPROM *
//***********************************************
void EEPROM_Write(unsigned char ctrl,unsigned char addr,unsigned char data)
{
IdleI2C(); // ensure module is idle
StartI2C(); // Start condition
I2C_Done(); // Wait Start condition completed and clear SSPIF flag
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done(); // Clear SSPIF flag
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done();
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done();
I2C_Done(); // Wait the Stop condition completed
}
//* Pae Write to EEPROM
//*
//* - ctrl : Control Byte of EEPROM
//* - addr : Location of EEPROM
//* - length : Write counter
//* - *dptr : RAM point --> EEPROM
//*
//***********************************************
void EE_Page_Write(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
IdleI2C(); // ensure module is idle
StartI2C(); // Start condition
I2C_Done(); // Wait Start condition completed
WriteI2C(ctrl); // Write Control+Write to EEPROM Check BF flag
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done(); // Clear SSPIF flag
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done();
{
WriteI2C(*dptr); // Write data to EEPROM
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done();
dptr++; // Point to next byte
length--;
}
StopI2C(); // Stop condition
I2C_Done(); // Wait the Stop condition completed
}
//***********************************************
//* EEPROM Acknowledge Polling *
//* -- The routine will polling the ACK *
//* response from EEPROM *
//* -- ACK=0 return *
//* -- ACK=1 send Restart loop check *
//***********************************************
void EEPROM_ACK(unsigned char ctrl)
{
unsigned char i;
StartI2C(); // Start condition
I2C_Done(); // Wait Start condition completed
实例 语言程序 EEPROM 系列 读写 24LCxx PIC: 相关文章:
- 国外现场总线应用实例两则(12-21)
- 三菱PLC编程技巧及实例图解(12-19)
- 单片机的七个创意设计实例(从易到难),附电路图、源代码、演示视频(06-11)
- PCI驱动程序开发实例(02-21)
- 51单片机做信号发生器实例编程(01-16)
- 简单的单片机I/O口扩展实例(05-18)