#include <pic.h>
#define dua RC0
#define wei RC1
__CONFIG (0X3B31);
bit AD_FLAG;
const char dua_table [] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 };
const char ddua_table[] = { 0x40,0x79,0x24,0x30,0x19,0x12,0x02, 0x78,0x00,0x10 };
const char wei_arry [] = {0x01,0x02,0x04,0x08}; //位选编码
void delay(unsigned int x)
{
unsigned int a,b;
for(a=x;a>0;a--)
for (b=18;b>0;b--);
}
void display (unsigned char dua,unsigned char wei)//显示函数
{
if (wei == 2)PORTB=ddua_table[dua];
else PORTB = dua_table[dua];
dua = 1;
dua = 0;
PORTB=wei_arry[wei-1];
wei = 1;
wei = 0;
delay(40);
PORTB = 0;
wei = 1;
wei = 0;
}
void inital () //初始化
{
TRISA=TRISA|0X01;
TRISB=0;
TRISC=0;
PORTB=0;
PORTC=0;
INTCON=0;
PIE1=0;
ADCON1=0X8E; //1000 1110
ADCON0=0X41; //0100 0001
}
void main()
{
volatile long int tmp; //一定要定义长整型,否则会溢出
int advalue;
char a1,a2,a3,a4;
inital();
ADIE=1;
PEIE=1;
GIE=1;
ADGO=1;
if(AD_FLAG==1)
{
tmp=get_advalue();
advalue = (tmp*500)>>10;
a1 = advalue / 1000;
a2 = advalue % 1000 / 100;
a3 = advalue % 100 / 10;
a4 = advalue % 10;
display (a1,1);
display (a2,2);
display (a3,3);
display (a4,4);
}
}
void interrupt get_ad() //中断函数
{
if(ADIF ==1 )
{
ADIF = 0;
AD_FLAG = 1;
}
}
完成代码见附件
PIC16F877_adc使用.rar
(19.21 KB, 下载次数: 31)
|