📄 x1228rw.h
字号:
//0xDE 写ccr阵列, 0xDF 读ccr阵列
//0xAE 写eeprom阵列,0xAF 读eeprom阵列 (0x0000---0x1FF)共512个字节
//P5.7--SCL BIT7
//P5.6--SDA BIT6
//****
//================================================================================================================
void w128xl(unsigned char sjdz,unsigned char zdz1,unsigned char zdz0,unsigned char *w128tp,unsigned char w128zjs);
void r128xl(unsigned char sjdz,unsigned char zdz1,unsigned char zdz0,unsigned char *r128tp,unsigned char r128zjs);
//================================================================================================================
//****
void w_ack(void);
void start();
void stop();
unsigned char r_ack(void) ;
void w1_byte(unsigned char w128_dat);
unsigned char r1_byte(void);
//==========================================================================
//
void start()
{
P5DIR|=BIT6; //SDA输出功能
P5OUT|=BIT7; //SCL 1
P5OUT&=~BIT6; //SDA 1->0
P5OUT&=~BIT7; //SCL 0
}
//
//
void stop()
{
P5DIR|=BIT6; //SDA输出功能
P5OUT&=~BIT7; //SCL 0
P5OUT&=~BIT6; //SDA 0
P5OUT|=BIT7; //SCL 1
P5OUT|=BIT6; //SDA 0->1
}
//============================================================================
void w_ack(void)
{
P5DIR|=BIT6; //SDA输出功能
P5OUT&=~BIT6; //SDA 0
P5OUT|=BIT7; //SCL 1
P5OUT&=~BIT7; //SCL 0
}
//==============================================================================
//
void w1_byte(unsigned char w128_dat)
{
unsigned char w_bit;
P5DIR|=BIT6; //SDA输出功能
for(w_bit=8;w_bit>0;w_bit--)
{
if(w128_dat&BIT7)
{
P5OUT|=BIT6; //SDA 1
P5OUT|=BIT7; //SCL 1
P5OUT&=~BIT7; //SCL 0
}
else
{
P5OUT&=~BIT6; //SDA0
P5OUT|=BIT7; //SCL 1
P5OUT&=~BIT7; //SCL 0
}
w128_dat=w128_dat<<1;
}
P5OUT|=BIT6; //SDA 1 释放SDA总线
}
//=============================================================================
//读从机是否有应答
unsigned char r_ack(void)
{
P5DIR&=~BIT6; //SDA输入功能
P5OUT|=BIT7; //SCL 1
if(P5IN&BIT6)
{
P5OUT&=~BIT7; //SCL 0
return(1); //无应答返回1
}
else
{
P5OUT&=~BIT7; //SCL 0
return(0); //有应答返回0
}
}
//===============================================================================================================
void w128xl(unsigned char sjdz,unsigned char zdz1,unsigned char zdz0,unsigned char *w128tp,unsigned char w128zjs)
{
loop_cx0: start();
w1_byte(0xde);
if(r_ack())goto loop_cx0;
loop_cx1: w1_byte(0x00);
if(r_ack())goto loop_cx1;
loop_cx2: w1_byte(0x3f);
if(r_ack())goto loop_cx2;
loop_cx3: w1_byte(0x02);
if(r_ack())goto loop_cx3;
stop();
loop_cx4: start();
w1_byte(0xde);
if(r_ack())goto loop_cx4;
loop_cx5: w1_byte(0x00);
if(r_ack())goto loop_cx5;
loop_cx6: w1_byte(0x3f);
if(r_ack())goto loop_cx6;
loop_cx7: w1_byte(0x06);
if(r_ack())goto loop_cx7;
stop();
loop_cx8: start();
w1_byte(sjdz);
if(r_ack())goto loop_cx8;
loop_cx9: w1_byte(zdz1);
if(r_ack())goto loop_cx9;
loop_cx10: w1_byte(zdz0);
if(r_ack())goto loop_cx10;
for(;w128zjs>0;w128zjs--)
{
loop_cx11: w1_byte(*w128tp);
if(r_ack())goto loop_cx11;
w128tp++;
}
stop();
}
//=============================================================================
unsigned char r1_byte(void)
{
unsigned char r_bit,i;
P5DIR&=~BIT6; //SDA选择输入功能
for(r_bit=8;r_bit>0;r_bit--)
{
i=i<<1;
P5OUT|=BIT7; //SCL 1
if(P5IN&BIT6)
{
i|=BIT0;
}
else
{
i&=~BIT0;
}
P5OUT&=~BIT7; //SCL 0
}
return(i);
}
//============================================================================
void r128xl(unsigned char sjdz,unsigned char zdz1,unsigned char zdz0,unsigned char *r128tp,unsigned char r128zjs)
{
if(sjdz==0xdf) //相等读CCR阵列
{
loop_cx0: start();
w1_byte(0xde);
if(r_ack())goto loop_cx0;
}
if(sjdz==0xaf) //相等读EEPROM
{
loop_cx1: start();
w1_byte(0xae);
if(r_ack())goto loop_cx1;
}
loop_cx2: w1_byte(zdz1);
if(r_ack())goto loop_cx2;
loop_cx3: w1_byte(zdz0);
if(r_ack())goto loop_cx3;
loop_cx4: start();
w1_byte(sjdz);
if(r_ack())goto loop_cx4;
for(;r128zjs>0;r128zjs--)
{
*r128tp=r1_byte();
if(r128zjs==1)
{
stop(); //主机最后一个字节收到后直接发送停止
}
else
{
w_ack(); //主机回应答
r128tp++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -