PIC microcontroller AD interrupt debugging (without digital filtering)

Publisher:快乐的旅程Latest update time:2014-12-23 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The interrupt debugging program of PIC microcontroller AD conversion is as follows (without digital filtering):

#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-division (4M), AN0, enable ADC
   ADCON1=0x8E; //right-align, AN0 channel
 ADRESH = ADRESL = 0;
 PIE1 |=0x40; //AD interrupt enable
 INTCON |= 0xC0; //Peripheral enable, global interrupt enable
 ADGO=1; //ADC conversion enable
    delayms(10);
}
void interrupt AD(void)
{
 if(ADIF&&ADIE) //AD interrupt enable and flag bit set 
 {
  ADIF = 0; //flag bit reset
  ADGO=0; //Pause AD conversion
  flag = 1; //Set flag bit
 }
}

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);
  }
 }
}

Reference address:PIC microcontroller AD interrupt debugging (without digital filtering)

Previous article:Analysis of PIC16F877 Asynchronous Serial Port Interrupt
Next article:PIC16F877A internal EEPROM reading and writing experiment

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号