pic多路adc 中断方式 在网上查相关资料没找到,自己写了有个,调试通过 ,pic16f1947

/* 
 * File:   main.c
 * Author: sm116
 *
 * Created on June 12, 2017, 9:27 AM
 */


#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF         // Watchdog Timer Enable (WDT enabled)ON
#pragma config PWRTE = ON       // Power-up Timer Enable (PWRT enabled)ON
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = ON         // Flash Program Memory Code Protection (Program memory code protection is enabled)ON
#pragma config CPD = ON         // Data Memory Code Protection (Data memory code protection is enabled)
#pragma config BOREN = SBODEN   // Brown-out Reset Enable (Brown-out Reset controlled by the SBOREN bit in the BORCON register)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)


// CONFIG2
#pragma config WRT = ALL       // Flash Memory Self-Write Protection (000h to FFFh write protected, no addresses may be modified by EECON control)ALL
#pragma config VCAPEN = OFF     // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled)
#pragma config PLLEN = ON      // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)




#define _XTAL_FREQ  32000000
#define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
#include
#include
#include
#include
#include;


#include
#include


unsigned int batter_ad=0;
unsigned int bat=0;


void init_system(void)//                                                                    时钟
{
OSCCON  =0xf0;//0xF0;//0xf3;//0x6B; //内部时钟32m/8M/4M
WDTCON  = 0x1f; //看门狗周期32S
}


void init_gpio(void)//                                                                      端口
{
TRISAbits.TRISA2=1;
ANSELAbits.ANSA2=1;
TRISAbits.TRISA3=1;
ANSELAbits.ANSA3=1;





}


void init_adc(void)
{


ADCON0=0X0b     ;     //默认采集的 
 ADCON1=0XE0;// FOSC/64 右对齐参考电压 VDD
    PIE1bits.ADIE=1;
    PIR1bits.ADIF=0;
    INTCONbits.GIE=1;
    INTCONbits.PEIE=1;
   
}




unsigned int get_ad(unsigned char n)
{
    unsigned int  value=0;
  
    ADCON0 = n ;
   
    ADCON1=0XE0;// FOSC/64 右对齐参考电压 VDD
    __delay_us(5);
 //   Delay_ms(20);//致命延时3天没找到问题 分时调用,暂时不用
//     ADCON0bits.ADON=1;//在定时器0中断中
    ADCON0bits.GO=1;
    while(ADCON0bits.GO);
    value=ADRESH *256 + ADRESL;
    return value;
}


void interrupt ISR(void) //                                                                    isr
{
if(ADIF)
{
ADIF=0;
        if( ADCON0==0X09)//在中断中实际的adcon0的GO 是0 所以是配置ADCON0-2
        {
batter_ad=(ADRESH<<8)+ADRESL; //存放ad值 
        ADCON0=0X0F;   //打开下一次要用的通道
            ADCON0bits.GO=1;//启动转换,不然只转化一次 
        }
          if( ADCON0==0X0d)
          {
          bat=(ADRESH<<8)+ADRESL;
          ADCON0=0X09;   
            ADCON0bits.GO=1;
          }

}
}
void main()
{
init_system();
init_gpio();
init_adc();
while(1)

    
    
    
//bat=get_ad(0X0B);
    //batter_ad=get_ad(0X0f);
//bat=batter_ad;
}




}

你可能感兴趣的:(单片机,pic)