#include"ADC电压表.H"
#include <intrins.h>
uchar value;
void adc_init(uchar ch)
{
ADC_CONTR = AD_SPEED; //选择AD转换
ch&=0x07; //选择通道
ADC_CONTR =ch;
ADC_CONTR |= 0x80; // 开A/D转换电源 ADC_CONTR |= 0x08; // 启动转换
PLASF=0X01;
delay(1);
}
uchar adc_read()
{
uchar value; uchar flag=0;
ADC_RES=0; //清A/D转换结果寄存器
ADC_RESL=0;
while(flag==0) //测试AD转换是否结束
flag=ADC_CONTR&0x10;
ADC_CONTR&=0xe7; //清FLAG位,关闭AD转换
value=ADC_RES;
return(ADC_RES);
}
void display() //显示程序
{
uchar i;
P2=0x00;
for(i=0;i<4;i++)
{
P2=bittab
;
P0=table[buff];
delay(10);
}
}
void delay(unsigned char i) //延时程序
{
uint j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void main()
{
P0M1=0xff;
P0M0=0x00;
P2M1=0xff;
P2M0=0x00;
// P1M1=0xff;
// P1M0=0x00;
while(1)
{
adc_init(0);
vmp=value*(5.0/255.0);
vmp=vmp*100;
huoquvmp=(int)vmp;
buff[3]=huoquvmp/100;
buff[2]=huoquvmp%100/10;
buff[1]=huoquvmp%10;
buff[0]=0;
display();
}
}
ADC电压表.H文件#ifndef __ADC_H__
#define __ADC_H__
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define AD_SPEED 0x60//模数转换器转速控制位
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0~9数组
uchar buff[4]={0,0,0,0}; //位
uchar code bittab[]={0x01,0x02,0x04,0x08}; //控制位
sfr ADC_CONTR=0XBC;
sfr ADC_RES=0XBD;
sfr ADC_RESL=0XBE;
sfr PLASF=0X9D;
sbit EADC=IE^5;
uchar huoquvmp,a,b,c;
float vmp;
sfr P0M1=0x94;
sfr P0M0=0x93;
sfr P2M1=0x96;
sfr P2M0=0x95;
sfr P1M1=0x92;
sfr P1M0=0x91;
//---------------------------
void adc_init(uchar ch);
uchar adc_read();
void delay(unsigned char i);
void display();#endif
此帖出自小平头技术问答
#include "stc12c56.h"
unsigned char AD_Result=0;
//sbit debug_led = P2^2;
/* */
void AD_SPI_Service () interrupt 5
{
if (ADC_CONTR & 0x10) //ad
{
AD_Result=ADC_DATA;
ADC_CONTR &= ~0x10; //ad flag=0
// ADC_CONTR |= 0x08; //start
}
if (SPSTAT & 0x80) //spi
{
SPSTAT &= ~0x80;
}
// debug_led ^= 1;
}
void InChip_AD_Init(void)
{
unsigned char tmp=60000;
ADC_CONTR |= 0x80; //Power on
while (tmp--); //1ms
//P1.0~P1.3 设置为AD输入端
P1M0 |= 0x0f;
P1M1 |= 0x0f;
// ADC_CONTR |= AD_SPEED; //speed
AUXR |= 0x10; //EADCI
EADC_SPI = 1; //中断允许
}
void Seting_AD_Channel(unsigned char channel)
{
//unsigned char tmp=200;
ADC_CONTR = (ADC_CONTR & 0xf8) | (channel & (0x07+0x60)); //60 is speed
//while (tmp--); //20~200us
// Start=1
ADC_CONTR |= 0x08;
}
这个是12c56调过的
一周热门 更多>