#include <16f876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT

#use delay (clock=20000000)

//#use fast_io(B)
//#use fast_io(C)
#use rs232(baud=38400, bits=8, xmit=PIN_C6, rcv=PIN_C7)

#define RS232_XMT       PIN_C6  // (output) RS232 serial transmit
#define RS232_RCV       PIN_C7  // (input)  RS232 serial receive
#define RLED	        PIN_C0  // (output) RED LED
#define GLED	        PIN_C5  // (output) GREEN LED
#define BUTTON1		    PIN_B1  // (input)  PUSH BUTTON 1
#define BUTTON2		    PIN_B2  // (input)	PUSH BUTTON 2
#define ENC				PIN_B0  // (input) ENCODER SIGNAL PROP BLADE
#define CAPPWM			PIN_B5	// (output) CAPACITANCE PULSING
#define CAPSENSE		PIN_B4	// (input) SENSING CAP CHARGE TIME

#define GLED_OFF    output_low(GLED)
#define GLED_ON     output_high(GLED)
#define RLED_OFF    output_low(RLED)
#define RLED_ON     output_high(RLED)
#define PRESS1	    input(BUTTON1)
#define PRESS2	    input(BUTTON2)

#use I2C(SLAVE, sda=PIN_C4, scl=PIN_C3, address=0xB0, SLOW, FORCE_HW)

int default_pwm1 = 64;
int default_pwm2 = 64;
int default_pwm3 = 64;

int pwm;
byte cap = 0x0A; 

byte default_cap1;
byte default_cap2;
byte default_cap3;

int pwm_address = 10;
int cap_address = 20;

byte data;
byte address;


byte read_cap(){
	return(cap);
}

#INT_SSP
void ssp_interrupt()
{
  byte incoming;
//  byte address;

  // turn a green LED on
  RLED_OFF;
  GLED_OFF;

  if(i2c_poll() == false){
	cap = read_cap();
	i2c_write(cap);
	GLED_ON;
	RLED_OFF;
  }
  else {
	incoming = i2c_read();
  	set_pwm1_duty(incoming);
	pwm = incoming;
	RLED_ON;
	GLED_OFF;
  }
}


	

  

main()
{

  RLED_ON;
  GLED_ON;
  delay_ms( 250 );
  RLED_OFF;
  GLED_OFF;
  delay_ms( 250 );

  // listen to any interrupts that might be coming from the SSP module
  enable_interrupts( GLOBAL );
  enable_interrupts( INT_SSP );

	setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
	setup_timer_2(T2_DIV_BY_16, 127, 1);
  
  // sit in an infinite loop
  while( true );

}
  
