zynq对sd卡读浮点数操作

最近一直在做zynq方面的学习,用到哪就写到哪吧
读写sd步骤如下
1、首先新建zynq的工程,连线如下
在这里插入图片描述2、然后综合,布线生成后bin文件,并按照下图操作在这里插入图片描述

打开sdk后新建工程
file 》new 》application project
新建helloworld工程
下面导入sd卡的库
点击xilinx 》board support package setting
选中xilffs库,其他默认即可
在这里插入图片描述
连接上板载的串口
在下方sdk terminal可以读出输出

#include "platform.h"
#include "xparameters.h"
#include "xil_printf.h"
#include "ff.h"
#include "xdevcfg.h"


static FATFS fatfs;


int SD_Init()
{
FRESULT rc;
rc = f_mount(&fatfs,"",0);
if(rc)
{
xil_printf("ERROR : f_mount returned %d\r\n",rc);
return XST_FAILURE;
}
return XST_SUCCESS;
}


int SD_Transfer_read(char *FileName,u32 DestinationAddress,u32 ByteLength)
{
FIL fil;
FRESULT rc;
UINT br;


rc = f_open(&fil,FileName,FA_READ);
if(rc)
{
xil_printf("ERROR : f_open returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_lseek(&fil, 0);
if(rc)
{
xil_printf("ERROR : f_lseek returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_read(&fil, (void*)DestinationAddress,ByteLength,&br);
if(rc)
{
xil_printf("ERROR : f_read returned %d\r\n",rc);
return XST_FAILURE;
}
rc = f_close(&fil);
if(rc)
{
xil_printf(" ERROR : f_close returned %d\r\n", rc);
return XST_FAILURE;
}
return XST_SUCCESS;
}

float power(float a,int b){
	float c=1;
	if(b>0){

		for(int i=0;i<b;i++){
			c=c*a;
		}
	}
	else{
		for(int i=0;i<(0-b);i++){
			c=c/a;
		}

	}
	return c;

}

#define FILE "test123.txt"
#define FILE2 "W_CONV3.txt"


//#define MAX_LEN 1024*1024*200
#define MAX_LEN 900000

static  char src_str[MAX_LEN] = { 0 } ;
static  char dst_str[MAX_LEN] = { 0 } ;

static float outfloat[48000];
int main()
{
int r;
unsigned int a;
int myfloat[15];
int i=0;//point location
unsigned int j=0;
int point=0;
int length=0;
float out;
float out1=1.1231;

int Boolfloatnext=0;
int Boolfloatnow=0;

int mode=0;
int nummode[5]={0};
int nummode_num=0;
int num=0;




init_platform();


r  = SD_Init();
if ( XST_SUCCESS != r ){

	printf("fail to open SD1\n\r");
	return 1 ;
}

printf("read  SD1\n\r");
SD_Transfer_read(FILE2,(void*)dst_str,(MAX_LEN));
printf("read done SD1\n\r");
printf("start check data ...\n\r");






for(a=0;(a<MAX_LEN)&&(j<48000);a++){
	if(dst_str[a]!=32){
		if(mode==0){
			if(dst_str[a]==46) {
				point=i;
			}
			//else if(dst_str[a]==45) {}//-operation
			else if(((dst_str[a-1]==101)||(dst_str[a-1]==69))&&(dst_str[a]==45)) {mode=1;}
			else if((dst_str[a]>47)&&(dst_str[a]<58)){
    			myfloat[i]=dst_str[a]-48;
        		i++;
			}
		}
		else
		{nummode[nummode_num]=dst_str[a]-48;
		nummode_num++;

		}
	}


	else if(dst_str[a+1]!=32){//next num
		mode=0;
		for(int k=0;k<nummode_num;k++){
			num+=(nummode[k]*power(10,nummode_num-k-1));
			nummode[k]=0;
		}

		if(dst_str[a+1]==45) {//next unsigned or not
			Boolfloatnow=Boolfloatnext;
			Boolfloatnext=1;
		}
		else{
			Boolfloatnow=Boolfloatnext;
			Boolfloatnext=0;
		}
		length=i;
		if(point==0) point=length;
	   // xil_printf("%d %d\r\n\r\n",length,point);
		for(int k=0;k<length;k++){
			out+=(myfloat[k]*power(10,point-k-1));
			myfloat[k]=0;
		}

	    xil_printf("%d\r\n",j);
	    if(Boolfloatnow==0) {outfloat[j]=out;}
	    else {outfloat[j]=0-out;}

	    outfloat[j]=outfloat[j]/(power(10,num));
		out=0;
		j++;
		i=0;
		nummode_num=0;
		num=0;
		length=0;
		point=0;
	}

  //  xil_printf("%d\r\n",a);
}
printf(" dst %d  \n\r",dst_str[150000]);
printf(" dst %f  \n\r",outfloat[0]);
printf(" dst %f  \n\r",outfloat[1]);
printf(" dst %f  \n\r",outfloat[47992]);
printf(" dst %f  \n\r",outfloat[47993]);
printf(" dst %f  \n\r",outfloat[47998]);
printf(" dst %f  \n\r",outfloat[47999]);
print("SD over!\r\n");

cleanup_platform();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值