unsigned char crcMediumCheck16 (unsigned char byte1, unsigned char byte2, unsigned char byte3) { unsigned char synd; synd = (byte1 ^ 0xEC); if (synd & 0x80) synd ^= 0xB7; synd = propagate7[synd] ^ byte2; if (synd & 0x80) synd ^= 0xB7; synd = propagate7[synd] ^ byte3; if (synd & 0x80) synd ^= 0xB7; return synd == 0; } uint16_t max14912_readback; /* cmd2 + data2 + crc2 + cmd1 + data1 + crc1 */ uint16_t Maxim14912_Data_Write(uint16_t data, uint16_t *pfault_data) { uint8_t dat1, dat2; //dat1 is first MAX14912(bit8-15),dat2 is second MAX14912(bit0-7) uint8_t CMD_Data[6]={0x80,0,0,0x80,0,0}; uint8_t data_rx[6]; uint8_t crc_check1, crc_check2; uint8_t ret = 0; uint16_t fault_data; dat1 = (uint8_t)((data >> 8) & 0xff); dat2 = (uint8_t)(data & 0xff); /* data build */ CMD_Data[4] = dat1; CMD_Data[1] = dat2; /* crc build */ CMD_Data[2] = crcMediumEncode16(CMD_Data[0], CMD_Data[1]); CMD_Data[5] = crcMediumEncode16(CMD_Data[3], CMD_Data[4]); /* spi send&recev */ Dio_Spi_Cs_Enable(DO_CS_SELECT); Dio_Spi_Transfer_Data(CMD_Data, data_rx, 6); Dio_Spi_Cs_Release(); /* crc check */ crc_check1 = crcMediumCheck16(data_rx[0], data_rx[1], data_rx[2]); crc_check2 = crcMediumCheck16(data_rx[3], data_rx[4], data_rx[5]); if((crc_check1 == 0) && (crc_check2 == 0)) { fault_data = ((uint16_t)data_rx[0]) | ((uint16_t)data_rx[3] << 8); *pfault_data = fault_data; max14912_readback = ((uint16_t)data_rx[1]) | ((uint16_t)data_rx[4] << 8); /* 2021.09.10 */ } else //crc错误 { max14912_readback = 0; /* 2021.09.10 */ ret = 1; } return ret; }
时间: 2024-04-27 15:22:18 浏览: 158
这段代码中主要实现了一个函数 Maxim14912_Data_Write,用于向一个 MAX14912 设备写入数据,并进行 CRC 校验。该函数的输入参数为一个 16 位的数据,其中高 8 位为 dat1,低 8 位为 dat2。函数首先将输入的数据按照一定的格式存储到数组 CMD_Data 中,然后计算出两个 CRC 校验码并存储到 CMD_Data 数组中。接着,函数通过 SPI 总线向 MAX14912 设备发送数据,并接收从设备返回的数据。最后,函数对接收到的数据进行 CRC 校验,若校验通过则将数据读取出来并返回 0,否则返回 1。
相关问题
void Zoom(const char *filename,double lx,double ly){ FILE *fp = fopen(filename,"rb"); if (fp == NULL) printf("Error"); BITMAPFILEHEADER fileHead; fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp); BITMAPINFOHEADER infoHead; fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp); int width = infoHead.biWidth; int height = infoHead.biHeight; int biCount = infoHead.biBitCount; RGBQUAD *pColorTable; int pColorTableSize = 0; pColorTable = new RGBQUAD[256]; fread(pColorTable, sizeof(RGBQUAD), 256, fp); pColorTableSize = 1024; unsigned char *pBmpBuf; int lineByte = (width*biCount / 8 + 3) / 4 * 4; pBmpBuf = new unsigned char[lineByte*height]; fread(pBmpBuf, lineByte*height, 1, fp); fclose(fp); int dstWidth = round(double(lx*width)); int dstHeight = round(double(ly*height)); int lineByte2 = (dstWidth*biCount / 8 + 3) / 4 * 4; unsigned char*pBmpBuf2; pBmpBuf2 = new unsigned char[lineByte2*dstHeight]; for (int i = 0; i < dstHeight; ++i){ for (int j = 0; j < dstWidth; ++j){ unsigned char *p; p = (unsigned char *)(pBmpBuf2 + lineByte2*i + j); (*p) = 255; } } int x = 0; int y = 0; for (int i = 0; i < height; ++i){ for (int j = 0; j < width; ++j){ unsigned char *p1, *p2; x = round(double(lx*j)); y = round(double(ly*i)); p1 = (unsigned char *)(pBmpBuf + i*lineByte + j); p2 = (unsigned char *)(pBmpBuf2 + y*lineByte2 + x); (*p2) = (*p1); } }
这段代码是一个图像缩放的函数,下面是对代码的中文注释:
```cpp
// 将指定文件中的图像进行缩放后保存
// 参数filename为图像文件名,lx和ly为横向和纵向的缩放比例
void Zoom(const char *filename, double lx, double ly){
FILE *fp = fopen(filename,"rb");
if (fp == NULL)
printf("Error");
// 读取位图文件头和位图信息头
BITMAPFILEHEADER fileHead;
fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);
BITMAPINFOHEADER infoHead;
fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);
// 获取图像的宽度、高度和颜色位数
int width = infoHead.biWidth;
int height = infoHead.biHeight;
int biCount = infoHead.biBitCount;
RGBQUAD *pColorTable;
int pColorTableSize = 0;
// 读取调色板数据
pColorTable = new RGBQUAD[256];
fread(pColorTable, sizeof(RGBQUAD), 256, fp);
pColorTableSize = 1024;
// 读取图像数据
unsigned char *pBmpBuf;
int lineByte = (width * biCount / 8 + 3) / 4 * 4;
pBmpBuf = new unsigned char[lineByte * height];
fread(pBmpBuf, lineByte * height, 1, fp);
fclose(fp);
// 计算缩放后的图像宽度和高度,并计算缩放后的每行字节数
int dstWidth = round(double(lx * width));
int dstHeight = round(double(ly * height));
int lineByte2 = (dstWidth * biCount / 8 + 3) / 4 * 4;
// 创建缩放后的图像数据缓冲区,并初始化为白色
unsigned char *pBmpBuf2;
pBmpBuf2 = new unsigned char[lineByte2 * dstHeight];
for (int i = 0; i < dstHeight; ++i){
for (int j = 0; j < dstWidth; ++j){
unsigned char *p;
p = (unsigned char *)(pBmpBuf2 + lineByte2 * i + j);
(*p) = 255;
}
}
// 对每个像素进行缩放处理
int x = 0;
int y = 0;
for (int i = 0; i < height; ++i){
for (int j = 0; j < width; ++j){
unsigned char *p1, *p2;
x = round(double(lx * j));
y = round(double(ly * i));
p1 = (unsigned char *)(pBmpBuf + i * lineByte + j);
p2 = (unsigned char *)(pBmpBuf2 + y * lineByte2 + x);
(*p2) = (*p1);
}
}
// 将缩放后的图像数据保存到新文件中
FILE *fp2 = fopen("zoomed.bmp", "wb");
if (fp2 == NULL)
printf("Error");
fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp2);
fwrite(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp2);
fwrite(pColorTable, sizeof(RGBQUAD), 256, fp2);
fwrite(pBmpBuf2, lineByte2 * dstHeight, 1, fp2);
fclose(fp2);
// 释放内存
delete[] pColorTable;
delete[] pBmpBuf;
delete[] pBmpBuf2;
}
```
希望这些注释能帮助你理解代码的功能和实现过程。如果还有其他问题,请随时提问。
void Write_Ds1302(unsigned char temp); void Write_Ds1302_Byte( unsigned char address,unsigned char dat ); unsigned char Read_Ds1302_Byte( unsigned char address );
### DS1302 C语言函数实现
#### 写入DS1302数据 `Write_Ds1302`
写入DS1302的数据操作通常涉及发送命令字节和随后的数据字节到DS1302寄存器。此过程通过SPI接口完成,具体实现在不同硬件平台可能有所差异。
```c
void Write_Ds1302(unsigned char cmd, unsigned char dat){
P2_7 = 1; // 设置RST高电平
Ds1302_Start(); // 启动通信
Ds1302_Write(cmd); // 发送命令字节
Ds1302_Write(dat); // 发送数据字节
Ds1302_Stop(); // 结束通信
}
```
上述代码展示了如何向DS1302写入特定命令`cmd`以及相应数据`dat`[^1]。
#### 单字节写入 `Write_Ds1302_Byte`
对于单字节写入功能,则更为简单明了:
```c
void Write_Ds1302_Byte(unsigned char byte_data){
P2_7 = 1;
Ds1302_Start();
Ds1302_Write(byte_data);
Ds1302_Stop();
}
```
这里仅需传递要写的单一字节参数即可执行写入动作[^2]。
#### 单字节读取 `Read_Ds1302_Byte`
而从DS1302读取单个字节的操作如下所示:
```c
unsigned char Read_Ds1302_Byte(void){
unsigned char data;
P2_7 = 1;
Ds1302_Start();
data = Ds1302_Read();
Ds1302_Stop();
return data;
}
```
该方法返回由DS1302提供的当前时间或日期的一个组成部分作为无符号字符型变量[^3]。
阅读全文
相关推荐















