求各路大神,帮看下程序。运行后读出的数据都不正确。用51读取LTC1867采集的电压。
#include<reg52.h>
sbit cs=P2^0;
sbit sck=P2^1;
sbit SDI=P2^2;
sbit SD0=P2^3;
//unsigned int high_byte;
unsigned int vale;
unsigned int read_high_byte();
unsigned char read_low_byte();
void delay(unsigned int x);
void UART_Init();
void UART_Send_Byte(unsigned char mydata);
void main()
{
UART_Init();
cs=0;
cs=1;
delay(1);
cs=0;
//high_byte=(read_high_byte())<<8;
//vale=high_byte+read_low_byte();
UART_Send_Byte(read_high_byte());
UART_Send_Byte(read_low_byte());
while(1);
}
void delay(unsigned int x)
{
unsigned char i;
while(x--){
for(i=0;i<100;i++);
}
}
unsigned int read_high_byte()
{
unsigned char i;
unsigned char date,high_byte;
date=0x88;
sck=0;
for(i=0;i<8;i++)
{
SDI=((date<<i)&0x80);
sck=1;
if(SD0) high_byte|=(0x80>>i);
delay(2);
sck=0;
delay(2);
}
return high_byte;
}
unsigned char read_low_byte()
{
unsigned int i,low_byte=0;
sck=0;
for(i=0;i<8;i++)
{
sck=1;
delay(10);
if(SD0) low_byte|=(0x80>>i);
sck=0;
delay(10);
}
// high_byte=high_byte<<8;
// high_byte+=low_byte;
return low_byte;
}
void UART_Init()
{
TMOD=0x20; //定时器T1使用工作方式2
TH1=0xfd; //设置初值
TH0=0xfd;
TR1=1; //开始计时
SM0=0;
SM1=1; //设置串口为方式1
REN=1; //接受使能
EA=1; //打开总中断开关
ES=1; //打开串口中断开关
}
void UART_Send_Byte(unsigned char mydata)
{
ES=0;
TI=0;
SBUF=mydata;
while(!TI);
TI=0;
ES=1;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>