哪位大虾用过8051f340和AD7732,我想用单片机控制AD,没弄明白啊,想用SPI
不知道8051f340是否得SPI,如有直接接上按AD7732的datasheet操作就可以了;没有就软件模拟,好好看看AD7732的时序就可以了。
不断地学习,才会有创新!
淘宝小店:手机、qq点卡、游戏点卡自动充值 http://shop63727265.taobao.com/
回复 沙发 lixiaohai8211 的帖子
8051F340有SPI,而且接法是标准3线式的,数据能发出去,从示波器上都能看到,但AD就是不动作,网上给的AD7734的例子应该也不对。AD7732的时序模拟的我感觉没啥问题,可能还是有什么地方没看明白吧!我一会把自己写的代码发下,请大家帮我看下,谢谢
#include
#include // SFR declarations
//-----------------------------------------------------------------------------
unsigned char t;
unsigned char k;
unsigned char rr[];
unsigned char aa[];
sbit RDY=P0^3;
sbit ADCS=P0^7;
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
OSCICN = 0x80; // Configure internal oscillator for
}
//-----------------------------------------------------------------------------
void delay(int n)
{
char j;
for(j=0;j
{;
}
}
void PORT_Init (void)
{
XBR0 = 0x02; // sclk digital peripherals selected
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0xFF; // Enable p0 as push-pull output
P1MDOUT |= 0xFF; // Enable p1 as push-pull output
P2MDOUT |= 0xFF; // Enable p2 as push-pull output
P3MDOUT |= 0xFF; // Enable p3 as push-pull output
P4MDOUT |= 0xFF;
}
//-----------------------------------------------------------------------------
void SPI0_Init (void)
{
SPI0CKR=0x07;
NSSMD0=0;
SPIEN=1;
SPI0CFG|=0x70;
}
//-----------------------------------------------------------------------------
void write(unsigned char bb )
{
ADCS=0;
SPI0DAT=bb;
// for(;SPI0CFG&0x80;)
// {
// }
ADCS = 1;
delay(5);
}
//-----------------------------------------------------------------------------
char read(void)
{
char temp;
ADCS=0;
temp=SPI0DAT;
// for(;SPI0CFG&0x01;)
// {
// }
ADCS = 1;
return temp;
}
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
OSCILLATOR_Init(); // Initialize system clock
PORT_Init(); // Initialize crossbar and GPIO
SPI0_Init();
EA=1;
ADCS = 1;
write(0x38);
if(WCOL)//写冲突处理,暂无处理
WCOL=0;
write(0x48);
if(WCOL)//写冲突处理,暂无处理
WCOL=0;
write(0x48);
/**/
for(;RDY==0;)
{
P1=read();
P2=read();
P3=read();
// }
}
}
:( 本来也有个自己仿时序的想法,仿完了还可以,不过AD7732有个时间限制很愁人SCLK上升沿时间和总线释放时间最大80ns,最小10ns。大家用AD的时候片选信号不用是不是也可以?CS直接接地。但是控制时序的时间要求中又有对CS下降沿到数据有效的时间要求,最小0ns,最大60ns,请用过的高手帮我看看,我的QQ号是249863865
/*********************************************************************
Author : ADI - Apps
www.analog.com/MicroConverter
Date : September 2003
File : AD7734.c
Hardware : ADUC832 is used to control ad7734.The core clock of ADUC832 is 2.097152MHZ.
Description :
*********************************************************************/
#include
#include
/******************************************************************
DEFINE CONTROL PINS OF ADUC832 FOR THE PURPOSE OF AD7734 CONTROL.
Customers should define the pins according to their design.
If P0 is used as the control port, pull-up resistors should be added to each pin of P0.
******************************************************************/
sbit CS=0x0A4;
sbit DIN=0x0A5;
sbit DOUT=0x0B4;
sbit DRDY=0x0B3;
sbit RESET=0x0A6;
sbit SCLOCK=0x0A7;
int sig;
void int0_int() interrupt 0{
sig=0;
return; }
void writetoreg(unsigned char);
void readfromreg(int);
void read(int);
void main()
{
int tim;
/* Set up UART */
T3CON = 0x082;
T3FD = 0x02D;
SCON = 0x052;
tim=1000;
/* PRECONFIGURE...*/
RESET=0;
while(tim--);
RESET=1;
SCLOCK=1;
DIN=1;
DOUT=1;
CS=1;
DRDY=1;
printf("\n");
writetoreg(0x28); //write to communication register. The next step is writing to channel setup register
writetoreg(0x08); //enable the channel AIN0, the input voltage range is +5V
writetoreg(0x30); //write to communication register. The next step is writing to channel conversion time register
writetoreg(0x91); //chop enabled, and the update rate is 2534Hz
/*writetoreg(0x38);//write to communication register . The next step is writing to MODE register
writetoreg(0x82);//ADC zero-scale calibration, 24 bit mode
while(DRDY);
printf("self zero-scale calibration finished.\n");
IT0=1;
EA=1;
EX0=1;
sig=1;
while(sig);*/
/*writetoreg(0x38);//write to communication register . The next step is writing to MODE register
writetoreg(0x0C2);//channel zero-scale calibration,24 bit mode
while(DRDY);
printf("channel zeroscale calibration finished.\n");
sig=1;
while(sig);
writetoreg(0x38);//write to communication register. The next step is writing to MODE register
writetoreg(0x0D2);//system full-scale calibration
while(DRDY);
printf("channel full-scale calibration finished.\n");
sig=1;
while(sig); */
writetoreg(0x44); //read from ADC status
readfromreg(8);
printf("\n");
writetoreg(0x60); //read from channel status
readfromreg(8);
printf("\n");
writetoreg(0x68); //read from channel setup
readfromreg(8);
printf("\n");
writetoreg(0x70); //read from channel conversion
readfromreg(8);
printf("\n");
writetoreg(0x78);//read from mode register
readfromreg(8);
printf("\n");
writetoreg(0x46);//read from adc zero-scale
readfromreg(24);
printf("\n");
writetoreg(0x50);//read from channel zero-scale
readfromreg(24);
printf("\n");
writetoreg(0x58);//read from channel full-scale
readfromreg(24);
printf("\n");
writetoreg(0x38);//writing to communication register, the next step is write to MODE register
writetoreg(0x22);//continuous conversion mode
read(200);
}
void writetoreg(byteword)
unsigned char byteword;
{
unsigned char temp;
int i;
CS=0;
temp=0x80;
for(i=0;i<8;i++)
{
if((temp&byteword)==0)
DIN=0;
else DIN=1;
SCLOCK=0;
SCLOCK=1;
temp=temp>>1;
}
CS=1;
}
void readfromreg(bytenumber)
int bytenumber;
{
int j;
unsigned char temp1;
DIN=0;
CS=0;
temp1=0x00;
for(j=0;j
{
SCLOCK=0;
if(DOUT==0)
temp1=temp1<<1;
else
{temp1=temp1<<1;
temp1=temp1+0x01;}
if(j==7||j==15||j==23)
{ printf("%02BX",temp1);
temp1=0x00;
}
SCLOCK=1;
}
CS=1;
}
void read(readtime)
int readtime;
{
unsigned char temp1;
int i,j;
temp1=0x00;
for(i=0;i
{ while(DRDY);
writetoreg(0x48);
DIN=0;
CS=0;
for(j=0;j<24;j++)
{SCLOCK=0;
if(DOUT==0)
temp1=temp1<<1;
else
{temp1=temp1<<1;
temp1=temp1+0x01;}
SCLOCK=1;
if(j==7||j==15||j==23)
{ printf("%02BX",temp1);
temp1=0x00;
}
}
printf("\n");
CS=1;
}
printf("\n\n\n");
}
上面的这个是AD7734的程序,网上下的,但是,按照他的改完了也不好使
LZ我也很头疼这样的问题,网上的例程,没有几个完全正确的,继续关注,希望LZ成功
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
OSCILLATOR_Init(); // Initialize system clock
PORT_Init(); // Initialize crossbar and GPIO
SPI0_Init(); //SPI初始化
EA=1;
ADCS = 0;
SPI0DAT =0X3a;
while(SPIF ==0);
SPIF =0;
delay(5);
SPI0DAT =0X4a;
while(SPIF ==0);
SPIF =0;
delay(5);
SPI0DAT =0X4a;
while(SPIF ==0);
SPIF =0;
/**/ delay(5);
SPI0DAT =0XFF;
while(SPIF ==0);
SPIF =0;
delay(5);
P1 = SPI0DAT;
SPI0DAT =0XFF;
while(SPIF ==0);
SPIF =0;
P2= SPI0DAT;
SPI0DAT =0XFF;
while(SPIF ==0);
SPIF =0;
P3 = SPI0DAT;
SPI0DAT =0XFF;
// }
ADCS = 1;
}
上面的是昨天一个高人指点我的,说需要将数从移位寄存器中推出来,SPI0DAT =0XFF;不是很明白,不过用上去竟然可以,但是采到的数还是不对,唉!郁闷啊
如果能看到总线的时序逻辑 你就会发现程序上存在的问题了
回复 10楼 249863865 的帖子
那个是用来同步时钟的!
回复 13楼 tagetage 的帖子
如果有硬件就不要用软件模拟,只要时序正确是没有问题的!
SPI0DAT =0XFF; 是为了同步时钟使用!
回复 11楼 hanker510 的帖子
输入时序我用示波器看了啊,没有问题的啊,不过片选信号CS芯片资料上面说可以接地,但是在单个时序图上面又出现了新的问题,就是CS的下降沿与数据有效之间是有时间限制的,既然CS都接地了,那时间限制又怎么可能满足呢?
回复 14楼 daicheng 的帖子
版主你好,如果您用过类似的AD芯片可否帮我看看我的程序是哪出了问题?AD7732的工作方式是不是只要设置好了模式寄存器然后就是等着AD采样完成了?我看PDF上面讲的也不清晰,不知道是不是应该看看这方面的书啊,若有讲解比较详细的书,烦劳推荐下,小虾米感激不尽
回复 14楼 daicheng 的帖子
版主可否留个QQ我好当面讨教
回复 14楼 daicheng 的帖子
或者加我的QQ也行,249863865
void write(unsigned char bb )
{
// ADCS=0;
SPI0DAT=bb;
while(SPIF ==0);
SPIF =0;
// delay(5);
// ADCS=1;
}
//-----------------------------------------------------------------------------
char read(void)
{
char temp;
SPI0DAT =0XFF;
while(SPIF ==0);
SPIF =0;
temp=SPI0DAT;
return temp;
}