[范例教程] 【M0】 MG32F02A 学习笔记⑧ SPI0 标准发送与接收

[复制链接]
1671|0
 楼主| noctor 发表于 2018-9-29 15:08 | 显示全部楼层 |阅读模式
本帖最后由 noctor 于 2018-9-29 15:06 编辑
     上回我们说到了MG32F02A的UART0通过DMA方式进行串口接收。帖子详情:https://bbs.21ic.com/icview-2560338-1-1.html

      这次我们来看一下SPI应该如何使用。
      由于标准四线制SPI是全双工的双向总线,所以在你发送的同时也发生了接收,因此读写的方式是不一样的,需要用不同的办法来进行主从机的通讯。
      首先发主机的发送程序:
  1. #include "MG32x02z_DRV.H"

  2. typedef uint8_t u8;
  3. typedef uint16_t u16;
  4. typedef uint32_t u32;
  5. typedef uint64_t u64;


  6. #define Dummy_Data         0xFFFFFFFF
  7. #define SPI_NSS         PB0     // SPI_NSS


  8. void CSC_Init (void)
  9. {
  10.         CSC_PLL_TyprDef CSC_PLL_CFG;
  11.    
  12.         
  13.     UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  14.     MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  15.     ProtectModuleReg(MEMprotect);

  16.   UnProtectModuleReg(CSCprotect);
  17.         CSC_CK_APB_Divider_Select(APB_DIV_1);        // Modify CK_APB divider        APB=CK_MAIN/1
  18.         CSC_CK_AHB_Divider_Select(AHB_DIV_1);        // Modify CK_AHB divider        AHB=APB/1

  19.         
  20.         /* CK_HS selection */
  21.         CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
  22.         CSC_IHRCO_Cmd(ENABLE);
  23.         while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
  24.         CSC_ClearFlag(CSC_IHRCOF);
  25.         CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO


  26.         /* PLL */
  27.         /**********************************************************/
  28.         CSC_PLL_CFG.InputDivider=PLLI_DIV_2;        // 12M/2=6M
  29.         CSC_PLL_CFG.Multiplication=PLLIx16;                // 6M*16=96M
  30.         CSC_PLL_CFG.OutputDivider=PLLO_DIV_2;        // PLLO=96M/2=48M
  31.         CSC_PLL_Config(&CSC_PLL_CFG);
  32.         CSC_PLL_Cmd(ENABLE);
  33.         while(CSC_GetSingleFlagStatus(CSC_PLLF) == DRV_Normal);
  34.         CSC_ClearFlag(CSC_PLLF);
  35.         /**********************************************************/

  36.         
  37.         /* CK_MAIN */
  38.         CSC_CK_MAIN_Select(MAIN_CK_HS);        


  39.         /* Configure ICKO function */
  40.                
  41.         /* Configure peripheral clock */
  42.         CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);
  43.          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0,ENABLE);
  44.          CSC_PeriphOnModeClock_Config(CSC_ON_PortB,ENABLE);
  45.         CSC_PeriphOnModeClock_Config(CSC_ON_PortE,ENABLE);

  46.     ProtectModuleReg(CSCprotect);
  47.    
  48. }



  49. void InitSPI0(void)
  50. {  
  51.         
  52.                 PIN_InitTypeDef PINX_InitStruct;
  53.         
  54.         //===Set CSC init====
  55.         //MG32x02z_CSC_Init.h(Configuration Wizard)
  56.         //Select CK_HS source = CK_IHRCO
  57.         //Select IHRCO = 12Mz
  58.         //Select CK_MAIN Source = CK_HS
  59.         //Configure PLL->Select APB Prescaler = CK_MAIN/1
  60.         
  61.         /*=== 1. Enable CSC to SPI clock ===*/
  62.         //[A] When Use Wizard
  63.         //Configure Peripheral On Mode Clock->SPI0 = Enable and Select SPI0_PR Source = CK_APB
  64.         //Configure Peripheral On Mode Clock->Port B = Enable
  65.         //[B] When Use Driver
  66. //          UnProtectModuleReg(CSCprotect);                                                          // Unprotect CSC module
  67. //          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0, ENABLE);                  // Enable SPI0 module clock
  68. //          CSC_PeriphOnModeClock_Config(CSC_ON_PortB, ENABLE);                  // Enable PortB clock
  69. //          CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);  // CK_SPIx_PR = CK_APB = 12MHz
  70. //          ProtectModuleReg(CSCprotect);                                                           // protect CSC module
  71.            
  72.         /*=== 2. Default Initial SPI ===*/
  73.         SPI_DeInit(SPI0);
  74.         
  75.         /*=== 3. Configure clock divider ===*/                                                // SPI clock = 1MHz
  76.         SPI_Clock_Select(SPI0, SPI_CK_SPIx_PR);                                         // CK_SPIx = CK_SPIx_PR
  77.         SPI_PreDivider_Select(SPI0, SPI_PDIV_2);                                        // PDIV outpu = CK_SPIx /2
  78.         SPI_Prescaler_Select(SPI0, SPI_PSC_3);                                                // Prescaler outpu = PDIV outpu /3
  79.         SPI_Divider_Select(SPI0, SPI_DIV_2);                                                // DIV outpu = PDIV outpu /2
  80.         
  81.         /*=== 4. Configure SPI data line, mode and data size... ===*/
  82.         SPI_DataLine_Select(SPI0, SPI_Standard);                        // SPI Standard Mode
  83.         SPI_ModeAndNss_Select(SPI0, SPI_Master);                                        // Master
  84.         SPI_ClockPhase_Select(SPI0, SPI_LeadingEdge);                                // CPHA = 0
  85.         SPI_ClockPolarity_Select(SPI0, SPI_Low);                                        // CPOL = 0
  86.         SPI_FirstBit_Select(SPI0, SPI_MSB);                                                 // MSB first
  87.         SPI_DataSize_Select(SPI0, SPI_8bits);                                                // Data size 8bits
  88.         
  89.         /*=== 5. Config SPI0 IO ===*/
  90.         PINX_InitStruct.PINX_Mode                                = PINX_Mode_PushPull_O;         // Pin select pusu pull mode
  91.         PINX_InitStruct.PINX_PUResistant                = PINX_PUResistant_Enable;        // Enable pull up resistor
  92.         PINX_InitStruct.PINX_Speed                                = PINX_Speed_Low;                        
  93.         PINX_InitStruct.PINX_OUTDrive                        = PINX_OUTDrive_Level1;         // Pin output driver full strength.
  94.         PINX_InitStruct.PINX_FilterDivider                = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  95.         PINX_InitStruct.PINX_Inverse                        = PINX_Inverse_Disable;         // Pin input data not inverse
  96.         PINX_InitStruct.PINX_Alternate_Function = 2;                                                // Pin AFS = 2
  97.         GPIO_PinMode_Config(PINB(2),&PINX_InitStruct);                                                // CLK setup at PB2
  98.         
  99.         PINX_InitStruct.PINX_OUTDrive                        = PINX_OUTDrive_Level1;         // Pin output drive strength 1/4
  100.         GPIO_PinMode_Config(PINB(3),&PINX_InitStruct);                                                // MOSI setup at PB3
  101.         
  102.         PINX_InitStruct.PINX_Mode                                = PINX_Mode_OpenDrain_O;    // Setting open drain mode
  103.   PINX_InitStruct.PINX_Alternate_Function = 2;                // Pin AFS = 2
  104.   GPIO_PinMode_Config(PINB(1),&PINX_InitStruct);              // MISO setup at PB1

  105.         PINX_InitStruct.PINX_Alternate_Function = 0;                                                // Pin AFS = 0
  106.         GPIO_PinMode_Config(PINB(0),&PINX_InitStruct);                                                // NSS setup at PB0

  107.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_OpenDrain_O;                  // Pin select Push Pull mode
  108.         PINX_InitStruct.PINX_Alternate_Function  = 0;                                                         // Pin AFS = 0
  109.         GPIO_PinMode_Config(PINE(13),&PINX_InitStruct);                                                  // PE13
  110.         GPIO_PinMode_Config(PINE(14),&PINX_InitStruct);                                                  // PE14

  111.         /*=== 6. Enable SPI ===*/
  112.         SPI_Cmd(SPI0, ENABLE);               

  113. }

  114. int main()
  115. {
  116.         u32 i,y;
  117.         u8 x;
  118.         uint32_t RDAT;
  119.         CSC_Init();
  120.         InitSPI0();
  121.         x=y=0;
  122.         i=0;
  123.     while(1)
  124.     {
  125.             i++;
  126.             if(i>=500000)
  127.             {
  128.                         i=0;           
  129.                         SPI_NSS = 0;  
  130.                 PE13=0;
  131.                 SPI_SetTxData(SPI0, SPI_1Byte, 0x05);                               // Send 1 byte and received 1 byte the same time
  132.     while(SPI_GetSingleFlagStatus(SPI0, SPI_TCF) == DRV_UnHappened);    // Wait TCF flag
  133.     SPI_ClearFlag(SPI0, SPI_TCF);    // Clear flag               
  134.                                 SPI_NSS = 1;   
  135.                                 PE13=1;                                       

  136.                         }
  137.     }
  138. }

         在发送的时候比较简单,直接主机拉低NSS线之后使用SPI_SetTxData函数直接发送你要发送的数据即可,后面的while保证了数据发送的完整性,发送完成之后需要手动清除flag并拉高NSS线,就这样,数据就发送到从机上去了。


          从机接收程序:

  1. #include "MG32x02z_DRV.H"
  2. #include <stdio.h>

  3. typedef uint8_t u8;
  4. typedef uint16_t u16;
  5. typedef uint32_t u32;
  6. typedef uint64_t u64;

  7. #define Dummy_Data         0xFFFFFFFF

  8. #define SPI_NSS         PB0     // SPI_NSS
  9. #define URTX URT0

  10. void CSC_Init (void)
  11. {
  12.         CSC_PLL_TyprDef CSC_PLL_CFG;
  13.    
  14.         
  15.     UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  16.     MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  17.     ProtectModuleReg(MEMprotect);

  18.     UnProtectModuleReg(CSCprotect);
  19.         CSC_CK_APB_Divider_Select(APB_DIV_1);        // Modify CK_APB divider        APB=CK_MAIN/1
  20.         CSC_CK_AHB_Divider_Select(AHB_DIV_1);        // Modify CK_AHB divider        AHB=APB/1

  21.         
  22.         /* CK_HS selection */
  23.         CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
  24.         CSC_IHRCO_Cmd(ENABLE);
  25.         while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
  26.         CSC_ClearFlag(CSC_IHRCOF);
  27.         CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO


  28.         /* PLL */
  29.         /**********************************************************/
  30.         CSC_PLL_CFG.InputDivider=PLLI_DIV_2;        // 12M/2=6M
  31.         CSC_PLL_CFG.Multiplication=PLLIx16;                // 6M*16=96M
  32.         CSC_PLL_CFG.OutputDivider=PLLO_DIV_2;        // PLLO=96M/2=48M
  33.         CSC_PLL_Config(&CSC_PLL_CFG);
  34.         CSC_PLL_Cmd(ENABLE);
  35.         while(CSC_GetSingleFlagStatus(CSC_PLLF) == DRV_Normal);
  36.         CSC_ClearFlag(CSC_PLLF);
  37.         /**********************************************************/

  38.         
  39.         /* CK_MAIN */
  40.         CSC_CK_MAIN_Select(MAIN_CK_HS);        


  41.         /* Configure ICKO function */
  42.                
  43.         /* Configure peripheral clock */
  44.         CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);
  45.         CSC_PeriphProcessClockSource_Config(CSC_UART0_CKS, CK_APB);
  46.          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0,ENABLE);
  47.         CSC_PeriphOnModeClock_Config(CSC_ON_UART0,ENABLE);
  48.          CSC_PeriphOnModeClock_Config(CSC_ON_PortB,ENABLE);
  49.         CSC_PeriphOnModeClock_Config(CSC_ON_PortE,ENABLE);

  50.     ProtectModuleReg(CSCprotect);
  51.    
  52. }



  53. int fputc(int ch,FILE *f)
  54. {
  55.         
  56.         URT_SetTXData(URTX,1,ch);
  57.         while(URT_GetITSingleFlagStatus(URTX,URT_IT_TC)==DRV_UnHappened);
  58.         URT_ClearITFlag(URTX,URT_IT_TC);
  59.         
  60.         return ch;
  61. }

  62. void UartSendByte(int ch)
  63. {
  64.         
  65.         URT_SetTXData(URTX,1,ch);
  66.         while(URT_GetITSingleFlagStatus(URTX,URT_IT_TC)==DRV_UnHappened);
  67.         URT_ClearITFlag(URTX,URT_IT_TC);
  68.         
  69. }


  70. void URT0_Init(void)
  71. {
  72.     URT_BRG_TypeDef  URT_BRG;
  73.     URT_Data_TypeDef DataDef;
  74.         PIN_InitTypeDef PINX_InitStruct;
  75.    
  76.         //==Set GPIO init
  77.         //PB8 PPO TX ,PB9 ODO RX
  78.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;                  // Pin select Push Pull mode
  79.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;          // Enable pull up resistor
  80.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  81.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;                 // Pin output driver full strength.
  82.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;        // Pin input deglitch filter clock divider bypass
  83.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;                 // Pin input data not inverse
  84.         PINX_InitStruct.PINX_Alternate_Function  = PB8_AF_URT0_TX;                                // Pin AFS = URT0_TX
  85.         GPIO_PinMode_Config(PINB(8),&PINX_InitStruct);                                                          // TXD at PB8

  86.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_OpenDrain_O;                 // Pin select Open Drain mode
  87.         PINX_InitStruct.PINX_Alternate_Function  = PB9_AF_URT0_RX;                                // Pin AFS = URT0_RX
  88.         GPIO_PinMode_Config(PINB(9),&PINX_InitStruct);                                                          // RXD at PB9

  89.    
  90.     //=====Set Clock=====//
  91.     //---Set BaudRate---//
  92.     URT_BRG.URT_InteranlClockSource = URT_BDClock_PROC;
  93.     URT_BRG.URT_BaudRateMode = URT_BDMode_Separated;
  94.     URT_BRG.URT_PrescalerCounterReload = 0;                                //Set PSR
  95.     URT_BRG.URT_BaudRateCounterReload = 3;                                //Set RLR
  96.     URT_BaudRateGenerator_Config(URTX, &URT_BRG);                            //BR115200 = f(CK_URTx)/(PSR+1)/(RLR+1)/(OS_NUM+1)
  97.     URT_BaudRateGenerator_Cmd(URTX, ENABLE);                            //Enable BaudRateGenerator
  98.     //---TX/RX Clock---//
  99.     URT_TXClockSource_Select(URTX, URT_TXClock_Internal);                //URT_TX use BaudRateGenerator
  100.     URT_RXClockSource_Select(URTX, URT_RXClock_Internal);                //URT_RX use BaudRateGenerator
  101.     URT_TXOverSamplingSampleNumber_Select(URTX, 25);                //Set TX OS_NUM
  102.     URT_RXOverSamplingSampleNumber_Select(URTX, 25);                //Set RX OS_NUM
  103.     URT_RXOverSamplingMode_Select(URTX, URT_RXSMP_3TIME);
  104.     URT_TX_Cmd(URTX, ENABLE);                                            //Enable TX
  105.     URT_RX_Cmd(URTX, ENABLE);                                            //Enable RX
  106.    
  107.    

  108.     //=====Set Mode=====//
  109.     //---Set Data character config---//
  110.     DataDef.URT_TX_DataLength  = URT_DataLength_8;
  111.     DataDef.URT_RX_DataLength  = URT_DataLength_8;
  112.     DataDef.URT_TX_DataOrder   = URT_DataTyped_LSB;
  113.     DataDef.URT_RX_DataOrder   = URT_DataTyped_LSB;
  114.     DataDef.URT_TX_Parity      = URT_Parity_No;
  115.     DataDef.URT_RX_Parity      = URT_Parity_No;
  116.     DataDef.URT_TX_StopBits    = URT_StopBits_1_0;
  117.     DataDef.URT_RX_StopBits    = URT_StopBits_1_0;
  118.     DataDef.URT_TX_DataInverse = DISABLE;
  119.     DataDef.URT_RX_DataInverse = DISABLE;
  120.     URT_DataCharacter_Config(URTX, &DataDef);
  121.     //---Set Mode Select---//
  122.     URT_Mode_Select(URTX, URT_URT_mode);
  123.     //---Set DataLine Select---//
  124.     URT_DataLine_Select(URTX, URT_DataLine_2);
  125.    
  126.    
  127.     //=====Set Data Control=====//
  128.     URT_RXShadowBufferThreshold_Select(URTX, URT_RXTH_1BYTE);
  129.     URT_IdlehandleMode_Select(URTX, URT_IDLEMode_No);
  130.     URT_TXGaudTime_Select(URTX, 0);
  131.    
  132.     //=====Enable URT Interrupt=====//
  133.     //URT_IT_Cmd(URTX, URT_IT_RX, ENABLE);
  134.     //URT_ITEA_Cmd(URTX, ENABLE);
  135.     //NVIC_EnableIRQ(URT0_IRQn);

  136.     //=====Enable URT=====//
  137.     URT_Cmd(URTX, ENABLE);
  138.                
  139.         //==See MG32x02z_URT0_IRQ.c when interrupt in
  140. }


  141. void InitSPI0(void)
  142. {  
  143.    
  144.         PIN_InitTypeDef PINX_InitStruct;

  145.         //===Set CSC init====
  146.         //MG32x02z_CSC_Init.h(Configuration Wizard)
  147.         //Select CK_HS source = CK_IHRCO
  148.         //Select IHRCO = 12Mz
  149.         //Select CK_MAIN Source = CK_HS
  150.         //Configure PLL->Select APB Prescaler = CK_MAIN/1

  151.         /*=== 1. Enable CSC to SPI clock ===*/
  152.         //[A] When Use Wizard
  153.         //Configure Peripheral On Mode Clock->SPI0 = Enable and Select SPI0_PR Source = CK_APB
  154.         //Configure Peripheral On Mode Clock->Port B = Enable
  155.         //[B] When Use Driver
  156.         //          UnProtectModuleReg(CSCprotect);                                                          // Unprotect CSC module
  157.         //          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0, ENABLE);                  // Enable SPI0 module clock
  158.         //          CSC_PeriphOnModeClock_Config(CSC_ON_PortB, ENABLE);                  // Enable PortB clock
  159.         //          CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);  // CK_SPIx_PR = CK_APB = 12MHz
  160.         //          ProtectModuleReg(CSCprotect);                                                           // protect CSC module

  161.         /*=== 2. Default Initial SPI ===*/
  162.         SPI_DeInit(SPI0);

  163.         /*=== 3. Configure clock divider ===*/                                                // SPI clock = 1MHz
  164.         SPI_Clock_Select(SPI0, SPI_CK_SPIx_PR);                                         // CK_SPIx = CK_SPIx_PR
  165.         SPI_PreDivider_Select(SPI0, SPI_PDIV_2);                                        // PDIV outpu = CK_SPIx /2
  166.         SPI_Prescaler_Select(SPI0, SPI_PSC_3);                                                // Prescaler outpu = PDIV outpu /3
  167.         SPI_Divider_Select(SPI0, SPI_DIV_2);                                                // DIV outpu = PDIV outpu /2

  168.         /*=== 4. Configure SPI data line, mode and data size... ===*/
  169.         SPI_DataLine_Select(SPI0, SPI_Standard);                                        // SPI data line 1-line Bidirectional~ SPI0_MOSI
  170.         SPI_ModeAndNss_Select(SPI0, SPI_SlaveWithNss);                                        // Slave
  171.         SPI_NSSInputSignal_Select(SPI0,SPI_NssPin);                                        // Nss
  172.         SPI_ClockPhase_Select(SPI0, SPI_LeadingEdge);                                // CPHA = 0
  173.         SPI_ClockPolarity_Select(SPI0, SPI_Low);                                        // CPOL = 0
  174.         SPI_FirstBit_Select(SPI0, SPI_MSB);                                                 // MSB first
  175.         SPI_DataSize_Select(SPI0, SPI_8bits);                                                // Data size 8bits
  176.         SPI_SlaveModeReceivedThreshold_Select(SPI0, SPI_1Byte);     // Set SPI0 received data buffer high threshold

  177.         /*=== 5. Config SPI0 IO ===*/
  178.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_Digital_I;          // Pin select digital input mode
  179.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
  180.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  181.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
  182.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  183.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
  184.         PINX_InitStruct.PINX_Alternate_Function = 2;                                                 // Pin AFS = 2

  185.         GPIO_PinMode_Config(PINB(0),&PINX_InitStruct);                                          // NSS setup at PB0
  186.         GPIO_PinMode_Config(PINB(2),&PINX_InitStruct);                                          // CLK setup at PB2
  187.         GPIO_PinMode_Config(PINB(3),&PINX_InitStruct);                                          // MOSI setup at PB3
  188.         
  189.   PINX_InitStruct.PINX_Mode                                = PINX_Mode_PushPull_O;     // Setting pusu pull mode
  190.   GPIO_PinMode_Config(PINB(1),&PINX_InitStruct);                      // MISO setup at PB1
  191.          PINX_InitStruct.PINX_Mode                                 = PINX_Mode_OpenDrain_O;         // Setting pusu pull mode
  192.          PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  193.          GPIO_PinMode_Config(PINE(13),&PINX_InitStruct);                                          // D4 setup at PE13
  194.          GPIO_PinMode_Config(PINE(14),&PINX_InitStruct);                                          // D4 setup at PE13
  195.          GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15

  196.         /*=== 6. Enable SPI ===*/
  197.         SPI_Cmd(SPI0, ENABLE);                                                                                          // Enable SPI

  198. }



  199. int main()
  200. {
  201.         
  202.         uint32_t RDAT,TDAT;
  203.         int i;
  204.         CSC_Init();
  205.         InitSPI0();        
  206.         URT0_Init();
  207.         printf("hello\n");
  208.     while(1)
  209.     {
  210.                                      i++;
  211.             if(i>=500000)
  212.             {
  213.                         i=0;
  214.                         
  215.                 PE13=0;
  216.    SPI_SetTxData(SPI0, SPI_1Byte, Dummy_Data);       // Received 1 byte and send 1 byte the same time
  217.     while(SPI_GetSingleFlagStatus(SPI0, SPI_RXF) == DRV_UnHappened);    // Wait RXF flag                        
  218.     SPI_ClearFlag(SPI0, (SPI_TXF | SPI_RXF));                           // Clear TXF and RXF
  219.     RDAT = SPI_GetRxData(SPI0);                                         // Get received data
  220.                 printf("0x%02X\n",RDAT);
  221.                 PE13=1;

  222.                         }
  223.         
  224.     }
  225. }

      你会发现,从机接收的部分就不一样了,首先,那个0x00是什么意思呢?刚刚我们说过,SPI总线比较特殊,发送的同时你就接收了一次数据,因此,你要接收数据,你就要发送数据,如果你要接收的数据是1字节的数据,就只需要发0x00,如果是3字节的数据,则需要发送0xffffff,因此平时方便起见直接发0xffffff即可,然后发送出去后等带flag的到来即可完成接收过程。
       从机要反过来做发送也是和主机一样的道理,仿照主机即可。
       另外从机建议模式选择SlaveWithNSS,为什么呢,因为这样的话能保证主机故障重启后从机接收到的数据依然是正常的,不然从机会一直接受到错误的数据直到你复位从机。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

82

帖子

3

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