C51单片机AD转换 50
我用C8051F040单片机做AD转换时,参考电压为2.4V,但是无论我输入电压是多少,采样值要么是0,要么是4095附近的数字,导致始终得不到正确的结果,这是为什么?#...
我用C8051F040单片机做AD转换时,参考电压为2.4V,但是无论我输入电压是多少,采样值要么是0,要么是4095附近的数字,导致始终得不到正确的结果, 这是为什么?
#include <globalvar.h>
void main()
{
EA=0;
Init_Device();
EA=1;
delay(200);
while(1);
}
void Init_Device(void)
{
PORT_Init();
Oscillator_Init();
Reset_Sources_Init();
ADC0ac_init();
ADC0timer_init();
}
#include <extern_var.h>
void PORT_Init ()
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-up
// Enable UART1
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void Reset_Sources_Init() // ½ûÖ¹¿´ÃŹ·
{
WDTCN = 0xDE; // Disable WDT
WDTCN = 0xAD;
}
void Oscillator_Init()
{
temp=SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
CLKSEL=0x00; //ÄÚ²¿¾§Õñ×öʱÖÓ
SFRPAGE=temp;
}
void ADC0ac_init()
{
temp=SFRPAGE;
SFRPAGE = ADC0_PAGE;
AMX0SL = 0x00;
REF0CN = 0x03;
ADC0CN = 0x84;
EIE2|=0X02;
SFRPAGE=temp;
}
void ADC0timer_init()
{
temp=SFRPAGE;
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x00; //stop timer3,clear TF3
RCAP3L = 0x10;
RCAP3H = 0x00;
TMR3CF = 0x08;
TMR3L = 0x10;
TMR3H = 0x00;
EIE2&=~0X01; // disable timer3 interrupt
TR3=1;
SFRPAGE=temp;
}
void ADC0_ISR() interrupt 15
{
temp=SFRPAGE;
SFRPAGE=0x00;
//result[n]=ADC0H;
result[n]=ADC0H*256+ADC0L;
n++;
total++;
AD0INT=0;
SFRPAGE = TMR3_PAGE;
TF3=0; //Çå³ýT3Òç³ö±êÖ¾
SFRPAGE=temp;
delay(50000);
if(total==127)
{
EA=0;
}
} 展开
#include <globalvar.h>
void main()
{
EA=0;
Init_Device();
EA=1;
delay(200);
while(1);
}
void Init_Device(void)
{
PORT_Init();
Oscillator_Init();
Reset_Sources_Init();
ADC0ac_init();
ADC0timer_init();
}
#include <extern_var.h>
void PORT_Init ()
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-up
// Enable UART1
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void Reset_Sources_Init() // ½ûÖ¹¿´ÃŹ·
{
WDTCN = 0xDE; // Disable WDT
WDTCN = 0xAD;
}
void Oscillator_Init()
{
temp=SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
CLKSEL=0x00; //ÄÚ²¿¾§Õñ×öʱÖÓ
SFRPAGE=temp;
}
void ADC0ac_init()
{
temp=SFRPAGE;
SFRPAGE = ADC0_PAGE;
AMX0SL = 0x00;
REF0CN = 0x03;
ADC0CN = 0x84;
EIE2|=0X02;
SFRPAGE=temp;
}
void ADC0timer_init()
{
temp=SFRPAGE;
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x00; //stop timer3,clear TF3
RCAP3L = 0x10;
RCAP3H = 0x00;
TMR3CF = 0x08;
TMR3L = 0x10;
TMR3H = 0x00;
EIE2&=~0X01; // disable timer3 interrupt
TR3=1;
SFRPAGE=temp;
}
void ADC0_ISR() interrupt 15
{
temp=SFRPAGE;
SFRPAGE=0x00;
//result[n]=ADC0H;
result[n]=ADC0H*256+ADC0L;
n++;
total++;
AD0INT=0;
SFRPAGE = TMR3_PAGE;
TF3=0; //Çå³ýT3Òç³ö±êÖ¾
SFRPAGE=temp;
delay(50000);
if(total==127)
{
EA=0;
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询