#include <c8051f020.h> // SFR declarations
#include <stdio.h>
#include <INTRINS.H>
#include <math.h>
xdata unsigned char zhi[10];
void SYSCLK_Init (void);
void PORT_Init (void);
void ADC1_Init(void);
void UART0_Init (void);
void Timer3_Init (int counts);
void ADC1_ISR (void);
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
void delay(unsigned char T)
{
while(T)
{
_nop_();
T--;
}
}
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SAMPLE_RATE 500000 // ADC1 Sample frequency in Hz
#define VREF1 2400 // Mv
#define AV+ 3300 // Mv
sbit LED = P3^3; // LED='1' means ON
sbit SW1 = P3^7;
unsigned int temp;
unsigned int j=0;// SW1='0' means switch pressed
void main(void)
{
unsigned char MUXSLECT=0;
int ii,jk,kj;
int i;
float jj;
float PI=3.141516*2;
WDTCN = 0xDE;
WDTCN = 0xAD;
EMI0CF=0x00;
SYSCLK_Init();
PORT_Init();
UART0_Init();
ADC1_Init();
Timer3_Init ();
EA = 1;
//for(kj=0;kj<3555;kj++)
//for(jk=0;jk<3566;jk++);
while (1)
{
//EA = 0; // disable interrupts
if(j)
{
j=0;
TMR3CN&=0xFB;
SBUF0=temp;
//printf("%fV\n",temp);
while(TI0==0);
TI0=0;
// EA = 1;
}
TMR3CN|=0x04; //开定时器3
}
}
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with, 22.1184MHz crystal
for (i=0; i < 256; i++); // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)); // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
} // source and enable missing clock
// detector
void PORT_Init (void)
{
XBR0 = 0x04; // Enable UART0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output
P3MDOUT |= 0xff; // enable LED as push-pull output
}
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1
TI0 = 0; // Indicate TX0 ready
RI0=0;
}
void ADC1_Init(void)
{
ADC1CF = 0x19; // SAR转换时间<=6MHz,取5.5,放大增益为0.5
AMX1SL = 0x00; // 暂时为AIN1.0,八路中可任选
P1MDIN = 0x00; // P1口为模拟输入
REF0CN = 0x0a; // ADC0电压取于DAC0,ADC1取于AV+,TEMPE关闭,内部偏压器工作,内部电压基准缓冲器关闭
ADC1CN = 0x82; // ADC1允许,一般工作方式,向ADC1BUSY写‘1’开始工作
EIE2 |= 0x08;
}
void Timer3_Init (int counts)
{
TMR3CN = 0x04; // 使用系统时钟12分频作为定时器3 的时钟源(22M)
TMR3RLL = 0x01;
TMR3RLH = 0x70; // 用定时器3 作为 AD0的启动转换标志 每 200us启动一次采集。
TMR3L = 0x01;
TMR3H = 0x70;
}
void ADC1_ISR (void) interrupt 17
{
EA = 0;
// temp=(float)ADC1*2430/4095;
temp=(double)ADC1/256*3300;
// temp=ADC1;
j=1;
}
------解决思路----------------------
如果结果不对,应该是数据类型溢出了
试试
temp=ADC1;
temp*=100;
temp/=256;
temp*=33;