📄 ad.c
字号:
//ad conversor
#include <pic.h>
const char table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
const char table0[]={0xbf,0x86,0xdb,0xcf,0xe6,
0xed,0xfd,0x87,0xff,0xef};
unsigned int adresult=0;
unsigned int ad_sample_result=0;
unsigned char ad_flag=0;
unsigned int ad_delay=0;
void initial()
{
INTCON=0x00;
ADCON1=0x07;
}
void ad_initial()
{ TRISA=TRISA|0x01;
ADCON0=0x41;
ADCON1=0x82;
ADIF=0;
ADIE=1;
}
void deal_ad()
{
unsigned int temp;
ad_sample_result=ADRESL+(ADRESH<<8);
ad_flag=0;
adresult=(ad_sample_result*50)>>10;
temp=adresult;
adresult=(((temp/10)<<4)&0xf0)+(adresult%10);
}
void interrupt ad()
{
if(ADIF==1)
{ ADIF=0;
ad_flag=1;
}
}
void delay()
{
int i;
for(i=0;i<10;i++)
i=i;
}
void display()
{
unsigned char data,data0,data1,data2;
data0=adresult&0x0f;
data=table[data0];
data1=(adresult&0xf0)>>4;
data2=table0[data1];
RB4=1;RB3=1;
RB1=0;
if(RB1==0) { PORTD=data; delay(); PORTD=0x00;RB1=1; RB2=0; }
if(RB2==0) { PORTD=data2; delay(); PORTD=0x00;RB2=1; }
}
void main()
{
TRISB=0x00;
TRISD=0x00;
initial();
ad_initial();
INTCON=0xc0;
while(1)
{
display();
if(ad_flag==1)
deal_ad();
if(ad_delay>0x0f)
{
ad_delay=0;
ADCON0=ADCON0|0x04;
}
else ad_delay++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -