本帖最后由 szlbz 于 2012-4-27 10:25 编辑
C8051F320用SPI方式和普通模拟方式与AD7714连接,都不能正常读数。
#define _MAIN_C_
#include "mytypedef.h"
#include "usb.H"
#include "F32x_USB_Register.h"
#include "C8051FMacros.H"
#define SYSCLK 24000000 // Internal oscillator frequency in Hz
#define F_SCK_MAX 2000000 // Max SCK freq (Hz)
#define uchar unsigned char
uchar data ad_data[3];
unsigned long addata;
INT8U USB_InBuff[EP1_PACKET_SIZE];
INT8U USB_OutBuff[EP2_PACKET_SIZE];
sbit AD_CS=P1^4;
sbit AD_CLK=P1^0;
sbit AD_DOUT=P1^1;
sbit AD_DIN=P1^2;
sbit AD_DRDY=P1^3;
void Clock_Init( void );
void GPIO_Init( void );
void WriteToReg_ADC(unsigned char byteword);
void delay_1ms(uchar time);
unsigned long ADC_data24bit(void);
void delay_1ms(uchar time)
{//延迟1ms
uchar i;
// uchar j;
for (i=0;i<time;i++){
// for(j=0;j<300;j++);
}
}
void InitADC(void)
{
WriteToReg_ADC(0x20);//选择下一个写滤波高寄存器
WriteToReg_ADC(0xc1);//单极性,24位
WriteToReg_ADC(0x30);//选择下一个写滤波低寄存器
WriteToReg_ADC(0x80);//写滤波低寄存器第一个凹点频率为50hZ
WriteToReg_ADC(0x10);//选择通道下冷饮对模式寄存器进行写操作
WriteToReg_ADC(0x20);//选择正常模式,增益1,自校准
// WriteToReg_ADC(0x28);//选择正常模式,增益4,自校准
delay_1ms(20);
}
void WriteToReg_ADC(unsigned char byteword)
{
unsigned char Token,i;
Token=0x80;
AD_CLK=0;
AD_CS=0;
for(i=0;i<8;i++)
{
AD_CLK=0;
if(byteword & Token) AD_DIN=1;
else AD_DIN=0;
AD_CLK= 1;
Token=Token>>1;
}
AD_CLK=0;
AD_DIN=1;
AD_CS=1;
}
unsigned long ADC_data24bit(void)
{
unsigned long temp;
unsigned char i;
while(AD_DRDY==1)
{
// delay_1ms(1);
}
WriteToReg_ADC(0x58);
AD_CLK=0;
AD_CS=0;
temp=0;
for(i=0;i<24;i++)
{
temp=temp<< 1;
AD_CLK=1;
temp=(temp|AD_DOUT);
AD_CLK=0;
}
AD_CLK=0;
AD_CS=1;
// addata=temp;
return(temp);
}
void GPIO_Init( void )
{
P1MDOUT = 0x02;
P0SKIP = 0xFF;
P1SKIP = 0xFF;
P2SKIP = 0x0F;
XBR1 = 0x40;
}
void Clock_Init( void )
{
INT8U i;
if( ( CLKMUL & ( 1<<5 ) ) ) return;
CLKMUL = 0;
CLKMUL |= ( 1<<7 );
for( i = 0; i < 250; i ++ );
CLKMUL |= 0xC0;
for( i = 0; i < 250; i ++ );
while( !( CLKMUL & ( 1<<5 ) ) );
CLKSEL = ( 2<<0 );
}
int main( void )
{
uchar i;
PCA0MD &= ~( 1<<6 ); //Disable the WDT
EA = 0;
GPIO_Init( );
Clock_Init( ); //Config the clock
USB_Init( USB_ENABLE ); //Init the USB module
AD_CS=0;
for(i=0;i<34;i++)
{
AD_DIN=1;
}
InitADC();
EA = 1; //Enable the global interrupt
delay_1ms(100);
AD_CLK=0;
while( 1 )
{
addata=ADC_data24bit();
USB_OutBuff[1] =addata>>0;
USB_OutBuff[2] =addata>>8;
USB_OutBuff[3] =addata>>16;
// //SPIEN=0;//关闭SPI
HID_SendData( USB_OutBuff, 32 );//通过USB传送数据
}
return 0;
}
这些代码都是网上找的。
请各位帮忙看看,谢谢各位!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>