MoistureSoftware/Source/FM24.c

135 lines
3.3 KiB
C

/**************** (C) COPYRIGHT 2023 山东云唐智能科技有限公司 *******************
* 文 件 名:
* 创 建 者: Kaiser
* 描 述 :
* 最后修改:
*********************************** 修订记录 ************************************
* 版 本:
* 修订人:
********************************************************************************/
/******************************** 功能说明 *************************************
*
*******************************************************************************/
#include "i2c.h"
#define FM24_ADDR 0xA0
#define ACK 1
#define NOACK 0
#include "i2c.h"
#define FM24_ADDR 0xA0
#define ACK 1
#define NOACK 0
void FM24_Write(uint16_t addr, uint8_t *data, uint8_t len)
{
I2C2_Start();
I2C2_SendByte(FM24_ADDR);
I2C2_Wait_ACK();
I2C2_SendByte(addr >> 8);
I2C2_Wait_ACK();
I2C2_SendByte(addr & 0xFF);
I2C2_Wait_ACK();
while (len--) {
I2C2_SendByte(*data++);
I2C2_Wait_ACK();
}
I2C2_Stop();
}
void FM24_Read(uint16_t addr, uint8_t *data, uint8_t len)
{
I2C2_Start();
I2C2_SendByte(FM24_ADDR);
I2C2_Wait_ACK();
I2C2_SendByte(addr >> 8);
I2C2_Wait_ACK();
I2C2_SendByte(addr & 0xFF);
I2C2_Wait_ACK();
I2C2_Start();
I2C2_SendByte(FM24_ADDR | 0x01);
I2C2_Wait_ACK();
while (len--) {
*data++ = I2C2_ReadByte(len ? ACK : NOACK);
}
I2C2_Stop();
}
void FM24Init()
{
I2C2_Init();
}
//void FM24_Write(uint16_t addr, uint8_t *data, uint8_t len)
//{
// I2C2_Start();
// I2C2_SendByte(FM24_ADDR);
// I2C2_Wait_ACK();
// I2C2_SendByte(addr & 0x00FF);
// I2C2_Wait_ACK();
// I2C2_SendByte(addr >> 8);
// I2C2_Wait_ACK();
//// I2C2_SendByte(addr & 0x00FF);
//// I2C2_Wait_ACK();
// while (len--) {
// I2C2_SendByte(*data++);
// I2C2_Wait_ACK();
// }
// I2C2_Stop();
//}
//void FM24_Read(uint16_t addr, uint8_t *data, uint8_t len)
//{
// uint8_t value;
// I2C2_Start();
// I2C2_SendByte(FM24_ADDR);
// I2C2_Wait_ACK();
// I2C2_SendByte(addr & 0x00FF);
// I2C2_Wait_ACK();
// I2C2_SendByte(addr >> 8);
// I2C2_Wait_ACK();
// I2C2_Wait_ACK();
// I2C2_SendByte(addr & 0x00FF);
// I2C2_Start();
// I2C2_SendByte(FM24_ADDR | 0x01);
// I2C2_Wait_ACK();
// while (len--) {
// *data++ = I2C2_ReadByte(len ? ACK : NOACK);
// }
// I2C2_Stop();
//}
//以上为标准写法 因为所用器件为FM24CL16 它的从机地址字节为 1 0 1 0 页 选 择 读/写
//void FM24_Write(uint16_t addr, uint8_t *data, uint8_t len)
//{
// I2C2_Start();
// I2C2_SendByte(FM24_ADDR | ((addr &0xFF00)>>7) );
// I2C2_Wait_ACK();
// I2C2_SendByte(addr & 0x00FF);
// I2C2_Wait_ACK();
// while (len--) {
// I2C2_SendByte(*data++);
// I2C2_Wait_ACK();
// }
// I2C2_Stop();
//}
//void FM24_Read(uint16_t addr, uint8_t *data, uint8_t len)
//{
// I2C2_Start();
// I2C2_SendByte(FM24_ADDR | ((addr &0xFF00)>>7) );
// I2C2_Wait_ACK();
// I2C2_SendByte(addr & 0x00FF);
// I2C2_Wait_ACK();
// I2C2_Start();
// I2C2_SendByte(FM24_ADDR | ((addr &0xFF00)>>7)| 0x01);
// I2C2_Wait_ACK();
// while (len--) {
// *data++ = I2C2_ReadByte(len ? ACK : NOACK);
// }
// I2C2_Stop();
//}
//void FM24Init()
//{
// I2C2_Init();
//}
/******** (C) COPYRIGHT 2023 山东云唐智能科技有限公司 **** End Of File ********/