打印
[DemoCode下载]

N76E003主从机中断通信

[复制链接]
841|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
小灵通2018|  楼主 | 2019-5-13 22:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

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

//***********************************************************************************************************
//  File Function: N76E003 SPI in Master mode demo code
//***********************************************************************************************************

#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"

//***********************************************************************************************************
//  Application: SPI Function
//  Master send 0x90 and recevie 0x4E
//  Master send 0x01 and recevie 0x55
//  Master send 0x02 and recevie 0x56
//  Master send 0x03 and recevie 0x4F
//  Master send 0x04 and recevie 0x54
//
//  Master recevie 0x4E and 0x4F form slave after transmitting
//
//  Output : P1.4 & P2.1 flash when SPI pass
//           UART show result on hyper-terminal
//           P0.7 flash when SPI error
//***********************************************************************************************************

#if 0
///*****************************************************************************************
//* For ADC INIT setting
//*****************************************************************************************/
//#define                SPICLK_DIV2                        clr_SPR0;clr_SPR1
//#define                SPICLK_DIV4                        set_SPR0;clr_SPR1
//#define                SPICLK_DIV8                        clr_SPR0;set_SPR1
//#define                SPICLK_DIV16                set_SPR0;set_SPR1
//#define                Enable_SPI_Interrupt                set_ESPI;set_EA
//#define                SS                P15
#endif

//-----------------------------------------------------------------------------------------------------------
void SPI_Error(void)
{
    printf ("\nSPI error.\n");
    while(1)                                    // SPI error and P0.7 flash/
    {
        P07 = 1;
        Timer0_Delay1ms(500);
        P07 = 0;
        Timer0_Delay1ms(500);
    }
        }
//-----------------------------------------------------------------------------------------------------------
void SPI_Initial(void)
{              
                P15_Quasi_Mode;                                                                                                                // P15 (SS) Quasi mode
                P10_Quasi_Mode;                                                                                                                // P10(SPCLK) Quasi mode
                P00_Quasi_Mode;                                                                                                                // P00 (MOSI) Quasi mode
    P01_Quasi_Mode;                                                                                                                // P22 (MISO) Quasi mode
            
    set_DISMODF;                                                                                                                        // SS General purpose I/O ( No Mode Fault )
    clr_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
     
    SPICLK_DIV2;                                                                                                                        // Select SPI clock
    Enable_SPI_Interrupt;                                                                                        // Enable SPI interrupt
    set_SPIEN;                                                                                                                                // Enable SPI function
}
//-----------------------------------------------------------------------------------------------------------
void Start_Sending_SPI(UINT8 *pu8MID,UINT8 *pu8DID)
{
    SS = 0;

    SPDR = 0x90;                                // Send 0x90 to Slave
    PCON |= SET_BIT0;                           // Enter idle mode
    if(SPDR != 0x4E)                            // Receive slave 1st DATA
       SPI_Error();
    printf ("\nSlave Return %c!\n",SPDR);
                                          
    SPDR = 0x01;                                // Send 0x01 to Slave
    PCON |= SET_BIT0;                           // Enter idle mode
    if(SPDR != 0x55)                            // Receive slave 2nd DATA  
       SPI_Error();
    printf ("\nSlave Return %c!\n",SPDR);

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

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

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

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

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

    SS = 1;   
}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{      
    UINT8 u8MID,u8DID;
   
    Set_All_GPIO_Quasi_Mode;
    InitialUART0_Timer1(115200);             /* 115200 Baud Rate*/

    SPI_Initial();

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

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

    printf ("\nSPI Test OK!\n");
    while(1)                                    // SPI transmission finish
    {
        P12 = 1;
        Timer0_Delay1ms(500);
        P12 = 0;
        Timer0_Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------
void SPI_ISR(void) interrupt 9                  // Vecotr [url=home.php?mod=space&uid=72445]@[/url]  0x4B
{
    clr_SPIF;
    Timer3_Delay10us(1);
}
//-----------------------------------------------------------------------------------------------------------


使用特权

评论回复
沙发
小灵通2018|  楼主 | 2019-5-13 22:17 | 只看该作者
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

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

//***********************************************************************************************************
//  File Function: N76E003 SPI in Slave mode demo code
//***********************************************************************************************************
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"

//***********************************************************************************************************
//  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.2 GPIO flash when SPI pass.
//           P0.7 flash when SPI error
//***********************************************************************************************************

UINT8   u8Receive_Data[5];
UINT16  u16CNT = 0;
bit     SPI_Send_Flag = 0;

//-----------------------------------------------------------------------------------------------------------
void SPI_Error(void)
{
    while(1)                                    // SPI error and P0.7 flash/
    {
        P07 = 1;
        Timer0_Delay1ms(500);
        P07 = 0;
        Timer0_Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------
void SPI_Initial(void)
{
                P15_Quasi_Mode;                                                                                                                //P15 (SS) Quasi mode
                P10_Quasi_Mode;                                                                                                                //P10(SPCLK) Quasi mode
                P00_Quasi_Mode;                                                                                                                //P00 (MOSI) Quasi mode
    P01_Quasi_Mode;                                                                                                                //P22 (MISO) Quasi mode;

    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     
      
    Enable_SPI_Interrupt;                             // Enable SPI interrupt
    set_SPIEN;                                  // Enable SPI function
   
    clr_SPIF;                                   // Clear SPI flag
}
//-----------------------------------------------------------------------------------------------------------
void Slave_Receive_Data(void)
{
    SPDR = 0x4E;                             
    PCON |= SET_BIT0;                           // Enter idle mode
    if(u8Receive_Data[0] != 0x90)               // Receive 1st DATA form master
        SPI_Error();
         
    SPDR = 0x55;
    PCON |= SET_BIT0;                           // Enter idle mode                       
    if(u8Receive_Data[1] != 0x01)               // Receive 2nd DATA form master
        SPI_Error();

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

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

    SPDR = 0x54;
    PCON |= SET_BIT0;                           // Enter idle mode
    if(u8Receive_Data[4] != 0x04)               // Receive 5th DATA form master
        SPI_Error();
}
//-----------------------------------------------------------------------------------------------------------
void Slave_Transmit_Data(void)
{
    SPI_Send_Flag = 1;
    SPDR = 0x4F;                                // Send 1st data (04F) to Master
    PCON |= SET_BIT0;                           // Enter idle mode      

    SPI_Send_Flag = 1;
    SPDR = 0x4E;                                // Send 2nd data (0x4E) to Master
    PCON |= SET_BIT0;                           // Enter idle mode
}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{   
    Set_All_GPIO_Quasi_Mode;
    InitialUART0_Timer1(115200);             /* 115200 Baud Rate*/

    SPI_Initial();

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

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

    clr_ESPI;
    SPDR = 0x00;
    printf ("\nSPI Test OK!\n");
    while(1)                                    // SPI transmission finish
    {
        P12 = 1;
        Timer0_Delay1ms(500);
        P12 = 0;
        Timer0_Delay1ms(500);
    }
}
//-----------------------------------------------------------------------------------------------------------
void SPI_ISR(void) interrupt 9                  // Vecotr @  0x4B
{
    clr_SPIF;                                   
    if(!SPI_Send_Flag)
    {      
        u8Receive_Data[u16CNT] = SPDR;
        u16CNT ++;
    }
    SPI_Send_Flag = 0;
}
//-----------------------------------------------------------------------------------------------------------

使用特权

评论回复
板凳
xinpian101| | 2019-5-14 22:07 | 只看该作者
SPI非常好用 一个接口。

使用特权

评论回复
地板
xuanhuanzi| | 2019-5-14 22:28 | 只看该作者
常用的一种方法。

使用特权

评论回复
5
zhuomuniao110| | 2019-5-15 00:23 | 只看该作者
这种应用,一般主机形式较多

使用特权

评论回复
6
捉虫天师| | 2019-5-15 00:32 | 只看该作者
中断操作使用很多。

使用特权

评论回复
7
dongnanxibei| | 2019-5-17 01:03 | 只看该作者
寄存器掌握牢固可以

使用特权

评论回复
8
yiyigirl2014| | 2019-5-17 22:10 | 只看该作者
还有错误指示,考虑全面。

使用特权

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

本版积分规则

117

主题

1444

帖子

4

粉丝