python生成xlsx
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
|
||||
#include "drv_at24cxx.h"
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
static I2C_t *prv_i2c = NULL;//私有i2c指针
|
||||
|
||||
I2C_t* AT24C_Init(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
I2C_t* i2c;
|
||||
i2c = (I2C_t *)malloc(sizeof(I2C_t));
|
||||
|
||||
i2c->hi2c = hi2c;
|
||||
i2c->address = DEV_ADDR;
|
||||
i2c->page_num = PAGE_NUM;
|
||||
i2c->page_size = PAGE_SIZE;
|
||||
i2c->capacity = CAPACITY_SIZE;
|
||||
i2c->addr_num = ADDR_BYTE_NUM;
|
||||
|
||||
i2c->delay = 2;
|
||||
|
||||
|
||||
/**
|
||||
* 给全局变量赋值
|
||||
*/
|
||||
prv_i2c = i2c;
|
||||
|
||||
return i2c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
unsigned char AT24C_Write(uint16_t addr,uint8_t* buff,uint16_t len)
|
||||
{
|
||||
if(!prv_i2c)
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef rst;
|
||||
|
||||
uint16_t i = 0;
|
||||
uint16_t cnt = 0; //写入字节计数
|
||||
uint16_t head;
|
||||
uint16_t left;
|
||||
uint16_t tail;
|
||||
|
||||
if((len + addr - 1)/prv_i2c->page_size == addr/prv_i2c->page_size) //如果在同一页
|
||||
{
|
||||
rst = HAL_I2C_Mem_Write(prv_i2c->hi2c, prv_i2c->address | 0x00, addr, prv_i2c->addr_num, buff, len, 0xFFFF);
|
||||
HAL_Delay(prv_i2c->delay * len);
|
||||
}
|
||||
else
|
||||
{
|
||||
head = (addr / prv_i2c->page_size+1) * prv_i2c->page_size - addr; //开始页剩余待写入字节数
|
||||
left = len - head; //除去开始页剩下字节数
|
||||
tail=left-left/prv_i2c->page_size*prv_i2c->page_size; //末页待写入字节数
|
||||
|
||||
rst = HAL_I2C_Mem_Write(prv_i2c->hi2c, prv_i2c->address | 0x00, addr, prv_i2c->addr_num, buff, head, 0xffff);
|
||||
HAL_Delay(prv_i2c->delay * head);
|
||||
|
||||
for(i=0; i<left/prv_i2c->page_size; i++)
|
||||
{
|
||||
rst = HAL_I2C_Mem_Write(prv_i2c->hi2c, prv_i2c->address | 0x00, addr + head + i * prv_i2c->page_size, prv_i2c->addr_num, buff + head + i * prv_i2c->page_size , prv_i2c->page_size, 0xFFFF);
|
||||
HAL_Delay(prv_i2c->delay*prv_i2c->page_size);
|
||||
|
||||
if(rst != HAL_OK)
|
||||
{
|
||||
return (unsigned char)rst;
|
||||
}
|
||||
}
|
||||
|
||||
rst = HAL_I2C_Mem_Write(&hi2c1, prv_i2c->address | 0x00, addr + head + i * prv_i2c->page_size, prv_i2c->addr_num, buff + head+ i * prv_i2c->page_size, tail, 0xffff);
|
||||
HAL_Delay(prv_i2c->delay * tail);
|
||||
}
|
||||
|
||||
return (unsigned char)rst;
|
||||
}
|
||||
|
||||
unsigned char AT24C_Read(uint16_t addr,uint8_t* buff,uint16_t len)
|
||||
{
|
||||
if(!prv_i2c)
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef rst;
|
||||
|
||||
if(addr + len < 0x8000)
|
||||
{
|
||||
rst = HAL_I2C_Mem_Read(prv_i2c->hi2c, prv_i2c->address | 0x01, addr, prv_i2c->addr_num, buff, len, 0xffff);
|
||||
}
|
||||
|
||||
return (unsigned char)rst;
|
||||
}
|
||||
|
||||
uint8_t AT24C_Check(void)
|
||||
{
|
||||
uint8_t temp;
|
||||
uint8_t data = 0XAB;
|
||||
|
||||
AT24C_Read(0x00,&temp,1);//避免每次开机都写AT24CXX
|
||||
|
||||
if(temp != 0XAB)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else//排除第一次初始化的情况
|
||||
{
|
||||
AT24C_Write(0x00,&data,1);
|
||||
AT24C_Read(0x00,&temp,1);
|
||||
|
||||
if(temp != 0XAB)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AT24C_Fill(uint8_t fill)
|
||||
{
|
||||
uint8_t erase[512];//这个缓存太大
|
||||
memset(erase,fill,512);
|
||||
for(uint16_t i=0;i<64;i++)
|
||||
{
|
||||
AT24C_Write(i*512,erase,512);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user