📄 fm31256.c
字号:
#include <string.h>
#include <stdio.h>
#include <reg52.h>
#include <FM31256.h>
//==================================================================
//功能: 发送起始命令
//输入:
//输出:
//==================================================================
bit I2C_Start(void)
{
I2C_SDA = 1;
I2C_SCK =1;
if ( I2C_SDA == 0)return 1;
if ( I2C_SCK == 0)return 1;
I2C_SDA = 0;
I2C_SCK =0;
return 0;
}
//==================================================================
//功能: 发送停止命令
//输入:
//输出:
//==================================================================
void I2C_Stop(void)
{
I2C_SDA = 0;
I2C_SCK =1;
I2C_SDA = 1;
}
//==================================================================
//功能:发送ack
//输入:
//输出:
//==================================================================
void I2C_Ack(void)
{
I2C_SDA = 0;
I2C_SCK =1;
I2C_SCK =0;
}
//==================================================================
//功能: 发送Nack
//输入:
//输出:
//==================================================================
void I2C_Nack(void)
{
I2C_SDA = 1;
I2C_SCK =1;
I2C_SCK =0;
}
//==================================================================
//功能: 发送1字节
//输入: 要发送数据 d
//输出: bit_ack
//==================================================================
bit I2C_Send_Byte( uchar d)
{
uchar i = 8;
bit bit_ack;
while( i-- )
{
if ( d &0x80 ) I2C_SDA =1;
else I2C_SDA =0;
I2C_SCK = 1;
I2C_SCK = 0;
d = d << 1;
}
I2C_SDA = 1;
I2C_SCK = 1;
bit_ack = I2C_SDA;
I2C_SCK =0;
return bit_ack;
}
//==================================================================
//功能: 接收1字节
//输入:
//输出: d 读出的数据
//==================================================================
uchar I2C_Receive_Byte(void)
{
uchar i = 8, d;
I2C_SDA = 1;
while ( i--)
{
d = d << 1;
I2C_SCK =1;
if ( I2C_SDA ) d++;
I2C_SCK =0;
}
return d;
}
//==================================================================
//功能: 向FM24C64写入数据0
//输入: FM24C64地址AT24C64_address
// 数据长度count
//输出: 0写入成功,1失败
//==================================================================
bit AT24C64_W_0(uint AT24C64_address,uint count)
{
unsigned char status, add_l,add_h,write_10;
add_l=AT24C64_address&0x00ff;
add_h=AT24C64_address>>8;
write_10=0; //读10次
start:
write_10++;
if(write_10>5){I2C_Stop(); return 1 ;}
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(IIC_WRITE);
if(status==1)goto start;
status=I2C_Send_Byte( add_h);
if(status==1)goto start;
status=I2C_Send_Byte( add_l);
if(status==1)goto start;
while(count--)
{
status=I2C_Send_Byte( 0 ); //存储器清零
if(status==1)goto start;
}
I2C_Stop();
return 0;
}
//==================================================================
//功能: 向FM24C64写入数据
//输入: 数据地址*mcu_address
// FM24C64地址AT24C64_address
// 数据长度count
//输出: 0写入成功,1失败
//==================================================================
bit AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
{
uchar i,status, add_l,add_h,write_10;
add_l=AT24C64_address&0x00ff; //低位地址
add_h=AT24C64_address>>8; //高位地址
write_10=0; //写10次
start:
write_10++;
if(write_10>2){I2C_Stop();return 1 ;}
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(IIC_WRITE); //发送字节A0标志
if(status==1)goto start;
status=I2C_Send_Byte( add_h);
if(status==1)goto start;
status=I2C_Send_Byte( add_l);
if(status==1)goto start;
i=0;
while(count--)
{
status=I2C_Send_Byte( *(uchar*)mcu_address );
if(status==1)goto start;
((uchar*)mcu_address)++;
}
I2C_Stop();
return 0;
}
//==================================================================
//功能: 从FM24C64读出数据
//输入: 数据地址*mcu_address
// FM24C64地址AT24C64_address
// 数据长度count
//输出: 0读出成功,1失败
//==================================================================
bit AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
uchar i, add_l,add_h,read_10,status;
add_l=AT24C64_address&0x00ff;
add_h=AT24C64_address>>8;
read_10=0; //读10次
start:
read_10++;
if(read_10>100){I2C_Nack(); I2C_Stop();return 1 ;}
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(IIC_WRITE);
if(status==1) goto start;
status=I2C_Send_Byte( add_h); //add_l
if(status==1) goto start;
status=I2C_Send_Byte( add_l ); //add_h
if(status==1) goto start;
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(IIC_READ);
if(status==1)goto start;
for(i=0;i<count-1;i++)
{
*(uchar*)mcu_address = I2C_Receive_Byte();
((uchar*)mcu_address)++;
I2C_Ack();
}
*(uchar*)mcu_address = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
return 0;
}
//======================================================
void write_reg(uchar add,uchar wbyte)
{
I2C_Start();
I2C_Send_Byte(REG_WRITE);
I2C_Send_Byte(add);
I2C_Send_Byte(wbyte);
I2C_Stop();
}
//=================================================
uchar read_reg (uchar add)
{
uchar temp;
I2C_Start();
I2C_Send_Byte(REG_WRITE);
I2C_Send_Byte(add);
I2C_Nack();
I2C_Stop();
I2C_Start();
I2C_Send_Byte(REG_READ);
temp=I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
return(temp);
}
//==================================================================
//功能: 读所有数据
//输入:
//输出:
//==================================================================
bit ic_m41t81_read_all_data()
{
uchar i,read_10,status;
uchar *pa;
uchar flagstempa,flagstempb;
flagstempa=read_reg(FLAGS);
flagstempb=(flagstempa|0x01)&0xFD;
write_reg(FLAGS, flagstempb);
read_10=0; //读10次
start:
read_10++;
if(read_10>10){I2C_Nack(); I2C_Stop();return 1 ;}
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(REG_WRITE);
if(status==1) goto start;
status=I2C_Send_Byte(SECONDS);
if(status==1) goto start;
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(REG_READ);
if(status==1)goto start;
pa=&newtime.miao;
for(i=0;i<7-1;i++)
{
*(pa+i)= I2C_Receive_Byte();
I2C_Ack();
}
*(pa+i)= I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
write_reg(FLAGS, flagstempb&0XFE);
return 0;
}
//==================================================================
//功能: 写入所有数据
//输入:
//输出:
//==================================================================
bit ic_m41t81_write_all_data()
{
uchar i,status,write_10;
uchar *pa;
uchar flagstempa,flagstempb;
flagstempa=read_reg(FLAGS);
flagstempb=(flagstempa|0x02)&0xFE;
write_reg(FLAGS, flagstempb);
write_10=0; //写10次
start:
write_10++;
if(write_10>10){I2C_Stop();return 1 ;}
status=I2C_Start();
if(status==1)goto start;
status=I2C_Send_Byte(REG_WRITE); //发送字节A0标志
if(status==1)goto start;
status=I2C_Send_Byte( SECONDS);
if(status==1)goto start;
pa=&newtime.miao;
for(i=0;i<7;i++)
{
status=I2C_Send_Byte(write_time[i+SECONDS]);
if(status==1)goto start;
}
I2C_Stop();
write_reg(FLAGS, flagstempb&0XFD);
return 0;
}
//==================================================================
//功能: 写入寄存器更新位
//输入:
//输出:
//==================================================================
void ic_m41t81_write_clock_run()
{
write_reg(CONTROL,0);
}
//==================================================================
//功能: 初始化fm24c64
//输入:
//输出:
//==================================================================
void initfm31256()
{
I2C_SCK =1;
I2C_SDA =1;
write_reg(CONTROL,0);
write_reg(COMPANION,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -