本程序用来读写测试25AA020A存储器,测试代码如下:
#include <stdio.h>
#include <Windows.h>
//定义SPI适配器底层函数的函数指针
typedef int(*SPI_Funtion)(char *in,char *out);
//定义存储器操作的命令
#define CMD_READ_DATA 0X03
#define CMD_PAGE_PROGRAM 0X02
#define CMD_WRITE_ENABLE 0X06
//数据存储区
unsigned char inString[10240];
unsigned char outString[10240];
//主函数
int main(void)
{
// 加载DLL
HINSTANCE hDLL; //DLL句柄
hDLL = LoadLibrary("VT_SPI_Driver.dll");
//获取SPI初始化函数指针地址
SPI_Funtion SPI_Init = (SPI_Funtion)GetProcAddress(hDLL,"VTIF_SPI_Init");
//获取SPI写数据函数指针地址
SPI_Funtion SPI_WriteData = (SPI_Funtion)GetProcAddress(hDLL,"VTIF_SPI_WriteData");
//获取SPI读数据函数指针地址
SPI_Funtion SPI_ReadData = (SPI_Funtion)GetProcAddress(hDLL,"VTIF_SPI_ReadData");
//获取SPI写读数据函数指针地址
SPI_Funtion SPI_WriteReadData = (SPI_Funtion)GetProcAddress(hDLL,"VTIF_SPI_WriteReadData");
//获取SPI设备选择函数指针地址
SPI_Funtion SPI_SelectDevice = (SPI_Funtion)GetProcAddress(hDLL,"VTIF_SPI_SelectDevice");
//选择SPI适配器
printf("选择SPI适配器\n");
SPI_SelectDevice("0",(char*)outString);//选择SPI适配器
printf("%s\n\n",outString);
//初始化SPI适配器
//数据格式为:“控制模式|时钟极性|时钟相位|主从模式|数据宽度|移位方向|片选极性|时钟频率”
printf("初始化SPI适配器\n");
SPI_Init("1|0|0|1|8|0|0|1000000",(char*)outString);//初始化SPI适配器
printf("%s\n\n",outString);
//控制SPI适配器写数据0x06(使能SPI存储器写命令)
printf("控制SPI适配器写数据0x06(使能SPI存储器写命令)\n");
SPI_WriteData("0x06",(char*)outString);
printf("%s\n\n",outString);
Sleep(1);
//控制SPI适配器写数据(向SPI存储器写入数据,开头的两个字节为存储器控制命令)
printf("控制SPI适配器写数据(向SPI存储器写入数据,开头的两个字节为存储器控制命令)\n");
SPI_WriteData("0x02|0x00|20|21|22|23|24|25|26|27|28|29",(char*)outString);
printf("%s\n\n",outString);
Sleep(1);
//控制SPI适配器从SPI存储器中读取10字节数据(开头两个字节为存储器控制命令)
printf("控制SPI适配器从SPI存储器中读取10字节数据(开头两个字节为存储器控制命令)\n");
SPI_WriteReadData("0x03|0x00|0x0A",(char*)outString);
printf("%s\n\n",outString);
Sleep(1);
FreeLibrary(hDLL);
system("pause");
return 0;
}
|