大神们:求助STC12C2052AD中SPI通信方式
本帖最后由 YIBAOJIA 于 2012-10-8 16:56 编辑/*适用单主单从系统的SPI功能测试程序*/
/*查询方式*/
#include "reg51.h"
//#define MASTER //define:master undefine:slave
#define FOSC 1 8432000L
//#define BAUD (256 - FOSC / 32 / 11 5200)
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;
sfr AUXR = 0x8e; //Auxiliary register
sfr SPSTAT = 0x84; //SPI status register
#define SPIF 0x80 //SPSTAT.7
#define WCOL 0x40 //SPSTAT.6
sfr SPCTL = 0x85; //SPI control register
#define SSIG 0x80 //SPCTL.7
#define SPEN 0x40 //SPCTL.6
#define DORD 0x20 //SPCTL.5
#define MSTR 0x10 //SPCTL.4
#define CPOL 0x08 //SPCTL.3
#define CPHA 0x04 //SPCTL.2
#define SPDHH 0x00 //CPU_CLK/4
#define SPDH 0x01 //CPU_CLK/16
#define SPDL 0x02 //CPU_CLK/64
#define SPDLL 0x03 //CPU_CLK/128
sfr SPDAT = 0x86; //SPI data register
sbit SPISS = P1^3; //SPI slave select, connect to slave' SS(P1.4) pin
void InitUart();
void InitSPI();
void SendUart(BYTE dat); //send data to PC
BYTE RecvUart(); //receive data from PC
BYTE SPISwap(BYTE dat); //swap SPI data between master and slave
///////////////////////////////////////////////////////////
void main()
{
InitUart(); //initial UART
InitSPI(); //initial SPI
while (1)
{
#ifdef MASTER //for master (receive UART data from PC and send it to slave,
// in the meantime receive SPI data from slave and send it to PC)
SendUart(SPISwap(RecvUart()));
#else //for salve (receive SPI data from master and
ACC = SPISwap(ACC); // send previous SPI data to master)
#endif
}
}
///////////////////////////////////////////////////////////
void InitUart()
{
SCON = 0x5a; //set UART mode as 8-bit variable baudrate
TMOD = 0x20; //timer1 as 8-bit auto reload mode
AUXR = 0x40; //timer1 work at 1T mode
TH1 = TL1 = 0xdd; //11 5200 bps
TR1 = 1;
}
///////////////////////////////////////////////////////////
void InitSPI()
{
SPDAT = 0; //initial SPI data
SPSTAT = SPIF | WCOL; //clear SPI status
#ifdef MASTER
SPCTL = SPEN | MSTR; //master mode
#else
SPCTL = SPEN; //slave mode
#endif
}
///////////////////////////////////////////////////////////
void SendUart(BYTE dat)
{
while (!TI); //wait pre-data sent
TI = 0; //clear TI flag
SBUF = dat; //send current data
}
///////////////////////////////////////////////////////////
BYTE RecvUart()
{
while (!RI); //wait receive complete
RI = 0; //clear RI flag
return SBUF; //return receive data
}
/////////////////////////////////////////////////////////
BYTE SPISwap(BYTE dat)
{
#ifdef MASTER
SPISS = 0; //pull low slave SS
#endif
SPDAT = dat; //trigger SPI send
while (!(SPSTAT & SPIF)); //wait send complete
SPSTAT = SPIF | WCOL; //clear SPI status
#ifdef MASTER
SPISS = 1; //push high slave SS
#endif
return SPDAT; //return received SPI data
}
/*适用单主单从系统的SPI功能测试程序*/
/*中断方式*/
#include "reg51.h"
#define MASTER //define:master undefine:slave
#define FOSC 1 8432000L
#define BAUD (256 - FOSC / 32 / 11 5200)
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;
sfr AUXR = 0x8e; //Auxiliary register
sfr SPSTAT = 0x84; //SPI status register
#define SPIF 0x80 //SPSTAT.7
#define WCOL 0x40 //SPSTAT.6
sfr SPCTL = 0x85; //SPI control register
#define SSIG 0x80 //SPCTL.7
#define SPEN 0x40 //SPCTL.6
#define DORD 0x20 //SPCTL.5
#define MSTR 0x10 //SPCTL.4
#define CPOL 0x08 //SPCTL.3
#define CPHA 0x04 //SPCTL.2
#define SPDHH 0x00 //CPU_CLK/4
#define SPDH 0x01 //CPU_CLK/16
#define SPDL 0x02 //CPU_CLK/64
#define SPDLL 0x03 //CPU_CLK/128
sfr SPDAT = 0x86; //SPI data register
sbit SPISS = P1^3; //SPI slave select, connect to slave' SS(P1.4) pin
sbit EADC_SPI = IE^5;
#define ESPI 0x08; //AUXR.3
void InitUart();
void InitSPI();
void SendUart(BYTE dat); //send data to PC
BYTE RecvUart(); //receive data from PC
///////////////////////////////////////////////////////////
void main()
{
InitUart(); //initial UART
InitSPI(); //initial SPI
AUXR |= ESPI;
EADC_SPI = 1;
EA = 1;
while (1)
{
#ifdef MASTER //for master (receive UART data from PC and send it to slave,
//in the meantime receive SPI data from slave and send it to PC)
ACC = RecvUart();
SPISS = 0; //pull low slave SS
SPDAT = ACC; //trigger SPI send
#endif
}
}
void spi_isr() interrupt 5 using 1 //SPI interrupt routine 5 (002BH)
{
SPSTAT = SPIF | WCOL; //clear SPI status
#ifdef MASTER
SPISS = 1; //push high slave SS
SendUart(SPDAT); //return received SPI data
#else //for salve (receive SPI data from master and
SPDAT = SPDAT; // send previous SPI data to master)
#endif
}
void InitUart()
{
SCON = 0x5a; //set UART mode as 8-bit variable baudrate
TMOD = 0x20; //timer1 as 8-bit auto reload mode
AUXR = 0x40; //timer1 work at 1T mode
TH1 = TL1 = BAUD; //11 5200 bps
TR1 = 1;
}
///////////////////////////////////////////////////////////
void InitSPI()
{
SPDAT = 0; //initial SPI data
SPSTAT = SPIF | WCOL; //clear SPI status
#ifdef MASTER
SPCTL = SPEN | MSTR; //master mode
#else
SPCTL = SPEN; //slave mode
#endif
}
///////////////////////////////////////////////////////////
void SendUart(BYTE dat)
{
while (!TI); //wait pre-data sent
TI = 0; //clear TI flag
SBUF = dat; //send current data
}
///////////////////////////////////////////////////////////
BYTE RecvUart()
{
while (!RI); //wait receive complete
RI = 0; //clear RI flag
return SBUF; //return receive data
}
其中中断方式:SPDAT = SPDAT;
查询方式 :ACC = SPISwap(ACC);
这两句是什么意思啊??? SPDAT = SPDAT;将从串口收到的数据再通过SPI 发送给串口,在串口小助手上显示
ACC = SPISwap(ACC); ACC是个累加器,功能和SPDAT = SPDAT一样 国防大学 发表于 2012-10-21 21:49 static/image/common/back.gif
SPDAT = SPDAT;将从串口收到的数据再通过SPI 发送给串口,在串口小助手上显示
ACC = SPISwap(ACC); ACC是个 ...
这个是两个单片机通信没有和PC通信 也没开串行通信 何来发送给串行助手?? YIBAOJIA 发表于 2012-10-21 23:39 static/image/common/back.gif
这个是两个单片机通信没有和PC通信 也没开串行通信 何来发送给串行助手?? ...
哥哥,那为何有串口初始化函数void InitUart();
为何有串口接收函数……
真心不明白了&
页:
[1]