用的C8051F410单片机,只用AD采入几个数值计算一下然后DA输出。之前用的好好的,然后突然出错了。。。。。太菜了,照着示例程序编写的,找不出毛病啊,目前就是只让他输出一个AD的数值,结果一直在波动,用两个电位器和示波器检查发现第二个AD端口的电压信号影响了第一个采值,大家能帮我看看哪里出错了吗,拜谢。
#include <c8051f410.h> // SFR declarations
#include <stdio.h>
int x[12];
int y ;
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 ADC0 = 0xbd; // ADC0 result
sfr16 IDA0 = 0x96; // IDA0 high and low bytes
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
sbit PWM_OUT = P1^7;
void SYSCLK_Init (void);
void PORT_Init (void);
void ADC0_Init(int channel);
void DAC0_Init(void) ;
void DelayUs(unsigned int );
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 关闭看门狗,上电默认打开
PORT_Init (); // Initialize crossbar and GPIO
DAC0_Init();
EA=1;
while(1)
{
PWM_OUT=1;
DelayUs(56);
ADC0_Init(0x11);
x[0] = ADC0;
ADC0_Init(0x10); //set p2.1 to adc channel
x[1] = ADC0;
ADC0_Init(0x11);
x[2] = ADC0;
ADC0_Init(0x10); //set p2.1 to adc channel
x[3] = ADC0;
ADC0_Init(0x11);
x[4] = ADC0;
ADC0_Init(0x10); //set p2.1 to adc channel
x[5] = ADC0;
DelayUs(56);
PWM_OUT=0;
DelayUs(56);
ADC0_Init(0x11);
x[6] = ADC0;
ADC0_Init(0x10); //set p2.1 to adc channel
x[7] = ADC0;
ADC0_Init(0x11);
x[8] = ADC0;
ADC0_Init(0x10); //set p2.1 to adc channel
x[9] = ADC0;
ADC0_Init(0x11);
x[10] = ADC0;
ADC0_Init(0x10); //set p2.1 to adc channel
x[11] = ADC0;
DelayUs(500);
IDA0=x[0]-x[1];
}
}
void SYSCLK_Init (void)
{
OSCICN = 0x87; // configure internal oscillator for
// 24.5MHz
RSTSRC = 0x04; // enable missing clock detector
}
void PORT_Init (void)
{
P0MDIN = 0xFE; // Configure P0.0 to analog
P0SKIP = 0x01; // Skip P0.0 on the crossbar
XBR1 = 0x40; // Enable Crossbar
P2MDIN = 0xFC; // set P2.0 P2.1as an analog input
P2SKIP = 0x03; // skip P2.0 P2.1pin
P1MDOUT = 0x8C; // Set LED to push-pull
}
void ADC0_Init (int channel)
{
ADC0TK = 0x04;
ADC0CF = 0x30;
REF0CN = 0x1b;
ADC0MX = channel;
ADC0CN = 0x00;
BURSTEN = 1;
AD0EN = 1;
AD0INT = 1;
AD0BUSY = 1;
while(AD0INT == 0);
}
void DAC0_Init(void)
{
REF0CN = 0x1b;
IDA0CN = 0xF7; //
}
void delayUs(int x)
{
while(--x);
}
|