[DemoCode下载] N76E003主从机中断通信

[复制链接]
 楼主| 小灵通2018 发表于 2019-5-13 22:16 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Website: http://www.nuvoton.com
  8. //  E-Mail : MicroC-8bit@nuvoton.com
  9. //  Date   : Jan/21/2017
  10. //***********************************************************************************************************

  11. //***********************************************************************************************************
  12. //  File Function: N76E003 SPI in Master mode demo code
  13. //***********************************************************************************************************

  14. #include "N76E003.h"
  15. #include "SFR_Macro.h"
  16. #include "Function_define.h"
  17. #include "Common.h"
  18. #include "Delay.h"

  19. //***********************************************************************************************************
  20. //  Application: SPI Function
  21. //  Master send 0x90 and recevie 0x4E
  22. //  Master send 0x01 and recevie 0x55
  23. //  Master send 0x02 and recevie 0x56
  24. //  Master send 0x03 and recevie 0x4F
  25. //  Master send 0x04 and recevie 0x54
  26. //
  27. //  Master recevie 0x4E and 0x4F form slave after transmitting
  28. //
  29. //  Output : P1.4 & P2.1 flash when SPI pass
  30. //           UART show result on hyper-terminal
  31. //           P0.7 flash when SPI error
  32. //***********************************************************************************************************

  33. #if 0
  34. ///*****************************************************************************************
  35. //* For ADC INIT setting
  36. //*****************************************************************************************/
  37. //#define                SPICLK_DIV2                        clr_SPR0;clr_SPR1
  38. //#define                SPICLK_DIV4                        set_SPR0;clr_SPR1
  39. //#define                SPICLK_DIV8                        clr_SPR0;set_SPR1
  40. //#define                SPICLK_DIV16                set_SPR0;set_SPR1
  41. //#define                Enable_SPI_Interrupt                set_ESPI;set_EA
  42. //#define                SS                P15
  43. #endif

  44. //-----------------------------------------------------------------------------------------------------------
  45. void SPI_Error(void)
  46. {
  47.     printf ("\nSPI error.\n");
  48.     while(1)                                    // SPI error and P0.7 flash/
  49.     {
  50.         P07 = 1;
  51.         Timer0_Delay1ms(500);
  52.         P07 = 0;
  53.         Timer0_Delay1ms(500);
  54.     }
  55.         }
  56. //-----------------------------------------------------------------------------------------------------------
  57. void SPI_Initial(void)
  58. {              
  59.                 P15_Quasi_Mode;                                                                                                                // P15 (SS) Quasi mode
  60.                 P10_Quasi_Mode;                                                                                                                // P10(SPCLK) Quasi mode
  61.                 P00_Quasi_Mode;                                                                                                                // P00 (MOSI) Quasi mode
  62.     P01_Quasi_Mode;                                                                                                                // P22 (MISO) Quasi mode
  63.             
  64.     set_DISMODF;                                                                                                                        // SS General purpose I/O ( No Mode Fault )
  65.     clr_SSOE;
  66.    
  67.     clr_LSBFE;                                                                                                                                // MSB first         

  68.     clr_CPOL;                                                                                                                                        // The SPI clock is low in idle mode
  69.     set_CPHA;                                                                                                                                        // The data is sample on the second edge of SPI clock
  70.    
  71.     set_MSTR;                                                                                                                                        // SPI in Master mode
  72.      
  73.     SPICLK_DIV2;                                                                                                                        // Select SPI clock
  74.     Enable_SPI_Interrupt;                                                                                        // Enable SPI interrupt
  75.     set_SPIEN;                                                                                                                                // Enable SPI function
  76. }
  77. //-----------------------------------------------------------------------------------------------------------
  78. void Start_Sending_SPI(UINT8 *pu8MID,UINT8 *pu8DID)
  79. {
  80.     SS = 0;

  81.     SPDR = 0x90;                                // Send 0x90 to Slave
  82.     PCON |= SET_BIT0;                           // Enter idle mode
  83.     if(SPDR != 0x4E)                            // Receive slave 1st DATA
  84.        SPI_Error();
  85.     printf ("\nSlave Return %c!\n",SPDR);
  86.                                           
  87.     SPDR = 0x01;                                // Send 0x01 to Slave
  88.     PCON |= SET_BIT0;                           // Enter idle mode
  89.     if(SPDR != 0x55)                            // Receive slave 2nd DATA  
  90.        SPI_Error();
  91.     printf ("\nSlave Return %c!\n",SPDR);

  92.     SPDR = 0x02;                                // Send 0x02 to Slave
  93.     PCON |= SET_BIT0;                           // Enter idle mode
  94.     if(SPDR != 0x56)                            // Receive slave 3rd DATA
  95.        SPI_Error();
  96.     printf ("\nSlave Return %c!\n",SPDR);

  97.     SPDR = 0x03;                                // Send 0x03 to Slave
  98.     PCON |= SET_BIT0;                           // Enter idle mode
  99.     if(SPDR != 0x4F)                            // Receive slave 4th DATA
  100.        SPI_Error();
  101.     printf ("\nSlave Return %c!\n",SPDR);

  102.     SPDR = 0x04;                                // Send 0x04 to Slave
  103.     PCON |= SET_BIT0;                           // Enter idle mode
  104.     if(SPDR != 0x54)                            // Receive slave 5th DATA
  105.        SPI_Error();
  106.     printf ("\nSlave Return %c!\n",SPDR);

  107.     SPDR = 0x4F;                  
  108.     PCON |= SET_BIT0;                           // Enter idle mode
  109.     *pu8MID = SPDR;                             // Receive Slave 1st DATA fron Slave      
  110.     printf ("\nSlave Return %c!\n",SPDR);

  111.     SPDR = 0x4E;                  
  112.     PCON |= SET_BIT0;                           // Enter idle mode            
  113.     *pu8DID = SPDR;                             // Receive Slave 2nd DATA from Slave
  114.     printf ("\nSlave Return %c!\n",SPDR);

  115.     SS = 1;   
  116. }
  117. //-----------------------------------------------------------------------------------------------------------
  118. void main(void)
  119. {      
  120.     UINT8 u8MID,u8DID;
  121.    
  122.     Set_All_GPIO_Quasi_Mode;
  123.     InitialUART0_Timer1(115200);             /* 115200 Baud Rate*/

  124.     SPI_Initial();

  125.     printf ("\nSPI Start Transmit...\n");

  126.     Start_Sending_SPI(&u8MID,&u8DID);
  127.         
  128.     if((u8MID != 0x4F)&&(u8DID != 0x4E))
  129.         SPI_Error();

  130.     printf ("\nSPI Test OK!\n");
  131.     while(1)                                    // SPI transmission finish
  132.     {
  133.         P12 = 1;
  134.         Timer0_Delay1ms(500);
  135.         P12 = 0;
  136.         Timer0_Delay1ms(500);
  137.     }
  138. }
  139. //-----------------------------------------------------------------------------------------------------------
  140. void SPI_ISR(void) interrupt 9                  // Vecotr [url=home.php?mod=space&uid=72445]@[/url]  0x4B
  141. {
  142.     clr_SPIF;
  143.     Timer3_Delay10us(1);
  144. }
  145. //-----------------------------------------------------------------------------------------------------------


 楼主| 小灵通2018 发表于 2019-5-13 22:17 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Website: http://www.nuvoton.com
  8. //  E-Mail : MicroC-8bit@nuvoton.com
  9. //  Date   : Jan/21/2017
  10. //***********************************************************************************************************

  11. //***********************************************************************************************************
  12. //  File Function: N76E003 SPI in Slave mode demo code
  13. //***********************************************************************************************************
  14. #include "N76E003.h"
  15. #include "SFR_Macro.h"
  16. #include "Function_define.h"
  17. #include "Common.h"
  18. #include "Delay.h"

  19. //***********************************************************************************************************
  20. //  Application: SPI Function
  21. //  Slave receive 0x90 and return 0x4E
  22. //  Slave receive 0x01 and return 0x55
  23. //  Slave receive 0x02 and return 0x56
  24. //  Slave receive 0x03 and return 0x4F
  25. //  Slave receive 0x04 and return 0x54
  26. //
  27. //  Slave send 0x4F and 0x4E to Master after receiving
  28. //  
  29. //  Output : P1.2 GPIO flash when SPI pass.
  30. //           P0.7 flash when SPI error
  31. //***********************************************************************************************************

  32. UINT8   u8Receive_Data[5];
  33. UINT16  u16CNT = 0;
  34. bit     SPI_Send_Flag = 0;

  35. //-----------------------------------------------------------------------------------------------------------
  36. void SPI_Error(void)
  37. {
  38.     while(1)                                    // SPI error and P0.7 flash/
  39.     {
  40.         P07 = 1;
  41.         Timer0_Delay1ms(500);
  42.         P07 = 0;
  43.         Timer0_Delay1ms(500);
  44.     }
  45. }
  46. //-----------------------------------------------------------------------------------------------------------
  47. void SPI_Initial(void)
  48. {
  49.                 P15_Quasi_Mode;                                                                                                                //P15 (SS) Quasi mode
  50.                 P10_Quasi_Mode;                                                                                                                //P10(SPCLK) Quasi mode
  51.                 P00_Quasi_Mode;                                                                                                                //P00 (MOSI) Quasi mode
  52.     P01_Quasi_Mode;                                                                                                                //P22 (MISO) Quasi mode;

  53.     clr_MSTR;                                   // SPI in Slave mode
  54.     clr_LSBFE;                                  // MSB first

  55.     clr_CPOL;                                   // The SPI clock is low in idle mode
  56.     set_CPHA;                                   // The data is sample on the second edge of SPI clock     
  57.       
  58.     Enable_SPI_Interrupt;                             // Enable SPI interrupt
  59.     set_SPIEN;                                  // Enable SPI function
  60.    
  61.     clr_SPIF;                                   // Clear SPI flag
  62. }
  63. //-----------------------------------------------------------------------------------------------------------
  64. void Slave_Receive_Data(void)
  65. {
  66.     SPDR = 0x4E;                             
  67.     PCON |= SET_BIT0;                           // Enter idle mode
  68.     if(u8Receive_Data[0] != 0x90)               // Receive 1st DATA form master
  69.         SPI_Error();
  70.          
  71.     SPDR = 0x55;
  72.     PCON |= SET_BIT0;                           // Enter idle mode                       
  73.     if(u8Receive_Data[1] != 0x01)               // Receive 2nd DATA form master
  74.         SPI_Error();

  75.     SPDR = 0x56;
  76.     PCON |= SET_BIT0;                           // Enter idle mode
  77.     if(u8Receive_Data[2] != 0x02)               // Receive 3rd DATA form master
  78.         SPI_Error();

  79.     SPDR = 0x4F;
  80.     PCON |= SET_BIT0;                           // Enter idle mode
  81.     if(u8Receive_Data[3] != 0x03)               // Receive 4th DATA form master
  82.         SPI_Error();

  83.     SPDR = 0x54;
  84.     PCON |= SET_BIT0;                           // Enter idle mode
  85.     if(u8Receive_Data[4] != 0x04)               // Receive 5th DATA form master
  86.         SPI_Error();
  87. }
  88. //-----------------------------------------------------------------------------------------------------------
  89. void Slave_Transmit_Data(void)
  90. {
  91.     SPI_Send_Flag = 1;
  92.     SPDR = 0x4F;                                // Send 1st data (04F) to Master
  93.     PCON |= SET_BIT0;                           // Enter idle mode      

  94.     SPI_Send_Flag = 1;
  95.     SPDR = 0x4E;                                // Send 2nd data (0x4E) to Master
  96.     PCON |= SET_BIT0;                           // Enter idle mode
  97. }
  98. //-----------------------------------------------------------------------------------------------------------
  99. void main(void)
  100. {   
  101.     Set_All_GPIO_Quasi_Mode;
  102.     InitialUART0_Timer1(115200);             /* 115200 Baud Rate*/

  103.     SPI_Initial();

  104.     printf ("\nSPI Start Receive...\n");

  105.     Slave_Receive_Data();                       // Slave receive data from master
  106.     Slave_Transmit_Data();                      // Slave transmit data to master

  107.     clr_ESPI;
  108.     SPDR = 0x00;
  109.     printf ("\nSPI Test OK!\n");
  110.     while(1)                                    // SPI transmission finish
  111.     {
  112.         P12 = 1;
  113.         Timer0_Delay1ms(500);
  114.         P12 = 0;
  115.         Timer0_Delay1ms(500);
  116.     }
  117. }
  118. //-----------------------------------------------------------------------------------------------------------
  119. void SPI_ISR(void) interrupt 9                  // Vecotr @  0x4B
  120. {
  121.     clr_SPIF;                                   
  122.     if(!SPI_Send_Flag)
  123.     {      
  124.         u8Receive_Data[u16CNT] = SPDR;
  125.         u16CNT ++;
  126.     }
  127.     SPI_Send_Flag = 0;
  128. }
  129. //-----------------------------------------------------------------------------------------------------------
xinpian101 发表于 2019-5-14 22:07 | 显示全部楼层
SPI非常好用 一个接口。
xuanhuanzi 发表于 2019-5-14 22:28 | 显示全部楼层
常用的一种方法。
zhuomuniao110 发表于 2019-5-15 00:23 | 显示全部楼层
这种应用,一般主机形式较多
捉虫天师 发表于 2019-5-15 00:32 | 显示全部楼层
中断操作使用很多。
dongnanxibei 发表于 2019-5-17 01:03 | 显示全部楼层
寄存器掌握牢固可以
yiyigirl2014 发表于 2019-5-17 22:10 | 显示全部楼层
还有错误指示,考虑全面。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

1727

帖子

4

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

157

主题

1727

帖子

4

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