//-----------------------------------------------------------------------------
// F32x_SPI0_Master.c
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "C8051F320.h" // SFR declarations
#include <intrins.h>
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK 12000000 // Internal oscillator frequency in Hz
#define SPI_CLOCK 50000 // Maximum SPI clock
// The SPI clock is a maximum of 250 kHz
// when this example is used with
// the SPI0_Slave code example.
#define MAX_BUFFER_SIZE 22 // Maximum buffer Master will send
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
unsigned char SPI_Data_Array[ ] = { 0XFD, 0X00, 0X14, 0X01, 0X01, 0XD1, 0XB6, 0XB7, 0XC9, 0XD3, 0XEF ,0XD2, 0XF4, 0XA3, 0XAC, 0XB9, 0XB5, 0XCD, 0XA8 ,0XCE, 0XDE, 0XCF, 0XDE };
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
sbit S4041RST=P2^2;
void PCA0_Init (void);
void Oscillator_Init (void);
void Port_Init (void);
void SPI0_Init (void);
void Init_Device (void);
void SPI_Delay(unsigned int);
void SPI_SendByte(unsigned char);
unsigned char SPI_RecvByte(void);
unsigned char ReceiveData;
#define DELAY_TIME 100
/**************************************************************
Name: main()
Argu: None
Ret: None
***************************************************************/
void main (void)
{
unsigned int num;
Init_Device (); // Initializes hardware peripherals
S4041RST=0;
SPI_Delay(5000);
S4041RST=1;
NSSMD0 = 0;
SPI_Delay(5000);
while(1)
{
for( num=0 ; num<=MAX_BUFFER_SIZE ; num++ ) //text to speech
{
SPI_SendByte(SPI_Data_Array[num]);
}
while(1)
{
SPI_Delay(50);
ReceiveData= SPI_RecvByte( );
if( ReceiveData == 0x4F)
{
break;
}
}
}
}
/**************************************************************
Name: _SPI_SendByte
Function: Send a byte frame data with SPI mode
Argu: unsigned char ucSPIByte
Ret: None
***************************************************************/
void SPI_SendByte(unsigned char ucSPIByte)
{
unsigned int uiWait=0;
NSSMD0 = 0;
while (!TXBMT) /* send buffer empty flag */
{
uiWait++;
if(uiWait>30000)
break;
}
uiWait=0;
SPI0DAT = ucSPIByte;
while(!SPIF) /* send finish flag */
{
uiWait++;
if(uiWait>30000)
{
uiWait=0;
break;
}
}
SPIF = 0;
NSSMD0 = 1;
SPI_Delay(35);
}
/**************************************************************
Name: _SPI_RecvByte
Function: Receive a byte frame data with SPI mode
Argu: unsigned char ucSPIByte
Ret: None
***************************************************************/
unsigned char SPI_RecvByte(void)
{
unsigned int uiWait=0;
unsigned char ucTemp;
NSSMD0 = 0;
while(!TXBMT)
{
uiWait++;
if(uiWait>30000)
break ;
}
uiWait=0;
SPI0DAT = 0;
while(!SPIF)
{
uiWait++;
if(uiWait>30000)
{
uiWait=0;
break ;
}
}
SPIF = 0;
NSSMD0 = 1;
SPI_Delay(35);
ucTemp = SPI0DAT;
return(ucTemp);
}
/**************************************************************
Name: PCA0_Init
Function: This function disables the watchdog timer.
Argu: None
Ret: None
***************************************************************/
void PCA0_Init (void)
{
PCA0MD &= ~0x40; // Disable the Watchdog Timer
PCA0MD = 0x00;
}
/**************************************************************
Name: Oscillator_Init
Function: This function initializes the system clock to use the internal oscillator at 12 MHz.
Argu: None
Ret: None
***************************************************************/
void Oscillator_Init (void)
{
OSCICN = 0x83; // Set the internal oscillator to
// 12 MHz
}
/**************************************************************
Name: Port_Init
Function: This function configures the crossbar and GPIO ports
Argu: None
Ret: None
P0.0 - SCK (SPI0), Push-Pull, Digital
P0.1 - MISO (SPI0), Open-Drain, Digital
P0.2 - MOSI (SPI0), Push-Pull, Digital
P0.3 - NSS (SPI0), Push-Pull, Digital
P2.2 - Skipped, Push-Pull, Digital (LED D4 on Target Board)
***************************************************************/
void PORT_Init (void)
{
P0MDOUT = 0x0D; // Make SCK, MOSI, and NSS push-pull
P2MDOUT = 0x08; // Make the LED push-pull
P2SKIP = 0x08; // Skip the LED (P2.2)
XBR0 = 0x02; // Enable the SPI on the XBAR
XBR1 = 0x40; // Enable the XBAR and weak pull-ups
}
/**************************************************************
Name: SPI0_Init
Function: Configures SPI0 to use 4-wire Single Master mode. The SPI timing is
configured for Mode 0,0 (data centered on first edge of clock phase and
SCK line low in idle state).CKPHA = '0', CKPOL = '0'
Argu: None
Ret: None
***************************************************************/
void SPI0_Init()
{
SPI0CFG = 0x40; // Enable the SPI as a Master
SPI0CN = 0x0f; // 4-wire Single Master, SPI enabled
SPI0CKR = (SYSCLK/(2*SPI_CLOCK))-1;
}
/**************************************************************
Name: Init_Device
Function: Calls all device initialization functions.
Argu: None
Ret: None
***************************************************************/
void Init_Device (void)
{
PCA0_Init ();
Oscillator_Init ();
Port_Init ();
SPI0_Init ();
}
/**************************************************************
Name: _SPI_Delay
Function: Delay some time
Argu: unsigned int uiDlyTime
Ret: None
***************************************************************/
void SPI_Delay(unsigned int uiDlyTime){
unsigned int i ;
for(i=0;i<uiDlyTime;i++)
{}
}
/****************************END******************************/
- 1
- 2
前往页