/*
 * File:   main.c
 * Author: boos
 *
 * Created on September 29, 2019, 12:03 AM
 */

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#include <xc.h>

// define locations of CD4094 controls
#define STROBE RB0
#define DATA RB1
#define CLOCK RB2
#define ENABLE RB3

// declare our function
void sendValue (unsigned char value);

// main function
void main (void) {
    
    // set outputs
    TRISB0 = 0;
    TRISB1 = 0;
    TRISB2 = 0;
    TRISB3 = 0;
    
    // what value do we want to send?
    unsigned char myValue = 27;
    
    // main loop
    while (1) {
        sendValue(myValue);
    }
    
    return;
    
}

// auxiliary function that sends a value
// to the CD4094 shift register
void sendValue (unsigned char value) {

    // auxiliary index variable
	unsigned char idx;
    
    // loop over all eight bits
	for (idx = 7; idx >= 0; idx--) {
		if (value & (1 << idx)) {
			DATA = 1;
		} else {
		 	DATA = 0;
		}
		NOP();
		CLOCK = 1;
		NOP();
		CLOCK = 0;
		NOP();
	}

    // make the data visible
	STROBE = 1;
    NOP();
    STROBE = 0;
	NOP();

}