#include <C8051F410.h>
#include <intrins.h>
sbit SPI_Clk = P0^2;
sbit SPI_Out = P0^3;
sbit SPI_In = P0^6;
sbit SPI_Cs = P0^7;
void System_Setup(void);
void SPI_Write(unsigned int SPI_Data);
unsigned int SPI_Read(void);
void main(void)
{
unsigned int j = 0;
unsigned char k = 0;
System_Setup();
SPI_Clk = 0;
SPI_Cs=1;
SPI_Cs=0;
SPI_Write(0x5853);
SPI_Cs=1;
SPI_Cs=0;
SPI_Read();
SPI_Cs=1;
}
void SPI_Write(unsigned int SPI_Data) //SPI写
{
bit j;
j = ((SPI_Data >> 15) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 14) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 13) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 12) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 11) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 10) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 9) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 8) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 7) & 0x01);
SPI_Out = j;
_nop_();
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 6) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 5) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 4) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 3) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 2) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 1) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = ((SPI_Data >> 0) & 0x01);
SPI_Out = j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
SPI_Out = 0;
}
unsigned int SPI_Read(void) //SPI读
{
unsigned int Read_Data = 0;
bit j;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
Read_Data = Read_Data<<1;
SPI_Clk = 1;
_nop_();
SPI_Clk = 0;
_nop_();
j = SPI_In;
Read_Data = Read_Data | j;
return(Read_Data);
}
void System_Setup(void)
{
PCA0MD &= ~0x40; //第6位置0,看门狗定时器被禁止
PCA0MD = 0x00; //Disable Watchdog timer
OSCICN = 0x87; // 内部振荡器不分频,Set clock to 24.5 MHz
RSTSRC = 0x04; // 使能时钟丢失检测器,Enable missing clock detector
P0MDIN = 0xff; //P0不配置为模拟输入
P0MDOUT = 0x0c; // P0.2、P0.3配置为推挽方式,p0.2 p0.3 as push-pull
P1MDOUT = 0xff; //P1口设置成推挽方式
XBR1 = 0x40; //交叉开关使能
读到的数据不是我想要的数据啊?这是为什么呢?大家帮忙分析下。刚接触单片机啊。谢谢。
此帖出自
小平头技术问答
一周热门 更多>