CRTC Electronics

Click here to edit subtitle

Light Program

#include <stdio.h>

#include <stdlib.h>

#include <xc.h>

#include <pic.h>

#define _XTAL_FREQ 16000000

//#pragma config MCLRE = OFF, CP = OFF, WDTE = OFF, FORC = INTRC

 // CONFIG

 #pragma config FOSC = INTOSC    // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)

 #pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)

 #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)

 #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)

 #pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)

 #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)

 #pragma config LVP = OFF         // Low-Voltage Programming Enable (Low-voltage programming enabled)

 #pragma config LPBOR = OFF      // Brown-out Reset Selection bits (BOR disabled)

 #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)

 #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)

#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)

#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

/*           Pic10F200

 *           _______

 *  nothing |       | RA 3

 *  2-5.5V  |       | ground

 *  RA 2   |       | nothing

 *  RA 1   |_______| RA 0

 */

char red=0;

char green=0;

char blue=0;

    char cycle()

    {

        char count=0;

        while(count<128)

        {

            char BRG=0;

            if (count < red) BRG+= 0B0001;

            if (count < green) BRG+= 0B0010;

            if (count < blue) BRG+= 0b0100;

            PORTA=BRG;

            count++;

        }

    }

   void fadeto(char R,char G,char B,char duration)

    {

        char count=0;

        while (count<duration)

        {

            if (red<R)  red+=1;

            if (red>R)  red-=1;

            if (green<G)  green+=1;

            if (green>G)  green-=1;

            if (blue<B)  blue+=1;

            if (blue>B)  blue-=1;

            cycle();

            if (PORTAbits.RA3 == 0) break;

            cycle();

            if (PORTAbits.RA3 == 0) break;

            cycle();

            if (PORTAbits.RA3 == 0) break;

            cycle();

            if (PORTAbits.RA3 == 0) break;

            cycle();

            if (PORTAbits.RA3 == 0) break;

            count++;

        }

    }

int main(int argc, char** argv)

{

    ANSELA = 0b0000;

    LATA = 0b0000;

    TRISA = 0b1000;

    while(1)

    {

        //STAGE 1

        // slow Green on / off

        red=0;

        green=0;

        blue=0;

        while (1)

        {

            fadeto (128,0,0,255);

            if (PORTAbits.RA3 == 0) break;

            fadeto (0,0,0,255);

            if (PORTAbits.RA3 == 0) break;

            fadeto (0,128,0,255);

            if (PORTAbits.RA3 == 0) break;

            fadeto (0,0,0,255);

            if (PORTAbits.RA3 == 0) break;

        }

        while (1)

        {

            fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 1) break;

        }

        //STAGE 2

        // fast Green on / off

        red=0;

        green=0;

        blue=0;

        while (1)

        {

            fadeto (0,128,0,25);

            if (PORTAbits.RA3 == 0) break;

            fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 0) break;

             fadeto (128,0,0,25);

            if (PORTAbits.RA3 == 0) break;

             fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 0) break;

              fadeto (0,0,128,25);

            if (PORTAbits.RA3 == 0) break;

              fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 0) break;

        }

        while (1)

        {

            fadeto (0,0,0,75);

            if (PORTAbits.RA3 == 1) break;

        }

        //STAGE 3

        // slow White on / off

        red=0;

        green=0;

        blue=0;

        while (1)

        {

            fadeto (0,0,128,255);

            if (PORTAbits.RA3 == 0) break;

            fadeto (0,0,0,255);

            if (PORTAbits.RA3 == 0) break;

        }

        while (1)

        {

            fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 1) break;

        }

        //STAGE 4

        // fast White on / off

        red=0;

        green=0;

        blue=0;

        while (1)

        {

            fadeto (0,0,128,25);

            if (PORTAbits.RA3 == 0) break;

            fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 0) break;

        }

        while (1)

        {

            fadeto (0,0,0,25);

            if (PORTAbits.RA3 == 1) break;

        }

    }

}