哪位用过ADS8344的,能否帮我看下程序,数字量输出老是跳,不稳定。比如2V输入,满量程5V,结果波动会在26200~26000.
或者谁有代码可以给我参考下,万分感谢!!
#include "C8051F040.h"
#include "ABSACC.H"
#include <intrins.h>
sbit ADC_CLK =P0^2;//19
sbit ADC_DOUT =P0^3;//15
sbit ADC_DIN =P0^4;//17
sbit ADC_CS0 =P0^5; //18
sbit ADC_CS1 =P0^6;//18
sbit ADC_BUSY0 =P0^7; //16
sbit ADC_BUSY1 =P1^0;//16
unsigned char data AttData[3];
void Timer_Init()
{
SFRPAGE = TIMER01_PAGE;
TMOD = 0x22;
TH1 = 0x4C;
CKCON = 0x12;
TCON = 0x44;
TH0 = 0x9C;
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x04;
}
void UART_Init()
{
SFRPAGE = UART0_PAGE;
SCON0 = 0x50;
SCON0 &= 0xFC;
SSTA0 = 0x10;
}
void EMI_Init()
{
SFRPAGE = EMI0_PAGE;
EMI0CF = 0x3B;
}
void Port_IO_Init()
{ SFRPAGE = CONFIG_PAGE;
P0MDOUT = 0x76;
P0=88;
P1MDOUT = 0xFF;
P4MDOUT = 0xFF;
P5MDOUT = 0xFF;
P6MDOUT = 0xFF;
P7MDOUT = 0xFF;
XBR0 = 0x04;
XBR2 = 0x40;
}
void Oscillator_Init()
{
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x80;
CLKSEL = 0x00;
}
void Interrupts_Init()
{
IE = 0xB2;
IP = 0xD2;
EIE2 = 0x20;
}
void WatchDog_Init()
{
WDTCN=0x47;
WDTCN=0xDE;
WDTCN=0xAD;
}
void Init_Device(void)
{
WatchDog_Init();
Timer_Init();
UART_Init();
EMI_Init();
Port_IO_Init();
Oscillator_Init();
Interrupts_Init();
}
void ADC_Delay(void)
{
int k=0;
//for(k=0;k<5;k++);
for(k=0;k<50;k++); //change by wb
}
//AD转换函数 chan:0-15
unsigned int ADC ( unsigned char chan)
{
unsigned int ADdata=0;
unsigned char idata outcmd;
unsigned char code ChannelTable[8]={0,4,1,5,2,6,3,7};//转换表格
int i;
ADC_DIN=0 ;
//通道有效性检查
if(chan<0)
chan=0;
if(chan>15)
chan=15;
//使能芯片
ADC_CLK=0;
ADC_CS0=0;
ADC_Delay();
// 写命令
outcmd=(ChannelTable[chan]<<4) | 0x86; //内时钟模式。
for(i=0;i<8;i++)
{
ADC_CLK=0;
ADC_Delay();
if(outcmd&0x80)
ADC_DIN=1;
else ADC_DIN=0;
outcmd=outcmd<<1;
ADC_CLK=1;
ADC_Delay();
}
ADC_CLK=0;
ADC_CS0=1;
//等待转换结束
if(chan<8)
while(!ADC_BUSY0);
else
while(!ADC_BUSY1);
//ADC_CLK=1;//by wb
ADC_CS0=0;
//ADC_Delay();
for(i=0;i<16;i++)
{ ADdata= ADdata<<1;
ADC_CLK=1;//by wb
ADC_Delay();//by wb
ADC_CLK=0;//by wb
ADC_Delay();//by wb
if(ADC_DOUT)
ADdata|=0x0001;
//ADC_Delay();
}
for(i=0;i<8;i++)
{
ADC_CLK=1;//by wb
ADC_Delay();//by wb
ADC_CLK=0;//by wb
ADC_Delay();//by wb
}
ADC_CS0=1;
return ADdata ;
}
void main()
{
unsigned int j=0;
int k;
Init_Device();
XBYTE[0xFF80]=0x00 ; //55;
j=ADC(0);
k=0;
} |