本人想利用TQ2440和AD5933做一个阻抗信号的采集,不知道为什么,最后出来的数清一色的是30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
30005fb8
,奇怪,发出代码,希望大家帮忙指正错误
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#define Set_SDA() rGPEDAT |= (1<<15)
#define Clr_SDA() rGPEDAT &= ~(1<<15)
#define Set_SCL() rGPEDAT |= (1<<14)
#define Clr_SCL() rGPEDAT &= ~(1<<14)
void init() //初始化
{
Set_SCL();
Delay(1);
Set_SDA();
Delay(1);
}
void start() //启动信号
{
Set_SDA();
Set_SCL();
Delay(1);
Clr_SDA();
Delay(1);
Clr_SCL();
Delay(1);
}
int ack() //应答信号
{
unsigned short temp;
rGPECON = 0x10000000;
Delay(1);
Set_SCL();
Delay(1);
temp = rGPEDAT & (1<<15);
Delay(1);
Clr_SCL();
Delay(1);
rGPECON = 0x50000000;
Delay(1);
if(temp)
return 1;
else
return 0;
}
void stop() //停止信号
{
Set_SCL();
Clr_SDA();
Delay(1);
Set_SCL();
Delay(1);
Set_SDA();
Delay(1);
}
void writebyte(unsigned short data) //写一个字节
{
unsigned short i;
for(i=0;i<8;i++)
{
if(data & (1<<(7-i)))
Set_SDA();
else
Clr_SDA();
Delay(1);
Set_SCL();
Delay(1);
Clr_SCL();
}
Delay(1);
}
int readbyte() //读一个字节
{
unsigned short i;
U8 temp = 0;
rGPECON = 0x10000000;
Delay(1);
for(i=0;i<8;i++)
{
Set_SDA();
if(rGPEDAT & (1<<15))
temp |= (1<<(7-i));
Delay(1);
Set_SCL();
Delay(1);
Clr_SCL();
Delay(1);
}
rGPECON = 0x50000000;
Set_SDA();
Delay(1);
return temp;
}
void _wr(unsigned short slv,unsigned short reg,unsigned short data)
{
start();
writebyte(slv);
if(ack())
{
stop();
return;
}
start();
writebyte(reg);
if(ack())
{
stop();
return;
}
writebyte(data);
if(ack())
{
stop();
return;
}
stop();
}
void _rd(U32 reg,unsigned short data)
{
unsigned short temp;
start();
writebyte(0x1a);
if(ack())
{
stop();
return;
}
writebyte(reg);
if(ack())
{
stop();
return;
}
writebyte(0x1b);
if(ack())
{
stop();
return;
}
Delay(1);
readbyte();
data = readbyte();
stop();
}
void iic()
{
unsigned short temp;
rGPEUP = 0xc000;
rGPECON = 0x50000000;
init();
_wr(0x1a,0x82,0x19); //start frequency
Delay(5);
_wr(0x1a,0x83,0x99); //50kHz
Delay(5);
_wr(0x1a,0x84,0x99);
Delay(5);
_wr(0x1a,0x81,0x08); //选着外部时钟
Delay(5);
_wr(0x1a,0x80,0x11);
Delay(5);
_wr(0x1a,0x8f,0x02);
Delay(5);
while(1)
{
_rd(0x1b,temp);
Uart_Printf("%x\n",temp);
Delay(200);
}
}
|