/*
 * File:   poker.c
 * Author: th
 * Aladdins magic lamp
 * Created on 29. Oktober 2019, 16:32
 */

// PIC12F1840 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = NSLEEP   // Brown-out Reset Enable (Brown-out Reset enabled while running and disabled in Sleep)
#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 = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF      // PLL Enable (4x PLL disabled)
#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 = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdlib.h>

// Fosc  frequency for _delay()  library
#define _XTAL_FREQ 1000000      // 1Mhz internal Clock 

//port defines
#define LED_Y LATAbits.LATA4  //yellow led
#define LED_R LATAbits.LATA5 //red led


//subroutines
void green(void)
{
    LED_Y = 1;   LED_R = 0;
}
void red(void)
{
    LED_Y = 0;   LED_R = 1;
}
void off(void)
{
    LED_Y = 0;   LED_R = 0;
}


/*
 * Main
 */
void main(void) {    
    

    
//Clock Source and Value: Internal 1Mhz
OSCCONbits.IRCF = 0b1011;

//external interrupt enable
OPTION_REGbits.nWPUEN = 0; //use individuell pulls ups
WPUAbits.WPUA3 = 1; //weak pull up for RA3
INTCONbits.GIE = 0; //only wake up from sleep, no INT routine
INTCONbits.IOCIE = 1; //enable interrupt on chnage module
IOCANbits.IOCAN3 = 1; //port RA3 falling edge
IOCAFbits.IOCAF3 = 0;  //clear flag
        
//all unused inputs pull up
WPUAbits.WPUA0 = 1; //PGD
WPUAbits.WPUA1 = 1; //PGC
WPUAbits.WPUA2 = 1; //RA2 

/* Port init */
// Port A
ANSELA = 0;   //All I/O pins are configured as digital
LATA = 0;     //clear port
TRISA = 0b001111;    //4&5 output other input
    
//variables
int i,randomBit,myrand;

//set startvalue for rand
srand(5963);




while(1)
    {
    //calculation quick 20 times
    for(i=0; i<=9; i++)
    {    
    red();
    __delay_ms(100);
    green();
    __delay_ms(100);
    }

    //calculate red or green
    myrand = rand();
    randomBit = myrand % 2;

    //result blink 3 times
    for(i=0; i<=2; i++)
    {    
      if (randomBit) green(); else red();        
      __delay_ms(500);
      off();
      __delay_ms(500);
    }
    
    if (randomBit) green(); else red();        
    i = randomBit;
    //stay for 3 seconds
    __delay_ms(500);
    __delay_ms(500);
    __delay_ms(500);
    __delay_ms(500);
    __delay_ms(500);
    __delay_ms(500);
    
    //enter sleep mode
    off();  //LED off    
    IOCAFbits.IOCAF3 = 0;  //clear Interrupt flag
    SLEEP();
    NOP();
    
    
  }

}
