|
单极性的
AIN0正通道
内部参考电压。
初次测试是正确的值,但输入不变,值自动增加。
代码如下。
#include <c8051f350.h> // SFR declarations
#include <stdio.h> // Standard I/O Library
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED=P1^2;
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0DEC = 0x9a;
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 PCA0CP0 = 0xe9; // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP1 = 0xeb; // PCA0 Module 2 Capture/Compare
sfr16 PCA0CP2 = 0xed; // PCA0 Module 2 Capture/Compare
sfr16 PCA0 = 0xf9; // PCA0 counter
#define SYSCLK 12250000 // SYSCLK frequency (Hz)
#define BAUDRATE 9600 // UART0 Baudrate (bps)
#define MDCLK 2457600 // Modulator Clock (Hz)
#define OWR 10 // desired Output Word Rate in Hz
#define VREF 2500 // External VREF (x 10^-2 V)
void SYSCLK_Init (void);
void PORT7_Init (void);
void ADC0_Init (void);
void UART0_Init (void);
long xdata sample_array[128];
long ADC_OutputVal=0;
int cyz=0;
void main (void)
{
unsigned int i=0;
float average=0;
long sum;
PCA0MD &= ~0x40;
SYSCLK_Init();
PORT7_Init();
UART0_Init();
ADC0_Init();
EA=1;
printf("³õʼ»¯Íê³É\r\n");
printf("×î´ó³ÆÖØ 4 Kg\r\n");
EIE1 &= ~0x08;
ADC0MD |= 0x01;
while (!AD0CALC);
ADC0MD &= ~0x07;
AD0INT = 0;
ADC0MD |= 0x83;
while(1)
{
average=0;
sum=0;
ADC_OutputVal=0;
// for(i=0;i<128;i++)
// {
// sample_array[i]=0;
//
// }
// for (i = 0; i < 128; i++)
// {
// while(!AD0INT);
// AD0INT = 0;
// ADC_OutputVal = ADC0H;
// ADC_OutputVal=ADC_OutputVal<<8;
// ADC_OutputVal =ADC_OutputVal+ADC0M;
// sample_array[i] = ADC_OutputVal;
// ADC_OutputVal=0;
//
//
// }
//
for(i=0;i<128;i++)
{
// sum=sum+sample_array[i];
while(!AD0INT);
AD0INT = 0;
}
printf("H%umv\r\n",ADC0H);
printf("MÊÇ%umv\r\n",ADC0M);
// average=sum/128;
// ADC_OutputVal = ADC0H;
// ADC_OutputVal=ADC_OutputVal<<8;
// ADC_OutputVal =ADC_OutputVal+ADC0M;
// printf("%fmv\r\n",(float)ADC_OutputVal);
// average=(float)ADC_OutputVal;
// average=average/0xffff;
// average=average*VREF;
// printf("µ±Ç°µçѹÊÇ%fmv\r\n",average);
// average=average*0.002298;
// printf("µ±Ç°ÖØÁ¿ÊÇ%fKg\r\n",average);
LED=1;
for(i=0;i<4000;i++);
for(i=0;i<4000;i++);
for(i=0;i<4000;i++);
LED=0;
for(i=0;i<4000;i++);
for(i=0;i<4000;i++);
for(i=0;i<4000;i++);
}
}
void SYSCLK_Init (void)
{
unsigned int i;
OSCICN = 0x80; // enable intosc
CLKSEL = 0x00; // select intosc as sysclk source
OSCICN = 0x82;
CLKMUL = 0x00; // Reset Clock Multiplier
for (i = 0; i < 125; i++); // Delay for at least 5us
VDM0CN = 0x80; // enable VDD monitor
RSTSRC = 0x06; // enable missing clock detector
// and VDD monitor reset sources
CLKSEL = 0x00; // select PLL as clock source
}
void PORT7_Init (void)
{
XBR0 |= 0x01; // UART0 Selected
XBR1 |= 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0xD0; // TX, LEDs = Push-pull
P1MDOUT |=0x04;
}
void UART0_Init (void)
{
SCON0 = 0x10;
if (SYSCLK/BAUDRATE/2/256 < 1)
{
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08;
}
else if (SYSCLK/BAUDRATE/2/256 < 4)
{
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B;
CKCON |= 0x01;
}
else if (SYSCLK/BAUDRATE/2/256 < 12)
{
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B;
}
else
{
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B;
CKCON |= 0x02;
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
void ADC0_Init (void)
{
// unsigned ADC0_decimation;
ADC0MD = 0x80;
REF0CN = 0x03; // disable internal vref
ADC0CN = 0x02; // Bipolar output codes, GAIN=1
ADC0CF = 0x00; // interrupts upon SINC3 filter output
ADC0CLK = (SYSCLK/MDCLK)-1; // Ideally, MDCLK = 2.4576 MHz
// Generate MDCLK for modulator.
ADC0DECH = 0x00;
ADC0DECL = 0xC0; // ???? = 100HZ/?
// program decimation rate for desired OWR
// ADC0_decimation = (unsigned long) SYSCLK/ (unsigned long) OWR /
// (unsigned long) (ADC0CLK+1)/(unsigned long)128;
//
// ADC0_decimation--;
//
// ADC0DEC = ADC0_decimation;
ADC0BUF = 0x00;
ADC0MUX = 0x08;
ADC0STA = 0x00;
ADC0MD |= 0x01; // ?????(?????)
while (!AD0CALC); // ??????
ADC0MD &= ~0x07; // ADC0??????
}
串口收到的数据。
³õʼ»¯Íê³É
×î´ó³ÆÖØ 4 Kg
H6656mv
MÊÇ10496mv
H6656mv
MÊÇ6400mv
H6656mv
MÊÇ12032mv
H6656mv
MÊÇ12544mv
H6656mv
MÊÇ10752mv
H6656mv
MÊÇ9984mv
H6656mv
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|