#include <intrins.h>
#include "..\lib\n76e003.h"
#include "..\lib\my.h"
unsigned char xdata UIDBuf[12]; //存储96bit的UID内容
//读出芯片的96bit的UID
void ReadUID(void)
{
unsigned char ch;
TA = 0xaa;
TA = 0x55;
CHPCON |= 0x01; //使能IAP
for (ch=0; ch<0x0c; ch++)
{
IAPAH = 0x00;
IAPAL = ch; //0x02;
IAPCN = 0X04;
TA = 0xaa;
TA = 0x55;
IAPTRG |= 0x01;
UIDBuf[ch] = IAPFD;
}
// ch = IAPFD;
// TA = 0xaa;
// TA = 0x55;
// IAPTRG |= 0x01;
/* TA = 0xaa;
TA = 0x55;
IAPUEN |= 0x01; //选择要写入的区域 BIT2 -- 1 使能IAP擦除或编程CONFIG BIT1 -- 1 使能IAP擦除或编程LDROM BIT0 -- 1 使能IAP擦除或编程APROM
IAPCN = 0x22;
IAPAH = 0x10;
IAPAL = 0x00;
IAPFD = 0xff;
TA = 0xaa;
TA = 0x55;
IAPTRG |= 0x01;
IAPCN = 0x21;
IAPAH = 0x10;
IAPAL = 0x00;
IAPFD = 0xaa;
TA = 0xaa;
TA = 0x55;
IAPTRG |= 0x01;
TA = 0xaa;
TA = 0x55;
IAPUEN &= ~0x01;*/
TA = 0xaa;
TA = 0x55;
CHPCON &= ~0x01;
}
bit CompUID(unsigned char code *cp_r)
{
unsigned char code *cp;
unsigned char xdata *xp;
unsigned char xdata tmpx[128];
unsigned char loop;
cp = cp_r; //&UID1;
xp = &tmpx;
for (loop=0,B=12; ; )
{
if (*cp == 0xff) loop++;
cp++;
B--;
if (B==0) break;
}
cp = cp_r; //&UID1;
if (loop>5) //没有写入UID,写入UID
{
for (loop=0; loop<12;loop++)
{
*xp = UIDBuf[loop];
xp++;
cp++;
}
for ( ; loop<128; )
{
*xp = *cp;
loop++;
xp++;
cp++;
}
TA = 0xaa;
TA = 0x55;
CHPCON |= 0x01; //使能IAP
// TA = 0xaa;
// TA = 0x55;
// IAPTRG |= 0x01;
TA = 0xaa;
TA = 0x55;
IAPUEN |= 0x01; //选择要写入的区域 BIT2 -- 1 使能IAP擦除或编程CONFIG BIT1 -- 1 使能IAP擦除或编程LDROM BIT0 -- 1 使能IAP擦除或编程APROM
IAPCN = 0x22; //APRROM擦除
IAPAH = (unsigned char)(((unsigned int)cp_r)/256); //0x10;
IAPAL = (unsigned char)cp_r; //0x00;
IAPFD = 0xff;
TA = 0xaa;
TA = 0x55;
IAPTRG |= 0x01; //执行IAP
//APROM写入
xp = &tmpx;
for (loop=0; loop<128; loop++)
{
IAPCN = 0x21;
IAPAH = (unsigned char)(((unsigned int)cp_r)/256); //0x10;
IAPAL = loop; //0x00;
IAPFD = *xp; //0xaa;
xp++;
TA = 0xaa;
TA = 0x55;
IAPTRG |= 0x01;
}
TA = 0xaa;
TA = 0x55;
IAPUEN &= ~0x01;
TA = 0xaa;
TA = 0x55;
CHPCON &= ~0x01;
return(TRUE);
}
else //比较UID
{
cp = cp_r; //&UID1;
xp = &UIDBuf;
for (B=12; ; )
{
if(*xp != *cp) return(FALSE);
B--;
if (B==0) break;
}
return(TRUE);
}
}
|