XC8 pic16877a için basit analog kütüphane

Başlatan seyityildirim, 11 Ağustos 2015, 10:56:34

seyityildirim

merhaba arkadaşlar belki faydalı olur diye ekleyeyim dedim. kütüphane de tek fonksiyon mevcut.
/* 
 * File:   analoglib.h
 * Author: Seyit YILDIRIM
 * 
 *  16f877a icin ayarlanabilir referans? olmayan 
 *  kesme kullanmadan AN0-AN5 aras? analog deger okuma
 *
 * Created on 10 A?ustos 2015 Pazartesi, 14:48
 */

#ifndef ANALOGLIB_H
#define	ANALOGLIB_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <xc.h>
#define _XTAL_FREQ 4000000 
#define _delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
  
    
int analog_oku(unsigned char kanal)
{
    int sonuc=0;                // donecek deger
    TRISA=0xFF;                 // PORTA input
    
    ADCON1bits.ADFM=1;          // deger saga dayali
    
    ADCON1bits.PCFG0=0;         // AN0-AN5 ANALOG ; +Vref=Vdd ; -Vref=Vss
    ADCON1bits.PCFG1=1; 
    ADCON1bits.PCFG2=0;  
    ADCON1bits.PCFG0=0;  
    if(kanal==0)
    {
      ADCON0bits.CHS0=0;        // AN0 channel seçildi
      ADCON0bits.CHS1=0;       
      ADCON0bits.CHS2=0;        
    }
    else if( kanal==1)
    {
      ADCON0bits.CHS0=1;        // AN1 channel seçildi
      ADCON0bits.CHS1=0;       
      ADCON0bits.CHS2=0;        
    }
    else if( kanal==2)
    {
      ADCON0bits.CHS0=0;        // AN2 channel seçildi
      ADCON0bits.CHS1=1;       
      ADCON0bits.CHS2=0;        
    }
    else if( kanal==3)
    {
      ADCON0bits.CHS0=1;        // AN3 channel seçildi
      ADCON0bits.CHS1=1;       
      ADCON0bits.CHS2=0;        
    }
    else if( kanal==4)
    {
      ADCON0bits.CHS0=0;        // AN4 channel seçildi
      ADCON0bits.CHS1=0;       
      ADCON0bits.CHS2=1;        
    }
    else if( kanal==5)
    {
      ADCON0bits.CHS0=1;        // AN5 channel seçildi
      ADCON0bits.CHS1=0;       
      ADCON0bits.CHS2=1;        
    }
    
    ADCON0bits.ADCS=0b01;       // conversion clock = Fosc/8
    ADCON1bits.ADCS2=0;         // conversion clock = Fosc/8
    
    ADCON0bits.ADON=1;          // A/D module acildi
    
    _delay_us(100);             // acquisition time( abartili)
    
    ADCON0bits.GO_nDONE=1;      // start conversion
    
    while(ADCON0bits.GO_nDONE==1);
    sonuc=(ADRESH<<8)+ ADRESL;
    return sonuc;
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


#ifdef	__cplusplus
}
#endif

#endif	/* ANALOGLIB_H */


örneğin

int deger;
deger=analog_oku(0);

deyip AN0 bitinden analog deger okuyabilirsiniz

mehmet

Bu şekilde sadeleştirilebilir...
/* 
 * File:   analoglib.h
 * Author: Seyit YILDIRIM
 * 
 *  16f877a icin ayarlanabilir referans? olmayan 
 *  kesme kullanmadan AN0-AN5 aras? analog deger okuma
 *
 * Created on 10 A?ustos 2015 Pazartesi, 14:48
 */

#ifndef ANALOGLIB_H
#define	ANALOGLIB_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <xc.h>
#define _XTAL_FREQ 4000000 
#define _delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
  
    
int analog_oku(unsigned char kanal)
{
    int sonuc=0;                // donecek deger
    TRISA=0xFF;                 // PORTA input
    
    ADCON1bits.ADFM=1;          // deger saga dayali
    
    ADCON1bits.PCFG0=0;         // AN0-AN5 ANALOG ; +Vref=Vdd ; -Vref=Vss
    ADCON1bits.PCFG1=1; 
    ADCON1bits.PCFG2=0;  
    ADCON1bits.PCFG0=0;  

    if(kanal > 7)
           return 0;

    ADCON0 |= (kanal << 3); 
   
    ADCON0bits.ADCS=0b01;       // conversion clock = Fosc/8
    ADCON1bits.ADCS2=0;         // conversion clock = Fosc/8
    
    ADCON0bits.ADON=1;          // A/D module acildi
    
    _delay_us(100);             // acquisition time( abartili)
    
    ADCON0bits.GO_nDONE=1;      // start conversion
    
    while(ADCON0bits.GO_nDONE==1);
    sonuc=(ADRESH<<8)+ ADRESL;
    return sonuc;
}

#ifdef	__cplusplus
}
#endif

#endif	/* ANALOGLIB_H */
Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr