Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: NESRGBSwitch.c
- * Author: Ben Whitman
- *
- * PIC10F322
- *
- * Created on April 2, 2015, 6:33 PM
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <xc.h>
- #include "HEFlash.h"
- // CONFIG
- #pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
- #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
- #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
- #pragma config PWRTE = ON // 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 = OFF // Code Protection bit (Program memory code protection is disabled)
- #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
- #pragma config LPBOR = ON // Brown-out Reset Selection bits (BOR enabled)
- #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)
- uint8_t input_state = 1;
- uint8_t output_state = 0;
- uint8_t shift_reg = 0xFF;
- void button_pressed();
- void button_released();
- void switch_output();
- void save_selection();
- /*
- *
- */
- int main(int argc, char** argv) {
- OSCCON = 0x00;
- INTCON = 0x00;
- OPTION_REGbits.nWPUEN = 0;
- OPTION_REGbits.T0CS = 0;
- OPTION_REGbits.PSA = 0;
- OPTION_REGbits.PS0 = 1;
- OPTION_REGbits.PS1 = 1;
- OPTION_REGbits.PS2 = 1;
- TRISA = 0x0F;
- LATA = 0x00;
- WPUA = 0x08;
- ANSELA = 0x00;
- INTCONbits.TMR0IF = 0;
- INTCONbits.TMR0IE = 0;
- INTCONbits.GIE = 0;
- HEFLASH_readBlock (&output_state, (HEFLASH_MAXROWS - 1), 1);
- while(1)
- {
- shift_reg = (shift_reg << 1) | PORTAbits.RA3;
- switch(input_state)
- {
- case 0:
- if(shift_reg == 0xFF)
- {
- input_state = 1;
- button_released();
- }
- break;
- case 1:
- if(shift_reg == 0x00)
- {
- input_state = 0;
- button_pressed();
- }
- break;
- default:
- input_state = PORTAbits.RA3;
- }
- switch(output_state)
- {
- case 0: // Natural palette: 011
- TRISA = 0x0B;
- break;
- case 1: // Improved palette: 101 | 001
- TRISA = 0x0D;
- break;
- case 2: // Garish palette: 110 | 010
- TRISA = 0x0E;
- break;
- case 3: // Composite out
- TRISA = 0x0F;
- break;
- default: // Natural palette
- TRISA = 0x0B;
- }
- }
- return (EXIT_SUCCESS);
- }
- void button_pressed()
- {
- TMR0IF = 0;
- TMR0 = 239;
- }
- void button_released()
- {
- if(TMR0IF) // Long press
- {
- save_selection();
- }
- else // Short press
- {
- switch_output();
- }
- }
- void switch_output()
- {
- if(output_state++ > 2) // State 3 = NESRGB disabled/PPU component out
- {
- output_state = 0;
- }
- }
- void save_selection()
- {
- char ret = HEFLASH_writeBlock ((HEFLASH_MAXROWS - 1), &output_state, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment