打印
[DemoCode下载]

N79e715的SPI轮巡模式

[复制链接]
1076|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//  Application: SPI Function
//  Master send 0x90 and receive 0x4E
//  Master send 0x01 and receive 0x55
//  Master send 0x02 and receive 0x56
//  Master send 0x03 and receive 0x4F
//  Master send 0x04 and receive 0x54
//
//  Master receive 0x4E and 0x4F from slave after transmitting
//
//  Output : P1.4 & P2.1 flash when SPI pass
//           UART show result on hyper-terminal
//           P0.7 flash when SPI error
//***********************************************************************************************************

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

#define Uart_Port_Sel   0x00
#define SPI_Pin_Sel     0x80

#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "SPI.h"
#include "Version.h"
#include <stdio.h>

#if (SPI_Pin_Sel == 0x00)
    #define SS              P14
#else
    #define SS              P24
#endif

//-----------------------------------------------------------------------------------------------------------
void SPI_Error(void)
{
    printf ("\nSPI error.\n");
    while(1)                                    // SPI error and P0.7 flash/
    {
        P07 = 1;
        Delay1ms(500);
        P07 = 0;
        Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------
void SPI_Clock_Select(E_SPICLK_Sel Clock)
{
    switch(Clock)
    {
        case E_ClockDev16:
            clr_SPR0;                           // SPI clock = Fsys/16
            clr_SPR1;
            break;
        case E_ClockDev32:
            set_SPR0;                           // SPI clock = Fsys/32
            clr_SPR1;
            break;
        case E_ClockDev64:
            clr_SPR0;                           // SPI clock = Fsys/64
            set_SPR1;
            break;
        case E_ClockDev128:
            set_SPR0;                           // SPI clock = Fsys/128
            set_SPR1;
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void SPI_Initial(void)
{
#if (SPI_Pin_Sel == 0x00)
    P3M1 |= SET_BIT4;                           // Enables Schmitt trigger inputs on Port 0.
    P3M1 |= SET_BIT5;                           // Enables Schmitt trigger inputs on Port 1.
#else
    P3M1 |= SET_BIT6;                           // Enables Schmitt trigger inputs on Port 2.
#endif
    set_DISMODF;                                // /SS output ( No Mode Fault )
    set_SSOE;

    clr_LSBFE;                                  // MSB first

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

    set_MSTR;                                   // SPI in Master mode
    SPI_Clock_Select(E_ClockDev128);            // Select SPI clock
    set_SPIEN;                                  // Enable SPI function
    clr_SPIF;
}
//-----------------------------------------------------------------------------------------------------------
void Start_Test_SPI(UINT8 *pu8MID,UINT8 *pu8DID)
{
    SPDR = 0x90;                                // Send 0x90 to Slave
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0x4E)
       SPI_Error();
    printf ("\nSlave Return %c!\n",SPDR);

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

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

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

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

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

    SPDR = 0xFF;
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    *pu8DID = SPDR;                             // Receive Slave 2nd DATA from Slave
    printf ("\nSlave Return %c!\n",SPDR);
}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{
    UINT8 u8MID,u8DID;

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


    SPI_Initial();

    Start_Test_SPI(&u8MID,&u8DID);

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

    printf ("\nSPI Test OK!\n");
    while(1)                                    // SPI transmission finish and P0.6 flash
    {
        P14 = 1;
        P21 = 1;
        Delay1ms(500);
        P14 = 0;
        P21 = 0;
        Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------


沙发
zhuotuzi|  楼主 | 2017-5-13 18:29 | 只看该作者
从机的代码

/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//  Application: SPI Function
//  Slave receive 0x90 and return 0x4E
//  Slave receive 0x01 and return 0x55
//  Slave receive 0x02 and return 0x56
//  Slave receive 0x03 and return 0x4F
//  Slave receive 0x04 and return 0x54
//
//  Slave send 0x4F and 0x4E to Master after receiving
//
//  Output : P1.4 & P2.1 flash when SPI pass.
//           P0.7 flash when SPI error
//***********************************************************************************************************

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

#define Uart_Port_Sel   0x00
#define SPI_Pin_Sel     0x80

#include <stdio.h>
#include <intrins.h>
#include <string.h>
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "SPI.h"
#include "Version.h"

//-----------------------------------------------------------------------------------------------------------
void SPI_Error(void)
{
    while(1)                                    // SPI error and P0.7 flash/
    {
        P07 = 1;
        Delay1ms(500);
        P07 = 0;
        Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------
void SPI_Initial(void)
{
#if (SPI_Pin_Sel == 0x00)
    P3M1 |= SET_BIT4;                           // Enables Schmitt trigger inputs on Port 0.
    P3M1 |= SET_BIT5;                           // Enables Schmitt trigger inputs on Port 1.
#else
    P3M1 |= SET_BIT6;                           // Enables Schmitt trigger inputs on Port 2.
#endif
    clr_MSTR;                                   // SPI in Slave mode
    clr_LSBFE;                                  // MSB first

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

    set_SPIEN;                                  // Enable SPI function
    clr_SPIF;
}
//-----------------------------------------------------------------------------------------------------------
void Slave_Receive_Data(void)
{
    SPDR = 0x4E;                                // Receive Master 1st DATA
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0x90)
       SPI_Error();

    SPDR = 0x55;                                // Receive Master 2nd DATA
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0x01)
       SPI_Error();

    SPDR = 0x56;                                // Receive Master 3rd DATA
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0x02)
        SPI_Error();

    SPDR = 0x4F;                                // Receive Master 4th DATA
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0x03)
        SPI_Error();

    SPDR = 0x54;                                // Receive Master 5th DATA
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0x04)
        SPI_Error();
}
//-----------------------------------------------------------------------------------------------------------
void Slave_tranmit_Data(void)
{
    SPDR = 0x4F;                                // Send 1st data (0x4F) to Master
    while(!(SPSR & SET_BIT7));
    clr_SPIF;
    if(SPDR != 0xFF)
        SPI_Error();

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

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


    Slave_Receive_Data();                       // Slave receive data from master
    Slave_tranmit_Data();                       // Slave transmit data to master
    printf ("\nSPI Test OK!\n");
    while(1)
    {
        P14 = 1;
        P21 = 1;
        Delay1ms(500);
        P14 = 0;
        P21 = 0;
        Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------


使用特权

评论回复
板凳
zhuotuzi|  楼主 | 2017-5-13 18:30 | 只看该作者
众所周知,这个单片机是新唐出的一款4T的8位单片机。

使用特权

评论回复
地板
zhuotuzi|  楼主 | 2017-5-13 18:30 | 只看该作者
N79E715高速4T 8051单片机系列


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)


使用特权

评论回复
5
zhuotuzi|  楼主 | 2017-5-13 18:32 | 只看该作者

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]






使用特权

评论回复
6
zhuotuzi|  楼主 | 2017-5-13 18:34 | 只看该作者
大家都知道,普通的51没有这么多的通信接口外设,一般也就一个UART串口。
新唐的比较给力,具备丰富的片上外设。
N79E715系列 有支持高速串行通信的SPI模块。SPI 为全双工、高速、同步传输总线在微芯片外设EEPROM, LCD 驱动, D/A 转换之间,提供主机从机模式传输,速度可达主机模式下FSYS/16和从机模式下FSYS/4,支持传输完成标志位 写 冲突标志位。 在多主机系统中,SPI 支持主机模式故障报错用以防止主机冲突。

使用特权

评论回复
7
zhuotuzi|  楼主 | 2017-5-13 18:36 | 只看该作者

         SPI寄存器板块是SPI模块的主要组成部分,包括逻辑控制,波特率控制和管脚逻辑控制,SPI 包括移位寄存器和读出数据缓冲器,传送数据是单缓冲器,接收数据是双缓冲器。在传送完成之前传送的数据不能写入移位装置。
         SPI 界面需要四个管脚 主进/从出(MISO),主出/从进(MOSI),移位时钟(SPCLK), 和从机选择(SS)。MOSI脚用于传输主机到从机的8位数据,因些,MOSI是一个主机设备的输出引脚,从机设备的输入引脚。相应的,MISO用于接收从机到主机的串行数据。SPCLK引脚为主机模式下的时钟输出,从机模式的时钟输入。移位时钟用于MOSI和MISO脚之间数据传输的时钟同步。位移时钟由主机输出,一组SPI传输系统上只能有一个主机以避免设备冲突。建议该管脚设置为史密特触发输入模式。

使用特权

评论回复
8
zhuotuzi|  楼主 | 2017-5-13 19:25 | 只看该作者



图13–2 为典型的SPI设备通信总线通常为 3 信号线相连, MOSI ~ MOSI, MISO ~ MISO, 和 SPCLK ~SPCLK. 主机通过四线并行连接的方式,每根SS线分别控制每个从机。MCU1 和 MCU2 可以任意定义为主/从机模式. SS需配置为主机模式侦测功能 避免多主机冲突。

使用特权

评论回复
9
zhuotuzi|  楼主 | 2017-5-13 20:10 | 只看该作者
强烈推荐用8位机可以选这个。

使用特权

评论回复
10
643757107| | 2017-5-14 00:06 | 只看该作者
51都可以用SPI接口了,也是很高端了。

使用特权

评论回复
11
huangcunxiake| | 2017-5-14 15:51 | 只看该作者
新唐的51,除了内核是51外,片上外设配置不比那些ARM的差。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

164

主题

3192

帖子

7

粉丝