找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3055|回复: 0
打印 上一主题 下一主题
收起左侧

SD卡读写SDIO接口整个keil工程带usart调试实测可用

[复制链接]
跳转到指定楼层
楼主
ID:39682 发表于 2017-9-7 10:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
亲测SD卡读写SDIO接口可用。
CPU  STM32F103ZET6.

所有资料51hei提供下载:
SD卡读写-SDIO接口 代码.7z (159.08 KB, 下载次数: 35)

单片机源程序如下:
  1. /******************** (C) COPYRIGHT 2009 奋斗嵌入式开发工作室 ********************
  2. * File Name          : main.c  
  3. * Author             : Sun68
  4. * Version            : V1.0
  5. * Date               : 11/11/2009
  6. * Description        : 演示将一段字符串写入AT45DB161D的1页中,然后读出并通过USART1传送出去。
  7.                        字符串:SPI AT45DB161D Example: This is SPI DEMO, 终端上出现这一行字,说明AT45DDB161的读写正常
  8.                        
  9.     定义:        
  10.         USART1
  11.         TXD1----- PA9-US1-TX
  12.         RXD1----- PA10-US1-RX          速率:115200,n,8,1

  13.         SPI2
  14.         NSS---PB12-SPI2-NSS
  15.     MISO--PB14-SPI2-MISO
  16.         MOSI--PB15-SPI2-MOSI
  17.         SCK---PB13-SPI2-SCK

  18.         
  19. *********************************************************************/
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x.h"
  22. #include "platform_config.h"
  23. #include "stm32f10x_usart.h"
  24. #include "misc.h"


  25. extern void SPI_Flash_Init(void);
  26. extern u8 SPI_Flash_ReadByte(void);
  27. extern u8 SPI_Flash_SendByte(u8 byte);
  28. extern void FlashPageEarse(u16 page);
  29. extern void FlashPageRead(u16 page,u8 *Data);
  30. extern void FlashPageWrite(u16 page,u8 *Data);
  31. extern void FlashWaitBusy(void);
  32. extern void AT45_RomTo_buf(unsigned char buffer,unsigned int page);
  33. extern u8 AT45_buf_ToRam(unsigned char buffer,unsigned int start_address,unsigned int length);
  34. extern u8 AT45_RamTo_buf(unsigned char buffer,unsigned int start_address,unsigned int length);         
  35. extern void AT45_buf_ToRom(unsigned char buffer,unsigned int page);
  36. extern void AT45_page_earse(unsigned int page);

  37. extern unsigned char AT45_buffer[];
  38. /** @addtogroup StdPeriph_Examples
  39.   * @{
  40.   */

  41. /** @addtogroup USART_Interrupt
  42.   * @{
  43.   */

  44. /* Private typedef -----------------------------------------------------------*/
  45. typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

  46. /* Private define ------------------------------------------------------------*/
  47. #define TxBufferSize1   (countof(TxBuffer1) - 1)
  48. #define RxBufferSize1   (countof(TxBuffer1) - 1)

  49. /* Private macro -------------------------------------------------------------*/
  50. #define countof(a)   (sizeof(a) / sizeof(*(a)))

  51. /* Private variables ---------------------------------------------------------*/
  52. USART_InitTypeDef USART_InitStructure;
  53. uint8_t TxBuffer1[] = "SPI AT45DB161D Example: This is SPI DEMO, 终端上出现这一行字,说明AT45DDB161的读写正常";
  54. uint8_t RxBuffer1[RxBufferSize1],rec_f;
  55. __IO uint8_t TxCounter1 = 0x00;
  56. __IO uint8_t RxCounter1 = 0x00;
  57. uint8_t NbrOfDataToTransfer1 = TxBufferSize1;
  58. uint8_t NbrOfDataToRead1 = RxBufferSize1;
  59. __IO TestStatus TransferStatus1 = FAILED;

  60. /* Private function prototypes -----------------------------------------------*/
  61. void RCC_Configuration(void);
  62. void GPIO_Configuration(void);
  63. void NVIC_Configuration(void);

  64. void Delay(__IO uint32_t nCount);
  65. void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len);

  66. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. USART_InitTypeDef USART_InitStruct;
  69. USART_ClockInitTypeDef USART_ClockInitStruct;
  70. int16_t USART_FLAG;

  71. /* Private functions ---------------------------------------------------------*/

  72. /**
  73.   * @brief  Main program
  74.   * @param  None
  75.   * @retval : None
  76.   */
  77. int main(void)
  78. {

  79.    uint8_t a=0;
  80.    uint16_t i=0;
  81.   /* System Clocks Configuration */
  82.   RCC_Configuration();
  83.       
  84.   /* NVIC configuration */
  85.   NVIC_Configuration();

  86.   /* Configure the GPIO ports */
  87.   GPIO_Configuration();
  88.   
  89. /* USART1 configuration ------------------------------------------------------*/
  90.   /* USART and USART2 configured as follow:
  91.         - BaudRate = 115200 baud  
  92.         - Word Length = 8 Bits
  93.         - One Stop Bit
  94.         - No parity
  95.         - Hardware flow control disabled (RTS and CTS signals)
  96.         - Receive and transmit enabled
  97.   */
  98.   USART_InitStructure.USART_BaudRate = 115200;
  99.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  100.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  101.   USART_InitStructure.USART_Parity = USART_Parity_No;
  102.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  103.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  104.   /* Configure USART1 */
  105.   USART_Init(USART1, &USART_InitStructure);

  106.   
  107.   /* Enable USART1 Receive and Transmit interrupts */
  108.   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  109.   USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
  110.    
  111.   /* Enable the USART1 */
  112.   USART_Cmd(USART1, ENABLE);
  113.   SPI_Flash_Init();
  114.   
  115.   GPIO_SetBits(GPIOB, GPIO_Pin_5);
  116.   
  117.   for(i=0; i<TxBufferSize1;i++) AT45_buffer[i]=TxBuffer1[i];

  118.   AT45_RamTo_buf(1,0,TxBufferSize1);  
  119.   AT45_buf_ToRom(1,1);  //1区to一页            

  120.   
  121.   AT45_RomTo_buf(1,1);        //1页to1区  
  122.   AT45_buf_ToRam(1,0,TxBufferSize1);   
  123.   //GPIO_SetBits(GPIOB, GPIO_Pin_5); while(1);
  124.   //GPIO_ResetBits(GPIOB, GPIO_Pin_5);
  125.   a=0;
  126.   for(i=0; i<TxBufferSize1;i++){
  127.           if(AT45_buffer[i]==TxBuffer1[i]) a=1;
  128.         else {a=0; i=TxBufferSize1;}
  129.   }
  130.   
  131.   if(a==1) USART_OUT(USART1,&AT45_buffer[0],TxBufferSize1);         
  132.    while (1)
  133.   {        
  134.   }
  135. }


  136. void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len){
  137.         uint16_t i;
  138.         for(i=0; i<Len; i++){
  139.                 USART_SendData(USARTx, Data[i]);
  140.                 while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
  141.         }
  142. }

  143. void Delay(__IO uint32_t nCount)
  144. {
  145.   for(; nCount != 0; nCount--);
  146. }

  147. void RCC_Configuration(void)
  148. {
  149.   /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
  150.      initialize the PLL and update the SystemFrequency variable. */
  151.   SystemInit();
  152.   
  153.   /* Enable USART1, GPIOA, GPIOx and AFIO clocks */
  154.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOx
  155.                          | RCC_APB2Periph_AFIO, ENABLE);
  156.   /* Enable USART2 clock */
  157.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  158. }

  159. /**
  160.   * @brief  Configures the different GPIO ports.
  161.   * @param  None
  162.   * @retval : None
  163.   */
  164. void GPIO_Configuration(void)
  165. {

  166.   RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 |RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
  167.                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
  168.                          RCC_APB2Periph_GPIOE, ENABLE);
  169.          
  170.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                                     //LED1
  171.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  172.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  173.   GPIO_Init(GPIOB, &GPIO_InitStructure);                                         

  174.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_3;                 //LED2, LED3
  175.   GPIO_Init(GPIOD, &GPIO_InitStructure);

  176.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                 //LCD 背光控制
  177.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  178.   GPIO_ResetBits(GPIOE, GPIO_Pin_0);                           


  179.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                 //USART1 TX
  180.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    //复用推挽输出
  181.   GPIO_Init(GPIOA, &GPIO_InitStructure);                    //A端口

  182.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                 //USART1 RX
  183.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //复用开漏输入
  184.   GPIO_Init(GPIOA, &GPIO_InitStructure);                         //A端口

  185.   //GPIO_PinRemapConfig(AFIO_MAPR_USART1_REMAP, ENABLE);
  186. }

  187. /**
  188.   * @brief  Configures the nested vectored interrupt controller.
  189.   * @param  None
  190.   * @retval : None
  191.   */
  192. void NVIC_Configuration(void)
  193. {
  194.   NVIC_InitTypeDef NVIC_InitStructure;

  195.   /* Configure the NVIC Preemption Priority Bits */  
  196.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  197.   
  198.   /* Enable the USART1 Interrupt */
  199.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  200.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  201.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  202.   NVIC_Init(&NVIC_InitStructure);

  203.   
  204. }

  205. /**
  206.   * @brief  Compares two buffers.
  207.   * @param pBuffer1, pBuffer2: buffers to be compared.
  208.   * @param BufferLength: buffer's length
  209.   * @retval : PASSED: pBuffer1 identical to pBuffer2
  210.   *   FAILED: pBuffer1 differs from pBuffer2
  211.   */
  212. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  213. {
  214.   while(BufferLength--)
  215.   {
  216.     if(*pBuffer1 != *pBuffer2)
  217.     {
  218.       return FAILED;
  219.     }

  220.     pBuffer1++;
  221.     pBuffer2++;
  222.   }

  223.   return PASSED;
  224. }
  225. ……………………

  226. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表