关于单片机AD会的不会的都看看吧,活生生的例子啊
时间:10-02
整理:3721RD
点击:
本人用的是c8051f310单片机,51内核的,目前在做一个检测P1.1口电压(220mV左右),并通过AD转换发送到串口显示出来,(用查询的方式,每1000次将采集的ADC值求均值发送出去)非常简单的一个例子,但是结果出问题了,printf显示的一直是0,会AD和不会AD的都来看看吧,活生生的例子啊,以下是程序,不多:
#include "c8051f310.h"
#include <stdio.h>
sfr16 ADC0 = 0xbd; // ADC0 result
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 115200
void SYSCLK_Init(void);
void PORT_Init(void);
void ADC0_Init(void);
void UART0_Init(void);
void main (void)
{
PCA0MD &= ~0x40;
SYSCLK_Init ();
PORT_Init ();
UART0_Init();
ADC0_Init();
while (1)
{
static unsigned int samples_counter =1000;
static unsigned long samples_num=0;
AD0INT=0;
AD0BUSY=1;
while (AD0INT==0);
samples_num +=ADC0*2480/1024;
samples_counter--;
if (samples_counter==0)
{
samples_counter =1000;
printf("The ADC0 sampled is :""%d\n",samples_num/1000);
samples_num=0;
}
}
}
void SYSCLK_Init(void)
{
OSCICN=0x83;
RSTSRC=0x04;
}
void PORT_Init(void)
{
XBR0=0x01;
XBR1=0x40;
P0SKIP=0x01;
P1SKIP=0x02;
P0MDIN &=~0x01;
P1MDIN &=~0x02;
}
void ADC0_Init(void)
{
ADC0CN=0x00;//禁止ADC0,正常跟踪方式,清零AD0INT,清零AD0BUSY,启动模式为向AD0BUSY写1
REF0CN=0x02;//使用外部参考电压
AMX0P=0x01;
AMX0N=0x1F;
ADC0CF=((SYSCLK/3000000)-1)<<3;//右对齐方式
ADC0CF |=0x00;
EIE1 &=~0x08;
AD0EN=1;
}
void UART0_Init(void)
{
SCON0=0x10;
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else if (SYSCLK/BAUDRATE/2/256 < 48) {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
} else {
while (1); // Error. Unsupported baud rate
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
}
#include "c8051f310.h"
#include <stdio.h>
sfr16 ADC0 = 0xbd; // ADC0 result
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 115200
void SYSCLK_Init(void);
void PORT_Init(void);
void ADC0_Init(void);
void UART0_Init(void);
void main (void)
{
PCA0MD &= ~0x40;
SYSCLK_Init ();
PORT_Init ();
UART0_Init();
ADC0_Init();
while (1)
{
static unsigned int samples_counter =1000;
static unsigned long samples_num=0;
AD0INT=0;
AD0BUSY=1;
while (AD0INT==0);
samples_num +=ADC0*2480/1024;
samples_counter--;
if (samples_counter==0)
{
samples_counter =1000;
printf("The ADC0 sampled is :""%d\n",samples_num/1000);
samples_num=0;
}
}
}
void SYSCLK_Init(void)
{
OSCICN=0x83;
RSTSRC=0x04;
}
void PORT_Init(void)
{
XBR0=0x01;
XBR1=0x40;
P0SKIP=0x01;
P1SKIP=0x02;
P0MDIN &=~0x01;
P1MDIN &=~0x02;
}
void ADC0_Init(void)
{
ADC0CN=0x00;//禁止ADC0,正常跟踪方式,清零AD0INT,清零AD0BUSY,启动模式为向AD0BUSY写1
REF0CN=0x02;//使用外部参考电压
AMX0P=0x01;
AMX0N=0x1F;
ADC0CF=((SYSCLK/3000000)-1)<<3;//右对齐方式
ADC0CF |=0x00;
EIE1 &=~0x08;
AD0EN=1;
}
void UART0_Init(void)
{
SCON0=0x10;
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else if (SYSCLK/BAUDRATE/2/256 < 48) {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
} else {
while (1); // Error. Unsupported baud rate
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
}
samples_num +=ADC0*2480/1024;
啥意思?基准电压2.48V?
你先输出ADC0看看ad转换成功了没?准不准