|
#include
#include
typedef unsigned int uint;
typedef unsigned char uchar;
#define KeyPort P1
uchar xdata cw _at_ 0xfcff;
uchar xdata dw _at_ 0xfdff;
uchar xdata cr _at_ 0xfeff;
uint PasswordType[8]={0,0,0,0,0,0,0,0,};
void Set_Password(bit passwordtype);
uint KEY[2];
long dispvalue;
uchar temp;
uchar tmp;
#define hangbit 0x0f
#define liebit 0xf0
#define du while(cr&0x80)
uchar key_value;
sbit SPK = P3^0;
void Password();
#define OP_READ 0xa1 // 器件地址以及读取操作
#define OP_WRITE 0xa0 // 器件地址以及写入操作
#define MAX_ADDR 0x7f // AT24C02最大地址
sbit SDA = P3^4;
sbit SCL = P3^3;
sbit CP = P3^1;
sbit Da = P3^2;
sbit RS = P3^0;
bit flag=0;
sbit LED=P3^5;
void start();
void stop();
unsigned char shin();
bit shout(unsigned char write_data);
unsigned char read_random(unsigned char random_addr);
void write_byte( unsigned char addr, unsigned char write_data);
void delayms(unsigned char ms);
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
void start()
// 开始位
{
SDA = 1;
SCL = 1;
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
}
void stop()
// 停止位
{
SDA = 0;
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
}
unsigned char shin()
// 从AT24Cxx移入数据到MCU
{
unsigned char i,read_data;
for(i = 0; i < 8; i++)
{
SCL = 1;
read_data <<= 1;
read_data |= (unsigned char)SDA;
SCL = 0;
}
return(read_data);
}
bit shout(unsigned char write_data)
// 从MCU移出数据到AT24Cxx
{
unsigned char i;
bit ack_bit;
for(i = 0; i < 8; i++) // 循环移入8个位
{
SDA = (bit)(write_data & 0x80);
_nop_();
SCL = 1;
_nop_();
_nop_();
SCL = 0;
write_data <<= 1;
}
SDA = 1; // 读取应答
_nop_();
|
|