久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2619|回復: 6
收起左側

這個51單片機程序為什么不能寫8G的TF卡?

[復制鏈接]
ID:382699 發表于 2020-10-31 14:44 | 顯示全部樓層 |閱讀模式
在網上找的SD卡程序和賣家給的程序,測試了一下都不能正常工作,不知道是不是因為卡是8G的原因?

f3652dade4f3831ec0b62919517f1da.jpg
單片機源程序如下:
  1. #include<reg52.h>
  2. #include<intrins.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5.     //錯誤碼定義//
  6. #define cmd0_error 0x01
  7. #define cmd1_error 0x02
  8. #define write_error 0x03
  9. #define read_error 0x04
  10.     /*位定義*/
  11. sbit cs=P1^0; //定義片選位
  12. sbit clk=P1^1;  //定義時鐘位
  13. sbit si=P1^2; //定義主機發送數據位
  14. sbit so=P1^3; //定義主機接收位
  15. uchar xdata shuju[512]={0};     //定義數據緩沖數組
  16. uchar flag_time;         
  17. //定義標志時間,因為當sd卡進行初始化時需要降低
  18. //通信速度,所以通過該標志來寫1來降低速度
  19. void delay(uint x)       //通信延時函數
  20. {
  21.     while(x--)
  22.     _nop_();
  23. }
  24. void delay1(uint a)     
  25. {
  26.     uint i,j;
  27.     for(i=0;i<a;i++)
  28.         for(j=0;j<120;j++);
  29. }
  30. //寫一字節數據//
  31. void write_sd(uchar date)  
  32. {
  33.     uchar i;
  34.     CY=0;
  35.     clk=1;
  36.     for(i=0;i<8;i++)
  37.     {
  38.         clk=0;
  39.         date=date<<1;
  40.         si=CY;
  41.         if(flag_time==1)  //用來判斷是否處于初始化,如果是降低通信速度
  42.         delay(10);
  43.         _nop_();   //用來讓io口數據更穩定,也可以省略
  44.         clk=1;
  45.         _nop_();
  46.         if(flag_time==1)
  47.         delay(10);
  48.     }
  49. }
  50. //讀取sd卡一個字節數據//
  51. uchar read_sd()      
  52. {
  53.     uchar i,temp=0;
  54.     so=1;          //一定要先將其置1否則會出現錯誤
  55.     //因為如果先置0單片機io口寄存器相應位電平為低當
  56.     //當接收到高電平后可能sd卡電平不足使其io變為高電平
  57.     clk=1;
  58.     for(i=0;i<8;i++)
  59.     {
  60.         clk=0;
  61.         if(flag_time==1)
  62.         delay(10);
  63.         temp<<=1;
  64.         temp=temp|so;
  65.         _nop_();
  66.         clk=1;
  67.         _nop_();
  68.         if(flag_time==1)
  69.         delay(10);
  70.     }
  71.     return temp;
  72. }
  73. //向sd卡寫命令//
  74. uchar write_cmd(uchar *cmd)
  75. {
  76.     uchar i,time,temp;  
  77.     si=1;
  78.     for(i=0;i<6;i++)  //發送六字節命令
  79.     {
  80.         write_sd(cmd[ i]);
  81.     }
  82.     time=0;
  83.     do
  84.     {  
  85.         temp=read_sd();
  86.         time++;
  87.     }
  88.     while((temp==0xff)&&(time<100));
  89.     //判斷命令是否寫入成功,當讀取到so不為0xff時命令寫入成功
  90.     //當temp==0xff時為真且沒發送100次為真繼續執行
  91.     //但是又不能無限制等待所以讓命令寫入100次結束
  92.     return temp;      //返回讀取的數據
  93. }
  94. //復位函數//
  95. uchar sd_reset()
  96. {
  97.     uchar i,temp=0xff,time;
  98.     uchar table[]={0x40,0x00,0x00,0x00,0x00,0x95};
  99.     flag_time=1;
  100.     cs=1;
  101.     for(i=0;i<0x0f;i++)      //復位時最少寫入74個時鐘周期
  102.     {
  103.         write_sd(0xff);
  104.     }  
  105.     cs=0;
  106.     time=0;//打開片選
  107.     do
  108.     {
  109.         temp=write_cmd(table);   //寫入cmd0
  110.         time++;
  111.         if(time==100)
  112.         return(cmd0_error);
  113.     }
  114.     while(temp!=0x01);       //等待命令CMD0的響應
  115.     cs=1;                  //關閉片選
  116.     write_sd(0xff);           //補償8個時鐘
  117.     return 0;
  118. }
  119. //初始化函數此函數決定SD卡的工作模式 選擇SPI還是SD模式//
  120. uchar sd_init()
  121. {
  122.     uchar time=0,temp=0xff;
  123.     uchar table[]={0x41,0x00,0x00,0x00,0x00,0xff};       //命令碼
  124.     flag_time=1;
  125.     cs=0;
  126.     time=0;
  127.     do
  128.     {
  129.         temp=write_cmd(table);
  130.         time++;
  131.         if(time==100)
  132.         return 0x02;
  133.     }
  134.     while(temp!=0x00);       //等待命令CMD1響應
  135.     flag_time=0;
  136.     cs=1;
  137.     write_sd(0xff);           //補償8個時鐘
  138.     return 0;
  139. }
  140. //寫sd卡扇區//
  141. uchar xie_sd_shanqu(unsigned long int add,uchar *buffer)
  142. {
  143.     uchar temp,time;
  144.     uint i;
  145.     uchar table[]={0x58,0x00,0x00,0x00,0x00,0xff};
  146.     add=add<<9;       //add=add*512
  147.     //由于sd卡操作一次性只能寫一個扇區也就是512個字節
  148.     //所以這里通過將長整型地址左移九位來將地址乘上512
  149.     //用于地址操作
  150.     table[1]=((add&0xff000000)>>24);
  151.     table[2]=((add&0x00ff0000)>>16);
  152.     table[3]=((add&0x0000ff00)>>8);
  153.     cs=0;
  154.     time=0;
  155.     do
  156.     {
  157.         temp=write_cmd(table);       //寫入寫扇區命令
  158.         time++;
  159.         if(time==100)
  160.         {
  161.             return(write_error);
  162.         }
  163.     }
  164.     while(temp!=0x00);               //判斷命令是否寫入成功成功時返回0x00
  165.     for(i=0;i<20;i++)     //補充若干時鐘
  166.     {
  167.         write_sd(0xff);
  168.     }
  169.     write_sd(0xfe);       //寫入開始字節0xfe,后面要寫入512字節數據
  170.     for(i=0;i<512;i++)
  171.     {
  172.         write_sd(buffer[ i]);
  173.     }
  174.     write_sd(0xff);  
  175.     write_sd(0xff);           //兩字節奇偶校驗

  176.     temp=read_sd();           //讀取返回值
  177.     if((temp&0x1f)!=0x05)        //如果返回值是 xxx00101 說明數據已經被寫入
  178.     {
  179.         cs=1;
  180.         return(write_error);
  181.     }
  182.     while(read_sd()!=0xff);          //等待sd卡不忙 數據寫入成功
  183.     cs=1;                  //關閉片選
  184.     write_sd(0xff);           //補償8 個時鐘
  185.     return 0;
  186. }
  187. //讀取sd卡扇區//
  188. uchar duqushanqu(unsigned long add,uchar *buffer)
  189. {
  190.     uchar temp,time=0;
  191.     uint i;
  192.     uchar  table[]={0x51,0x00,0x00,0x00,0x00,0xff};
  193.     add=add<<9;
  194.     table[1]=((add&0xff000000)>>24);
  195.     table[2]=((add&0x00ff0000)>>16);
  196.     table[3]=((add&0x0000ff00)>>8);
  197.     cs=0;                          //打開片選
  198.     do
  199.     {
  200.         temp=write_cmd(table);       //寫命令
  201.         time++;
  202.         if(time==100)
  203.         {
  204.             return read_error;
  205.         }
  206.     }
  207.     while(temp!=0);
  208.     write_sd(0xff);               //補償8個時鐘
  209.     while(read_sd()!=0xfe);      //一直讀取等待0xfe
  210.     for(i=0;i<512;i++)
  211.     {
  212.         buffer[ i]=read_sd();
  213.     }
  214.     write_sd(0xff);           //兩字節奇偶校驗位
  215.     write_sd(0xff);
  216.     cs=1;
  217.     write_sd(0xff);           //補償8個時鐘
  218.     return 0;
  219. }
  220. /*在P0上接八個發光二極管用來顯示讀取到的數據
  221. 首先在數組(shuju)里面放入i用于顯示,再將其
  222. 寫入SD卡扇區,然后在讀取出SD卡里的數據*/
  223. void main()
  224. {
  225.     uint i=0;
  226.         uint a=0xaa;
  227.     P1=0x00;
  228.     P0=0xff;
  229.     sd_reset();  
  230.     sd_init();     ///初始化sd卡
  231.     for(i=0;i<512;i++)
  232.     {
  233.         shuju[ i]=a;           //向數據數組里面寫入數據
  234.     }
  235.     for(i=0;i<512;i++)
  236.     {
  237.         xie_sd_shanqu(1,shuju);      //將數據數組里面的數據寫入sd卡
  238.     }
  239.     for(i=0;i<512;i++)
  240.     {
  241.         shuju[ i]=0;           //清零數據數組用來存儲從sd卡讀取到的數據
  242.     }         
  243.     duqushanqu(1,shuju);     //讀取扇區數據
  244.     while(1)
  245.     {
  246.         for(i=0;i<512;i++)
  247.         {
  248.             P0=shuju[ i];      //顯示扇區數據
  249.             delay1(200);
  250.         }
  251.     }
  252. }


  253.                                                                          /*
  254. * SD模塊測試程序
  255. *
  256. * 用途:SD模塊測試程序
  257. *
  258. * 作者                                        日期                                備注
  259. * Huafeng Lin                        20010/10/03                        新增
  260. * Huafeng Lin                        20010/10/03                        修改
  261. *
  262. */
  263. #include "REG52.H"
  264. ////////////////////////****************/
  265. unsigned char *SDInfo1="SD Init Success.";
  266. unsigned char *SDInfo2="SD Init Fail.";
  267. unsigned int xdata ReadBuffer[128]  ;
  268. unsigned int xdata WriteBuffer[128] ;
  269. unsigned int BlockSize;
  270. unsigned long int BlockNR;
  271. //sbit sd_clk=P3^2;
  272. //sbit sd_cse=P3^0;
  273. //sbit sd_dai=P3^3; //Do
  274. //sbit sd_dao=P3^1;  //DI
  275. sbit sd_cse=P1^0;
  276. sbit sd_dao=P1^2;//DI
  277. sbit sd_clk=P1^1;
  278. sbit sd_dai=P1^3;//Do
  279. void Delay5us()
  280. {
  281.         unsigned char a=0;
  282.         for(a=0;a<40;a++)
  283.         ;
  284. }
  285. //********************************************
  286. void SD_2Byte_Write(unsigned int IOData)
  287. {
  288.         unsigned char BitCounter;
  289.                
  290.         for (BitCounter=0;BitCounter<16;BitCounter++)
  291.         {
  292.                 sd_clk=0;//CLK Low
  293.                
  294.                 if(IOData&0x8000)//If the MSB of IOData is 1, then Do=1, else Do=0.
  295.                         sd_dao=1;//Do High
  296.                 else
  297.                         sd_dao=0;//Do Low
  298.                                 
  299.                 sd_clk=1;//CLK High
  300.                 Delay5us();
  301.                
  302.                 IOData=IOData<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  303.         }
  304. }
  305. //********************************************
  306. void SD_Write(unsigned int IOData)
  307. {
  308.         unsigned char BitCounter;
  309.         IOData=IOData<<8;
  310.         
  311.         for (BitCounter=0;BitCounter<8;BitCounter++)
  312.         {
  313.                 sd_clk=0;//CLK Low
  314.                
  315.                 if(IOData&0x8000)//If the MSB of IOData is 1, then Do=1, else Do=0.
  316.                         sd_dao=1;//Do High
  317.                 else
  318.                         sd_dao=0;//Do Low
  319.                                 
  320.                 sd_clk=1;//CLK High
  321.                 Delay5us();
  322.                
  323.                 IOData=IOData<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  324.         }
  325. }
  326. //********************************************
  327. unsigned int SD_2Byte_Read()
  328. {
  329.         unsigned int Buffer;
  330.         unsigned char BitCounter;
  331.         Buffer=0;
  332.         
  333.         for (BitCounter=0;BitCounter<16;BitCounter++)
  334.         {
  335.                 sd_clk=0;//CLK Low
  336.                 Delay5us();
  337.                 sd_clk=1;//CLK High
  338.                 Buffer=Buffer<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  339.                                  //Because the LSB will be damaged, we can not put this line under next line.
  340.                 if(sd_dai)
  341.                         Buffer++;//If SPI_Din=1 then the LSB_of_Buffer=1.               
  342.         }
  343.         
  344.         return Buffer;
  345. }
  346. //********************************************
  347. unsigned int SD_Read()
  348. {
  349.         unsigned int Buffer;
  350.         unsigned char BitCounter;
  351.         Buffer=0xffff;
  352.         
  353.         for (BitCounter=0;BitCounter<8;BitCounter++)
  354.         {
  355.                 sd_clk=0;//CLK Low
  356.                 Delay5us();
  357.                 sd_clk=1;//CLK High
  358.                 Buffer=Buffer<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  359.                                  //Because the LSB will be damaged, we can not put this line under next line.
  360.                 if(sd_dai)
  361.                         Buffer++;//If SPI_Din=1 then the LSB_of_Buffer=1.               
  362.         }
  363.         
  364.         return Buffer;
  365. }
  366. //********************************************
  367. unsigned int SD_CMD_Write(unsigned int CMDIndex,unsigned long CMDArg,unsigned int ResType,unsigned int CSLowRSV)//ResType:Response Type, send 1 for R1; send 2 for R1b; send 3 for R2.
  368. {        //There are 7 steps need to do.(marked by [1]-[7])
  369.         unsigned int temp,Response,Response2,CRC,MaximumTimes;
  370.         Response2=0;
  371.         MaximumTimes=10;
  372.         CRC=0x0095;//0x0095 is only valid for CMD0
  373.         if (CMDIndex!=0) CRC=0x00ff;
  374.         
  375.         sd_cse=0;//[1] CS Low
  376.         
  377.         SD_2Byte_Write(((CMDIndex|0x0040)<<8)+(CMDArg>>24));//[2] Transmit Command_Index & 1st Byte of Command_Argument.
  378.         SD_2Byte_Write((CMDArg&0x00ffff00)>>8);                                //[2] 2nd & 3rd Byte of Command_Argument
  379.         SD_2Byte_Write(((CMDArg&0x000000ff)<<8)+CRC);                //[2] 4th Byte of Command_Argument & CRC only for CMD0
  380.         
  381.         sd_dao=1;//[3] Do High
  382.                                                 //[3] Restore Do to High Level
  383.         
  384.          for (temp=0;temp<8;temp++)//[4] Provide 8 extra clock after CMD
  385.         {
  386.                 sd_clk=0;//CLK Low
  387.                 Delay5us();
  388.                 sd_clk=1;//CLK High
  389.                 Delay5us();
  390.         }
  391.         
  392.         switch (ResType)//[5] wait response
  393.         {
  394.                 case 1://R1
  395.                                 {
  396.                                         do
  397.                                                 Response=SD_Read();
  398.                                         while (Response==0xffff);
  399.                                         break;
  400.                                 }
  401.                 case 2://R1b
  402.                                 {
  403.                                         do
  404.                                                 Response=SD_Read();
  405.                                         while (Response==0xffff);//Read R1 firstly
  406.                                        
  407.                                         do
  408.                                                 Response2=SD_Read()-0xff00;
  409.                                         while (Response2!=0);//Wait until the Busy_Signal_Token is non-zero
  410.                                         break;        
  411.                                 }
  412.                 case 3: Response=SD_2Byte_Read();break;//R2
  413.         }
  414.         
  415.         if (CSLowRSV==0) sd_cse=1;//[6] CS High (if the CMD has data block response CS should be kept low)
  416.          
  417.          for (temp=0;temp<8;temp++)//[7] Provide 8 extra clock after card response
  418.         {
  419.                 sd_clk=0;//CLK Low
  420.                 Delay5us();
  421.                 sd_clk=1;//CLK High
  422.                 Delay5us();
  423.         }
  424.         return Response;
  425. }
  426. //********************************************
  427. unsigned int SD_Reset_Card()
  428. {
  429.         unsigned int temp,MaximumTimes;
  430.         MaximumTimes=10;
  431.         
  432.         for (temp=0;temp<80;temp++)//Send 74+ Clocks
  433.         {
  434.                 sd_clk=0;//CLK Low
  435.                 Delay5us();
  436.                 sd_clk=1;//CLK High
  437.                 Delay5us();
  438.         }
  439.                
  440.         return SD_CMD_Write(0x0000,0x00000000,1,0);//Send CMD0
  441. }
  442. //********************************************
  443. unsigned int SD_Initiate_Card()//Polling the card after reset
  444. {
  445.         unsigned int temp,Response,MaximumTimes;
  446.         MaximumTimes=50;
  447.         
  448.         for(temp=0;temp<MaximumTimes;temp++)
  449.         {
  450.                 Response=SD_CMD_Write(0x0037,0x00000000,1,0);//Send CMD55
  451.                 Response=SD_CMD_Write(0x0029,0x00000000,1,0);//Send ACMD41
  452.                 if (Response==0xff00)
  453.                         temp=MaximumTimes;
  454.         }
  455.         return Response;
  456. }
  457. //********************************************
  458. unsigned int SD_Get_CardInfo()//Read CSD register
  459. {
  460.         unsigned int temp,Response,MaximumTimes;
  461.         MaximumTimes=50;
  462.         
  463.         for(temp=0;temp<MaximumTimes;temp++)
  464.         {
  465.                 Response=SD_CMD_Write(9,0x00000000,1,1);//Send CMD9
  466.                 if (Response==0xff00)
  467.                         temp=MaximumTimes;
  468.         }
  469.         
  470.          for (temp=0;temp<8;temp++)//Provide 8 clock to romove the first byte of data response (0x00fe)
  471.         {
  472.                 sd_clk=0;//CLK Low
  473.                 Delay5us();
  474.                 sd_clk=1;//CLK High
  475.                 Delay5us();
  476.         }
  477.         
  478.         for (temp=0;temp<8;temp++) ReadBuffer[temp]=SD_2Byte_Read();//Get the CSD data
  479.         
  480.         for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
  481.         {
  482.                 sd_clk=0;//CLK Low
  483.                 Delay5us();
  484.                 sd_clk=1;//CLK High
  485.                 Delay5us();
  486.         }
  487.         
  488.         sd_cse=1;//CS_High()
  489.         
  490.         for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
  491.         {
  492.                 sd_clk=0;//CLK Low
  493.                 Delay5us();
  494.                 sd_clk=1;//CLK High
  495.                 Delay5us();
  496.         }
  497.         
  498.         BlockNR=((ReadBuffer[3]<<2)&0x0fff)+((ReadBuffer[4]>>14)&0x0003)+1;//Calcuate MULT
  499.         BlockNR=BlockNR*(0x0002<<(((ReadBuffer[4]<<1)&0x0007)+((ReadBuffer[5]>>15)&0x0001)+1));//Calcuate Block_Number
  500.         return Response;
  501. }
  502. //********************************************
  503. unsigned int SD_Overall_Initiation()
  504. {
  505.         unsigned int Response,Response_2;
  506.         Response=0x0000;
  507.         Response_2=0xff00;
  508.         
  509.         sd_dao=1;//[1] Do High
  510.                                                 //[1] Do must be High when there is no transmition
  511.         do
  512.                 Response=SD_Reset_Card();//[2] Send CMD0
  513.         while (Response!=0xff01);
  514.         
  515.         if (Response!=0xff01) Response_2+=8;
  516.         
  517.         //Response=SD_CMD_Write(8,0x00000000,1,0);//Send CMD8
  518.         
  519.         Response=SD_Initiate_Card();//[3] Send CMD55+ACMD41
  520.         if (Response==0xff00)
  521.                 ;
  522.         else
  523.                 {
  524.                 Response_2+=4;
  525.                 ;
  526.                 }
  527.         
  528.         do
  529.                 Response=SD_Get_CardInfo();//[4] Read CSD
  530.         while (Response!=0xff00);
  531.         if (Response==0xff01) Response_2+=2;
  532.         
  533.         return Response_2;
  534. //        0000|0000||0000|0000 Response_2
  535. //                  |||_CSD Fail
  536. //                  ||__CMD55+ACMD41 Fail
  537. //                  |___CMD0 Fail
  538. }
  539. //********************************************
  540. unsigned int SD_Get_CardID()//Read CID register
  541. {
  542.         unsigned int temp,Response,MaximumTimes;
  543.         MaximumTimes=10;
  544.         
  545.         for(temp=0;temp<MaximumTimes;temp++)
  546.         {
  547.                 Response=SD_CMD_Write(10,0x00000000,1,1);//Send CMD9
  548.                 if (Response==0xff00)
  549.                         temp=MaximumTimes;
  550.         }
  551.         
  552.          for (temp=0;temp<8;temp++)//Provide 8 clock to romove the first byte of data response (0x00fe)
  553.         {
  554.                 sd_clk=0;//CLK Low
  555.                 Delay5us();
  556.                 sd_clk=1;//CLK High
  557.                 Delay5us();
  558.         }
  559.         
  560.         for (temp=0;temp<8;temp++) ReadBuffer[temp]=SD_2Byte_Read();//Get the CID data
  561.         
  562.         for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
  563.         {
  564.                 sd_clk=0;//CLK Low
  565.                 Delay5us();
  566.                 sd_clk=1;//CLK High
  567.                 Delay5us();
  568.         }
  569.         
  570.         sd_cse=1;//CS_High()
  571.         
  572.         for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
  573.         {
  574.                 sd_clk=0;//CLK Low
  575.                 Delay5us();
  576.                 sd_clk=1;//CLK High
  577.                 Delay5us();
  578.         }
  579.         
  580.         return Response;
  581. }
  582. //********************************************
  583. unsigned int Read_Single_Block(unsigned long int BlockAddress)
  584. {
  585.         unsigned int temp,Response,MaximumTimes;
  586.         MaximumTimes=10;
  587.         
  588.         if (BlockAddress>BlockNR) return 0xff20;//whether BlockAddress out of range?
  589.         
  590.         for(temp=0;temp<MaximumTimes;temp++)
  591.         {
  592.                 Response=SD_CMD_Write(17,BlockAddress,1,1);//Send CMD17
  593.                 if (Response==0xff00)
  594.                         temp=MaximumTimes;
  595.         }
  596.         
  597.         while (SD_Read()!=0xfffe) {;}
  598.         //這里為了使只有512byte的單片機能夠讀寫SD卡,特意節省了RAM的使用量,每次讀寫只有兩個重復的128byte
  599.         //如果您使用的單片機擁有1K以上的RAM請將"%128"去掉        
  600.         for (temp=0;temp<256;temp++) ReadBuffer[temp%128]=SD_2Byte_Read();//Get the readed data
  601.         
  602.         for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
  603.         {
  604.                 sd_clk=0;//CLK Low
  605.                 Delay5us();
  606.                 sd_clk=1;//CLK High
  607.                 Delay5us();
  608.         }
  609.         
  610.         sd_cse=1;//CS_High()
  611.         
  612.         for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
  613.         {
  614.                 sd_clk=0;//CLK Low
  615.                 Delay5us();
  616.                 sd_clk=1;//CLK High
  617.                 Delay5us();
  618.         }
  619.         
  620.         return Response;
  621. }
  622. //********************************************
  623. unsigned int Write_Single_Block(unsigned long int BlockAddress)
  624. {
  625.         unsigned int temp,Response,MaximumTimes;
  626.         MaximumTimes=10;
  627.         
  628.         if (BlockAddress>BlockNR) return 0xff20;//whether BlockAddress out of range?
  629.         
  630.         for(temp=0;temp<MaximumTimes;temp++)
  631.         {
  632.                 Response=SD_CMD_Write(24,BlockAddress,1,1);//Send CMD24
  633.                 if (Response==0xff00)
  634.                         temp=MaximumTimes;
  635.         }
  636.         
  637.         for (temp=0;temp<8;temp++)//Provide 8 extra clock after CMD response
  638.         {
  639.                 sd_clk=0;//CLK Low
  640.                 Delay5us();
  641.                 sd_clk=1;//CLK High
  642.                 Delay5us();
  643.         }
  644.         
  645.         SD_Write(0x00fe);//Send Start Block Token
  646.         //這里為了使只有512byte的單片機能夠讀寫SD卡,特意節省了RAM的使用量,每次讀寫只有兩個重復的128byte
  647.         //如果您使用的單片機擁有1K以上的RAM請將"%128"去掉
  648.         for (temp=0;temp<256;temp++) SD_2Byte_Write(WriteBuffer[temp%128]);//Data Block
  649.         SD_2Byte_Write(0xffff);//Send 2 Bytes CRC
  650.         
  651.         Response=SD_Read();
  652.         while (SD_Read()!=0xffff) {;}
  653.         
  654.         sd_cse=1;//CS_High()
  655.         
  656.         for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
  657.         {
  658.                 sd_clk=0;//CLK Low
  659.                 Delay5us();
  660.                 sd_clk=1;//CLK High
  661.                 Delay5us();
  662.         }
  663.         
  664.         return Response;
  665. }
  666. /* 串行通信初始化*/
  667. void UartInit()
  668. {
  669.         SCON = 0x50;                    // uart 模式1 (8 bit), REN=1;
  670.         TMOD = TMOD | 0x20 ;        // 定時器1 模式2;
  671. //        TH1  = 0xF7;                // 9600 Bds at 32MHz
  672. //        TL1  = 0xF7;        

  673.         TH1  = 0xFD;                // 9600 Bds at 11.059MHz
  674.          TL1  = 0xFD;        
  675. //        TH1  = 0xFA;                // 9600 Bds at 22.1184MHz
  676. //        TL1  = 0xFA;        
  677.         ES = 1;                     //允許uart中斷;
  678.         EA = 1;                            //允許CPU中斷;
  679.         TR1 = 1;                    //運行定時器1:
  680. }
  681.         
  682. void send_char_com(unsigned char ch)  
  683. {
  684.     SBUF=ch;
  685.     while(TI==0);
  686.     TI=0;
  687. }
  688. //向串口發送一個字符串
  689. void send_string_com(unsigned char *str,unsigned int len)
  690. {
  691.     unsigned char k=0;
  692.     do
  693.     {
  694.         send_char_com(*(str + k));
  695.         k++;
  696.     } while(k <len);
  697. }
  698. main()
  699. {
  700.         unsigned int Data,M_Response;
  701.     unsigned char i;
  702.         //初始化要寫入的數據
  703.     for(i=0;i<128;i++)
  704.         {
  705.       if (i < 64)
  706.           {
  707.             WriteBuffer[ i]=0x4141;
  708.           }
  709.           else
  710.           {
  711.                   WriteBuffer[ i]=0x4242;
  712.           }
  713.         }
  714.     UartInit();
  715.          TI = 0;        
  716.     M_Response=0x0000;
  717.          M_Response=SD_Overall_Initiation();
  718.         M_Response=SD_CMD_Write(16,512,1,0);
  719.           Data=SD_Get_CardID();
  720.          Write_Single_Block(0x0000);
  721.     Read_Single_Block(0x0000);
  722.     for(i=0; i<128; i++)
  723.         {         
  724.      send_char_com(ReadBuffer[ i]>>8)  ; //向串口發送高8位數據
  725.      send_char_com(ReadBuffer[ i] )  ;
  726.         }
  727.          
  728.         while(1);         
  729. }
復制代碼
回復

使用道具 舉報

ID:983266 發表于 2021-11-22 00:25 | 顯示全部樓層
你好,后來你這個程序成功了嗎?
回復

使用道具 舉報

ID:96682 發表于 2021-11-22 15:04 來自手機 | 顯示全部樓層
記得 10 年前的大部分手機還用不了 8G TF 卡,
回復

使用道具 舉報

ID:96682 發表于 2021-11-22 15:08 來自手機 | 顯示全部樓層
表現為讀不出卡的大小,讀不出全部內容
回復

使用道具 舉報

ID:983444 發表于 2021-11-22 15:21 | 顯示全部樓層
是不是尋址空間的原因?地址位數不夠,或者處理不恰當
回復

使用道具 舉報

ID:57657 發表于 2021-11-22 17:37 | 顯示全部樓層
long為32位變量,32位最大4G,請拆分成兩個long。
回復

使用道具 舉報

ID:46065 發表于 2022-4-6 21:42 | 顯示全部樓層
FAT32最大支持4G
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲精品国产电影 | 国产精品1区 | 国产精品一区二区欧美 | 国产成人jvid在线播放 | 精品影视| 国产成人精品免高潮在线观看 | 亚洲精品国产电影 | 天天操夜夜操 | 91视频在线看 | 中文字幕一区二区三 | 国产欧美精品一区二区色综合 | 91精品国产综合久久久动漫日韩 | 久久av资源网 | 亚洲www啪成人一区二区麻豆 | 99精品久久久| 日韩成人高清在线 | 日韩欧美在线观看视频 | 欧美aaaaaaaaaa| 国产亚洲精品一区二区三区 | 国产日产欧产精品精品推荐蛮挑 | 国产这里只有精品 | av福利网 | 欧美激情久久久 | 91精品国产91久久久久久密臀 | 国产黄色在线观看 | www.se91| 国产美女特级嫩嫩嫩bbb片 | 91精品国产综合久久婷婷香蕉 | 亚洲精品丝袜日韩 | 亚洲国产专区 | 91高清免费观看 | 亚洲风情在线观看 | 国产精品久久精品 | 国产天堂 | 日本精品久久久一区二区三区 | 91精品国产一区二区三区动漫 | 久久三区| 国产精品综合色区在线观看 | 日韩精品久久久久 | 在线成人免费视频 | 亚洲 中文 欧美 日韩 在线观看 |