Nuvoton M051 spi usage program

Publisher:丝路老君Latest update time:2014-12-12 Source: 51heiKeywords:spi Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "SPI.h"
#define DEBUGMSG printf
STATIC UINT32 g_unSpi0Rx0Data = 0, g_unSpi1Rx0Data = 0;
/****************************************
*Function name: Spi0MasterInit
*Input: None
*Output: None
*Function: Initialize SPI0 to master mode
**********************************************/
VOID Spi0MasterInit(VOID)
{                                    
    P1_MFP &= ~(P14_AIN4_SPI0SS | P15_AIN5_SPI0MOSI | P16_AIN6_SPI0MISO | P17_AIN7_SPI0CLK); 
 P1_MFP |= (SPI0SS | SPI0MOSI | SPI0MISO | SPI0CLK); //Enable SPI0 related pins
 ENABLE_SPI0_CLK; //SPI0 clock enable                  
    SPI0_SSR &= ~LVL_H; //The slave select signal is activated by a low level
    SPI0_CNTRL &= ~LSB_FIRST; //The most significant bit is sent/received first
    SPI0_CNTRL &= ~CLKP_IDLE_H; //SCLK is low when idle
    SPI0_CNTRL |= TX_NEG_F; //SDO signal changes on the falling edge of SPICLK
    SPI0_CNTRL &= ~RX_NEG_F; //SDI signal is latched on the rising edge of SPICLK
    CLKDIV &= 0xFFFFFFF0; //HCLK_N = 0, Pclk = SYSclk/(HCLK_N+1)
    SPI0_DIVIDER &= 0xFFFF0000; //SPIclk = Pclk/((HCLK_N+1)*2)
    SPI0_DIVIDER |= 0x00000002;   
    SET_SPI0_MASTER_MODE; //SPI0 works in master mode
    ENABLE_SPI0_AUTO_SLAVE_SLECT; //Enable automatic slave selection
    SPI0_SSR |= SSR_ACT;     
}
/****************************************
*Function name: Spi0Send1W
*Input: ulData sent data
          ucLength data length
*Output: None
*Function: SPI0 sends data
**********************************************/
VOID Spi0Send1W(UINT32 ulData, UINT8 ucLength)
{
    SPI0_CNTRL &= TX_NUM_ONE;      
    Spi0Length(ucLength);
    SPI0_TX0 = ulData;
    SPI0_CNTRL |= GO_BUSY;         
}

 

/****************************************
*Function name: GetSlaveID
*Input: None
*Output: None
*Function: Get slave ID
******************************************/
VOID GetSlaveID(VOID)
{
    Spi0Send1W(0x00000055, 0x08);
    while((SPI0_CNTRL & GO_BUSY)!=0);

    Spi0Send1W(0x00000000, 0x08);
    while((SPI0_CNTRL & GO_BUSY)!=0);
    g_unSpi0Rx0Data = SPI0_RX0;
}
/****************************************
*Function name: Spi0Length
*Input: ucLength data length
*Output: None
*Function: Set SPI0 data length
******************************************/
VOID Spi0Length(UINT8 ucLength)
{
     if(ucLength <= 0x20)
    {
        if((ucLength & 0x01) == 0)
            SPI0_CNTRL &= ~(1<<3);
        else
            SPI0_CNTRL |= (1<<3);
        if((ucLength & 0x02) == 0)
            SPI0_CNTRL &= ~(1<<4);
        else
            SPI0_CNTRL |= (1<<4);

        if((ucLength & 0x04) == 0)
            SPI0_CNTRL &= ~(1<<5);
        else
            SPI0_CNTRL |= (1<<5);

        if((ucLength & 0x08) == 0)
            SPI0_CNTRL &= ~(1<<6);
        else
            SPI0_CNTRL |= (1<<6);

        if((ucLength & 0x10) == 0)
            SPI0_CNTRL &= ~(1<<7);
        else
            SPI0_CNTRL |= (1<<7);
    }
}
/****************************************
*Function name: Spi1SlaveInit
*Input: None
*Output: None
*Function: Initialize SPI1 as slave
**********************************************/
VOID Spi1SlaveInit(VOID)
{                              
    P0_MFP &= ~(P04_AD4_SPI1SS | P05_AD5_SPI1MOSI | P06_AD6_SPI1MISO | P07_AD7_SPI1CLK); 
 
 P0_MFP |= (SPI1SS | SPI1MOSI | SPI1MISO | SPI1CLK); //Enable SPI1 related pins 
 ENABLE_SPI1_CLK; 
 SPI1_SSR &= LTRIG_EDG; //Slave input edge trigger
    SPI1_SSR &= ~LVL_H; //Slave select signal is activated by low level

    SPI1_CNTRL &= ~LSB_FIRST; //First send/receive the most significant bit
    SPI1_CNTRL &= ~CLKP_IDLE_H; //SCLK is low when idle
    SPI1_CNTRL |= TX_NEG_F; //SDO signal changes on the falling edge of SPICLK
    SPI1_CNTRL &= ~RX_NEG_F; //SDI signal is latched on the rising edge of SPICLK

    CLKDIV &= 0xFFFFFFF0; //HCLK_N = 0, Pclk = SYSclk/(HCLK_N+1)
    SPI1_DIVIDER &= 0xFFFF0000; //SPIclk = Pclk/((HCLK_N+1)*2)
    SPI1_DIVIDER |= 0x00000002;
    SET_SPI1_SLAVE_MODE; //SPI1 works in slave mode
   
    ENABLE_SPI1_INTERRUPT; //Enable SPI1 interrupt
    NVIC_ISER |= SPI1_INT;       
    Spi1Length(8); //Set SPI1 data length
    SPI1_GO_BUSY; //Wait for SPI1 RX0 to receive signal
}  
/********************************************
*Function name: Spi1Length
*Input: ucLength data length
*Output: None
*Function: Set SPI1 data length
********************************************/
VOID Spi1Length(UINT8 ucLength)
{
     if(ucLength <= 0x20)
    {
        if((ucLength & 0x01) == 0)
            SPI1_CNTRL &= ~(1<<3);
        else
            SPI1_CNTRL |= (1<<3);
        if((ucLength & 0x02) == 0)
            SPI1_CNTRL &= ~(1<<4);
        else
            SPI 1_CNTRL |= (1<<4);

        if((ucLength & 0x04) == 0)
            SPI1_CNTRL &= ~(1<<5);
        else
            SPI1_CNTRL |= (1<<5);

        if((ucLength & 0x08) == 0)
            SPI1_CNTRL &= ~(1<<6);
        else
            SPI1_CNTRL |= (1<<6);

        if((ucLength & 0x10) == 0)
            SPI1_CNTRL &= ~(1<<7);
        else
            SPI1_CNTRL |= (1<<7);
    }
}
/****************************************
*Function name: PrintGetData
*Input: None
*Output: None
*Function: Print the data received by SPI0/1
******************************************/
VOID PrintGetData(VOID)
{
    DEBUGMSG("Slave Get Command = %X ", g_unSpi1Rx0Data);
    DEBUGMSG("Master Get Feed Back Data = %X ", g_unSpi0Rx0Data);
}
/*********************************************
*Function name: SPI1_IRQHandler
*Input: None
*Output: None
*Function: SPI1 interrupt service function
************************************************/
VOID SPI1_IRQHandler(VOID)
{
    SPI1_CNTRL |= SPI_IF; //Clear interrupt flag
    if(SPI1_RX0 == 0x55)
    {
        g_unSpi1Rx0Data = SPI1_RX0;
        SPI1_TX0 = 0x0000000DA;
    }
    else
        SPI1_TX0 = 0x00000000;
    SPI1_GO_BUSY; //Wait for SPI1 RX0 to receive signal
}
/****************************************
*Function name: main
*Input: None
*Output: None
*Function: Function body
******************************************/
INT32 main(VOID)
{
    PROTECT_REG //Protect FLASH memory during ISP download
 (
   PWRCON |= XTL12M_EN; //Default clock source is external crystal
   while((CLKSTATUS & XTL12M_STB) == 0); //Wait for 12MHz clock to stabilizeCLKSEL0  
      = (CLKSEL0 & (~HCLK)) | HCLK_12M; //Set external crystal as system clock 
 ) 
 UartInit(12000000,9600); //Set baud rate to 9600bps 
    Spi0MasterInit(); //SPI0 initialized to master modeSpi1SlaveInit
    (); //SPI1 initialized to slave modewhile   
    (1)
    {
        DEBUGMSG("Master will send 0x55 data to slave and receive 0xDA data ");
        DEBUGMSG("Put AnyKey to Start Test ");
        GetSlaveID();
        PrintGetData();
        printf(" "); 
  Delayms(1000);
    }
}

Keywords:spi Reference address:Nuvoton M051 spi usage program

Previous article:Nuvoton M051 ADC analog-to-digital conversion program
Next article:Nuvoton M051 WDT watchdog program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号