资讯详情

SLE4442卡_IC的51单片机驱动程序

整理了最初的实验草稿版,将端口宏定义,函数声明以及常用的函数声明建立头文件,感觉清楚多了,在不断地修改中凝练,在不停的实践中提高,满眼的思绪,在小小的Readme中划过一笔~~~~~~~~ char data_R[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个BY 可用后面的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) //初始化 { SD1 = 0; RD1 = 0; //上电 TR4 = 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); }

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++; } }

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++; } }

}

-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台