[mw_shl_code=c,true]#include<stc12c5a.h>
#define BYTE unsigned char
#define WORD unsigned int
#define HIGH 1
#define LOW 0
#define FALSE 0
#define TRUE 1
sbit SCLK=P1^7;
sbit DIN=P1^5;
sbit DOUT=P1^6;
void ClrCS(void)
{P14=0;}
void SetCS(void)
{P14=1;}
void Delay(unsigned int nTime)
{
while(--nTime) {;}
}
/***********************************************************
Function: Delay
Describe: Time delay
Input: Byte formated time
Output: void
***********************************************************/
/***********************************************************
Function: SPI_Initial
Describe: Initialize the SPI interface
Input: void
Output: void
***********************************************************/
void SPI_Initial(void)
{
SCLK = LOW;
DIN = LOW;
DOUT = LOW;
Delay(5);
}
/***********************************************************
Function: SPI_Transmit
Describe: Send and receive 16-bit data from the SPI interface
Input: 16-bit data to be send
Output: 16-bit data of read, user analyse the validity
***********************************************************/
WORD SPITransmit(WORD wSendData)
{
BYTE i;
WORD wReadData;
wReadData = 0;
for(i = 0; i < 16; i++)
{
if(wSendData & 0x8000)
DIN = 1;
else
DIN = 0;
wReadData<<=1; //Shift first, then fill the bit
SCLK = HIGH;
wSendData<<= 1;
wReadData |= DOUT; //Receive the 16-bit data
Delay(5);
SCLK = LOW;
Delay(5); //Can be comment by acture use
}
return(wReadData);
}
unsigned int SendTo814X(unsigned char chigh,unsigned char clow)
{
unsigned int cTempHigh,cTempLow;
unsigned int RevData;
ClrCS(); //置814x片选有效
Delay(10); //延时时间参考814x数据手册的要求
cTempHigh=SPITransmit(chigh); //向814X发送高位数据
cTempLow=SPITransmit(clow); //向814X发送低位数据
Delay(10); //延时时间参考814x数据手册的要求
SetCS(); //置814x片选无效
RevData=cTempHigh<<8|cTempLow;
return RevData; //返回从814x读出的数据
}
unsigned int Write814xConfig(unsigned char n,unsigned char high,unsigned char low) //n 表示设置的串口号,high表示给配置寄存器的高位字节(低三位有效),low表示给配置寄存器的低位字节
{
unsigned char ch;
unsigned int RevData;
high=high&0x07;
ch= 0xc0 | (n<< 3);
high=high|ch;
RevData=SendTo814X(high,low); //向指定的串口功能设置寄存器写配置
Delay(5);
return RevData;
}
unsigned int SendToAllCom(unsigned char c,bit Pt) //c为需要发送的数据,Pt为第九位数据
{
unsigned char ch;
unsigned int RevData;
if(Pt)
ch=0xa1;
else
ch=0xa0;
RevData=SendTo814X(ch,c);
return RevData; //向所有串口发送数据并读出接收FIFO数据
}
void main()
{
Write814xConfig(1,0xc0,0xaa);
//SendToAllCom(0xaa,0);
}[/mw_shl_code]
|