char data_RST[4], ErrorCount[4];
void IC_RST(void)
{
uchar value,i,count;
DATA_IN; //IC输入
RST_LOW; //复位时序
CLK_LOW;
DATA_HIGH;
DelayUs(5);
RST_HIGH;
DelayUs(5);
CLK_HIGH;
DelayUs(5);
CLK_LOW;
DelayUs(5);
RST_LOW;
DelayUs(2);
for(i=0;i<4;i++) //读4个BYTE 可用后面的Byte_Read()代替
{
value = 0xff;
for(count=0;count<8;count++)
{
value = value >> 1;
DelayUs(2);
CLK_LOW;
DelayUs(2);
CLK_HIGH;
DelayUs(2);
if(RD5 == 1)
{
value |= 0x80; //判断IO脚是否为1,是则位置1
}
else
{
value &= 0x7f; //否则位置0
}
DelayUs(2);
}
data_RST[i] = value;
DelayUs(2);
}
DelayUs(2);
CLK_LOW;
DelayUs(2);
DATA_HIGH;
}
void IC_Init(void) //初始化
{
TRISD1 = 0;
RD1 = 0; //上电
TRISD4 = 0; //时钟输出
TRISD3 = 0; //RST输出
DelayMs(5); //上电的必要延时,否则程序出错
}
void Start(void)
{
DATA_OUT; //开始时序
CLK_LOW;
DATA_HIGH;
DelayUs(2);
CLK_HIGH;
DelayUs(2);
DATA_LOW;
DelayUs(2);
CLK_LOW;
}
void Stop(void) //结束时序
{
DATA_OUT;
CLK_LOW;
NOP();
NOP();
DATA_LOW;
DelayUs(2);
CLK_HIGH;
DelayUs(2);
DATA_HIGH;
DelayUs(2);
}
uchar Byte_Read(void) //读字节
{
uchar count;
uchar value;
DATA_IN;
DelayUs(2);
value = 0xff;
for(count=0;count<8;count++)
{
value = value >> 1; //循环右移,从最低位开始读
DelayUs(2);
CLK_LOW;
DelayUs(2);
CLK_HIGH;
DelayUs(2);
if(RD5 == 1)
{
value |= 0x80;//判断IO脚是否为1
}
else
{
value &= 0x7f;
}
DelayUs(2);
}
return (value);
}
[page]
void Byte_WRT(uchar Xdata) //写字节
{
uchar count;
DATA_OUT;
DelayUs(2);
for(count=8;count!=0;count--)
{
CLK_LOW;
DelayUs(2);
if((Xdata)&0x01)
{
DATA_HIGH;
}
else
{
DATA_LOW;
}
DelayUs(2);
CLK_HIGH;
DelayUs(2);
Xdata = Xdata >> 1; //循环右移,从最低位开始写
}
}
void Command(uchar command,uchar address,uchar IC_data)
{
Start();
Byte_WRT(command); //发送命令
Byte_WRT(address); //发送地址
Byte_WRT(IC_data); //发送数据
Stop(); //操作命令结束
}
void Process(void)
{
uint j; //写指令后的处理过程
DATA_OUT;
DelayUs(2);
CLK_LOW;
DATA_LOW;
DelayUs(2);
for(j = 0;j < 255;j++)
{
CLK_HIGH;
DelayUs(2);
CLK_LOW;
DelayUs(2);
}
DATA_HIGH;
}
void Process2(void)
{
uint j; //写指令后的短处理过程
DATA_OUT;
DelayUs(2);
CLK_LOW;
DATA_LOW;
DelayUs(2);
for(j = 0;j < 2;j++)
{
CLK_HIGH;
DelayUs(2);
CLK_LOW;
DelayUs(2);
}
DATA_HIGH;
}
uchar Code_Check(uchar Code1,uchar Code2,uchar Code3) //密码校验函数
{
uchar i;
Command(0x31,0x00,0x00);
for(i = 0;i < 4;i++)
{
ErrorCount[i] = Byte_Read();
}
if(ErrorCount[0] == 0)
{
return 0;
}
else
{
if((ErrorCount[0]&0x01) == 1)
{
ErrorCount[0] &= 0x06; //bit0=0;
}
else if((ErrorCount[0]&0x02) == 1)
{
ErrorCount[0] &= 0x05; //bit1=0;
}
else
{
ErrorCount[0] &= 0x03; //bit2=0
}
}
Command(0x39,0x00,ErrorCount[0]);
Process();
Command(0x33,0x01,Code1);
Process2();
Command(0x33,0x02,Code2);
Process2();
Command(0x33,0x03,Code3);
Process2();
Command(0x39,0x00,0xff);
Process();
Command(0x31,0x00,0x00);
for(i = 0;i < 4;i ++)
{
ErrorCount[i] = Byte_Read();
}
if(ErrorCount[0] == 0x07)
{
return 1;
}
else
{
return 0;
}
}
void ReadMainROM(uchar addr,uchar *p,uchar N) //读主存区
{
Command(0x30,addr,0xff);
while(N--)
{
*p=Byte_Read();
p++;
}
}
[page]
void WriteMainROM(uchar addr,uchar *t,uchar N) //写主存区
{
while(N--)
{
Command(0x38,addr,*t);
Process();
addr++;
t++;
}
}
void ReadProROM(uchar addr,uchar *p,uchar N) //读保护区
{
Command(0x34,addr,0xff);
while(N--)
{
*p=Byte_Read();
p++;
}
}
void WriteProROM(uchar addr,uchar *t,uchar N) //写保护区
{
while(N--)
{
Command(0x3c,addr,*t);
Process();
addr++;
t++;
}
}
void ReadCode(uchar addr,uchar *p,uchar N) //读密码
{
Command(0x31,addr,0xff);
while(N--)
{
*p=Byte_Read();
p++;
}
}
void WriteCode(uchar addr,uchar *t,uchar N) //写密码
{
while(N--)
{
Command(0x39,addr,*t);
Process();
addr++;
t++;
}
}
}
上一篇:T6963C的51单片机驱动程序
下一篇:51单片机控制P2口8位LED灯左移C语言程序
推荐阅读最新更新时间:2024-03-16 13:59