📄 flash.c
字号:
#include "msp430x14x.h"
#include "def.h"
//程序接口开始
#define SDAH P5DIR|=BIT6;P5OUT|=BIT6;_NOP()
#define SDAL P5DIR|=BIT6;P5OUT&=(~BIT6);_NOP()
#define SDAIN P5DIR&=(~BIT6)
#define SDAport ((P5IN&BIT6)==0)?0:1
#define SCLH P5DIR|=BIT7;P5OUT|=BIT7;_NOP()
#define SCLL P5DIR|=BIT7;P5OUT&=(~BIT7);_NOP()
//程序接口结束
#define SCL BIT7
#define SDA BIT6
extern struct STRWORK work;
void gettimer(void);
void restarttime(void);
void settimer(void);
void readdata(void);
void reflashdata(void);
unsigned char timer_tmp[9];
void ini_iic(void)
{
char i;
//for iic init
P5DIR |= SDA + SCL;
P5OUT |= SDA;
for(i = 0; i < 9; i++)
{
P5OUT &= (~SCL);//clr
P5OUT |= SCL;//set
}
}
///////////////////////////////////////////////////////////////////////////
#define ERRORCOUNT 10
void delayiic(int delayiicCount);
void delayus(int us)
{
int i;
for(i = 0; i < us; i++);
}
unsigned char RW30256(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte);
/*****************以下是对IIC总线的操作子程序***/
/*****************启动总线**********************/
void IICStart(void)
{
SDAH;
SCLH;
SDAL;
SCLL;
}
/*****************停止IIC总线****************/
void IICStop(void)
{
SCLL;
SDAL;
SCLH;
SDAH;
}
/**************检查应答位*******************/
unsigned char IICRecAck(void)
{
unsigned char tmp;
SDAIN;
SCLH;
tmp = SDAport;
SCLL;
return(tmp);
}
/***************对IIC总线产生应答*******************/
void IICAck(void)
{
SDAL;
SCLH;
SCLL;
}
/*****************不对IIC总线产生应答***************/
void IICNoAck(void)
{
SDAH;
SCLH;
SCLL;
}
/*******************向IIC总线写数据*********************/
void IICSendByte(unsigned char sendbyte)
{
unsigned char j ;
for(j = 8; j > 0; j--)
{
SCLL;
if((sendbyte & 0x80) == 0)
{
SDAL;
}
else
{
SDAH;
}
sendbyte<<=1;
SCLH;
}
SCLL;
}
/**********************从IIC总线上读数据子程序**********/
unsigned char IICReceiveByte(void)
{
unsigned char receivebyte, i = 8;
SDAIN;
SCLL;
while(i--)
{
SCLH;
receivebyte = (receivebyte << 1);
receivebyte += SDAport;
SCLL;
}
return(receivebyte);
}
/***************一个简单延时程序************************/
void delayiic(int delayiicCount)
{
while(delayiicCount--);
}
//DataBuff为读写数据输入/输出缓冲区的首址
//ByteQuantity 为要读写数据的字节数量
//Address 为EEPROM的片内地址
//ControlByte 为EEPROM的控制字节,具体形式为(1)(0)(1)(0)(A2)(A1)(A0)(R/W),其中R/W=1,
//表示读操作,R/W=0为写操作,A2,A1,A0为EEPROM的页选或片选地址;
//函数返回值为一个位变量,若返回1表示此次操作失效,0表示操作成功;
/***********************************************************************************/
unsigned char RW30256(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte)
{
unsigned char j, i = ERRORCOUNT;
unsigned char errorflag = 1;
while(i--)
{
IICStart();
IICSendByte(ControlByte & 0xfe);
if(IICRecAck())
continue;
IICSendByte((unsigned char)(Address >> 8));
if(IICRecAck())
continue;
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
if(!(ControlByte & 0x01))
{
j = ByteQuantity;
errorflag = 0; //********clr errorflag
while(j--)
{
IICSendByte(*DataBuff++);
if(!IICRecAck())
continue;
errorflag = 1;
break;
}
if (errorflag == 1)
continue;
break;
}
else
{
IICStart();
IICSendByte(ControlByte);
if(IICRecAck())
continue;
while(--ByteQuantity)
{
*DataBuff++=IICReceiveByte();
IICAck();
}
*DataBuff=IICReceiveByte(); //read last byte data
IICNoAck();
errorflag=0;
break;
}
}
IICStop();
return(errorflag);
}
////////////////////////////////////////////////
unsigned char R_FM30256_RTC(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte)
{
unsigned char i=ERRORCOUNT;
unsigned char errorflag=1;
while(i--)
{
IICStart();
IICSendByte(ControlByte&0xfe);
if(IICRecAck())
continue;
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
IICStart();
IICSendByte(ControlByte);
if(IICRecAck())
continue;
while(--ByteQuantity)
{
*DataBuff++=IICReceiveByte();
IICAck();
}
*DataBuff=IICReceiveByte(); //read last byte data
IICNoAck();
errorflag=0;
break;
}
IICStop();
return(errorflag);
}
unsigned char W_FM30256_RTC(unsigned char data,unsigned int Address,
unsigned char ControlByte)
{
unsigned char i=ERRORCOUNT;
unsigned char errorflag=1;
while(i--)
{
IICStart();
IICSendByte(ControlByte&0xfe);
if(IICRecAck())
continue;
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
errorflag=0; //********clr errorflag
IICSendByte(data);
if(!IICRecAck())
continue;
break; //add 20031125 liu
}
IICStop();
return(errorflag);
}
////////////////////////////////////////////////////////////////////
///////////////// RTC
unsigned char SetFM30256_RTC(unsigned char* DataBuff)//write 1--8 reg
{
unsigned char j=ERRORCOUNT;
unsigned char errorflag=1;
unsigned i=8;
unsigned char* tmpDataBuff;
while(j--)
{
IICStart();
IICSendByte(0xd0);
if(IICRecAck())
continue;
IICSendByte(0x0);
if(IICRecAck())
continue;
IICSendByte(0x02);
if(IICRecAck())
continue;
tmpDataBuff = DataBuff;
errorflag = 0;
while(i--)
{IICSendByte(*tmpDataBuff++);
if(!IICRecAck())
continue;
errorflag = 1;
break;
}
if (errorflag == 1)
continue;
IICStart();
IICSendByte(0xd0);
IICRecAck();
IICSendByte(0x0);
IICRecAck();
IICSendByte(0x0);
IICRecAck();
break;
}
IICStop();
return errorflag;
}
void GetFM30256_RTC(unsigned char *DataBuff)
{
W_FM30256_RTC(1,0,0xd0);
R_FM30256_RTC(DataBuff,9,0,0xd1);
W_FM30256_RTC(0,0,0xd0);
}
void settimer(void)
{
unsigned char timer_tmpp[8];
unsigned char fenTen,fenOne,houeten,shiOne;
houeten=work.shi;
houeten=houeten/10;
shiOne=work.shi-houeten*10;
fenTen=work.fen;
fenTen=fenTen/10;
fenOne=work.fen-fenTen*10;;
timer_tmpp[0]=0;//
timer_tmpp[1]=1;
timer_tmpp[2]=(fenTen<<4)+fenOne;//fen
timer_tmpp[3]=(houeten<<4)+shiOne;//shi
timer_tmpp[4]=1;
timer_tmpp[5]=1;
timer_tmpp[6]=1;
timer_tmpp[7]=1;
SetFM30256_RTC(timer_tmpp);
}
void restarttime(void)
{
unsigned char timer_tmpp[8];
timer_tmpp[0]=0;//
timer_tmpp[1]=1;
timer_tmpp[2]=0;//fen
timer_tmpp[3]=18;//shi
timer_tmpp[4]=1;
timer_tmpp[5]=1;
timer_tmpp[6]=1;
timer_tmpp[7]=1;
SetFM30256_RTC(timer_tmpp);
}
void gettimer(void)
{
unsigned char temp;
GetFM30256_RTC((unsigned char *)timer_tmp);
temp=timer_tmp[2]>>4;
temp=temp*10;
temp=temp+(timer_tmp[2]&0x0f);
timer_tmp[2]=temp;
temp=timer_tmp[3]>>4;
temp=temp*10;
temp=temp+(timer_tmp[3]&0x0f);
timer_tmp[3]=temp;
work.realfen=temp;
temp=timer_tmp[4]>>4;
temp=temp*10;
temp=temp+(timer_tmp[4]&0x0f);
timer_tmp[4]=temp;
work.realshi=temp;
work.RealTime=temp;
work.RealTime=work.RealTime*100+timer_tmp[3];
}
void reflashdata(void)
{
unsigned char dnremw[4];
dnremw[0]=0;
dnremw[1]=work.Iset;
dnremw[2]=work.Tset;
dnremw[3]=0;
RW30256((unsigned char*)dnremw,4,10,WRITE_FM);
}
void readdata(void)
{
unsigned char dnremr[4];
unsigned int temp;
RW30256((unsigned char*)dnremr,4,10,READ_FM);
work.Iset=dnremr[1];
work.Tset=dnremr[2];
temp=work.Iset;
temp=1500/temp;
if(temp<250)work.Isetzqi=temp; //12000/4*work.Iset;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -