Guest User

NESRGBSwitch.c

a guest
Jun 8th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * File:   NESRGBSwitch.c
  3.  * Author: Ben Whitman
  4.  *
  5.  * PIC10F322
  6.  *
  7.  * Created on April 2, 2015, 6:33 PM
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <xc.h>
  14.  
  15. #include "HEFlash.h"
  16.  
  17. // CONFIG
  18. #pragma config FOSC = INTOSC    // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
  19. #pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
  20. #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
  21. #pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
  22. #pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
  23. #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  24. #pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
  25. #pragma config LPBOR = ON       // Brown-out Reset Selection bits (BOR enabled)
  26. #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
  27. #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
  28.  
  29.  
  30. uint8_t input_state = 1;
  31. uint8_t output_state = 0;
  32. uint8_t shift_reg = 0xFF;
  33.  
  34. void button_pressed();
  35. void button_released();
  36. void switch_output();
  37. void save_selection();
  38.  
  39. /*
  40.  *
  41.  */
  42. int main(int argc, char** argv) {
  43.  
  44.     OSCCON = 0x00;
  45.     INTCON = 0x00;
  46.  
  47.     OPTION_REGbits.nWPUEN = 0;
  48.     OPTION_REGbits.T0CS = 0;
  49.     OPTION_REGbits.PSA = 0;
  50.     OPTION_REGbits.PS0 = 1;
  51.     OPTION_REGbits.PS1 = 1;
  52.     OPTION_REGbits.PS2 = 1;
  53.  
  54.     TRISA = 0x0F;
  55.     LATA = 0x00;
  56.     WPUA = 0x08;
  57.     ANSELA = 0x00;
  58.  
  59.     INTCONbits.TMR0IF = 0;
  60.     INTCONbits.TMR0IE = 0;
  61.     INTCONbits.GIE = 0;
  62.  
  63.     HEFLASH_readBlock (&output_state, (HEFLASH_MAXROWS - 1), 1);
  64.  
  65.     while(1)
  66.     {
  67.         shift_reg = (shift_reg << 1) | PORTAbits.RA3;
  68.  
  69.         switch(input_state)
  70.         {
  71.             case 0:
  72.                 if(shift_reg == 0xFF)
  73.                 {
  74.                     input_state = 1;
  75.                     button_released();
  76.                 }
  77.                 break;
  78.  
  79.             case 1:
  80.                 if(shift_reg == 0x00)
  81.                 {
  82.                     input_state = 0;
  83.                     button_pressed();
  84.                 }
  85.                 break;
  86.  
  87.             default:
  88.                 input_state = PORTAbits.RA3;
  89.         }
  90.  
  91.         switch(output_state)
  92.         {
  93.             case 0: // Natural palette: 011
  94.                 TRISA = 0x0B;
  95.                 break;
  96.  
  97.             case 1: // Improved palette: 101 | 001
  98.                 TRISA = 0x0D;
  99.                 break;
  100.  
  101.             case 2: // Garish palette: 110 | 010
  102.                 TRISA = 0x0E;
  103.                 break;
  104.  
  105.             case 3: // Composite out
  106.                 TRISA = 0x0F;
  107.                 break;
  108.  
  109.             default: // Natural palette
  110.                 TRISA = 0x0B;
  111.         }
  112.     }
  113.  
  114.     return (EXIT_SUCCESS);
  115. }
  116.  
  117. void button_pressed()
  118. {
  119.     TMR0IF = 0;
  120.     TMR0 = 239;
  121. }
  122.  
  123.  
  124. void button_released()
  125. {
  126.     if(TMR0IF)  // Long press
  127.     {
  128.         save_selection();
  129.     }
  130.  
  131.     else  // Short press
  132.     {
  133.         switch_output();
  134.     }
  135. }
  136.  
  137.  
  138. void switch_output()
  139. {
  140.     if(output_state++ > 2) // State 3 = NESRGB disabled/PPU component out
  141.     {
  142.         output_state = 0;
  143.     }
  144. }
  145.  
  146.  
  147. void save_selection()
  148. {
  149.     char ret = HEFLASH_writeBlock ((HEFLASH_MAXROWS - 1), &output_state, 1);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment