打印

max1067单片机程序问题

[复制链接]
1673|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
max1067 是14位AD,带SPI接口,由于51单片机没有SPI接口,所以用程序模拟SPI,程序如下,但是转换结果总是为0,有知道原因的吗
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^2;                          // 将p1.2口模拟时钟输出
sbit MOSI=P1^1;                         // 将p1.1口模拟主机输出
sbit MISO=P1^3;                        // 将p1.3口模拟主机输入
sbit SS1=P1^0;                          // 将p1.0口模拟片选
sbit  EOC=P1^4;                        //AD转换结束标志位
#define delayNOP(); {_nop_();_nop_();};   

//--------------------------------------------------------------------------------------------------
// 函数名称: SPIsend_receiveByte
// 入口参数: ch
// 返回接收的数据
// 函数功能:串行输入/输出子程序
//--------------------------------------------------------------------------------------------------
unsigned char SPIsend_receiveByte(unsigned char ch)
{
unsigned char idata n=8;    // 从MISO线上读取一上数据字节,共八位
unsigned int tdata;
        SCK = 1;                   //时钟为高
        SS1 = 0;                   //选择从机
while(n--)
{      
             delayNOP();
             SCK = 0;                   //时钟为低
            //delayNOP();
              if((ch&0x80) == 0x80)    // 若要发送的数据最高位为1则发送位1
             {      
                  MOSI = 1;    // 传送位1
              }
         else
              {  
            MOSI = 0;    // 否则传送位0
              }
          ch = ch<<1;        // 数据左移一位
        SCK=1;

}
SCK=0;
while(EOC) ;
n=16;
   while(n--){
   delayNOP();
             SCK = 0;                   //时钟为低
            //delayNOP();
tdata = tdata<<1;    // 左移一位,或_crol_(temp,1)
if(MISO == 1)
   tdata = tdata|0x0001;    // 若接收到的位为1,则数据的最后一位置1
else
   tdata = tdata&0xfffe;    // 否则数据的最后一位置0
SCK=1;
}
SS1 = 1;                   //选择从机
return(tdata);
}

相关帖子

沙发
green512|  楼主 | 2011-6-22 09:48 | 只看该作者
等待高手回复

使用特权

评论回复
板凳
green512|  楼主 | 2011-6-22 13:01 | 只看该作者
给max1067指令,通道0,内部时钟

接受的数据

miso.JPG (249.23 KB )

miso.JPG

使用特权

评论回复
地板
green512|  楼主 | 2011-6-22 15:02 | 只看该作者
问题已经解决,unsigned char SPIsend_receiveByte(unsigned char ch)应该为
unsigned int SPIsend_receiveByte(unsigned char ch)

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:新手,多多帮忙

2

主题

20

帖子

1

粉丝