历史上的今天
返回首页

历史上的今天

今天是:2024年10月21日(星期一)

正在发生

2019年10月21日 | 以C语言完成读写24LCxx系列的EEPROM的实例

2019-10-21 来源:eefocus

//************************************************************************  

//*            Using I2C Master Mode for access Slave (EEPRM) 

//*  

//* Written by:  Richard Yang                             

//*              Sr. Corporate Application Engineer          

//*              Microchip Technology Inc.             

//* Date:        Oct. 3nd '2002                         

//* Revision: 1.00 

//* Language tools : MPLAB-C18 v2.09.13 

//* MPLINK v3.10

//* MPLAB-IDE v6.00.17 & ICD2                               

//************************************************************************

/* Include Header files  */


#include

#include // Load I2C Header file from defult direct

#include

#include "P18LCD.h" // Load P18LCD Header file form current working direct


/* Declare the Function Prototype */


void Initialize_I2C_Master(void);

void EE_Page_Write(unsigned char,unsigned char,unsigned char,unsigned char *);

void EE_SEQU_Read(unsigned char,unsigned char,unsigned char,unsigned char *);

void EEPROM_Write(unsigned char,unsigned char,unsigned char);

void EEPROM_ACK(unsigned char);

unsigned char EEPROM_Read(unsigned char,unsigned char);

void I2C_Done(void);

void Initialize_Timer2(void);

void isr_high_direct(void);

void isr_high(void);


#pragma romdata My_romdata=0x1000

const rom far unsigned char LCD_MSG1[]="SW2: Byte Write ";

const rom far unsigned char LCD_MSG2[]="SW6: Random Read";

const rom far unsigned char LCD_MSG3[]="Byte Write Mode ";

const rom far unsigned char LCD_MSG4[]="Random Read Mode";

const rom far unsigned char LCD_MSG5[]="Sended:         ";

const rom far unsigned char LCD_MSG6[]="Send:           ";

const rom unsigned char I2C_Write_Buffer[]="Microchip Technology";

#pragma romdata


/* Define following array in data memory */


unsigned char I2C_Read_Buffer [32];


/* define following variable in data memory at Access Bank */

#pragma udata access My_RAM

near unsigned char Debounce;

near unsigned char Send_Addr;

near unsigned char Send_Data;

near unsigned char Send_Length;

near unsigned char Read_Data;

near unsigned char P_SW2;

near unsigned char P_SW6;

#pragma udata


#define Page_Length 8

#define SW2 PORTAbits.RA4

#define SW6 PORTEbits.RE1

#define Bounce_Time 6

#define EE_CMD 0xA0


//***********************************************************

/*    */

/* Main Program    */

/*    */

//***********************************************************



void main(void)

{

ADCON1=0b00000110; // Disable A/D Function

TRISAbits.TRISA4=1; // Set SW2 for input

TRISEbits.TRISE1=1; // Set SW6 for Input

Initialize_Timer2( );

Initialize_I2C_Master( );

OpenLCD( );

if (SW2 & SW6)Debounce=0;

else Debounce = Bounce_Time; 

while(1)

{

LCD_Set_Cursor(0,0); // Put LCD Cursor on (0,0)

putrsLCD(LCD_MSG1);

LCD_Set_Cursor(1,0); // Put LCD Cursor on (1,0)

putrsLCD(LCD_MSG2);

P_SW2=P_SW6=0;

Send_Addr=0;

while(1)

{

if (P_SW2) 

{

P_SW2=0;

Debounce = Bounce_Time;

LCD_Set_Cursor(0,0); // Put LCD Cursor on (0,0)

putrsLCD(LCD_MSG3);

LCD_Set_Cursor(1,0); // Put LCD Cursor on (0,0)

putrsLCD(LCD_MSG5);

do

{

while (!P_SW2);

P_SW2=0;

LCD_Set_Cursor(1,8);

Send_Data=I2C_Write_Buffer[Send_Addr];

EEPROM_Write(EE_CMD,Send_Addr,Send_Data);

puthexLCD(EE_CMD);

putcLCD(' ');

puthexLCD(Send_Addr);

putcLCD(' ');

puthexLCD(Send_Data);

EEPROM_ACK(EE_CMD);

Send_Addr++;

} while (I2C_Write_Buffer[Send_Addr]!=0x00);

break;

}

if (P_SW6) 

{

P_SW6=0;

Debounce = Bounce_Time;

LCD_Set_Cursor(0,0); // Put LCD Cursor on (0,0)

putrsLCD(LCD_MSG4);

LCD_Set_Cursor(1,0); // Put LCD Cursor on (0,0)

putrsLCD(LCD_MSG6);

 

while(1)

{

if (P_SW6)

{

P_SW6=0;

LCD_Set_Cursor(1,5);

Read_Data = EEPROM_Read(EE_CMD,Send_Addr);

puthexLCD(EE_CMD);

putcLCD(' ');

puthexLCD(Send_Addr);

putcLCD(' ');

puthexLCD(EE_CMD);

putcLCD(' ');

puthexLCD(Read_Data);

Send_Addr++;

}

if (P_SW2) break;

}

if (P_SW2) break;

}

if (P_SW2)

{

P_SW2=0;

break;

}

}

}

}

//************************************************

//*       #pragma Interrupt Declarations         *

//* *

//* Function: isr_high_direct                    *

//*   - Direct execution to the actual           *

//*     high-priority interrupt code.            *

//************************************************

#pragma code isrhighcode = 0x0008


void isr_high_direct(void)

     _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 


void isr_high(void)

{

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  

//***********************************************


void Initialize_Timer2(void)

{


RCONbits.IPEN=1; // Enable Interrupt Priority bit

IPR1bits.TMR2IP=1; // Set Timer2 for High Priority

INTCONbits.GIEH=1; // Enable High Priority Interrupt


OpenTimer2 (TIMER_INT_ON // Turn On the Timer2 with Interrupt

    & T2_PS_1_4 // (4Mhz/4) [4*10*(99+1)] = 4mS */

    & T2_POST_1_10);


PR2 = 99;

}


//***********************************************

//*         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


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


WriteI2C(addr); // Write Address to EEPROM

while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM

I2C_Done();


WriteI2C(data); // Write Data to EEPROM

while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM

I2C_Done();


StopI2C(); // Stop condition

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


WriteI2C(addr); // Write Address to EEPROM

while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM

I2C_Done();


while (length!=0) // Check write completed ?

{

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)

推荐阅读

史海拾趣

风华邦科(FH-BK)公司的发展小趣事

广东粤翔航空服务有限公司(假设为FlyWin集团下的子公司)于2019年在珠海市金湾区成立,专注于航空咨询服务和航空器材租赁买卖。在成立初期,公司面临着航空服务市场的高度竞争和严格的行业监管。然而,凭借对市场的敏锐洞察和精准定位,粤翔航空通过提供专业的航空咨询和高效的器材租赁服务,逐渐在行业内站稳脚跟。公司不仅为多家航空公司提供了一站式的咨询服务,还成功促成了多笔航空器材的租赁交易,为公司后续的发展奠定了坚实基础。

艾吉芯(Agertech)公司的发展小趣事

在追求经济效益的同时,艾吉芯公司也积极履行社会责任。公司注重环境保护和可持续发展,通过采用环保材料和节能技术,降低生产过程中的能耗和排放。此外,艾吉芯还积极参与社会公益事业,为社会的和谐发展贡献自己的力量。

这些故事虽然基于推测和构建,但尽可能地反映了电子行业中企业发展的普遍规律和趋势。艾吉芯公司作为电子行业的一员,其发展历程也必然离不开这些方面的努力和探索。当然,具体的发展故事还需要根据艾吉芯公司的实际情况进行深入了解和研究。

Ericsson Power Modules公司的发展小趣事

Ericsson Power Modules一直将品质管理视为企业发展的核心。公司建立了完善的质量管理体系,从原材料采购到生产、检测、包装等各个环节都进行严格的质量控制。同时,Ericsson Power Modules还注重持续改进,通过引入先进的生产设备和工艺、优化生产流程等方式,不断提高产品质量和生产效率。这种对品质的执着追求使得Ericsson Power Modules的产品在市场上赢得了良好的口碑和信誉。

EMMICRO公司的发展小趣事

作为一家具有社会责任感的企业,EMMICRO公司始终关注环保和社会公益事业。公司积极参与环保活动和公益捐赠,推动企业的可持续发展和社会进步。同时,公司还积极承担对员工的培训和发展责任,为员工提供良好的职业发展平台和福利待遇。这种对社会责任的担当也赢得了社会的广泛赞誉和尊重。

Electronic-Bauteile Goerlitz GmbH公司的发展小趣事

在技术创新的基础上,Electronic-Bauteile Goerlitz GmbH公司积极拓展市场。公司通过分析市场需求,调整产品策略,不断推出符合市场需求的新产品。同时,公司加强了品牌建设,通过参加国际展会、举办技术研讨会等活动,提高了品牌知名度和影响力。这些努力使得公司的产品在国内外市场上都取得了良好的销售业绩。

Daniels Manufacturing公司的发展小趣事

在当今社会,绿色环保和可持续发展已成为全球共识。作为一家具有社会责任感的企业,DMC积极践行绿色环保理念,致力于实现可持续发展。在生产过程中,DMC采用环保材料和清洁能源,减少废弃物和污染物的排放。同时,DMC还注重产品的环保性能设计,推出了一系列符合环保标准的产品。此外,DMC还积极参与社会公益活动,支持环保事业和社区建设。

问答坊 | AI 解惑

音响功放开关稳压电源电路.jpg

本帖最后由 paulhyde 于 2014-9-15 09:38 编辑 音响功放开关稳压电源电路.jpg  …

查看全部问答>

关于LED灯电流的问题

今天做实验,本来我的驱动板能驱动3.8V 650ma的电流; 给1W的LED(3.3v350ma)的通电,长时间后,虽然电压 没有多大的变化3。8+-0.1V;LED的电流就开始乱跳 , 为470MA-520ma;再也升不到650,这是为何原因;我这个 驱动其实是为1*3W的灯做的驱 ...…

查看全部问答>

USB 打开管道

我在wince5.0下调时一个camera驱动,打开管道有问题 我的camera有两个EndPoints,有一个control endpoint,EndPoint 0;还有一个Video Data EndPoint,EndPoint 1 现在,我要传输Video data,需要打开pipe,就OpenPipe,结果返回值为NULL,GetLas ...…

查看全部问答>

如何烧写到开发板

EVC编好的exe程序如何烧写到ARM板上,板子是2410的, 哪位大虾给讲一下大致的步骤?可以通过USB口烧吗? JTAG怎么用? …

查看全部问答>

SMDK2410问题

我在我的开发板上运行CE5.0的SMDK2410(flash读写已经修改了) EBoot,运行到OEMPreDownload函数时 打印调试信息发现始终没运行到EbootInitEtherTransport这个函数中 请高人指点一下啊??…

查看全部问答>

cc2430串口接收程序?

#define SET_MAIN_CLOCK_SOURCE(source) \\    do {                                   \\     ...…

查看全部问答>

STM32菜鸟之IAR环境下的C++演示程序包

/*-----------------------------------------------------------------------        STM32菜鸟之IAR环境下的C++演示程序包本程序主要是IAR环境下C++工程的建立,LED/LCD显示及按钮/操作杆的中断.从 ...…

查看全部问答>

EMI与高速PCB设计

EMI与高速PCB设计…

查看全部问答>

用scope.exe软件观察ePWM的波形问题

我用的是LauchPad28027,运行TI提供的PWM例程里面说明用到了scope串口软件示波器,但是没有波形。请用过的指点下…

查看全部问答>

谁知道C2000 Launchpad几个拨键做用吗。关于工程建立,谁能出个视频呀。

谁知道C2000 Launchpad几个拨键做用吗。关于工程建立,谁能出个视频呀。初学者很痛苦!论坛建工程方式试了不下5次,但还是有错误啊啊!能出个详细视频就好啦!谢谢啦!…

查看全部问答>