?? i2cwr1.c
字號:
#include "reg52.h"
#include <stdio.h>
#include <intrins.h>
sbit SCL = P1 ^ 6;
sbit SDA = P1 ^ 7;
struct buf {
unsigned char command, addressh, addressl, rdata;
} data buf_IIC;
void send_start()
{
SCL = 0;
SDA = 1;
SCL = 1;
SCL = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
void send_stop()
{
SDA = 0;SDA = 0;SCL = 0;SCL = 0;
SCL = 1;SCL = 1;SDA = 1;SDA = 1;
}
void i2c_init()
{
SCL = 0;
send_stop();
}
bit i2c_clock()
{
bit sm;
SCL = 1;SCL = 1;
sm = SDA;
SCL = 0;SCL = 0;
return(sm);
}
void flashA()
{ bit NOP;
NOP=~NOP; NOP=~NOP; NOP=~NOP; NOP=~NOP; NOP=~NOP; }
void I2C_Ack(bit a)
{
if(a==0)SDA=0;
else SDA=1;
flashA();
SCL=1;
flashA();
flashA();
SCL=0;
flashA();
}
bit get_bit(unsigned char my_data, n)
{
bit bdata byte_bit;
unsigned char x;
x = (my_data >>= n);
byte_bit = x & 0x01;
return(byte_bit);
}
void send_command()
{
char i;
for (i = 0; i < 8; i++) {
SDA = get_bit(buf_IIC.command, 7 - i);
i2c_clock();
};
}
void send_address()
{
char i;
bit get_ack();
for (i = 7; i >= 0; i--) {
SDA = get_bit(buf_IIC.addressh, i);
i2c_clock();
};
get_ack();
for (i = 7; i >= 0; i--) {
SDA = get_bit(buf_IIC.addressl, i);
i2c_clock();
};
}
delay(unsigned char mm10)
{
unsigned char i,j;
for (j = 1; j <= mm10; j++) {
for (i = 0; i <= 108; i++) {
_nop_();
_nop_();
};
};
}
/*IIC總線發送一個字節*/
void IIC_send_data()
{
char i;
for (i = 7; i >= 0; i--) {
SDA = get_bit(buf_IIC.rdata, i);
i2c_clock();
};
}
void IIC_get_data() /*IIC總線接收一個字節*/
{
char i; bit ss;
buf_IIC.rdata = 0;
SDA = 1;
for (i = 7; i >= 0; i--) {
ss = i2c_clock();
buf_IIC.rdata = buf_IIC.rdata * 2;
if (ss)
buf_IIC.rdata = buf_IIC.rdata + 1;
};
}
bit get_ack()
{
bit bb;
bb = i2c_clock();
return(bb);
}
void no_ack()
{
//i2c_clock();
i2c_clock();
}
/*=============================*/
void i2c_write(int address, rdata, delays)/*rdata是16位的,實際被寫入EEPROM的只有低8位*/
{
wr_led = 0;
buf_IIC.command = 0xa0;
buf_IIC.addressl = address & 0xff;//0x100;
buf_IIC.addressh = address >>= 8;//0x100;
i2c_init();
buf_IIC.rdata = rdata;
send_start();
send_command();get_ack();
send_address();get_ack();
IIC_send_data();get_ack();
send_stop();
if (delays != 0)
delay(8);
wr_led = 1;
}
char i2c_read(int address)
{
buf_IIC.command = 0xa0;
buf_IIC.addressl = address & 0xff;//0x100;
buf_IIC.addressh = address >>= 8;//0x100;
i2c_init();
send_start();
send_command();get_ack();
send_address();get_ack();
send_start();
buf_IIC.command = 0xa1;
send_command();get_ack();
IIC_get_data();no_ack();
send_stop();
return(buf_IIC.rdata);
}
void i2c_read_leng(uint address,uchar leng) //讀短信內容
{ unsigned char iq;
buf_IIC.command = 0xa0;
buf_IIC.addressl = address & 0xff;//0x100;
buf_IIC.addressh = address >>= 8;//0x100;
i2c_init();
send_start();
send_command();get_ack();
send_address();get_ack();
send_start();
buf_IIC.command = 0xa1;
send_command();get_ack();
Poutbuf=&Key[9];
for(iq=0;iq<(leng-1);iq++){
IIC_get_data();
I2C_Ack(0);
*Poutbuf=buf_IIC.rdata;
Poutbuf++;
}
IIC_get_data();
I2C_Ack(1);
*Poutbuf=buf_IIC.rdata;
Poutbuf++;
send_stop();
}
void i2c_write_int(int address, rdata) /*rdata是16位的,實際被寫入EEPROM低16位*/
{
char hc, lc;
lc = rdata % 0x100;hc = rdata >>= 8;
i2c_write(address, hc, 1);
i2c_write(address + 1, lc, 1);
}
int i2c_read_int(int address)
{
uint i;
i = i2c_read(address);
i = i <<= 8;
i = i | (i2c_read(address + 1) & 0x00ff);
return(i);
}
void i2c_write_leng(uint address,uchar leng,uchar indata) //rdata是16位的,實際被寫入EEPROM的只有低8位
{
unsigned char iq;
wr_led = 0;
buf_IIC.command = 0xa0;
buf_IIC.addressl = address & 0xff;//0x100;
buf_IIC.addressh = address >>= 8;//0x100;
i2c_init();
send_start();
send_command();get_ack();
send_address();get_ack();
for(iq=0;iq<leng;iq++)
{ buf_IIC.rdata = indata;
IIC_send_data();get_ack(); //數據連續寫
}
send_stop();
delay(8);
wr_led = 1;
}
//======刪除所有卡========================
void erase_all()
{ uchar leng;
unsigned int i;
for(i=5;i<3167;) {
leng=32-(i%32);
if((i+leng)>3167){
leng=3167-i;
}
i2c_write_leng(i,leng,0);
i=i+leng;
}
for(i=255;i<767;) {
leng=32-(i%32);
if((i+leng)>767){
leng=767-i;
}
i2c_write_leng(i,leng,0xff);
i=i+leng;
}
}
//======刪除所有密碼========================
void erase_all_password()
{
i2c_write_int(PW_Total,0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -