本帖最后由 milktang 于 2010-4-12 08:34 编辑
本人刚接触PIC单片机,请教各位大虾我的这段AD转换数码管显示为什么一直显示0,C口的灯一直都无变化。数码管显示用中断扫描,程序如下:附件为电路图
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
int y;
int result=0x000;
uchar pos=0;
union adres
{int y1;
uchar adre[2];
}adresult;
void delay(uchar x)
{uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void delay1(uchar x)
{uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TRISA=0xCF;
TRISB=0X00;
TRISC=0x00;
ADCON1=0X85;
ADCON0=0X81;
delay1(100);
}
void display(int x)
{int temp;
temp=x;
if(temp>=0x000&&temp<=0x080){PORTC=0xFF;}
else if(temp>=0x080&&temp<=0x100){PORTC=0x7F;}
else if(temp>=0x100&&temp<=0x180){PORTC=0x3F;}
else if(temp>=0x180&&temp<=0x200){PORTC=0x1F;}
else if(temp>=0x200&&temp<=0x280){PORTC=0x0F;}
else if(temp>=0x280&&temp<=0x300){PORTC=0x07;}
else if(temp>=0x300&&temp<=0x380){PORTC=0x03;}
else if(temp>=0x380&&temp<=0x3FF){PORTC=0x01;}
}
void main()
{
RA4=1;
RA5=1;
GIE=1;
TMR0IE=1;
OPTION=0x02;
TMR0=0x00;
T0IF=0;
while(1)
{int i;
for(i=5;i>0;i--)
{
init();
ADGO=0X1;
while(ADGO);
adresult.adre[0]=ADRESL;
adresult.adre[1]=ADRESH;
result=result+adresult.y1;
}
result=result/5;
display(result);
}
}
void interrupt dsp(void)
{
T0IF=0;
y=result;
if(pos==0)
{
PORTB=table[y/100];
RA4=0;
delay(20);
RA4=1;
}
if(pos==1)
{
PORTB=table[y%100/10];
RA5=0;
delay(20);
RA5=1;
}
pos++;
if(pos>=2)
{pos=0;}
TMR0=0x00;
} |