问有用51单片机与MAX31865模拟SPI通信的吗,通过串口助手在电脑上显示始终为0x00,希望有人能给指导一下,帮我看看是不是程序的问题
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDI=P2^0;
sbit SCLK=P2^1;
sbit CS=P2^2;
sbit DRDY=P2^3;
sbit SDO=P2^4;
uchar i=0;
float temps;
void Delay(uint nCount)
{
for(; nCount != 0; nCount--);
}
uchar MAX31865_Read(uchar addr) //SPI Single-Byte Read
{
uchar read = 0;
CS=0;
//SCLK=1;
Delay(0x1f);
for(i = 0; i < 8; i++)
{
SCLK=1;
if (addr & 0x80){SDI=1;}
else {SDI=0;}
Delay(0x1f);
SCLK=0;
addr <<= 1;
Delay(0x1f);
}
Delay(0x1f);
for(i = 0; i < 8; i++)
{
SCLK=1;
read = read<<1;
Delay(0x1f);
if(SDO){read++;}
SCLK=0;
Delay(0x1f);
}
CS=1;
return read;
}
void MAX31865_Write(uchar addr,uchar wdata)//SPI Single-Byte Write
{
CS=0;
Delay(0x1f);
for(i = 0; i< 8; i++)
{
SCLK=1;
if (addr & 0x80){SDI=1;}
else {SDI=0;}
Delay(0x1f);
SCLK=0;
addr <<= 1;
Delay(0x1f);
}
for(i = 0; i < 8; i++)
{
SCLK=1;
if (wdata & 0x80){SDI=1;}
else {SDI=0;}
Delay(0x1f);
SCLK=0;
wdata <<= 1;
Delay(0x1f);
}
CS=1;
}
void MAX31865_Init(void)
{
MAX31865_Write(0x80,0xd1);
Delay(0x1f);
CS=1;
SCLK=1;
}
void UART_send_byte(uchar dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
uchar Get_tempture(void)
{
uint dtemp[2];
uint data_temp;
dtemp[0]=MAX31865_Read(0x01);
dtemp[1]=MAX31865_Read(0x02);
data_temp=(dtemp[0]<<7)+(dtemp[1]>>1); //Get 15Bit DATA
//temps=(data_temp*402)/32768.0; //Here is the rtd R value;
//temps=(temps-100)/0.385055; //temperature
temps=data_temp/32.0-256;
return temps;
}
void configUART()
{
TMOD&=0X0F;
TMOD|=0x20;
SCON=0X50;
TH1=0Xfd;
TL1=TH1;
ET1=0;
TR1=1;
}
void main()
{
configUART();
MAX31865_Init();
Delay(0xff);
while(1)
{
Delay(0xff);
UART_send_byte(Get_tempture());
}
}
|