PIC单片机AD转换的中断调试程序如下(无数字滤波):
#include
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
const uchar disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint adval;
float advalf;
uchar flag;
void delayms(uint x)
{
uint j,k;
for(j=x;j>0;j--)
for(k=110;k>0;k--);
}
void display(uint x )
{
PORTD= disp[x/1000]|0x80;
PORTA= 0xF1;
delayms(1);
PORTD= disp[x/100%10];
PORTA= 0xF3;
delayms(1);
PORTD= disp[x/10%10];
PORTA= 0xF5;
delayms(1);
PORTD= disp[x%10];
PORTA= 0xF7;
delayms(1);
}
void init()
{
uint adval=0;
float advalf=0;
flag = 0;
TRISA=0x01;
TRISC=0;
PORTC=0x01;
TRISD=0;
ADCON0=0x41; //8分频(4M)、AN0、使能ADC
ADCON1=0x8E; //右对齐、AN0通道
ADRESH = ADRESL = 0;
PIE1 |=0x40; //AD中断使能
INTCON |= 0xC0; //外设使能、全局中断使能
ADGO=1; //ADC转换使能
delayms(10);
}
void interrupt AD(void)
{
if(ADIF&&ADIE) //AD中断使能且标志位置位
{
ADIF = 0; //标志位复位
ADGO=0; //暂停AD转换
flag = 1; //置标志位
}
}
void main()
{
init();
while(1)
{
if(flag)
{
flag = 0;
adval=ADRESH;
adval=adval<<8|ADRESL;
advalf=adval/1023.0*5.0;
adval=advalf*1000;
ADGO=1; //ADC转换使能
display(adval);
}
}
}
上一篇:PIC16F877异步串口中断探析
下一篇:PIC16F877A 内部EEPROM读写实验
推荐阅读最新更新时间:2024-03-16 13:49