Analog girişe çoklu buton...

Uzun zaman önce forumda paylaştığım örnek çalışmayı buraya taşıyorum. Asıl link; http://www.picproje.org/index.php/topic,54691.msg418419.html#msg418419

Çıkış noktası; "Pull-up/pull-down haricinde buton başka şekilde bağlanır mı?"  sorusuydu...

Mavi çerçevedeki bölüm oluşan analog değerin yaklaşık olarak 10-bit karşılığı olarak görülebilir.

 

/*
 * File:   main.c
 * Author: Mehmet Bilgi
 *
 * Created on 07 Ağustos 2014 Perşembe, 20:30
 ********************************************************************************
 *                         12F683                                               *
 *                         ------                                               *
 *                     Vdd |1  Y  8| Vss                                        *
 *    GP5/T1CKI/OSC1/CLKIN |2     7| GP0/AN0/CIN+/ICSPDAT/ULPWU                 *
 * GP4/AN3/T1G/OSC2/CLKOUT |3     6| GP1/AN1/CIN-/V REF /ICSPCLK                *
 *           GP3/!MCLR/Vpp |4     5| GP2/AN2/T0CKI/INT/COUT/CCP1                *
 *                          ------                                              *
 ********************************************************************************/

#include <xc.h>
#include <stdint.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = ON          // Code Protection bit (Program memory code protection is enabled)
#pragma config CPD = ON         // Data Code Protection bit (Data memory code protection is enabled)
#pragma config BOREN = ON       // Brown Out Detect (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

//**************Declare Global Variables**************************************
#define _XTAL_FREQ      8000000

#define KIRMIZI         GP5
#define YESIL  GP4
#define MAVI  GP2
#define SARI  GP1
#define RENK_DEGISTIR   GP3

//------------------------------------------------------------------------------
//

void init_ADC(void) // ADC Hazirla
{
    // ANSEL register
    ANSEL = 0b01110001; // ANS0 analog giris, Frc (internal clk  for Tad)

    // Init ADC
    ADCON0 = 0b10000000; //saga yaklasik, vdd, -, -, 00=an0, 0=done,  0=adon off
    ADON = 1; // analog cevrimi baslat
    __delay_ms(1000);

    GO_nDONE = 1; // analog okuma hazır mı?
}

//------------------------------------------------------------------------------
//

void init_MCU(void)
{
    CMCON0 = 0x07;
    CMCON1 = 0x00;
    OSCCON = 0b01111000; //8mhz

    GPIO = 0x00;
    TRISIO = 0b00000001; //GP0 (AN0) + GP3 giris, digerleri cikis
}
//------------------------------------------------------------------------------
//

uint8_t buton_sec(void)
{
    uint16_t an_buton = 0;
    uint8_t deger = 255;

    while (GO_nDONE) continue; // ADC azır olana kadar bekle

    an_buton = ADRESL + (ADRESH << 8); // ADC bilgisi al
   
    if(an_buton > 194 && an_buton < 215) deger = 1;
    if(an_buton > 245 && an_buton < 266) deger = 2;
    if(an_buton > 330 && an_buton < 350) deger = 3;
    if(an_buton > 500 && an_buton < 520) deger = 4;

    if(RENK_DEGISTIR == 0) deger += 10;

    __delay_ms(1);
    GO_nDONE = 1;
    return deger;
}
//------------------------------------------------------------------------------
//

void kirmizi(void)
{
    KIRMIZI = 1;
    YESIL   = 0;
    MAVI    = 0;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//

void yesil(void)
{
    KIRMIZI = 0;
    YESIL   = 1;
    MAVI    = 0;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//

void mavi(void)
{
    KIRMIZI = 0;
    YESIL   = 0;
    MAVI    = 1;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//

void sari(void)
{
    KIRMIZI = 0;
    YESIL   = 0;
    MAVI    = 0;
    SARI    = 1;
}
//------------------------------------------------------------------------------
//

void beyaz(void)
{
    KIRMIZI = 1;
    YESIL   = 1;
    MAVI    = 1;
    SARI    = 1;
}
//------------------------------------------------------------------------------
//

void turkuaz(void)
{
    KIRMIZI = 0;
    YESIL   = 1;
    MAVI    = 1;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//

void pembe(void)
{
    KIRMIZI = 1;
    YESIL   = 0;
    MAVI    = 1;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//

void turuncu(void)
{
    KIRMIZI = 1;
    YESIL   = 1;
    MAVI    = 0;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//
void sondur(void)
{
    KIRMIZI = 0;
    YESIL   = 0;
    MAVI    = 0;
    SARI    = 0;
}
//------------------------------------------------------------------------------
//

void main(void)
{
    init_MCU();
    init_ADC();

    while (1)
    {
        switch(buton_sec())
        {
            case 1:
                kirmizi();
                break;
            case 2:
                yesil();
                break;
            case 3:
                mavi();
                break;
            case 4:
                sari();
                break;
            case 11:
                turuncu();
                break;
            case 12:
                pembe();
                break;
            case 13:
                turkuaz();
                break;
            case 14:
                beyaz();
                break;
            default:
                sondur();
                break;
        }
        __delay_ms(5);
    }
}

 

Tags: