CH552/project/examples/SPI/SPI.C

142 lines
5.6 KiB
C++
Raw Normal View History

2018-10-17 10:44:45 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name : SPI.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554 SPI<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD>ӿں<EFBFBD><EFBFBD><EFBFBD>
ע<EFBFBD><EFBFBD>Ƭѡ<EFBFBD><EFBFBD>Чʱ<EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SPI0_S_PRE<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭѡ
<EFBFBD><EFBFBD>Чǰ<EFBFBD><EFBFBD>SPI0_S_PRE<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ȡ<EFBFBD><EFBFBD>SPI0_S_PRE<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>S0_IF_BYTE<EFBFBD>жϡ<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD>з<EFBFBD><EFBFBD>͵Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ð<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽڷŵ<EFBFBD>SPI0_S_PRE<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭѡ<EFBFBD><EFBFBD>Ч<EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>SPI0_DATA<EFBFBD>Ϳ<EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
#include "..\Public\CH554.H" //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>ӡ
#include "..\Public\Debug.H"
#include "SPI.H"
#pragma NOAREGS
/*******************************************************************************
* Function Name : SPIMasterModeSet( UINT8 mode )
* Description : SPI<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* Input : UINT8 mode
* Output : None
* Return : None
*******************************************************************************/
void SPIMasterModeSet(UINT8 mode)
{
SPI0_SETUP &= ~bS0_MODE_SLV; //Masterģʽ
if(mode == 0){
SPI0_CTRL = SPI0_CTRL & ~(bS0_MST_CLK | bS0_CLR_ALL) | bS0_MOSI_OE | bS0_SCK_OE;//ģʽ0
}
else if(mode == 3){
SPI0_CTRL = SPI0_CTRL & ~bS0_CLR_ALL | bS0_MST_CLK | bS0_MOSI_OE | bS0_SCK_OE;//ģʽ3
}
SPI0_CTRL &= ~bS0_MISO_OE;
P1_MOD_OC &= 0x0F;
P1_DIR_PU |= 0xB0; //SCS,MOSI,SCK<43><4B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
P1_DIR_PU &= 0xBF; //MISO<53><EFBFBD><E8B8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/*******************************************************************************
* Function Name : CH554SPIInterruptInit()
* Description : CH554SPI<EFBFBD>жϳ<EFBFBD>ʼ<EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CH554SPIInterruptInit()
{
//IP_EX |= bIP_SPI0; //SPI0<49>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD>
SPI0_SETUP |= bS0_IE_FIFO_OV | bS0_IE_BYTE; //ʹ<>ܽ<EFBFBD><DCBD><EFBFBD>1<EFBFBD>ֽ<EFBFBD><D6BD>жϣ<D0B6>ʹ<EFBFBD><CAB9>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
SPI0_CTRL |= bS0_AUTO_IF; //<2F>Զ<EFBFBD><D4B6><EFBFBD>S0_IF_BYTE<54>жϱ<D0B6>־
SPI0_STAT |= 0xff; //<2F><><EFBFBD><EFBFBD>SPI0<49>жϱ<D0B6>־
#ifdef SPI_Interrupt
IE_SPI0 = 1; //ʹ<><CAB9>SPI0<49>ж<EFBFBD>
#endif
}
/*******************************************************************************
* Function Name : CH554SPIMasterWrite(UINT8 dat)
* Description : CH554Ӳ<EFBFBD><EFBFBD>SPIд<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
* Input : UINT8 dat <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Output : None
* Return : None
*******************************************************************************/
void CH554SPIMasterWrite(UINT8 dat)
{
SPI0_DATA = dat;
while(S0_FREE == 0); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>bS0_DATA_DIRΪ1<CEAA><31><EFBFBD>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӷ<EFBFBD>ȡһ<C8A1><D2BB><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><DABF>ٶ<EFBFBD>д
}
/*******************************************************************************
* Function Name : CH554SPIMasterRead( )
* Description : CH554Ӳ<EFBFBD><EFBFBD>SPI0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
* Input : None
* Output : None
* Return : UINT8 ret
*******************************************************************************/
UINT8 CH554SPIMasterRead()
{
SPI0_DATA = 0xff;
while(S0_FREE == 0);
return SPI0_DATA;
}
/*******************************************************************************
* Function Name : SPISlvModeSet( )
* Description : SPI<EFBFBD>ӻ<EFBFBD>ģʽ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPISlvModeSet( )
{
SPI0_SETUP |= bS0_MODE_SLV; //Slvģʽ
SPI0_CTRL = SPI0_CTRL & ~( bS0_CLR_ALL | bS0_MOSI_OE | bS0_MISO_OE | bS0_SCK_OE) | bS0_AUTO_IF;//<2F><>дFIFO,<2C>Զ<EFBFBD><D4B6><EFBFBD>S0_IF_BYTE<54><45>־
P1_MOD_OC &= 0x0F;
P1_DIR_PU &= 0x0F; //SCS,MOSI,SCK,MISOȫ<4F><C8AB><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/*******************************************************************************
* Function Name : CH554SPISlvWrite(UINT8 dat)
* Description : CH554Ӳ<EFBFBD><EFBFBD>SPIд<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD>ӻ<EFBFBD>ģʽ
* Input : UINT8 dat <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Output : None
* Return : None
*******************************************************************************/
void CH554SPISlvWrite(UINT8 dat)
{
while(S0_IF_BYTE);
S0_IF_BYTE = 0;
SPI0_DATA = dat;
}
/*******************************************************************************
* Function Name : CH554SPISlvRead( )
* Description : CH554Ӳ<EFBFBD><EFBFBD>SPI0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD>ӻ<EFBFBD>ģʽ
* Input : None
* Output : None
* Return : UINT8 ret
*******************************************************************************/
UINT8 CH554SPISlvRead()
{
while(S0_IF_BYTE);
S0_IF_BYTE = 0;
return SPI0_DATA;
}
#if SPI_Interrupt
/*******************************************************************************
* Function Name : ADCInterrupt(void)
* Description : ADC <EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
void SPIInterrupt( void ) interrupt INT_NO_SPI0 using 1 //SPI<50>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>1
{
}
#endif