/*---------------------------------------------------------------------------------------------------------*/
/* */
/* 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);
}
}
//-----------------------------------------------------------------------------------------------------------
|