logo资料库

51单片机读写SD卡程序.pdf

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
51单片机读写SD卡程序!!! 第1页 首页 - 资讯 - 厂商 - 下载 - 论坛 - 博客 - 视频 - 百科 - 问答 - 会展 - 培训 - 高校 - 杂志  搜 索:  新闻 论坛 博客 问答 论坛 » 嵌入式系统设计 » 51单片机读写SD卡程序!!! 广告招商 版主: yaopg , woodhead , 永远在路上 51单片机读写SD卡程序!!! 发表于 2007-11-12 16:02:25 l0p0c 有没有做过51单片机读写SD卡项目的同行? 现在需要帮助一下咯! o(∩_∩)o... 我是GG 高级会员 加为好友 发短消息 最后登陆时间:2009-05-21 15:30:00 状态:离线 电子学习,从EEPW开始! [电子书]逻辑分析仪选型指南 RE: 51单片机读写SD卡程序!!! 发表于 2007-11-12 16:58:36 Tony 有什么问题写下来大家帮你看! 我是GG 高级会员 加为好友 发短消息 最后登陆时间:2009-06-04 17:01:32 状态:离线 If winter comes,Can spring be far behind?? 爱“拼”才会赢,1980元仿真器免费等你拿! RE: 51单片机读写SD卡程序!!! 发表于 2007-11-13 09:38:52 l0p0c 现在不知道该怎么整? SD卡的资料是英文的,有好多地方整明白啊! 我是GG 高级会员 加为好友 发短消息 最后登陆时间:2009-05-21 15:30:00 状态:离线 电子学习,从EEPW开始! [电子书]逻辑分析仪选型指南 RE: 51单片机读写SD卡程序!!! 发表于 2007-11-15 09:32:32 SnIIper 我是MM 普通会员 加为好友 发短消息 最后登陆时间:2008-05-13 14:33:37 状态:离线 分享一个AVR的SD卡接口程序给你,希望对你有参考作用/************************************************************/ /* SD Code for M32L */ /* By pasyong */ /* 2006-4 */ /* Base ICC6.31A */ /************************************************************/ #include #include #include "1011.h" #define uchar unsigned char #define uint unsigned int #define MMC_CS_PIN BIT(4) //PORTB.4 #define MMC_PORT PORTB 发表新贴 发表回复 [您是本帖的第5110位阅读者] [1楼] 回复 回复本帖 回到顶端 [2楼] 回复 回复本帖 回到顶端 [3楼] 回复 回复本帖 回到顶端 [4楼] uchar reading=0,a=0,pointer=0; void sd_port_init() { MMC_PORT|=MMC_CS_PIN; } uchar BUFFER[512]; //扇区缓冲区 uint i=0; void delay_nus(uint n) { unsigned char b; for (b = 1; b>24); Write_Byte_SPI(arg>>16); //send 6 Byte Command to MMC/SD-Card Write_Byte_SPI(arg>>8); Write_Byte_SPI(arg&0xff); Write_Byte_SPI(0x95); //仅仅对RESET有效的CRC效验码 //get 8 bit response //Read_Byte_MMC(); //read the first byte,ignore it. do { //Only last 8 bit is used here.Read it out. tmp = Read_Byte_SPI(); retry++; } while((tmp==0xff)&&(retry<100)); //当没有收到有效的命令的时候 if(reading==0) MMC_PORT|=MMC_CS_PIN; //MMC_CS_PIN=1; else MMC_PORT&=~MMC_CS_PIN; //MMC_CS_PIN=0; return(tmp); } //**************************************************************************** //SD卡初始化(SPI-MODE) //**************************************************************************** http://forum.eepw.com.cn/thread/118916/1 2009-6-4 23:32:41
51单片机读写SD卡程序!!! 第2页 uchar SD_Init(void) { uchar retry,temp; uchar i; MMC_PORT&=~MMC_CS_PIN; //SD卡使能 delay_nus(250); //Wait MMC/SD ready... for (i=0;i<0x0f;i++) { Write_Byte_SPI(0xff); //send 74 clock at least!!! } //Send Command CMD0 to MMC/SD Card retry=0; do { //retry 200 times to send CMD0 command temp=SD_Write_Command(0,0); retry++; if(retry==100) { ;//CMD0 Error! } } while(temp!=1); //Send Command CMD1 to MMC/SD-Card retry=0; do { //retry 100 times to send CMD1 command temp=SD_Write_Command(1,0); retry++; if(retry==100) { ; } } while(temp!=0); retry=0; SD_Write_Command(16,512); //设置一次读写BLOCK的长度为512个字节 [5楼] 回复 回复本帖 回到顶端 MMC_PORT|=MMC_CS_PIN; //MMC_CS_PIN=1; //set MMC_Chip_Select to high return(0); //All commands have been taken. } //**************************************************************************** //从SD卡读一个扇区 Return 0 if no Error. //**************************************************************************** uchar SD_Read_Block(unsigned long address) { uchar temp=0;uint i=0; reading=1; temp=SD_Write_Command(17,address); //读出RESPONSE while (Read_Byte_SPI()!= 0xfe) {;} //直到读取到了数据的开始头0XFE,才继续 for(i=0; i<512; i++) { BUFFER[i]=Read_Byte_SPI(); } Read_Byte_SPI();//CRC - Byte Read_Byte_SPI();//CRC - Byte reading=0; MMC_PORT|=MMC_CS_PIN; //关闭SD卡 return(temp); } RE: 51单片机读写SD卡程序!!! 发表于 2007-11-15 09:45:12 SnIIper SD卡读写子程序 我是MM 普通会员 加为好友 发短消息 最后登陆时间:2008-05-13 14:33:37 状态:离线 爱“拼”才会赢,1980元仿真器免费等你拿! 硬件平台:atmega8L最小系统 硬spi(sd卡的初始化采用了io口模拟时序,因为在实验中发现要使用较低的速率才能稳定的初始化) 软件开发平台:ICC-AVR version 6.31a 硬件配置:atmega8L 内部8m时钟 sandisk 128m sd卡 几个基本的子程序及其介绍: 1.io口模拟spi,实现数据发送,在初始化时使用 void iodatatransfer(unsigned char iodata) { unsigned char num,b; num=0x80; for (b=0;b<0x08;b++) { WDR();//feed the dog //SD_Write|=(1<0x01) num=num>>1; } } 2.io口模拟spi,实现数据读取,在初始化时使用 unsigned char iodataread(void) { unsigned char data,temp,b; data=0; temp=0; for (b=0;b<0x08;b++) { WDR();//feed the dog SD_Write&=~(1<
51单片机读写SD卡程序!!! 第3页 { data|=0x01; } if(b<7) { data=data<<1;// } delay_4ms();//4ms WDR();//feed the dog } return data; } 3.io口模拟spi,实现指令发送及读响应,在初始化时使用 unsigned char iocmd(unsigned char *cmd) { unsigned temp,a,timeout; temp=0xff; timeout=0; // Raise chip select -----/ss=1 SD_Disable(); WDR();//feed the dog // Send an 8 bit pulse iodatatransfer(0xff); WDR();//feed the dog // Lower chip select SD_Enable(); for(a=0;a<6;a++) { iodatatransfer(*cmd++); WDR(); //transfer cmd in io_mode } while(temp==0xff)// { WDR();//feed the dog temp=iodataread(); if(timeout++>100) { break; } } WDR();//feed the dog return(temp);//the respone of the byte_write_operation } 4.硬spi读数据 unsigned char Read_Byte_SD(void) { char Byte; //SD_Enable(); SPDR=0xff; while(!(SPSR&(1<100) { break; } } //for some reason we need to delay here //delay_1ms(); return(tmp);//the respone of the byte_write_operation } 7.初始化 unsigned char SDInit(void) { unsigned char a,b,retry,erroetoken; unsigned char CMD[]={0x40,0x00,0x00,0x00,0x00,0x95};//cmd0 // Set certain pins to inputs and others to outputs // Only SPI_DI (data in) is an input //SD_Direction_REG==ddrb SD_Direction_REG&=~(1<
51单片机读写SD卡程序!!! 第4页 nop(); }; delay_1ms(); SD_Disable();// iodatatransfer(0xff); WDR();//feed the dog //return 0;/////////// retry=0; SD_Enable(); while((erroetoken=iocmd(CMD))!=0x01)// { WDR(); //serial(erroetoken); if(retry++>200) { //fail and return return 1; } } //return 0;/////////// //Send the 2nd command retry=0; CMD[0]=0x41; CMD[5]=0xFF; while(erroetoken=iocmd(CMD)!=0x00) { WDR(); if (retry++>200) { return 2; } } //Set the SPI bus to full speed SPCR=0x50; SPSR|=0x01; //Raise Chip Select SD_Disable(); return 0; } 8.设置每次读的字节数 char SD_set_length(unsigned int length) { unsigned char retry; //Command to set the block length; char CMD[]={0x50,0x00,0x00,0x00,0x00,0xFF}; //cmd16 CMD[3]=((length&0xFF00)>>8);// CMD[4]= (length&0x00FF); while(Write_Command_SD(CMD)!=0)// { WDR(); if (retry++>200) { return 1; } } SD_Disable(); return 0; } 9.write 512 bytes to a given sector from a Byte_byte_long Buffer unsigned char SD_write_sector(unsigned long addr,unsigned char *Buffer,unsigned int Bytes) { unsigned int a; unsigned char retry,temp; //Command to read a block; char CMD[]={0x58,0x00,0x00,0x00,0x00,0xFF};//cmd24 CMD[1]=((addr&0xFF000000)>>24); CMD[2]=((addr&0x00FF0000)>>16); CMD[3]=((addr&0x0000FF00)>>8); CMD[4]=(addr&0x000000FF); //Send the write command while(Write_Command_SD(CMD)!=0) { if (retry++>50) { return 1; } } //Send the start byte Write_Byte_SD(0xfe); //Read off all the bytes in the block for(a=0;a>24); CMD[2]=((addr&0x00FF0000)>>16); CMD[3]=((addr&0x0000FF00)>>8); CMD[4]=(addr&0x000000FF); http://forum.eepw.com.cn/thread/118916/1 2009-6-4 23:32:41
51单片机读写SD卡程序!!! 第5页 //Send the read command while(Write_Command_SD(CMD)!=0) { WDR();//feed the dog if (retry++>200) { return 1; } } //Send the start byte while(Read_Byte_SD()!=0xfe) { WDR();//feed the dog } //Read off all the bytes in the block for(a=0;a
51单片机读写SD卡程序!!! 共10条 1/1 1 Go 快速回复主题 用户名 : 标 题: 内 容 : 匿名不能发帖!请先 [ 登陆 ] RE: 51单片机读写SD卡程序!!! 第6页 发表 EEPW元器件搜索 搜索 强力提供 关于我们 | 广告服务 | 企业会员服务 | 新手上路 | 联系我们 | 友情链接 《电子产品世界》杂志社 版权所有 北京东晓国际技术信息咨询有限公司 Copyright ©2002 ELECTRONIC ENGINEERING & PRODUCT WORLD. All rights reserved. 京ICP备060382号 http://forum.eepw.com.cn/thread/118916/1 2009-6-4 23:32:41
分享到:
收藏