/*
 * File:   main.c
 * Author: skoshi
 *
 * Created on 8. Dezember 2011, 17:14
 */


/* Präprozessor */
/* ############ */
#include "htc.h"
#include "pic.h"
#include "stdlib.h"
#include "delay.c"
#define SEG1 RA1
#define SEG2 RA0
#define Taster RA2

/* Konfiguration des PCIs
 *
 * WDTE_OFF                 - Watchdogtimer off
 * PWRTE_OF                 - Power-on Timer off
 * FOSC_INTOSCIO            - Interner Taktgeber 8MHz
 * MCLRE_OFF                - Master Clear off
 */

__CONFIG(WDTE_OFF & PWRTE_ON & FOSC_INTOSCIO & MCLRE_OFF & LVP_OFF);


/* Unterfunktionen deklarieren */
/* ########################### */
void init();                            // initialisiere SFR's
void check();                           // überprüft RA2 und speichert Status inn button

unsigned char button;

int main(void)
{
    init();
    while(1)                            // Mainloop
    {
        check();

        while(button == 1)              // wenn Taster nicht betätigt wurde
        {
            SEG1 = 0;                   // SEG1 soll leuchten
            RA0 = 1;
            PORTB = 0b10010001;
            check();                    // prüfe RA2
        }
        while(button == 0)              // wennn Taster betätigt wurde
        {
            SEG2 = 0;                   // SEG2 soll leuchten
            SEG1 = 1;
            PORTB = 0x10;
            check();                    // prüfe RA2
        }
    }

    return 0;
}


void init()
{
    OSCCON = 118;                 // 8MHz interner Takt
    TRISB = 0x00;                 // PortB auf Ausgang
    TRISA = 0b00000100;           // A2 als Eingang und Rest Ausgang
    PORTB = 0x00;                 // clear PortB
    PORTA = 0x00;                 // clear PortA
    SEG1 = 1;                     // Segment 1 u. 2 aus
    SEG2 = 1;
}

void check()
{
    button = RA2;
    DelayMs(100);
}