📄 rwe2rom.c
字号:
/**********************************************************
* 读写E2PROM 读出系统设定值,包括检测项目,默认的上下限等
*
* E2中的存储格式:BOM 号,()
********************************************************/
//#include <msp430x11x.h>
#include "msp430x14x.h"
#include "head.h"
//unsigned char ReadWord;
/*
define SCLIN P2IN * P6 input register P0IN *
#define SDAIN P2IN ******* * P6 input register P0IN *
#define SCLDAT P2OUT P6 OUT register address *
#define SDADAT P2OUT ****** * P6 output register P0OUT *
#define SCLEN P2DIR ***** * P6 DIR register address *
#define SDAEN P2DIR ******
*/
void nop(void)
{
_NOP();
_NOP();
_NOP();
}
void start(void) //set Start Condition CLK hight,DOUT from High to Low
{
SCLEN &= (~(SDA+SCL)); //BIC.B #SDA+SCL,&SCLEN ;SCL, SDA=high
_NOP();
_NOP();
_NOP();
SDAEN |= SDA; //BIS.B #SDA,&SDAEN
SCLEN |= SCL; //BIS.B #SCL,&SCLEN
return;
}
void stop(void)
{
SDAEN |= SDA; //BIS.B #SDA,&SDAEN
SCLEN &= (~SCL); //BIC.B #SCL,&SCLEN
SDAEN &= (~SDA); //;BIC.B #SDA,&SDAEN
return;
}
//void ControlByte(unsigned int *dataadr,unsigned int *e2adr)
//取E2 地址 高字节,左移一位,与上OXOE,加上0A0
//unsigned char *ControlByte(unsigned char *dataadr,unsigned char *e2adr)
unsigned char ControlByte(int AddressByte)
{
unsigned char temp;
_BIC_SR(0X0001); //CLEAR SR 'C'
temp = (char)(AddressByte>>8);
temp <<= 1 ;
temp &= 0X0E;
temp += Code;
return temp;
}
//Write data to P2PORT return 0 if right,if error,return 1
//; Shift out a byte to the EEPROM, most significant bit first.
//; SCL, SDA expected low on entry. Return with SCL low.
//; Called with data to send in I2CDATA.
int shout( unsigned char data )
{
unsigned char mask,i,temp;
unsigned char Value;
Value = data;
mask=0X80;
for (i=0;i<=7;i++){
temp = (Value & mask); // temp=((*p) & (mask));
if( temp !=0 ){ //
SDAEN &= (~SDA); //BIC.B #SDA,&SDAEN ; SDA = ’H’
SCLEN &= (~SCL); //BIC.B #SCL,&SCLEN ; SCL = ’H’
if( !( SDAIN & SDA ) ){ // BIT.B #SDA,&SDAIN 非0时候C置位
return 1;
}
}
else {
SDAEN |= SDA ; //BIS.B #SDA,&SDAEN ; SDA = ’L’
SCLEN &= (~SCL); //BIC.B #SCL,&SCLEN ; SCL = ’H’
}
//_BIC_SR(0X0001); 'C' CLEAR
mask>>=1;
// mask <<= 1;
SCLEN |= SCL; // BIS.B #SCL,&SCLEN ; SCL =L
}
SDAEN &= (~SDA); //BIC.B #SDA,&SDAEN ; Acknoledge bit
SCLEN &= (~SCL); //BIC.B #SCL,&SCLEN ; raise clock
/* if ( SDA & (*(unsigned char*)(SDAIN)) ){ //BIT.B #SDA,&SDAIN ; Read Ack bit to carry
return 1;
}*/
if ( !(SDAIN & SDA )){
// return 1;
}
SCLEN |= SCL;
return 0;
}
// 写 Number 个字节到 E2 中
//int WriteBlock(int number,unsigned int *dataadr,unsigned int *e2adr)
int WriteBlock(int number,unsigned char *dataadr,unsigned char *e2adr)
{
unsigned char *p1;
int i;
unsigned char temp;
unsigned int aaa;
//unsigned char *temp1;
_DINT(); //Disable Interrupt
SDADAT &= (~(SCL+SDA)); //Clear CLK AND DATA OutPut Buffer
// BIC.B #SCL+SDA,&SDADAT ; SCL and SDA output buffer low
start(); //开始
aaa = (int)(e2adr );
temp = ControlByte(aaa);
shout(temp);
shout( (int)(e2adr));
// shout( 0); // shout( ( unsigned char*)e2adr );
/* temp = *(unsigned char *)e2adr;
shout( temp ); // shout( ( unsigned char*)e2adr );
*/
p1 = dataadr; // p1=(unsigned char*)(*dataadr); // p1=(unsigned int*)(0x200);
for (i=1;i<=8;i++) {
//if ( shout( p1 ) ) return 1; //Set Device Address
shout(*p1);
p1++;
}
stop(); //Stop
_EINT(); //Enable Interrupt
//Write a block use the address and back..
return 0; //if error return 1 right return 0
}
// 写N * 8个字节
int WriteN8Byte(int Number)
{
unsigned char *e2adr,*dataadr;
unsigned int i,j;
//int WriteBlock(int number,unsigned char *dataadr,unsigned char *e2adr)
dataadr = (char*)0x200;
e2adr = (char*)0x00;
for( i=1;i<=Number;i++){
WriteBlock(8,dataadr,e2adr);
dataadr += 8;
e2adr += 8;
for (j=0;j<=3333;j++); //延迟 10ms
}
return 0;
}
//; Clock out an acknowledge bit (low).
//; SCL expected low on entry. Return with SCL, SDA low.
void Ack(void)
{
SDAEN |= SDA;
SCLEN &= ~SCL;
SCLEN |= SCL;
}
//; Clock out a negative acknowledge bit (high).
//; SCL expected low on entry. Return with SCL low, SDA high.
void Nak(void)
{
SDAEN &= ~SDA; //; NAK bit
SCLEN &= ~SCL; //; raise clock
SCLEN |= SCL; //; drop clock
}
//由E2 中 读8位 一个字节 数据
unsigned char shin(void)
{
int i;
unsigned char temp;
unsigned char Mask;
SDAEN &= ~SDA; // make SDA an input
temp =0;
//读8位数据
for( i=1;i<=8;i++) {
SCLEN &= ~SCL; //SCL = H
Mask = 0;
if (SDAIN &SDA)
Mask = 1;
else
Mask =0;
// Mask = SDAIN & SDA;
Mask <<= (8 - i);
temp ^= Mask;
SCLEN |= SCL;
}
return temp;
}
// 24c256 512 页 64字节 14-15bit Word Address!!!
//**********************************************************************
// 读取N 个字节 到RAM 中
// 参数说明: number 读取的个数,dataadr,读到RAM 的地址
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -