您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 基于Atmega128单片机SD卡读写程序(分享)
基于Atmega128单片机SD卡读写程序实物图对照接线图以下是一个简单的测试SD卡读写的程序,程序是基于Atmega128单片机编写的,对于Atmega的其他单片机仅需要做管脚改动就可以使用,其他单片机更改要更大。sd.h//******************************************************************//SPI各线所占用的端口#defineSD_SSPB6#defineSD_SCKPB1#defineSD_MOSIPB2#defineSD_MISOPB3//******************************************************************#defineSD_DDRDDRB#defineSD_PORTPORTB#defineSD_PINPINB#defineSD_SS_HSD_PORT|=(1#defineSDSS_LSD_PORT&=~(1#defineSD_SCK_HSD_PORT|=(1#defineSD_SCK_LSD_PORT&=~(1#defineSD_MOSI_HSD_PORT|=(1#defineSD_MOSI_LSD_PORT&=~(1#defineSD_MISO_IN(SD_PIN&(1//-------------------------------------------------------------//错误号//-------------------------------------------------------------#defineINIT_CMD0_ERROR0xFF#defineINIT_CMD1_ERROR0xFE#defineWRITE_BLOCK_ERROR0xFD#defineREAD_BLOCK_ERROR0xFC#defineTRUE0x01//-------------------------------------------------------------//MMC/SD命令(命令号从40开始,只列出基本命令,并没有都使用)//-------------------------------------------------------------#defineSD_RESET0x40+0#defineSD_INIT0x40+1#defineSD_READ_CSD0x40+9#defineSD_READ_CID0x40+10#defineSD_STOP_TRANSMISSION0x40+12#defineSD_SEND_STATUS0x40+13#defineSD_SET_BLOCKLEN0x40+16#defineSD_READ_BLOCK0x40+17#defineSD_READ_MULTI_BLOCK0x40+18#defineSD_WRITE_BLOCK0x40+24#defineSD_WRITE_MULTI_BLOCK0x40+25//片选关(MMC/SD-CardInvalid)#defineSD_Disable()SD_SS_H//片选开(MMC/SD-CardActive)#defineSD_Enable()SD_SS_LSD_TEST.C//****************************************************************************************///ICC-AVRapplicationbuilder:03-5-208:39:11//Target:M128//Crystal:3.6864Mhz#include#include#include'sd.h'voiduart0_init(void);voidputchar(unsignedcharcontent);voidputstr(unsignedchar*s);voidSD_Port_Init(void);unsignedcharSD_Init(void);unsignedcharSD_write_sector(unsignedlongaddr,unsignedchar*Buffer);unsignedcharSD_read_sector(unsignedlongaddr,unsignedchar*Buffer);unsignedcharSPI_TransferByte(unsignedcharbyte);unsignedcharWrite_Command_SD(unsignedcharcmd,unsignedlongaddress);unsignedlongSD_find(void);//**************************************************************************//串口调试程序//**************************************************************************voiduart0_init(void){UCSR0B=0x00;//disablewhilesettingbaudrateUCSR0A=0x00;UCSR0C=0x06;//00000110UART0设置为异步模式、无奇偶校验、1位停止位、8位数据位UBRR0L=0x17;//setbaudrateloUBRR0H=0x00;//setbaudratehi设置UART0口通信速率9600UCSR0B=0x18;}voidputchar(unsignedcharcontent){while(!(UCSR0A&(1UDRE0)));/*判断上次发送有没有完成*/UDR0=content;/*发送数据*/}voidputstr(unsignedchar*s){while(*s){putchar(*s);s++;}}//****************************************************************************//端口初始化voidSD_Port_Init(void)//****************************************************************************{SD_PORT|=(1SD_DDR|=(1SD_DDR&=~(1}//****************************************************************************//初始化MMC/SD卡为SPI模式unsignedcharSD_Init(void)//****************************************************************************{unsignedcharretry,temp;unsignedchari;SPCR=0x53;//设定SPI为128分频,慢速进行初始化SPSR=0x00;for(i=0;i0x0f;i++){SPI_TransferByte(0xff);//延迟74个以上的时钟}SD_Enable();//开片选SPI_TransferByte(SD_RESET);//发送复位命令SPI_TransferByte(0x00);SPI_TransferByte(0x00);SPI_TransferByte(0x00);SPI_TransferByte(0x00);SPI_TransferByte(0x95);SPI_TransferByte(0xff);SPI_TransferByte(0xff);retry=0;do{temp=Write_Command_SD(SD_INIT,0);//发送初始化命令retry++;if(retry==100)//重试100次{SD_Disable();//关片选return(INIT_CMD1_ERROR);//如果重试100次失败返回错误号}}while(temp!=0);MSD_Disable();//关片选SPCR=0x50;//设置SPI为2分频。进行高速读写SPSR=0x01;return(TRUE);//返回成功}//****************************************************************************//发送命令给MMC/SD卡//Return:返回MMC/SD卡对命令响应的第2字节,作为命令成功判断unsignedcharWrite_Command_SD(unsignedcharcmd,unsignedlongaddress)//****************************************************************************{unsignedchartmp;unsignedcharretry=0;SD_Disable();SPI_TransferByte(0xFF);SD_Enable();SPI_TransferByte(cmd);//将32位地址进行移位作为地址字节SPI_TransferByte(address24);SPI_TransferByte(address16);SPI_TransferByte(address8);SPI_TransferByte(address);SPI_TransferByte(0xFF);SPI_TransferByte(0xFF);do{tmp=SPI_TransferByte(0xFF);//发送8个时钟接受最后一个字节retry++;}while((tmp==0xff)&&(retry8));return(tmp);}//****************************************************************************//写一个扇区(512Byte)toMMC/SD-Card//如果写完成返回TRUEunsignedcharSD_write_sector(unsignedlongaddr,unsignedchar*Buffer)//****************************************************************************{unsignedchartemp;unsignedinti;SPI_TransferByte(0xFF);//延迟8个时钟SD_Enable();//开片选temp=Write_Command_MMC(MMC_WRITE_BLOCK,addr9);//发送写扇区命令if(temp!=0x00){SD_Disable();return(temp);}SPI_TransferByte(0xFF);SPI_TransferByte(0xFF);SPI_TransferByte(0xFE);for(i=0;i512;i++){SPI_TransferByte(*Buffer++);//发送512字节数据}//CRC-ByteSPI_TransferByte(0xFF);//DummyCRCSPI_TransferByte(0xFF);//CRCCodetemp=SPI_TransferByte(0xFF);//读SD卡运行响应if((temp&0x1F)!=0x05)//如果最后4位为0101,为操作成功。否则为操作失败。{SD_Disable();return(WRITE_BLOCK_ERROR);//返回错误}while(SPI_TransferByte(0xFF)!=0xFF);SD_Disable
本文标题:基于Atmega128单片机SD卡读写程序(分享)
链接地址:https://www.777doc.com/doc-2568569 .html