[新手园地] 傻瓜菜鸟第7帖——SPI

[复制链接]
 楼主| weshiluwei6 发表于 2011-5-26 23:30 | 显示全部楼层 |阅读模式
本帖最后由 weshiluwei6 于 2011-5-26 23:33 编辑


本人初次接触SPI,三心爱意大哥的程序好长,水王愚钝,花了半天才看懂一二,把大哥的程序注释了下,做了自己的理解。

1.
W25Q16BV 读状态寄存器1函数



/*
发送数据到 SPI 总线: 0x0500 (Read status register 1) */

ByteCode = 0x0500;

注释:此处不解,datasheet里写的是05H 为什么此处是0500 改成0005 程序无**常运行

2.W25Q16BV
忙状态检查函数
SPI_WaitReady

S7  S6  S5  S4  S3  S2  S1  S0

SRP0 SEC TB BP2 BP1 BP0 WEL BUSY
检测最低位( BusyFlag =SPI_ReadStatusReg1();
              BusyFlag&=0x01;

3.
W25Q16BV按页编程函数
SPI_PageProgram


配置8比特传输,写,擦除


配置8比特传输数据(由图),配置从模式,配置写。
配置24比特传输地址
配置8比特写数据



4.
W25Q16BV读数据函数



配置8比特,使能CS,写模式,判繁忙
配置24比特 address
配置8比特  读数据



















本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| weshiluwei6 发表于 2011-5-26 23:33 | 显示全部楼层
  1. #include
  2. #include "NUC1xx.h"
  3. #include "Driver\DrvGPIO.h"
  4. #include "Driver\DrvSYS.h"
  5. #include "Driver\DrvUART.h"
  6. #include "Driver\DrvSPI.h"


  7. volatile uint8_t IsStart = FALSE;
  8. volatile uint8_t Receive_Data = 0;

  9. /*  PA14(38脚)接的SPI_Flash_CS   */
  10. #define  SPI_CS_PORT                E_GPA
  11. #define  SPI_CS_PORT_NUM        14
  12. #define  Enable_SPI_CS                DrvGPIO_ClrBit(SPI_CS_PORT,SPI_CS_PORT_NUM)       
  13. #define  DISABLE_SPI_CS                DrvGPIO_SetBit(SPI_CS_PORT,SPI_CS_PORT_NUM)       


  14. /**************************************************************************************
  15. ** Name:      SPI_ReadStatusReg1
  16. ** Function:  W25Q16BV读状态寄存器1函数      
  17. ***************************************************************************************/
  18. uint32_t SPI_ReadStatusReg1(void)
  19. {
  20.    /*
  21.        Status  Register - 1
  22.                S7      S6     S5      S4        S3      S2     S1      S0
  23.        SRP0           SEC           TB           BP2                 BP1     BP0    WEL     BUSY

  24.            Status  Register - 2
  25.               S15             S14     S13     S12       S11     S10    S9      S8
  26.      Suspend Status           (R)           (R)           (R)                 (R)     (R)    CE      SRP1
  27.    */
  28.     uint32_t ByteCode;                  //相当于命令
  29.     uint32_t ReceiveData;                                //接收
  30.                 
  31.         /*   配置SPI传输的比特长度:16 bits  */
  32.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 16);               
  33.         Enable_SPI_CS;

  34.         /*  发送数据到 SPI 总线: 0x0500 (Read status register 1) */
  35.         ByteCode = 0x0500;
  36.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);
  37.        
  38.         while (DrvSPI_IsBusy(eDRVSPI_PORT1));               
  39.         DISABLE_SPI_CS;
  40.         /*
  41.            从接收寄存器读数据.  但是不触发下一次数据传输.
  42.            uint32_t DrvSPI_DumpRxRegister(eSpiPort,*pu32Buf,DataCount)
  43.            (SPI端口,缓存指针,用来存放从接收器收到的数据,要收的数据笔数,不能超过2笔)
  44.            */
  45.         DrvSPI_DumpRxRegister(eDRVSPI_PORT1, &ReceiveData, 1);
  46.         return (ReceiveData & 0xFF);
  47. }


  48. /*****************************************************************************************
  49. ** Name:      SPI_WaitReady
  50. ** Function:  W25Q16BV忙状态检查函数
  51. *******************************************************************************************/
  52. void SPI_WaitReady(void)
  53. {
  54. uint32_t  BusyFlag;

  55. /*
  56.     S7      S6     S5      S4        S3      S2     S1      S0
  57.    SRP0           SEC           TB           BP2                 BP1     BP0    WEL     BUSY

  58.    检查从设备状态寄存器1的BUSY位 等待其为0
  59. */
  60. do
  61. {
  62.   BusyFlag     =     SPI_ReadStatusReg1();
  63.   BusyFlag    &=     0x01;
  64. }while( BusyFlag != 0 );

  65. }

  66. /**********************************************************************************
  67. ** Name:      SPI_PageProgram
  68. ** Function:  W25Q16BV按页编程函数  
  69. *********************************************************************************/
  70. void SPI_PageProgram(uint8_t *DataBuffer,uint32_t StartAddress,uint32_t ByteCount)
  71. {
  72.   uint32_t    ByteCode;
  73.   uint32_t    Counter;
  74.   /*
  75.     DrvSPI_SetBitLength(E_DRVSPI_PORT eSpiPort,BitLength)
  76.         这里选择配置SPI传输的比特长度:8 bits  
  77.   */
  78.   DrvSPI_SetBitLength(eDRVSPI_PORT1,8);
  79.   /*
  80.    激活/配置从设备片选信号
  81.   */
  82.   Enable_SPI_CS;
  83.   /* Enable Write(06H) From DataSheet */
  84.   ByteCode  =  0x06;
  85.   /*
  86.      BOOL  DrvSPI_SingleWrite( eSpiPort,*pu32Data )
  87.          发送数据到SPI总线
  88.   */
  89.   DrvSPI_SingleWrite( eDRVSPI_PORT1,&ByteCode);
  90.   while( DrvSPI_IsBusy(eDRVSPI_PORT1) ){}
  91.   DISABLE_SPI_CS;
  92.   Enable_SPI_CS;

  93.   /*
  94.                           由手册可知
  95.                              Page  Program  Instruction Sequence Diagram
  96.                                  FIRST:   Instrustion  02H
  97.                                  Second:  24-Bit Address
  98.                                  Third:   Data Byte1
  99.                                           Data Byte2
  100.                                                     …………………………
  101.                                                   Data Byte256
  102.   */
  103.   /* Page Program(02H) From DataSheet
  104.      发送数据到 SPI 总线: 0x02 */

  105.   ByteCode    =     0x02;
  106.   DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);

  107.   while (DrvSPI_IsBusy(eDRVSPI_PORT1));

  108.    /*  配置SPI传输的比特长度:24 bits  */
  109.   DrvSPI_SetBitLength(eDRVSPI_PORT1, 24);
  110.                        
  111.   /*        BIT- Address        */       
  112.   ByteCode    =    StartAddress;
  113.   DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);                
  114.   while (DrvSPI_IsBusy(eDRVSPI_PORT1));
  115.        
  116.   /* DATABIT  8位  */
  117.   DrvSPI_SetBitLength(eDRVSPI_PORT1, 8);                       
  118.   for(Counter=0; Counter<ByteCount; Counter++)
  119.   {
  120.                 ByteCode   =    DataBuffer[Counter];
  121.             DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);               
  122.                 while (DrvSPI_IsBusy(eDRVSPI_PORT1));
  123.   }
  124.          
  125.         DISABLE_SPI_CS;
  126. }


  127. /****************************************************************************************
  128. ** Name:      SPI_ChipErase
  129. ** Function:  W25Q16BV片擦除函数
  130. *****************************************************************************************/
  131. void SPI_ChipErase(void)
  132. {
  133.        
  134.         uint32_t ByteCode;
  135.         /*  配置SPI传输的比特长度:8 bits        */
  136.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 8);
  137.         Enable_SPI_CS;

  138.         /*  发送数据到 SPI 总线: 0x06 (Write enable)        */
  139.         ByteCode    =     0x06;
  140.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);
  141.         while (DrvSPI_IsBusy(eDRVSPI_PORT1));

  142.         DISABLE_SPI_CS;
  143.         Enable_SPI_CS;

  144.     /* 发送数据到 SPI 总线: 0xC7 (Chip Erase) */
  145.         ByteCode        =      0xC7;
  146.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);

  147.         while (DrvSPI_IsBusy(eDRVSPI_PORT1));
  148.         DISABLE_SPI_CS;
  149. }

  150. /*****************************
  151. ** Name:      SPI_ReadData
  152. ** Function:  W25Q16BV读数据函数
  153. ***********************************/
  154. void SPI_ReadData(uint8_t *DataBuffer, uint32_t StartAddress, uint32_t ByteCount)
  155. {
  156.         uint32_t ByteCode;
  157.     uint32_t ReceiveData;        
  158.            uint32_t Counter;

  159.         /* 配置SPI传输的比特长度:8 bits */
  160.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 8);
  161.         Enable_SPI_CS;

  162.         /*  发送数据到 SPI 总线: 0x03 (Read data) */
  163.         ByteCode    =    0x03;
  164.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);        

  165.         while (DrvSPI_IsBusy(eDRVSPI_PORT1));

  166.     /*  配置SPI传输的比特长度:24 bits  */
  167.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 24);

  168.         /*   发送数据到 SPI 总线: StartAddress   */
  169.         ByteCode = StartAddress;
  170.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);

  171.         while (DrvSPI_IsBusy(eDRVSPI_PORT1)) {}

  172.         /*  配置SPI传输的比特长度:8 bits   */
  173.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 8);
  174.                        
  175.         for(Counter=0; Counter<ByteCount; Counter++)
  176.         {
  177.                 ByteCode = 0x0;
  178.                 DrvSPI_SingleWrite(eDRVSPI_PORT1, &ByteCode);
  179.                
  180.                 while (DrvSPI_IsBusy(eDRVSPI_PORT1));
  181.                
  182.                 /*   从接收寄存器读数据.  但是不触发下一次数据传输.        */
  183.                 DrvSPI_DumpRxRegister(eDRVSPI_PORT1, &ReceiveData, 1);
  184.        
  185.                 DataBuffer[Counter] = (uint8_t) ReceiveData;
  186.         }       
  187.         DISABLE_SPI_CS;
  188. }

  189. /******************************************************************************************
  190. ** Name:      UART_INT_HANDLE
  191. ** Function:  UART Callback function      
  192. *******************************************************************************************/
  193. void UART_INT_HANDLE(uint32_t u32IntStatus)
  194. {

  195.         uint8_t bInChar[1]={0xFF};

  196.         if(u32IntStatus & DRVUART_RDAINT)
  197.         {
  198.                 /* Get all the input characters */
  199.                 while(UART0->ISR.RDA_IF==1)
  200.                 {
  201.                         /* Get the character from UART Buffer */
  202.                         DrvUART_Read(UART_PORT0,bInChar,1);
  203.                         if (IsStart!=TRUE)
  204.                         {
  205.                                 IsStart = TRUE;
  206.                                 Receive_Data = bInChar[0];
  207.                         }
  208.                 }
  209.         }
  210. }

  211. /*******************************************************************************************
  212. ** Name:      Init_System
  213. ** Function:  系统初始化函数      
  214. *******************************************************************************************/
  215. void Init_System(void)
  216. {
  217.         /* Unlock the locked registers before access */
  218.     UNLOCKREG(x);        //寄存器锁定键地址寄存器(RegLockAddr) :有些系统控制寄存器需要被保护起来,以防止误操作而影响芯片运行,
  219.                                     //这些寄存器在上电复位到用户解锁定之前是锁定的。用户可以连续依次写入“59h”, “16h” “88h”到0x5000_0100解锁定.
  220.         /* Enable the 12MHz oscillator oscillation */
  221.         DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);                                                                //SYSCLK->PWRCON.XTL12M_EN = 1;

  222.         /* Waiting for 12M Xtal stable */
  223.         //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);                //SYSCLK->CLKSTATUS.XTL12M_STB
  224.         /*eClkSrc  - [in]  E_SYS_XTL12M / E_SYS_XTL32K / E_SYS_OSC22M / E_SYS_OSC10K / E_SYS_PLL    */
  225.         // Note: Only some of NuMicro NUC100 Series support this function.

  226.         DrvSYS_Delay(5000);
  227.         LOCKREG(x);
  228.         //向“0x5000_0100”写入任何值,就可以重锁保护寄存器
  229. }

  230. void Init_Uart(void)
  231. {
  232.         STR_UART_T param;
  233.         /*
  234.         声明 UART设置的结构体 位于DRVUART.H
  235.         结构体如下
  236.         typedef struct DRVUART_STRUCT
  237.         {
  238.                 uint32_t            u32BaudRate;                         
  239.                 E_DATABITS_SETTINS  u8cDataBits;                         
  240.                 E_STOPBITS_SETTINS  u8cStopBits;                         
  241.                 E_PARITY_SETTINS         u8cParity;                                 
  242.                 E_FIFO_SETTINGS     u8cRxTriggerLevel;                 
  243.                 uint8_t             u8TimeOut ;                                 
  244.       }STR_UART_T;
  245.         */
  246.         DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);                    //使能UART时钟
  247.         //SYSCLK->CLKSEL1.UART_S = 0;                //UART时钟源选择. 00 =外部12MHz 晶振 01 = PLL 1x =内部 22MHz 振荡器

  248.         DrvGPIO_InitFunction(E_FUNC_UART0);                                                    //GPB_MFP0-1-2-3置位 GPIO使能UART功能
  249.         //outpw(&SYS->GPBMFP, inpw(&SYS->GPBMFP) | (0xF<<0));

  250.         param.u32BaudRate        = 115200;                                                        //         波特率
  251.         param.u8cDataBits        = DRVUART_DATABITS_8;                                //         数据位
  252.         param.u8cStopBits        = DRVUART_STOPBITS_1;                                //         停止位
  253.         param.u8cParity          = DRVUART_PARITY_NONE;                                //         校验位
  254.         param.u8cRxTriggerLevel  = DRVUART_FIFO_1BYTES;                                //         FIFO存储深度 1 字节
  255.         param.u8TimeOut                 = 0;                                                                //         FIFO超时设定
  256.         /* Set UART Configuration */
  257.         if(DrvUART_Open(UART_PORT0,&param) != E_SUCCESS)                        //         串口开启、结构体整体赋值
  258.                 printf("UART0 open failed\n");                                                 
  259.         /* u32Port -[in] UART Channel:  UART_PORT0 / UART_PORT1 /UART_PORT2        */
  260.         /* sParam  -[in] the struct parameter to configure UART                         */   

  261.         /* Enable Interrupt and install the call back function */
  262.         DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE);
  263.         /*u32Port                   -[in] UART Channel:  UART_PORT0 / UART_PORT1 / UART_PORT2                                               */
  264.         /*u32InterruptFlag -[in] DRVUART_LININT/DRVUART_WAKEUPINT/DRVUART_BUFERRINT/DRVUART_RLSINT                          */
  265.         /*                                                                                DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT          */
  266.         /*pfncallback      -[in] A function pointer for callback function                                                   */
  267. }

  268. /*****************************************************************************************
  269. ** Name:      Init_SPI
  270. ** Function:  SPI初始化函数   
  271. *****************************************************************************************/
  272. void Init_SPI(void)
  273. {
  274.     /*
  275.           DrvSPI_Open(eSpiPort,eMode,eType,i32BitLength)
  276.           打开SPI功能
  277.           配置SPI工作在主/从模式,SPI总线时序和每笔传输的长度
  278.           这里与手册略有差别
  279.           DrvSPI_Open(E_DRVSPI_PORT eSpiPort, E_DRVSPI_MODE eMode,
  280.                      E_DRVSPI_TRANS_TYPE eType, int32_t i32BitLength, uint8_t bQFN36PIN)
  281.         */
  282.         /*  配置SPI1为主模式 TYPE1波形 32位传输         */
  283.         DrvSPI_Open(eDRVSPI_PORT1, eDRVSPI_MASTER, eDRVSPI_TYPE1, 32,FALSE);       

  284.         /*  配置传输比特的顺序:优先发送/接收MSB         */
  285.         DrvSPI_SetEndian(eDRVSPI_PORT1, eDRVSPI_MSB_FIRST);                       
  286.        
  287.         /* 这里与手册不同
  288.           DrvSPI_DisableAutoCS( eSpiPort )
  289.           这个函数用来关闭自动片选功能,如果在多次传输过程中需要一直保持高/低
  290.           用户应该关闭自动片选功能,改为手动控制,只用于NUC1xx是主模式       
  291.         */
  292.         DrvSPI_DisableAutoSS(eDRVSPI_PORT1);
  293.        
  294.         /*
  295.           设定从选择线的激活级别:
  296.           DrvSPI_SetSlaveSelectActiveLevel
  297.           (eSpiPort,eSSActType)
  298.           Type: eDRVSPI_ACTIVE_LOW _FALLING:从片选择信号低/下降沿激活
  299.                 eDRVSPI_ACTIVE_HIGH_RISING :从片选择信号高/上升沿激活          
  300.         */
  301.         DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_PORT1, eDRVSPI_ACTIVE_LOW_FALLING);
  302.        
  303.         /* 与手册不同
  304.            DrvSPI_Set2BitSerialDataIOMode( eSpiPort,bEnable )
  305.            设置2比串行数据IO模式   TRUE(使能)/关闭       
  306.         */       
  307.         DrvSPI_Set2BitTransferMode(eDRVSPI_PORT1, TRUE);

  308.         /* 与手册不同 设置SPI时钟1MHZ
  309.            DrvSPI_SetClock( eSpinPort,Clock1,Clock2 )
  310.            在固定时钟频率模式下,Clock1是SPI的基本时钟频率,在可变模式下,他是可变时钟1
  311.            CLOCK2,他是可变时钟2       
  312.         */
  313.         DrvSPI_SetClockFreq(eDRVSPI_PORT1, 1000000, 0);

  314.         DrvGPIO_Open(SPI_CS_PORT,SPI_CS_PORT_NUM, E_IO_OUTPUT);                                  //SPI_FLAH_CS
  315.         DISABLE_SPI_CS;       
  316. }

  317. /******************************************************************************************
  318. ** Name:      SPI_ReadMidDid
  319. ** Function:  W25Q16BV读制造商ID及设备ID函数:      
  320. *******************************************************************************************/
  321. uint32_t SPI_ReadMidDid(void)
  322. {
  323.         uint32_t au32SourceData;
  324.     uint32_t au32DestinationData;        

  325.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 8);
  326.         //配置SPI传输的比特长度:8 bits

  327.         Enable_SPI_CS;
  328.         //激活/配置从设备片选信号
  329.        
  330.         au32SourceData = 0x90;
  331.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &au32SourceData);
  332.         //发送数据到 SPI 总线: 0x90 (Read Manufacturer/Device ID)
  333.            
  334.         while (DrvSPI_IsBusy(eDRVSPI_PORT1)) {}
  335.         //等待SPI端口空闲
  336.        
  337.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 24);       
  338.         //配置SPI传输的比特长度:24 bits

  339.         au32SourceData = 0x0;
  340.     DrvSPI_SingleWrite(eDRVSPI_PORT1, &au32SourceData);
  341.         //发送数据到 SPI 总线: 0x00 (24-bit Address)

  342.         while (DrvSPI_IsBusy(eDRVSPI_PORT1)) {}
  343.         //等待SPI端口空闲
  344.    
  345.         DrvSPI_SetBitLength(eDRVSPI_PORT1, 16);
  346.         //配置SPI传输的比特长度:16 bits


  347.         au32SourceData = 0x0;
  348.         DrvSPI_SingleWrite(eDRVSPI_PORT1, &au32SourceData);
  349.         //接收数据
  350.        
  351.         while (DrvSPI_IsBusy(eDRVSPI_PORT1)) {}
  352.         //等待SPI端口空闲
  353.        
  354.         DISABLE_SPI_CS;
  355.         //从设备片选信号取消激活

  356.         DrvSPI_DumpRxRegister(eDRVSPI_PORT1, &au32DestinationData, 1);
  357.         //从接收寄存器读数据.  但是不触发下一次数据传输.

  358.     return (au32DestinationData & 0xffff);
  359. }

  360. int main(void)
  361. {
  362. uint8_t   test  =   250;
  363. uint32_t  Tmp   =   0;
  364. uint8_t   DataBuffer[256];

  365. Init_System();
  366. Init_Uart();
  367. Init_SPI();

  368. DrvGPIO_Open(E_GPA,14,E_IO_OUTPUT);//程序运行指示灯
  369. DrvGPIO_ClrBit( E_GPA,14 );

  370. printf("\n");
  371. printf("======菜农 %d 助学计划======\n", test);
  372. printf("========NUC120助学板========\n");
  373. printf("======程序模仿三心爱意0322例程======\n");
  374. printf("=======2011年05月26日=======\n");
  375. printf("=======SPI(查询方式)=====\n");

  376. /* WINBOND   W25Q16BV Page 0 初始化         */

  377. SPI_WaitReady();
  378. Tmp = SPI_ReadMidDid();
  379. printf("W25Q16BV制造商ID及设备ID为:0x%X\n",Tmp);

  380. SPI_WaitReady();
  381. SPI_ReadData( DataBuffer,0x1000,1 );

  382. if( DataBuffer[0]  != 0xff)
  383. {
  384.   DataBuffer[0]  =  0xff;
  385.   SPI_WaitReady();
  386.   SPI_ChipErase();
  387.   SPI_WaitReady();
  388.   SPI_PageProgram( DataBuffer,0x1000,1 );
  389.   for( Tmp=0;Tmp<256;Tmp++)DataBuffer[Tmp] = Tmp;
  390.   SPI_PageProgram( DataBuffer,0,256 );
  391. }

  392. /*  WINBOND  W25Q16BV Page 0 读取          */
  393. printf("Read Flash (Page 0) ...\n");
  394.         SPI_WaitReady();
  395.         SPI_ReadData(DataBuffer,0,256);
  396.         for (Tmp=0;Tmp<256;Tmp++)
  397.         {
  398.                 printf("0x%X ",DataBuffer[Tmp]);
  399.                 if ((Tmp%16)==15)printf("\n");
  400.         }
  401.         printf("Read Flash (Page 0) done!\n");

  402.         printf("'R/r'为读指令、'U/u'为Page 0加1并存储指令、'D/d'为Page 0减1并存储指令\n");
  403.         printf("====请输入字符开始测试!===\n");
  404.         printf("==========================*/\n");

  405.     while(1)
  406.     {
  407.         if (IsStart)
  408.         {
  409.                 switch (Receive_Data)
  410.                 {
  411.                         case 'R':
  412.                                 case 'r':
  413.                                         printf("Read Flash (Page 0) ...\n");
  414.                                         SPI_WaitReady();
  415.                                         SPI_ReadData(DataBuffer,0,256);
  416.                                         for (Tmp=0;Tmp<256;Tmp++)
  417.                                         {
  418.                                                 printf("0x%X ",DataBuffer[Tmp]);
  419.                                                 if ((Tmp%16)==15)printf("\n");
  420.                                         }
  421.                                         printf("Read Flash (Page 0) done!\n");
  422.                                         break;
  423.                                 case 'U':
  424.                                 case 'u':
  425.                                         printf("Program Flash (Page 0)[Add 1] ...\n");
  426.                                         SPI_WaitReady();
  427.                                         SPI_ReadData(DataBuffer,0,256);
  428.                                         for (Tmp=0;Tmp<256;Tmp++)
  429.                                         {
  430.                                                 DataBuffer[Tmp] += 1;
  431.                                         }
  432.                                         SPI_WaitReady();
  433.                                         SPI_PageProgram(DataBuffer,0,256);
  434.                                         printf("Program Flash (Page 0)[Add 1] done!\n");
  435.                                         break;
  436.                                 case 'D':
  437.                                 case 'd':
  438.                                         printf("Program Flash (Page 0)[Minus 1] ...\n");
  439.                                         SPI_WaitReady();
  440.                                         SPI_ReadData(DataBuffer,0,256);
  441.                                         for (Tmp=0;Tmp<256;Tmp++)
  442.                                         {
  443.                                                 DataBuffer[Tmp] -= 1;
  444.                                         }
  445.                                         SPI_WaitReady();
  446.                                         SPI_PageProgram(DataBuffer,0,256);
  447.                                         printf("Program Flash (Page 0)[Minus 1] done!\n");
  448.                                         break;
  449.                                 default:
  450.                                         printf("请确认您输入的指令是否合法!\n");
  451.                                
  452.                 }
  453.                 IsStart = FALSE;
  454.         }
  455.     }
  456. }
Swallow_0322 发表于 2011-5-27 08:12 | 显示全部楼层
顶水王!

我理解读状态寄存器1的指令可以为16位的0x0500、可以为0x0005、可以为0x0505、还可以为8位的0x05!详见下图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
hotpower 发表于 2011-5-27 09:11 | 显示全部楼层
进步很大,这样的学习方法很正确!
开始学习要从山寨做起。
 楼主| weshiluwei6 发表于 2011-5-27 11:02 | 显示全部楼层
3# Swallow_0322

谢谢大哥 可是我理解 就是05H 不是只读寄存器1么 可我换成05H以后 我看了串口 是不对的 输入R 和U都米反应的
Swallow_0322 发表于 2011-5-27 11:20 | 显示全部楼层
3# Swallow_0322  

谢谢大哥 可是我理解 就是05H 不是只读寄存器1么 可我换成05H以后 我看了串口 是不对的 输入R 和U都米反应的
weshiluwei6 发表于 2011-5-27 11:02


贴出来读寄存器1函数代码看看,最近没时间搞M0!
 楼主| weshiluwei6 发表于 2011-5-28 07:30 | 显示全部楼层
6# Swallow_0322
时序图就是您发的那个

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| weshiluwei6 发表于 2011-5-28 07:30 | 显示全部楼层
4# hotpower

哈哈 俺是菜鸟 多谢大叔指点
hotpower 发表于 2011-5-28 08:59 | 显示全部楼层
模仿很重要。
Swallow_0322 发表于 2011-5-28 09:37 | 显示全部楼层
7# weshiluwei6

用下面的读寄存器1状态的函数试试,因为比较忙再加之最近有点累,所以没有亲自测试,有时间你可以试试!

  1. /*****************************
  2. ** Name:      SPI_ReadStatusReg1
  3. ** Function:  W25Q16BV读状态寄存器1函数
  4. ** Input:     None
  5. ** OutPut:    ReadStatusReg1
  6. ** Data:      2011-05-28
  7. ** Note:      用8位的ReadStatusReg1指令0x05读寄存器(未测试)
  8. ****************************/
  9. uint32_t SPI_ReadStatusReg1(void)
  10. {
  11.     uint32_t au32SourceData;
  12.     uint32_t au32DestinationData;  
  13. DrvSPI_SetBitLength(eDRVSPI_PORT1, 8);
  14. //配置SPI传输的比特长度:8 bits
  15.   
  16. Enable_SPI_CS;
  17. //激活/配置从设备片选信号

  18. au32SourceData = 0x05;
  19. DrvSPI_SingleWrite(eDRVSPI_PORT1, &au32SourceData);
  20. //发送数据到 SPI 总线: 0x05 (Read status register 1)
  21. while (DrvSPI_IsBusy(eDRVSPI_PORT1)) {}
  22. //等待SPI端口空闲
  23. au32SourceData = 0x0;
  24. DrvSPI_SingleWrite(eDRVSPI_PORT1, &au32SourceData);
  25. //接收数据

  26. while (DrvSPI_IsBusy(eDRVSPI_PORT1)) {}
  27. //等待SPI端口空闲
  28.   
  29. DISABLE_SPI_CS;
  30. //从设备片选信号取消激活
  31. DrvSPI_DumpRxRegister(eDRVSPI_PORT1, &au32DestinationData, 1);
  32. //从接收寄存器读数据.  但是不触发下一次数据传输.
  33. return (au32DestinationData & 0xFF);
  34. }

plc_avr 发表于 2011-5-28 19:30 | 显示全部楼层
这几天忙于其它事,有空也玩玩,多谢共享!
 楼主| weshiluwei6 发表于 2011-5-28 20:39 | 显示全部楼层
10# Swallow_0322
俺就是把0500 给成
au32SourceData = 0x05;之后 发现 串口输入D U 没反应
Swallow_0322 发表于 2011-5-30 07:54 | 显示全部楼层
12# weshiluwei6

呵呵!用我给你的函数测试了吗?
564451696 发表于 2011-10-13 15:34 | 显示全部楼层
看看,学学,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:250水王,种菜浇水,要输就输给理想,要败就败给高手。不可浮躁

7

主题

1126

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:250水王,种菜浇水,要输就输给理想,要败就败给高手。不可浮躁

7

主题

1126

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部