1. 用51单片机引脚实现spi功能的程序
可以的,但SPI接口的器件有多种工作森尺方式,如高位在前还是低位在前,空闲时时钟线高电平还是低电平
第一个跳变沿还是第二个跳变沿数据有效,程序是不同的,下面程序供参考
sbit CLK=P2^2;
sbit MOSI=P2^3; //发送方方管脚配置
sbit MISO=P2^4;
sbit BIT0=ACC^0;
sbit BIT7=ACC^7;//
void Write(uchar byte)//写数据
{
uchar i;
ACC=byte;
i=8;
while(i)
{
MOSI=BIT7;
CLK=1; // output 'uchar', MSB to MOSI
_nop_();
_nop_(); // shift next bit into MSB..
_nop_();
_nop_();
ACC<<=1;
CLK=0; // Set SCK high..
i--; // ..then set SCK low again
_nop_();
}
}
/****************************************************************************************************
/*函数:Read(uchar reg)
/*功能:NRF24L01的读敏做时序
/****************************************************************************************************/
uchar Read(void)
{
uchar i;
i=8;
sbit BIT0=ACC^0;
sbit BIT7=ACC^7;
while(i)
{
CLK=1; // output 'uchar', MSB to MOSI
_nop_();
_nop_();
_nop_();
_nop_(); // shift next bit into MSB..
ACC<<=1;
BIT0=MISO ;
CLK=0; /桥春衡/ Set SCK high..
i--; // ..then set SCK low again
_nop_();
//led1=~led1;
}
return ACC; // return register value
}
2. 求大侠们,ADXL345与STC15系列单片机进行SPI通讯程序,或者类似单片机的SPI通讯程序
stc spi的例程
#include "reg51.h"
#define FOSC 18432000L
#define BAUD (256 - FOSC / 32 / 115200)
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;
sfr AUXR = 0x8e; //Auxiliary register
sfr SPSTAT = 0xcd; //SPI status register
#define SPIF 0x80 //SPSTAT.7
#define WCOL 0x40 //SPSTAT.6
sfr SPCTL = 0xce; //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 = 0xcf; //SPI data register
sbit SPISS = P1^3; //SPI slave select, connect to other MCU's SS(P1.4) pin
sfr IE2 = 0xAF; //interrupt enable rgister 2
#define ESPI 0x02 //IE2.1
void InitUart();
void InitSPI();
void SendUart(BYTE dat); //send data to PC
BYTE RecvUart(); //receive data from PC
bit MSSEL; //1: master 0:slave
/////////////////余宽//////////////脊码///////////////竖野亮/////////////
void main()
{
InitUart(); //initial UART
InitSPI(); //initial SPI
IE2 |= ESPI;
EA = 1;
while (1)
{
if (RI)
{
SPCTL = SPEN | MSTR; //set as master
MSSEL = 1;
ACC = RecvUart();
SPISS = 0; //pull low slave SS
SPDAT = ACC; //trigger SPI send
}
}
}
///////////////////////////////////////////////////////////
void spi_isr() interrupt 9 using 1 //SPI interrupt routine 9 (004BH)
{
SPSTAT = SPIF | WCOL; //clear SPI status
if (MSSEL)
{
SPCTL = SPEN; //reset as slave
MSSEL = 0;
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)
}
}
///////////////////////////////////////////////////////////
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; //115200 bps
TR1 = 1;
}
///////////////////////////////////////////////////////////
void InitSPI()
{
SPDAT = 0; //initial SPI data
SPSTAT = SPIF | WCOL; //clear SPI status
SPCTL = SPEN; //slave mode
}
///////////////////////////////////////////////////////////
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
}
3. 求51单片机模拟SPI通讯的C程序,最好带详解!谢谢
调试已经通过
///////////////////////////spi.h/////////////////////////////
#ifndef
SPI_H
#define
SPI_H
#include
<stc12le5a60s2.h>
#include
<spi.h>
//sfr
P4
=
0xe8;
//STC12LE5A60S2单片机自带SPI控制器连接
//sbit
VCC1
=
P2^0;//
VCC1
NO
USE
//sbit
SON
=
P1^6
;//
MISO
//sbit
SIN
=
P1^5
;//
MOSI
//sbit
SCKN
=
P1^7
;
//
SCK
sbit
CSN
=
P1^4
;//
28J60
--
CS
//sbit
RSTN
=
P3^5
;
//RST,
no
use
//sbit
INTN
=
P3^3
;
//
INT,
no
use
void
init_spi(void);
void
WriteByte(u8_t
temp);
u8_t
ReadByte(void);
#endif
////////////////////////////////////////////////////////////////
///////////////////////////spi.c/////////////////////////////
#include<spi.h>
//STC12LE5A60S2单片机自带SPI控制器连接
void
init_spi(void)
{
//SSIG
=
1;
//忽略SS脚
//SPEN
=
1;
//允许SPI工作
//DORD
=
0;
//先传高位MSB
//MSTR
=
1;
//设置单片机为主机
SPCTL
=
0xD0;
//SPI
Control
Register
SSIG
SPEN
DORD
MSTR
CPOL
CPHA
SPR1
SPR0
0000,0100
SPSTAT
=
0xC0;
//
//IE2
|=
0x02;
//允许SPI中断控制位
}
void
WriteByte(u8_t
temp)
{
SPDAT
=
temp;
while(!(SPSTAT
&
0x80));
SPSTAT
=
0xC0;
}
u8_t
ReadByte(void)
{
idata
u8_t
temp;
//SPSTAT
=
0xC0;
SPDAT
=
0x00;
while(!(SPSTAT
&
0x80));
temp
=
SPDAT;
SPSTAT
=
0xC0;
return
temp;
}
////////////////////////////////////////////////////////////////
4. 怎么实现单片机和PC机进行SPI通讯
实现单片机和PC机进行SPI通讯方法:x0dx0a1:电路设计x0dx0a设计的电路,利用两片AT89C52芯片,一片做为发送模块,一片做为接收模块。分别编写发送和接收程序,实现数据的发送和接受。通过LED显示接收到的数据。通过示波器观察输出的波形。x0dx0a2:编写程序x0dx0a根据设计好的电路及题目要求分别编写数据发送程序和数据接收程序。①:数据发送程序###defineulongunsignedlongx0dx0a//---------------------------#include
5. 写一个51单片机模拟SPI总线的代码
这是我复制的,一搜一大堆
#include <reg52.h>
sbit SPI_DI =P0^1; //MMC数据输入
sbit SPI_DO =P0^0;//MMC数据输出,可不接
sbit SPI_SCL=P0^2;//时钟线
void Write_Byte(unsigned char value)
{
unsigned char i;
for (i=0;i<8;i++)
{
if (((value>>(7-i))&0x01)==0x01)
SPI_DI=1;
else SPI_DI=0;
SPI_SCL=0;
delay(5);
//必须要加延时,否则会因为操作太快而不响应。
SPI_SCL=1;
delay(5);
//必须要加延时,否则会因为操作太快而不响应。
}
}
unsigned char Read_Byte()
{
unsigned char temp=0;
unsigned char i;
for (i=0;i<8;i++)
{
SPI_DO=1;
SPI_SCL=0;
delay(5);
temp=(temp<<1)+(unsigned char)SPI_DO;
SPI_SCL=1;
delay(5);
}
return (temp);
}