本帖最后由 YZW977 于 2017-12-30 16:35 编辑
#include "stc15fxxxx.h"
#include "stdio.h"
#include "intrins.h"
#define FOSC 12000000
#define ADC_POWER 0x80 //ADC µçÔ´¿ØÖÆλ
#define ADC_FLAG 0x10 // ADC Íê³É±êÖ¾
#define ADC_START 0x08 //ADCÆðʼ¿ØÖÆλ
#define ADC_SPEEDLL 0x00 //540¸öʱÖÓ
#define ADC_SPEEDL 0x20 //360¸öʱÖÓ
#define ADC_SPEEDH 0x40 //180¸öʱÖÓ
#define ADC_SPEEDHH 0x60 //90¸öʱÖÓ
u8 Result;
/*----------------------------
ÑÓʱº¯Êý
----------------------------*/
void Delay1000ms() [url=]//@12.000MHz[/url]
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 46;
j = 153;
k = 245;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
/*----------------------------
´®¿Ú³õʼ»¯
----------------------------*/
void Usart_Init(u16 Baud)
{
SCON |= 0x50;
PCON &= 0x7f;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD |= 0x21;
TH1 = 256-FOSC/Baud/32;//²¨ÌØÂʲ»¼Ó±¶¡£(256-FOSC/16/12/BAUD) ¼Ó±¶
TL1 = TH1;
TR1 = 1;
ET1 = 0;
ES = 1;
EA = 1;
}
/*----------------------------
ADC³õʼ»¯¼°²É¼¯×ª»»10λ
----------------------------*/
u16 ADCResult(u8 ch)
{
u16 Result;
P1ASF = 0x80; //ÉèÖÃP17ΪADÈë¿Ú
ADC_RES = 0; //Çå³þ½á¹û¼Ä´æÆ÷
ADC_CONTR |= ADC_POWER | ADC_SPEEDLL | ADC_START | ch;//0x00|0x00|ch|0x08:Ñ¡ÔñADÊäÈëͨµÀ£¬¿ªÊ¼ADת»»
_nop_();
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG)); //µÈ´ýADCת»»Íê³É
ADC_CONTR &= ~ADC_FLAG; //¹Ø±ÕADC £¬½«±ê־λÇåÁ㣬µÈ´ýÏÂÒ»´ÎÓ²¼þÖÃ1
Result = (u16)((ADC_RES<<2)+ADC_RESL);
return (Result); //·µ»Ø½á¹û
}
void main()
{
u16 value;
double dat;
Usart_Init(9600);
while (1)
{
Delay1000ms();
value = ADCResult(0x07); //ÉèÖÃP17ΪÊäÈë¿Ú
TI = 1;
dat = (double)value*0.0048828125;
printf("电压值:
");
printf("value = %.3f V
",dat);
while(!TI);
TI= 0;
Delay1000ms();
}
}
P17时AD输入口,在调试时单片机引脚上没接任何东西,串口上传是下面的情况
电压值
value =0.029v
此帖出自
小平头技术问答
一周热门 更多>