Source file: /~heha/basteln/PC/oszi/PCS500/Firmware.zip/Main.c

/*
 * File:   Main.c
 * Author: Szymon Roslowski
 *
 * Created on 13 October 2014, 17:41
 */

#ifdef SDCC
# define NO_BIT_DEFINES	// sonst hagelt es Fehler wegen Doppeldefinitionen
# include <pic16f1454.h>
# define __persistent	// not available here
#else
# include <htc.h>
#endif
#include "Usb.h"


#ifdef SDCC
__code unsigned __at(_CONFIG1) cfg1 = 0x0FCC;
__code unsigned __at(_CONFIG2) cfg2 = 0x3FCE;
#else	// #pragma config kann sdcc nur bei PIC18! (sdcc-Typo: PIC16)
// CONFIG1
#pragma config FOSC	= INTOSC	// Oscillator Selection Bits (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	= ON	// MCLR Pin Function Select (MCLR/VPP pin function is Reset)
#pragma config CP	= OFF	// Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN	= ON	// Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF	// Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO	= OFF	// Internal/External Switchover Mode (Internal/External Switchover Mode is enabled)
#pragma config FCMEN	= OFF	// 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 CPUDIV	= NOCLKDIV	// CPU System Clock Selection Bit (CPU system clock divided by 6)
#pragma config USBLSCLK = 48MHz	// USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.)
#pragma config PLLMULT	= 3x	// PLL Multipler Selection Bit (3x Output Frequency Selected)
#pragma config PLLEN	= ENABLED	// PLL Enable Bit (3x or 4x PLL Enabled)
#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 LPBOR	= OFF	// Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP	= ON	// Low-Voltage Programming Enable (Low-voltage programming enabled)
#endif

// Local Defines
#define Button		PORTCbits.RC2
#define Led		PORTCbits.RC3

// Local Variables
BYTE ButtonStatus;  // This is to hold last status of the button so that we only report if it changes

static void InitializeSystem(void) {
 OSCCON =	0xFC;		// 16MHz HFINTOSC with 3x PLL enabled (48MHz operation)
 ACTCON =	0x90;		// Enable active clock tuning with USB

 TRISC =	0b00000100;	// Set RC3 as output except RC2 for Button
 LATC =		0b00000000;	// Clear Port C Latches;

 ButtonStatus = 0;
}

static void PrepareTxBuffer(void) {
 BYTE i;

 InReport.data[0] = 0x80;      // Status
 InReport.data[1] = (BYTE)(Button);    // Button Status
 InReport.data[2] = (BYTE)(Led);       // Led Status

    // Fill The Rest Of Buffer with 0
 for (i=3 ; i<sizeof InReport; i++) InReport.data[i]=0;
}

static void ProcessIO(void) {
    // Check USB for incomming Commands
 if (UADDR /*DeviceState==0x05*/ && !UCONbits.SUSPND) {
  if(IsUsbDataAvaialble() > 0 ) {
   if (OutReport.data[0] == 0x80) PrepareTxBuffer();
   if (OutReport.data[0] == 0x81) {
	// Toggle Led
    if (Led) Led = 0; else Led = 1;
	// Report status
    PrepareTxBuffer();
   }
	// PrepareTxBuffer Can be Moved Here and first If Discarded
	// But I Left it like this for better Picture.
   ReArmInterface();
   HIDSend();
  }
 }
    // Check Status Of the Button
 if (ButtonStatus!=Button) {	// If Button Status Changed - Report
  PrepareTxBuffer();
  HIDSend();
    // Save New Button Status
  ButtonStatus = (BYTE)Button;
 }
}

void main(void) {
 InitializeSystem();
 InitializeUSB();
 for(;;) {
  if (PIR2bits.USBIF) ProcessUSBTransactions();
  ProcessIO();
 }
}
Detected encoding: ASCII (7 bit)2