PIC:读写24LCxx系列的EEPROM的实例C语言程序

pic:读写24lcxx系列的eeprom的实例c语言程序
//*********************************************************
//* 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 */
#i nclude
#i nclude // load i2c header file from defult direct
#i nclude
#i nclude 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)
{
unsigned char i;
idlei2c(); // ensure module is idle
starti2c(); // start condition
i2c_done(); // wait start condition completed
writei2c(ctrl); // write control to eeprom (write)
i2c_done(); // clear sspif flag
while (sspcon2bits.ackstat) // test for acknowledge from eeprom
{
for (i=0;i<100;i++); // delay for next repet-start
restarti2c(); // initiate repet-start condition
i2c_done(); // wait repet-start condition completed
writei2c(ctrl); // write control to eeprom (write)
i2c_done(); // clear sspif flag
}
stopi2c(); // send stop condition
i2c_done(); // wait until stop condition is over
}
//***********************************************
//* random read a byte from eeprom *
//* - ctrl : control byte of eeprom (write) *
//* (ctrl +1 ) : read command *
//* - addr : address byte of eeprom *
//* - return : read data from eeprom *
//***********************************************
unsigned char eeprom_read(unsigned char ctrl,unsigned char addr)
{
unsigned char f;
idlei2c(); // ensure module is idle
starti2c(); // start condition
i2c_done(); // wait start condition completed
writei2c(ctrl); // write control to eeprom
while(sspcon2bits.ackstat); // test for ack condition, if received
i2c_done(); // clear sspif flag
writei2c(addr); // write address to eeprom
while(sspcon2bits.ackstat); // test for ack condition, if received
i2c_done(); // clear sspif flag
restarti2c(); // initiate restart condition
i2c_done();
writei2c(ctrl+1); // write control to eeprom
while(sspcon2bits.ackstat); // test for ack condition, if received
i2c_done(); // clear sspif flag
f=readi2c(); // enable i2c receiver & wait bf=1 until received data
i2c_done(); // clear sspif flag
notacki2c(); // genarate non_acknowledge to eeprom
i2c_done();
stopi2c(); // send stop condition
i2c_done(); // wait until stop condition is over
return(f); // return data from eeprom
}
//***********************************************
//* sequential read from eeprom
//*
//* - ctrl : control byte of eeprom
//* - addr : location of eeprom
//* - length : read counter
//* - *dptr : store eeprom data to ram
//*
//***********************************************
void ee_sequ_read(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 to eeprom
while(sspcon2bits.ackstat); // test for ack condition, if received
i2c_done(); // clear sspif flag
writei2c(addr); // write address to eeprom
while(sspcon2bits.ackstat); // test for ack condition, if received
i2c_done(); // clear sspif flag
restarti2c(); // initiate restart condition
i2c_done();
writei2c(ctrl+1); // write control to eeprom
while(sspcon2bits.ackstat); // test for ack condition, if received
i2c_done(); // clear sspif flag
while (length!=0)
{
*dptr=readi2c(); // enable i2c receiver & store eeprom data to point buffer
i2c_done();
dptr++;
length--;
if (length==0) notacki2c();
else acki2c(); // continue read next data, send a acknowledge to eeprom
i2c_done();
}
stopi2c(); // send stop condition
i2c_done(); // wait until stop condition is over
}
//***********************************************
//* check i2c action that is completed *
//***********************************************
void i2c_done(void)
{
while (!pir1bits.sspif); // completed the action when the sspif is hi.
pir1bits.sspif=0; // clear sspif
}
//************************************************
//* initial i2c master mode with 7 bits address *
//* clock speed : 100khz @4mhz *
//************************************************
void initialize_i2c_master(void)
{
openi2c(master,slew_on);
sspadd= 9;
}

任何虚拟现实或者模拟现实都离不开现实
奔驰、广汽三菱为消除安全隐患,发布公告共召回汽车42,196辆
数字家庭术语集合
WS51F7030 系列的特点和概括
用于电力电子的LabVIEW调试工具
PIC:读写24LCxx系列的EEPROM的实例C语言程序
IBM Watson的创新功能,旨在帮助企业扩展人工智能的使用
物联网卡有什么模式
基于Linux的步进电机驱动程序设计
互联网结构已从传统接入模式明显发展到了服务器位于边缘的模式
小米11再次在京东、天猫商城开售
方言语音数据在方言语音识别中的关键作用
逆变电源整流模块和监控模块通信功能介绍
生产企业采用自动化视觉检测系统的优势是什么
新能源汽车的快充与慢充接口原理图分析
充电宝什么牌子好,大容量移动电源推荐
MAX2051 Highest linearity, up/
DC/DC开关电源电感下方到底是否铺铜?
数显谷物硬度计的功能特点
LED大屏幕显示屏的显示系统设计