龙空技术网

基于STC12C5A32AD单片机的电压表设计

晶格好物 314

前言:

当前你们对“stc12c5a60s2的ad转换程序”大概比较注意,你们都想要分析一些“stc12c5a60s2的ad转换程序”的相关知识。那么小编也在网络上搜集了一些关于“stc12c5a60s2的ad转换程序””的相关内容,希望小伙伴们能喜欢,我们一起来学习一下吧!

//晶振11.0592M 定时器0定时,方式1,25ms常数a600,10ms常数dc00,5ms常数ee00,1ms常数fc66,

//2010年4月28日写,作者:徐学军

#include <absacc.h>

#include <intrins.h>

#include <reg52.h>

sfr P1ASF = 0x9D;

sfr ADC_CONTR = 0xBC;

sfr ADC_RES = 0xBD;

sfr ADC_RESL = 0xBE;

sfr AUXR1 = 0xA2;

sfr P1M1 = 0x91 ;

sfr P1M0 = 0x92 ;

sfr smdis = 0x80;

sbit led = P3^7;

sbit weishuang0 = P2^6; //数码管第一个位选

sbit weishuang1 = P2^3; //数码管第二个位选

sbit weishuang2 = P2^4; //数码管第三个位选

sbit weishuang3 = P2^5; //数码管第三个位选

//以下选择 ADC 转换速率,只能选择其中一种

// SPEED1 SPEED0 A/D转换所需时间

//#define AD_SPEED 0x60 // 0110,0000 1 1 70 个时钟周期转换一次,

// CPU工作频率21MHz时 A/D转换速度约 300KHz

//#define AD_SPEED 0x40 //0100,0000 1 0 140 个时钟周期转换一次

//#define AD_SPEED 0x20 //0010,0000 0 1 280 个时钟周期转换一次

#define AD_SPEED 0x00 //0000,0000 0 0 420 个时钟周期转换一次

unsigned char dp=0x10;

#define uchar unsigned char

#define uint unsigned int

unsigned char code md[]={0xaf,0xa0,0xcd,0xe9,0xe2,0x6b,0x6f,0xa1,0xef,0xe3,

0xbf,0xb0,0xdd,0xf9,0xf2,0x7b,0x7f,0xb1,0xff,0xf3,0x40,0x00};

//0,1,2,3,4,5,6,7,8,9,1.,2.,3.,4.,5.,6.,7.,8.,9., -,灭 ,

unsigned char shu0,shu1,shu2,shu3,tc=0;

unsigned char vccreal; //设定实际的电压值

bit FLAG1=0;//是否检测到DS18B20标志位

/*

//-----变量定义-----

void delayt(unsigned char t) //延时约11us

{

unsigned char i;

unsigned int j;

for(i=t;i>0;i--)

{

for(j=0;j<5;j++);

}

}

*/

void int_t0() interrupt 1

{

TR0 = 0;

tc++;

if(tc>=4) {tc=0;}

TH0 = 0xee;

TL0 = 0x00;

if(tc==0) { smdis=md[shu0]; weishuang0 = 0; weishuang1 = 1; weishuang2 = 1; weishuang3 = 1;}

else if(tc==1) { smdis=md[shu1]; weishuang0 = 1; weishuang1 = 0; weishuang2 = 1; weishuang3 = 1; }

else if(tc==2) { smdis=md[shu2]; weishuang0 = 1; weishuang1 = 1; weishuang2 = 0; weishuang3 = 1; }

else if(tc==3) { smdis=md[shu3]; weishuang0 = 1; weishuang1 = 1; weishuang2 = 1; weishuang3 = 0;}

TR0 = 1;

}

void init_mcu()

{

TMOD=0x01; //定时器0方式1

TH0 = 0xee;//0xa6; //25ms定时常数

TL0 = 0x00;

EA = 1;

ET0 = 1;

TR0 = 1;

tc = 0;

//

shu0=shu1=shu2=shu3=0;

}

void delayt(unsigned char t) // 延时函数

{

unsigned int n;

while(t--)

{

n = 6000;

while(--n);

}

}

//---------------------------------------------------------------------

unsigned int get_AD_result(unsigned char channel)

{

unsigned int AD_RESULT=0; //存储 A/D 转换结果

ADC_RES = 0;

ADC_RESL = 0;

channel &= 0x07; //0000,0111 清0高5位

ADC_CONTR = AD_SPEED;

_nop_(); _nop_();

ADC_CONTR |= channel; //选择 A/D 当前通道

_nop_(); _nop_();

ADC_CONTR |= 0x80; //启动 A/D 电源

delayt(3); //使输入电压达到稳定

ADC_CONTR |= 0x08; //0000,1000 令 ADCS = 1, 启动A/D转换,

led = 1;

while (!(ADC_CONTR & 0x10) ) ; //等待A/D转换结束 //0001,0000 测试A/D转换结束否

ADC_CONTR &= 0xE7; //1111,0111 清 ADC_FLAG 位, 关闭A/D转换,

AD_RESULT = ADC_RES * 4+ ADC_RESL ; // &0x03

return (AD_RESULT); //返回 A/D 高 8 位转换结果

}

void main() //主函数

{

unsigned char d0,d1,d2,d3;

unsigned int num,num1;

//unsigned int num2;

led = 0;

vccreal = 5; //设定实际的电压值

init_mcu();

P1M1 = 0xff ; //P1口设置为高阻

P1M0 = 0x00 ;

P1ASF = 0x80; //0000,0010, 将 P1.7 置成模拟口

AUXR1 &= ~0x04; //0000,0100, 令 ADRJ=0

// 0: 10 位A/D 转换结果的高8 位放在ADC_RES 寄存器, 低2 位放在ADC_RESL 寄存器

// 1: 10 位A/D 转换结果的最高2 位放在ADC_RES 寄存器的低2 位, 低8 位放在ADC_RESL 寄存器

ADC_CONTR |= 0x80; //1000,0000 打开 A/D 转换电源

while(1)

{

num = get_AD_result(7);//P1.7 为 A/D 当前通道, 测量并发送结果

delayt(1);

led = 0;

_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();

//num = 512;

num1 = num*vccreal;

d0 = num1/1024 ; //取出个位

// num = num1-d0*1024; //小数点后的数

num = num1%1024;

d1 = (num*10)/1024; //取出 位

num1 = (num*10)%1024;

// num1 = num*10-d1*1024;

d2 = (num1*10)/1024;

//d3 = (num1*10 -d2*1024);//%10 ;

num = (num1*10)%1024 ;

d3 = (num *10)/1024;

d0=d0+10; //加上小数点 个位

shu0=d3; //显示

shu1=d2; //显示 小数点后一位

shu2=d1; //显示 小数点后两位

shu3=d0; //显示 小数点后三位 19;//

//shu0=0; shu1=1; shu2=2; shu3=3;

}

}

标签: #stc12c5a60s2的ad转换程序