欢迎来到天天文库
浏览记录
ID:16893739
大小:33.50 KB
页数:10页
时间:2018-08-25
《单片机读写sd卡api模式读写》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、单片机读写SD卡最简单最基本的程序处理器:s3c44b0(arm7)SD卡与处理器的引脚连接:MISO-->SIORxD MOSI-->SIOTxD CLK-->SCLK CS-->PE5四个文件:sd_drive.c:用户API函数,移植时不需修改 sd_cmd.c:中间层函数,移植时不需修改 sd_hard.c:硬件层函数,移植时需修改 sd_config.h:一些功能的宏定义,移植时需修改第一次读写SD卡时
2、,需调用SD_Init(void),然后就可以条用Read_Single_Block或者Write_Single_Block进行读写操作注意:进行写操作时,最好不要写前700个扇区,应为这些扇区都是FAT文件系统的重要扇区,一旦误写则可能会导致SD无法被电脑识别,需格式化。/*******************************************************文件名:sd_drive.c作用:用户API函数,包括四个函数, 读取一块扇区(512字节)U8Read_Single_Block(U32blk_addr,
3、U8*rx_buf) 写一个扇区(512字节)U8Write_Single_Block(U32blk_addr,U8*tx_buf) 获取SD卡基本信息,即读CSD寄存器信息(16字节):voidSD_info() SD卡初始化:U8SD_Init(void)********************************************************//********************************************功能:读取一个block输入:blk_ad
4、dr为第几个block,rx_buf为数据缓存区首地址输出:返回NO_ERR则成功,其它则读取失败********************************************/U8Read_Single_Block(U32blk_addr,U8*rx_buf){U16rsp=1;U8i=0;SD_sel(); //使能SD卡while(rsp&&(i5、Jingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitt6、eeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand100)){ write_cmd(CMD17,blk_addr<<9);//写命令CMD17 rsp=Get_rsp(R1); //获取答应 send_clk();}if(i>99) //如果命令超时,则执行超时处理 { SD_desel(); Uart_Printf("failinwritingCMD17"); returnWR_SGL_BLK_ERR;}spi_ro_mode(7、);send_clk(); //发送8个clkread_data(rx_buf);//读取512字节SD_desel(); Uart_Printf("succeedinreadingthe%dstblock!!!",blk_addr);returnNO_ERR;}/********************************************功能:写一个block输入:blk_addr为要写第几个block,tx_buf为数据区输出:返回NO_ERR则成功,其它则读取失败**************************8、******************/U8Write_Single_Block(
5、Jingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitt
6、eeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand100)){ write_cmd(CMD17,blk_addr<<9);//写命令CMD17 rsp=Get_rsp(R1); //获取答应 send_clk();}if(i>99) //如果命令超时,则执行超时处理 { SD_desel(); Uart_Printf("failinwritingCMD17"); returnWR_SGL_BLK_ERR;}spi_ro_mode(
7、);send_clk(); //发送8个clkread_data(rx_buf);//读取512字节SD_desel(); Uart_Printf("succeedinreadingthe%dstblock!!!",blk_addr);returnNO_ERR;}/********************************************功能:写一个block输入:blk_addr为要写第几个block,tx_buf为数据区输出:返回NO_ERR则成功,其它则读取失败**************************
8、******************/U8Write_Single_Block(
此文档下载收益归作者所有