[DemoCode下载] N79e715的SPI轮巡模式

[复制链接]
 楼主| zhuotuzi 发表于 2017-5-13 18:28 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technology Corp.
  8. //  E-mail: MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************
  10. //  Application: SPI Function
  11. //  Master send 0x90 and receive 0x4E
  12. //  Master send 0x01 and receive 0x55
  13. //  Master send 0x02 and receive 0x56
  14. //  Master send 0x03 and receive 0x4F
  15. //  Master send 0x04 and receive 0x54
  16. //
  17. //  Master receive 0x4E and 0x4F from slave after transmitting
  18. //
  19. //  Output : P1.4 & P2.1 flash when SPI pass
  20. //           UART show result on hyper-terminal
  21. //           P0.7 flash when SPI error
  22. //***********************************************************************************************************

  23. //------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
  24. //     <o0.6> UART pin Select
  25. //          <0=> Select P1.0, P1.1 as UART pin(default)
  26. //          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
  27. //     <o1.7> SPI pin Select
  28. //          <0=> Select P1.6, P1.7, P1.4, P0.0 as SPI pin(default)
  29. //          <1=> Select P2.2, P2.3, P2.4, P2.5 as SPI pin(28 pin only)
  30. //-------------------------------- <<< end of configuration section >>> -------------------------------------

  31. #define Uart_Port_Sel   0x00
  32. #define SPI_Pin_Sel     0x80

  33. #include "N79E715.h"
  34. #include "Typedef.h"
  35. #include "Define.h"
  36. #include "Common.h"
  37. #include "Delay.h"
  38. #include "SPI.h"
  39. #include "Version.h"
  40. #include <stdio.h>

  41. #if (SPI_Pin_Sel == 0x00)
  42.     #define SS              P14
  43. #else
  44.     #define SS              P24
  45. #endif

  46. //-----------------------------------------------------------------------------------------------------------
  47. void SPI_Error(void)
  48. {
  49.     printf ("\nSPI error.\n");
  50.     while(1)                                    // SPI error and P0.7 flash/
  51.     {
  52.         P07 = 1;
  53.         Delay1ms(500);
  54.         P07 = 0;
  55.         Delay1ms(500);
  56.     }
  57. }
  58. //-----------------------------------------------------------------------------------------------------------
  59. void SPI_Clock_Select(E_SPICLK_Sel Clock)
  60. {
  61.     switch(Clock)
  62.     {
  63.         case E_ClockDev16:
  64.             clr_SPR0;                           // SPI clock = Fsys/16
  65.             clr_SPR1;
  66.             break;
  67.         case E_ClockDev32:
  68.             set_SPR0;                           // SPI clock = Fsys/32
  69.             clr_SPR1;
  70.             break;
  71.         case E_ClockDev64:
  72.             clr_SPR0;                           // SPI clock = Fsys/64
  73.             set_SPR1;
  74.             break;
  75.         case E_ClockDev128:
  76.             set_SPR0;                           // SPI clock = Fsys/128
  77.             set_SPR1;
  78.             break;
  79.     }
  80. }
  81. //-----------------------------------------------------------------------------------------------------------
  82. void SPI_Initial(void)
  83. {
  84. #if (SPI_Pin_Sel == 0x00)
  85.     P3M1 |= SET_BIT4;                           // Enables Schmitt trigger inputs on Port 0.
  86.     P3M1 |= SET_BIT5;                           // Enables Schmitt trigger inputs on Port 1.
  87. #else
  88.     P3M1 |= SET_BIT6;                           // Enables Schmitt trigger inputs on Port 2.
  89. #endif
  90.     set_DISMODF;                                // /SS output ( No Mode Fault )
  91.     set_SSOE;

  92.     clr_LSBFE;                                  // MSB first

  93.     clr_CPOL;                                   // The SPI clock is low in idle mode
  94.     set_CPHA;                                   // The data is sample on the second edge of SPI clock

  95.     set_MSTR;                                   // SPI in Master mode
  96.     SPI_Clock_Select(E_ClockDev128);            // Select SPI clock
  97.     set_SPIEN;                                  // Enable SPI function
  98.     clr_SPIF;
  99. }
  100. //-----------------------------------------------------------------------------------------------------------
  101. void Start_Test_SPI(UINT8 *pu8MID,UINT8 *pu8DID)
  102. {
  103.     SPDR = 0x90;                                // Send 0x90 to Slave
  104.     while(!(SPSR & SET_BIT7));
  105.     clr_SPIF;
  106.     if(SPDR != 0x4E)
  107.        SPI_Error();
  108.     printf ("\nSlave Return %c!\n",SPDR);

  109.     SPDR = 0x01;                                // Send 0x01 to Slave
  110.     while(!(SPSR & SET_BIT7));
  111.     clr_SPIF;
  112.     if(SPDR != 0x55)
  113.        SPI_Error();
  114.     printf ("\nSlave Return %c!\n",SPDR);

  115.     SPDR = 0x02;                                // Send 0x02 to Slave
  116.     while(!(SPSR & SET_BIT7));
  117.     clr_SPIF;
  118.     if(SPDR != 0x56)
  119.        SPI_Error();
  120.     printf ("\nSlave Return %c!\n",SPDR);

  121.     SPDR = 0x03;                                // Send 0x03 to Slave
  122.     while(!(SPSR & SET_BIT7));
  123.     clr_SPIF;
  124.     if(SPDR != 0x4F)
  125.        SPI_Error();
  126.     printf ("\nSlave Return %c!\n",SPDR);

  127.     SPDR = 0x04;                                // Send 0x04 to Slave
  128.     while(!(SPSR & SET_BIT7));
  129.     clr_SPIF;
  130.     if(SPDR != 0x54)
  131.        SPI_Error();
  132.     printf ("\nSlave Return %c!\n",SPDR);

  133.     SPDR = 0xFF;
  134.     while(!(SPSR & SET_BIT7));
  135.     clr_SPIF;
  136.     *pu8MID = SPDR;                             // Receive Slave 1st DATA from Slave
  137.     printf ("\nSlave Return %c!\n",SPDR);

  138.     SPDR = 0xFF;
  139.     while(!(SPSR & SET_BIT7));
  140.     clr_SPIF;
  141.     *pu8DID = SPDR;                             // Receive Slave 2nd DATA from Slave
  142.     printf ("\nSlave Return %c!\n",SPDR);
  143. }
  144. //-----------------------------------------------------------------------------------------------------------
  145. void main(void)
  146. {
  147.     UINT8 u8MID,u8DID;

  148.     AUXR1 |= SPI_Pin_Sel;                       // Select P1.6, P1.7, P1.4, P0.0 as SPI pin(default)
  149.     AUXR1 |= Uart_Port_Sel;                     // Select P10/P11 as UART pin(default)
  150.     InitialUART0_Timer1(9600);                  // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
  151.     Show_Version_Number_To_PC();
  152.     printf ("\n*===================================================================");
  153.     printf ("\n*  Name: N79E715 Series SPI(Polling) Sample Code.");
  154.     printf ("\n*===================================================================");
  155.     printf ("\nSPI Demo Start.\n");


  156.     SPI_Initial();

  157.     Start_Test_SPI(&u8MID,&u8DID);

  158.     if((u8MID != 0x4F)&&(u8DID != 0x4E))
  159.         SPI_Error();

  160.     printf ("\nSPI Test OK!\n");
  161.     while(1)                                    // SPI transmission finish and P0.6 flash
  162.     {
  163.         P14 = 1;
  164.         P21 = 1;
  165.         Delay1ms(500);
  166.         P14 = 0;
  167.         P21 = 0;
  168.         Delay1ms(500);
  169.     }
  170. }
  171. //-----------------------------------------------------------------------------------------------------------


 楼主| zhuotuzi 发表于 2017-5-13 18:29 | 显示全部楼层
从机的代码

  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technology Corp.
  8. //  E-mail: MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************
  10. //  Application: SPI Function
  11. //  Slave receive 0x90 and return 0x4E
  12. //  Slave receive 0x01 and return 0x55
  13. //  Slave receive 0x02 and return 0x56
  14. //  Slave receive 0x03 and return 0x4F
  15. //  Slave receive 0x04 and return 0x54
  16. //
  17. //  Slave send 0x4F and 0x4E to Master after receiving
  18. //
  19. //  Output : P1.4 & P2.1 flash when SPI pass.
  20. //           P0.7 flash when SPI error
  21. //***********************************************************************************************************

  22. //------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
  23. //     <o0.6> UART pin
  24. //          <0=> Select P1.0, P1.1 as UART pin(default)
  25. //          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
  26. //     <o1.7> SPI pin Select
  27. //          <0=> Select P1.6, P1.7, P1.4, P0.0 as SPI pin(default)
  28. //          <1=> Select P2.2, P2.3, P2.4, P2.5 as SPI pin(28 pin only)
  29. //-------------------------------- <<< end of configuration section >>> -------------------------------------

  30. #define Uart_Port_Sel   0x00
  31. #define SPI_Pin_Sel     0x80

  32. #include <stdio.h>
  33. #include <intrins.h>
  34. #include <string.h>
  35. #include "N79E715.h"
  36. #include "Typedef.h"
  37. #include "Define.h"
  38. #include "Common.h"
  39. #include "Delay.h"
  40. #include "SPI.h"
  41. #include "Version.h"

  42. //-----------------------------------------------------------------------------------------------------------
  43. void SPI_Error(void)
  44. {
  45.     while(1)                                    // SPI error and P0.7 flash/
  46.     {
  47.         P07 = 1;
  48.         Delay1ms(500);
  49.         P07 = 0;
  50.         Delay1ms(500);
  51.     }
  52. }
  53. //-----------------------------------------------------------------------------------------------------------
  54. void SPI_Initial(void)
  55. {
  56. #if (SPI_Pin_Sel == 0x00)
  57.     P3M1 |= SET_BIT4;                           // Enables Schmitt trigger inputs on Port 0.
  58.     P3M1 |= SET_BIT5;                           // Enables Schmitt trigger inputs on Port 1.
  59. #else
  60.     P3M1 |= SET_BIT6;                           // Enables Schmitt trigger inputs on Port 2.
  61. #endif
  62.     clr_MSTR;                                   // SPI in Slave mode
  63.     clr_LSBFE;                                  // MSB first

  64.     clr_CPOL;                                   // The SPI clock is low in idle mode
  65.     set_CPHA;                                   // The data is sample on the second edge of SPI clock

  66.     set_SPIEN;                                  // Enable SPI function
  67.     clr_SPIF;
  68. }
  69. //-----------------------------------------------------------------------------------------------------------
  70. void Slave_Receive_Data(void)
  71. {
  72.     SPDR = 0x4E;                                // Receive Master 1st DATA
  73.     while(!(SPSR & SET_BIT7));
  74.     clr_SPIF;
  75.     if(SPDR != 0x90)
  76.        SPI_Error();

  77.     SPDR = 0x55;                                // Receive Master 2nd DATA
  78.     while(!(SPSR & SET_BIT7));
  79.     clr_SPIF;
  80.     if(SPDR != 0x01)
  81.        SPI_Error();

  82.     SPDR = 0x56;                                // Receive Master 3rd DATA
  83.     while(!(SPSR & SET_BIT7));
  84.     clr_SPIF;
  85.     if(SPDR != 0x02)
  86.         SPI_Error();

  87.     SPDR = 0x4F;                                // Receive Master 4th DATA
  88.     while(!(SPSR & SET_BIT7));
  89.     clr_SPIF;
  90.     if(SPDR != 0x03)
  91.         SPI_Error();

  92.     SPDR = 0x54;                                // Receive Master 5th DATA
  93.     while(!(SPSR & SET_BIT7));
  94.     clr_SPIF;
  95.     if(SPDR != 0x04)
  96.         SPI_Error();
  97. }
  98. //-----------------------------------------------------------------------------------------------------------
  99. void Slave_tranmit_Data(void)
  100. {
  101.     SPDR = 0x4F;                                // Send 1st data (0x4F) to Master
  102.     while(!(SPSR & SET_BIT7));
  103.     clr_SPIF;
  104.     if(SPDR != 0xFF)
  105.         SPI_Error();

  106.     SPDR = 0x4E;                                // Send 2nd data (0x4E) to Master
  107.     while(!(SPSR & SET_BIT7));
  108.     clr_SPIF;
  109.     if(SPDR != 0xFF)
  110.         SPI_Error();
  111. }
  112. //-----------------------------------------------------------------------------------------------------------
  113. void main(void)
  114. {
  115.     AUXR1 |= SPI_Pin_Sel;                       // Select P1.6, P1.7, P1.4, P0.0 as SPI pin(default)
  116.     AUXR1 |= Uart_Port_Sel;                     // Select P10/P11 as UART pin(default)
  117.     InitialUART0_Timer1(9600);                  // 9600 Baud Rate @ 11.0592MHz
  118.     Show_Version_Number_To_PC();
  119.     printf ("\n*===================================================================");
  120.     printf ("\n*  Name: N79E715 Series SPI Slave(Polling) Sample Code.");
  121.     printf ("\n*===================================================================");
  122.     SPI_Initial();

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


  124.     Slave_Receive_Data();                       // Slave receive data from master
  125.     Slave_tranmit_Data();                       // Slave transmit data to master
  126.     printf ("\nSPI Test OK!\n");
  127.     while(1)
  128.     {
  129.         P14 = 1;
  130.         P21 = 1;
  131.         Delay1ms(500);
  132.         P14 = 0;
  133.         P21 = 0;
  134.         Delay1ms(500);
  135.     }
  136. }
  137. //-----------------------------------------------------------------------------------------------------------


 楼主| zhuotuzi 发表于 2017-5-13 18:30 | 显示全部楼层
众所周知,这个单片机是新唐出的一款4T的8位单片机。
 楼主| zhuotuzi 发表于 2017-5-13 18:30 | 显示全部楼层
N79E715高速4T 8051单片机系列

NU_N79E715AT20.JPG
Part Number : N79E715AT20
产品线 : Nuvoton


产品特性
Flash:16K
RAM:512B
ADC 8CH@10bit
2SPI
1 I2C
2UART
产品应用
门禁系统/警报器、温度传感设备、iPod 充电音箱底座(iPod docking)、投影机、DVD机
文字介绍
高速4T8051单片机系列,其特点为提供工业温度规格,宽电压工作范围 2.4V 至 5.5V、22.1184MHz内建RC晶振(1%精确度)、并内建DataFlash、欠压检测、高抗干扰能力(8KVESD/4KVEFT)、支持在线系统编程(ISP)和在线电路编程(ICP)


 楼主| zhuotuzi 发表于 2017-5-13 18:32 | 显示全部楼层

N79E715.jpg

N79E715为高速4T 8051单片机系列,其特点为提供工业温度规格,宽电压工作范围 2.4V 至 5.5V、22.1184 MHz内建RC晶振(1%精确度)、并内建Data Flash、欠压检测、高抗干扰能力(8KV ESD/4KV EFT)、支持在线系统编程(ISP)和在线电路编程(ICP),提供TSSOP28、TSSOP20和SOP16选择。

应用领域 :

门禁系统/警报器、温度传感设备、iPod 充电音箱底座(iPod docking)、投影机、DVD机、电子秤及LED/照明控制等等。

关键特性:

  • 内核(core)
    • 4T 8051 微 处理器
    • 工件频率 可达 24 MHz
    • 工作电压 : 2.4V to 5.5V
    • 工作温度 : -40 ℃ ~85 ℃
  • 内存(memory)
    • 16 KB 应用 程序
    • 内嵌 512B SRAM
    • 可配置 的 Data Flash
    • 支持 在线系统编程 ISP(In-System Programming)
    • 支持 在线电路编 ICP(In-Circuit Programming)
  • 模数转换器(ADC)
    • 提供 8 通道
    • 10 位分辨率
    • 可达 150 k SPS ( 每秒采样率 )
  • 通讯接口(connectivity)


    • 最多 2 通道 SPI ( 可达 1.25 MHz)
    • 一 组 I²C ( 可达 400 kHz)
    • 最多 2 通道 UART
  • 时钟控制(clock control)
    • 外部 晶振 4 to 2 4 MHz
    • 常温 ,5V 下± 1% 22.1184 MHz 内 RC 晶振
    • 内 RC 晶振 可配置 22.1184 或 11.0592 M Hz

[url=]规格数据[/url]










置顶使用者回馈[url=]Share to Sina Weibo[/url][url=]Share to Twitter[/url][url=]Share to Google+[/url][url=]Share to Facebook[/url]






 楼主| zhuotuzi 发表于 2017-5-13 18:34 | 显示全部楼层
大家都知道,普通的51没有这么多的通信接口外设,一般也就一个UART串口。
新唐的比较给力,具备丰富的片上外设。
N79E715系列 有支持高速串行通信的SPI模块。SPI 为全双工、高速、同步传输总线在微芯片外设EEPROM, LCD 驱动, D/A 转换之间,提供主机从机模式传输,速度可达主机模式下FSYS/16和从机模式下FSYS/4,支持传输完成标志位 写 冲突标志位。 在多主机系统中,SPI 支持主机模式故障报错用以防止主机冲突。
 楼主| zhuotuzi 发表于 2017-5-13 18:36 | 显示全部楼层
QQ截图20170513183506.png
         SPI寄存器板块是SPI模块的主要组成部分,包括逻辑控制,波特率控制和管脚逻辑控制,SPI 包括移位寄存器和读出数据缓冲器,传送数据是单缓冲器,接收数据是双缓冲器。在传送完成之前传送的数据不能写入移位装置。
         SPI 界面需要四个管脚 主进/从出(MISO),主出/从进(MOSI),移位时钟(SPCLK), 和从机选择(SS)。MOSI脚用于传输主机到从机的8位数据,因些,MOSI是一个主机设备的输出引脚,从机设备的输入引脚。相应的,MISO用于接收从机到主机的串行数据。SPCLK引脚为主机模式下的时钟输出,从机模式的时钟输入。移位时钟用于MOSI和MISO脚之间数据传输的时钟同步。位移时钟由主机输出,一组SPI传输系统上只能有一个主机以避免设备冲突。建议该管脚设置为史密特触发输入模式。
 楼主| zhuotuzi 发表于 2017-5-13 19:25 | 显示全部楼层


QQ截图20170513192509.png
图13–2 为典型的SPI设备通信总线通常为 3 信号线相连, MOSI ~ MOSI, MISO ~ MISO, 和 SPCLK ~SPCLK. 主机通过四线并行连接的方式,每根SS线分别控制每个从机。MCU1 和 MCU2 可以任意定义为主/从机模式. SS需配置为主机模式侦测功能 避免多主机冲突。
 楼主| zhuotuzi 发表于 2017-5-13 20:10 | 显示全部楼层
强烈推荐用8位机可以选这个。
643757107 发表于 2017-5-14 00:06 | 显示全部楼层
51都可以用SPI接口了,也是很高端了。
huangcunxiake 发表于 2017-5-14 15:51 | 显示全部楼层
新唐的51,除了内核是51外,片上外设配置不比那些ARM的差。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

214

主题

3368

帖子

7

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