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);
}