Пример #1
0
void main(void)
{
    //variables
    unsigned char i;

    //inputs and outputs
    TRISA = 0;
    TRISB = 0;
    TRISC = 0;
    TRISD = 0;
    TRISE = 0;
    ADCON1 = 0x0F;
    
    LCD_Init();

    LCD_Move(0,0);  for (i=0; i<20; i++) LCD_Write(MSG0[i]);
    Wait_ms(3000);
    LCD_Inst(1);

    // set up Timer0 for PS = 1
    T0CS = 0;
    T0CON = 0x88;
    TMR0ON = 1;
    TMR0IE = 1;
    TMR0IP = 1;
    PEIE = 1;
    
    // set up Timer1 for 1ms
    TMR1CS = 0;
    T1CON = 0x81;
    TMR1ON = 1;
    TMR1IE = 1;
    TMR1IP = 1;
    PEIE = 1;

    // set up Timer3 for 1ms
    TMR3CS = 0;
    T3CON = 0x81;
    TMR3ON = 1;
    TMR3IE = 1;
    TMR3IP = 1;
    PEIE = 1;

    //Turn on all interrupts
    GIE = 1;

    while(1){
        LCD_Move(1,0); LCD_Out(Timer0, 3);
    }
}
Пример #2
0
//%%%%%%%%%%%%%%%%%%%%%%%% Main %%%%%%%%%%%%%%%%%%%%%%%%
void main(void)
{
// Initialize the SPI port
   SSPIE = 0;
   TRISC5 = 0;
   TRISC4 = 1;
   TRISC3 = 0;
   TRISC6 = 0;
   SSPSTAT = 0x40;
   SSPCON1 = 0x22;
// Turn on Timer0
   T0CON = 0x88;
   TMR0ON = 1;
   TMR0IE = 1;
   TMR0IP = 1;
   TMR0 = -3150;
// Turn on TMR1
   TMR1ON = 1;
// Turn on Timer3
   T3CON = 0x81;
   TMR3ON = 1;
   TMR3IE = 1;
   TMR3IP = 1;
   TMR3 = -2500;
// Capture on RC1 for rising edge
   TRISC1 = 1; 
   CCP2CON = 0x05;
   CCP2IE = 1;
   CCP2IP=1;
 // Capture on RC2 for falling edge
   TRISC2 = 1;
   CCP1CON = 0x04; 
   CCP1IE = 1;
   CCP1IP=1;
// initialize INT0 interrupts
   TRISB0 = 1;
   INT0IE = 1;
   //INT0IP = 1;
   INTEDG0 = 1; // rising edges
// initialize INT0 interrupts
   INT1IE = 1;
   INT1IP = 1;
   TRISB1 = 1;
   INTEDG1 = 1; // rising edges

   PEIE = 1;
   GIE = 1;
   ADCON1 = 0x0F;


// PORTD assignments (not used)
   TRISD=0x00;
// PORTC assignments
   TRISC0 = 0; // RF Signal Output
   TRISC1 = 1; // Capture IR edges
   TRISC2 = 1; // Capture IR edges
   TRISC3 = 0; // EEPROM SCK
   TRISC4 = 1; // EEPROM SO
   TRISC5 = 0; // EEPROM SI
   TRISC6 = 0; // EEPROM CS
   TRISC7 = 0; // LED Program Indicator Light
// PORTB assignments
   TRISB0 = 1; // Right Button (Forward: single press & Enter: double press)
   TRISB1 = 1; // Left Button  (Backward: single press & Cancel: double press)
   TRISB2 = 1; // Program Button (Red)
   TRISB3 = 1; // IR Out Button          !!!!REMOVE LATER
   TRISB4 = 1; // RF Out Button          !!!!REMOVE LATER
   TRISB5 = 0; // Enclosure Red   LED
   TRISB6 = 0; // Enclosure Green LED
   TRISB7 = 0; // Enclosure Blue  LED
// PORTA assignments
   TRISA0 = 0;
   TRISA1 = 0; // IR Singal Output
   TRISA2 = 0; // Voice Chip
   TRISA3 = 0; // Voice Chip
   TRISA4 = 1; // Voice Chip
   TRISA5 = 1; // Voice Chip
// PORTE assignments
   TRISE0 = 1; // Radio
   TRISE1 = 0; // Radio



// Assign values to variables
   Time = 0;
   x = 0;
   Color = 0;
   RED = 1;
   GREEN = 0;
   BLUE = 0;
   Count = 0;
   R=0;
   B=0;
   G=0;
   Dnr=0;
   Dnb=0;
   Dng=0;
   soothing = 0;
   glower = 0;
   z0=0;
   z1=2;
   Move=0;
   Menu=1;
   Sub=0;
   Cat=0;
   Com=0;
   Module = 0x0000;
   LED = 1;
   DO=0;

// Ports
   RA1 = 0;
   RC7 = 0;
   PORTD=0x00;
   RA2=0;
   RC0 = 0;


   LCD_Init();

while(1) {


if      (LED==0){RB7=0; RB6=0; RB5=0;}
else if (LED==1){Soothing();}
else if (LED==2){Glower();} 


//%%%%%%%%%%%%%%%% TEST %%%%%%%%%%%%%%%%
if(RB4) {RF_output(A2, A2_ON[18], A2_OFF[18]); A2=(A2+1)%2;} 	// RF     {IR_output(0);}
if(RB2) {IR_Program(0);}  											// IR Program Button
if(RB3) {IR_output(0);}   											// IR Transmit Button
if(RA5) Set_Play(0x1B, 0x28);
// Radio
if      (RE0==1){RE1=1;} // 
else{RE1=0;}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 



// Control States
if((z0!=0)|(z1!=0)){
Move=1;
if(Menu==1){
	if(z0==1)	  {Module=(Module+0x1000); z0=0;
		switch(Module){
		case 0x7000: Module=0x0000; break;}
	}
	else if(z1==1){Module=(Module-0x1000); z1=0;
		switch(Module){
		case 0xF000: Module=0x6000; break;}
	}	
	else if(z0==2){z0=0; Menu=0; Sub=1;}
	else if(z1==2){z1=0;}
}
else if(Sub==1){
	if(z0==1)	  {Module=(Module+0x0100); z0=0;
		switch(Module){
		case 0x0400: Module=0x0000; break;
		case 0x1500: Module=0x1000; break;
		case 0x2300: Module=0x2000; break;
		case 0x3300: Module=0x3000; break;
		case 0x4200: Module=0x4000; break;
		case 0x5500: Module=0x5000; break;
		case 0x6C00: Module=0x6000; break;}
	}
	else if(z1==1){Module=(Module-0x0100); z1=0;
		switch(Module){
		case 0xFF00: Module=0x0300; break;
		case 0x0F00: Module=0x1400; break;
		case 0x1F00: Module=0x2200; break;
		case 0x2F00: Module=0x3100; break;
		case 0x3F00: Module=0x4100; break;
		case 0x4F00: Module=0x5400; break;
		case 0x5F00: Module=0x6B00; break;}
	}
	else if(z0==2){
		switch(Module){
		case 0x0000: DO=1;
		case 0x0100: DO=1;
		case 0x0400: DO=1;
		case 0x0500: DO=1;
		case 0x1000: DO=0;
		case 0x1100: DO=0;
		case 0x1200: DO=0;
		case 0x1300: DO=0;
		case 0x1400: DO=0;
		case 0x2000: DO=0;
		case 0x2100: DO=0;
		case 0x2200: DO=0;
		case 0x3000: DO=0;
		case 0x3100: DO=0;
		case 0x3200: DO=0;
		case 0x4000: DO=0;
		case 0x4100: DO=0;
		case 0x5000: DO=0;
		case 0x5100: DO=0;
		case 0x5200: DO=0;
		case 0x5300: DO=0;
		case 0x5400: DO=0;
		default: z0=0; Sub=0; Cat=1;
		}
	}
	else if(z1==2){z1=0; Sub=0; Menu=1; Module=(Module & 0xF000);}
}
else if(Cat==1){
	if(z0==1)	  {Module=(Module+0x0010); z0=0;
		switch(Module){
		case 0x0240: Module=0x0200; break;
		case 0x03A0: Module=0x0300; break;}
	}
	else if(z1==1){Module=(Module-0x0010); z1=0;
		switch(Module){
		case 0x01F0: Module=0x0230; break;
		case 0x02F0: Module=0x0390; break;}
	}
	else if(z0==2){
		switch(Module){
		case 0x0300: DO=1;
		case 0x0310: DO=1;
		case 0x0320: DO=1;
		case 0x0330: DO=1;
		case 0x0340: DO=1;
		case 0x0350: DO=1;
		case 0x0360: DO=1;
		case 0x0370: DO=1;
		case 0x0380: DO=1;
		case 0x0390: DO=1;
		default: z0=0; Cat=0; Com=1;
		}
	}
	else if(z1==2){z1=0; Cat=0; Sub=1; Module=(Module & 0xFF00);}
}
else if(Com==1){
	if(z0==1)	  {Module=(Module+0x0001); z0=0;
		switch(Module & 0x020F){
		case 0x0205: Module=Module & 0x02F0; break;}
	}
	else if(z1==1){Module=(Module-0x0001); z1=0;
		switch(Module & 0x000F){
		case 0x000F: Module=((Module+0x0010) & 0x00F0)+0x0204; break;}
	}
	else if(z0==2){z0=0; DO=1;}
	else if(z1==2){z1=0; Com=0; Cat=1; Module=(Module & 0xFFF0);}
}
}



// Output voice and maybe function
if(Move==1){
  Move=0;
         LCD_Move(3,0); LCD_Out(Module); /// REMOVE LATER!!!!!!!!
    if(Menu==1){
	switch(Module){
		case 0x0000: // TV
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG[i]); break;
		case 0x1000: // X-10
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG1[i]); break;
		case 0x2000: // Radio
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG2[i]); break;
		case 0x3000: // LED Control
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG3[i]); break;
		case 0x4000: // Volume
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG4[i]); break;
		case 0x5000: // Comments
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG5[i]); break;
		case 0x6000: // IR Programing
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG49[i]); break;
	}
	}

	else if(Sub==1){
	switch (Module){
// TV
		case 0x0000:													
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG6[i]); 
			if(DO==1){IR_output(0);} 
		break;
		case 0x0100: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG0[i]);
			if(DO==1){IR_output(1);} 
		break;
		case 0x0200:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG7[i]); 
		break;
		case 0x0300: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG8[i]); 
		break;
		case 0x0400: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG50[i]); 
			if(DO==1){IR_output(2);}
		break;
		case 0x0500: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG51[i]); 
			if(DO==1){IR_output(3);}
		break;

// X-10
		case 0x1000: 														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG9[i]);
			if(DO==1){RF_output(A1, A1_ON[18], A1_OFF[18]); A1=(A1+1)%2;} 
		break;
		case 0x1100: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG10[i]);
			if(DO==1){RF_output(A2, A2_ON[18], A2_OFF[18]); A2=(A2+1)%2;} 
		break;
		case 0x1200:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG11[i]); 			
			if(DO==1){RF_output(A3, A3_ON[18], A3_OFF[18]); A3=(A3+1)%2;}
		break;
		case 0x1300: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG12[i]); 
			if(DO==1){RF_output(A4, A4_ON[18], A4_OFF[18]); A4=(A4+1)%2;}
		break;
		case 0x1400:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG13[i]);
			if(DO==1){RF_output(A5, A5_ON[18], A5_OFF[18]); A5=(A5+1)%2;} 
		break;

// Radio
		case 0x2000:  														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG14[i]); 
			if(DO==1){}
		break;
		case 0x2100:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG15[i]); 
			if(DO==1){}
		break;
		case 0x2200: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG16[i]); 
			if(DO==1){}
		break;

// LED Control
		case 0x3000: 														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG19[i]); 
			if(DO==1){LED = 0;}
			break;
		case 0x3100:
			if(DO==1){LED = 1;} 
		break;
		case 0x3200:
			if(DO==1){LED = 2;} 
		break;

// Master Volume
		case 0x4000: 														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG17[i]);
			if(DO==1){} 
		break;
		case 0x4100:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG18[i]); 
			if(DO==1){} 
		break;

// Comments
		case 0x5000: 														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG20[i]); 
			if(DO==1){} 
		break;
		case 0x5100: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG21[i]);
			if(DO==1){}  
		break;
		case 0x5200:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG22[i]); 
			if(DO==1){} 
		break;
		case 0x5300: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG23[i]);
			if(DO==1){}  
		break;
		case 0x5400:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG24[i]); 
			if(DO==1){} 
		break;
	}
	}

	else if(Cat==1){
	switch (Module){

// Channel Categories
		case 0x0200: 														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG25[i]); 
		break;
		case 0x0210: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG26[i]); 
		break;
		case 0x0220:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG27[i]); 
		break;
		case 0x0230: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG28[i]); 
		break;

// Channel Numbers
		case 0x0300:  														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG39[i]); 
			if(DO==1){IR_output(4);}
		break;
		case 0x0310:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG40[i]); 
			if(DO==1){IR_output(5);}
		break;
		case 0x0320: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG41[i]); 
			if(DO==1){IR_output(6);}
		break;
		case 0x0330:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG42[i]);
			if(DO==1){IR_output(7);} 
		break;
		case 0x0340: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG43[i]); 
			if(DO==1){IR_output(8);}
		break;
		case 0x0350:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG44[i]);
			if(DO==1){IR_output(9);} 
		break;
		case 0x0360: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG45[i]);
			if(DO==1){IR_output(10);} 
		break;
		case 0x0370:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG46[i]);
			if(DO==1){IR_output(11);} 
		break;
		case 0x0380: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG47[i]); 
			if(DO==1){IR_output(12);}
		break;
		case 0x0390:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG48[i]); 
			if(DO==1){IR_output(13);}
		break;
	}
	}

	else if(Com==1){
	switch (Module){	

// Sports
		case 0x0200: 														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG29[i]);
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			} 
		break;
		case 0x0201: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG30[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
		case 0x0202:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG31[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
		case 0x0203: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG32[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
		case 0x0204:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG33[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;

// Cartoons
		case 0x0210:  														
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG34[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
		case 0x0211:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG35[i]);
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			} 
		break;
		case 0x0212: 
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG36[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
		case 0x0213:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG37[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
		case 0x0214:
			LCD_Move(0,0);for (i=0; i<5; i++) LCD_Write(MSG38[i]); 
			if(DO==1){
				IR_output(10); 
				for(i=0;i<10000;i++); 
				IR_output(9); 
				for(i=0;i<10000;i++); 
				IR_output(13);
			}
		break;
	}
	}
} 



}
}
int main(void) {

	// Cache on, min flash wait, interrupts on, LED/button init, UART init
  NU32_Startup();

  // Setup LCD and create message to be sent
  LCD_Setup();
  char message[MAX_MESSAGE_LENGTH];

  // Local variables for determining and setting Timer interrupts based on FPS
	int Time1_Side = 0, N_Side = 0, PR_Side = 0, TCKPS_Side = 0;
	int Time1_Top = 0, N_Top = 0, PR_Top = 0, TCKPS_Top = 0;


	// =============================================================================================
	// Change Notification Digital Input Interrupt from PPOD
	// =============================================================================================
  CNPUEbits.CNPUE17 = 0;  						// CN17/RF4 input has no internal pull-up

  oldF = PORTF;           						// all pins of port F are inputs, by default

  __builtin_disable_interrupts(); 		// step 1: disable interrupts
  CNCONbits.ON = 1;               		// step 2: configure peripheral: turn on CN
  CNENbits.CNEN17 = 1; 								//         listen to CN17/RF4
  IPC6bits.CNIP = 3;              		// step 3: set interrupt priority
  IPC6bits.CNIS = 2;              		// step 4: set interrupt subpriority
  IFS1bits.CNIF = 0;              		// step 5: clear the interrupt flag
  IEC1bits.CNIE = 1;              		// step 6: enable the _CN interrupt
  __builtin_enable_interrupts();  		// step 7: CPU enabled for mvec interrupts



	// =============================================================================================
	// PWM and digital output for piezoelectric droplet generator
	// =============================================================================================
  OC1CONbits.OCTSEL = 1;  			 			// Select Timer3 for comparison

  T3CONbits.TCKPS = 0;     						// Timer3 prescaler N=1 (1:1)
  PR3 = 3999;              						// period = (PR3+1) * N * 12.5 ns = 20 kHz
  TMR3 = 0;                						// initial TMR3 count is 0

  OC1CONbits.OCM = 0b110; 						// PWM mode with no fault pin; other OC1CON bits are defaults
  OC1RS = 0;           								// duty cycle = OC1RS/(PR3+1) = 75%
  OC1R = 0;             							// initialize before turning OC1 on; afterward it is read-only

  T3CONbits.ON = 1;        						// turn on Timer3
  OC1CONbits.ON = 1;       						// turn on OC1

	// Set A10/A2 to digital output pins
	TRISAbits.TRISA10 = 0;							// RA10 is an output pin
	TRISAbits.TRISA2 = 0;								// RA2 is an output pin

	//-Vcc: set L1 to HIGH, L2 to LOW, PWMA to >0
	OC1RS = 4000;
	LATAbits.LATA10 = 1;
	LATAbits.LATA2 = 0;



	// =============================================================================================
	// TrackCam ExSync Trigger - Side (A3), Top (A4)
	// =============================================================================================

	// Digital output pin
	TRISAbits.TRISA3 = 0;
	TRISAbits.TRISA4 = 0;

	// Set to HIGH
	LATAbits.LATA3 = 1;
	LATAbits.LATA4 = 1;



	// =============================================================================================
	// Keep program running to look for command from master to start record procedure
	// =============================================================================================
  while(1) {

		// Get message from computer
    NU32_ReadUART1(message, MAX_MESSAGE_LENGTH);

		//Serial message: [NUMIMAGES_Side, FPS_Side, NUMIMAGES_Top, FPS_Top, PULSETIME, DELAYTIME]
		sscanf(message, "%d%*c %d%*c %d%*c %d%*c %d%*c %f", &NUMIMAGES_Side, &FPS_Side, &NUMIMAGES_Top, &FPS_Top, &PULSETIME, &DELAYTIME);		//%*c reads in comma and ignores it


		// -------------------------------------------------------------------------------------------
		// Side Camera Interrupt -  NUMIMAGES_Side Hz - Timer2
		// -------------------------------------------------------------------------------------------

		// Calculate pre-scaler based on FPS_Side
		Time1_Side = (((1.0/FPS_Side*1.0)*1e9)/12.5);
		N_Side = ceil( (Time1_Side*1.0/65535.0) );

		// Select best pre-scaler of 1, 2, 4, 8, 16, 32, 64, or 256 
		if( N_Side == 1 ) {
			N_Side = 1;
			TCKPS_Side = 0;
		}
		else if( N_Side <= 2 ) {
			N_Side = 2;
			TCKPS_Side = 1;
		}
		else if( N_Side <= 4 ) {
			N_Side = 4;
			TCKPS_Side = 2;
		}
		else if( N_Side <= 8 ) {
			N_Side = 8;
			TCKPS_Side = 3;
		}
		else if( N_Side <= 16 ) {
			N_Side = 16;
			TCKPS_Side = 4;
		}
		else if( N_Side <= 32 ) {
			N_Side = 32;
			TCKPS_Side = 5;
		}
		else if( N_Side <= 64 ) {
			N_Side = 64;
			TCKPS_Side = 6;
		}
		else {
			N_Side = 256;
			TCKPS_Side = 7;
		}

		// Calculate period register based on selected prescaler
		PR_Side = (Time1_Side / N_Side) - 1;



		__builtin_disable_interrupts(); 	// INT step 2: disable interrupts at CPU

																			// INT step 3: 	setup TMR2 to call ISR at frequency of 5 kHz
		PR2 = PR_Side;										// 							set period register to 16,000
		TMR2 = 0;													// 							initialize count to 0
		T2CONbits.TCKPS = TCKPS_Side;			// 							set prescaler to 1:1
		T2CONbits.ON = 1; 								// 							turn on Timer2
		IPC2bits.T2IP = 5; 								// INT step 4: 	priority 5
		IPC2bits.T2IS = 2; 								// 							subpriority 2
		IFS0bits.T2IF = 0; 								// INT step 5: 	clear interrupt flag
		IEC0bits.T2IE = 1; 								// INT step 6: 	enable interrupt

		__builtin_enable_interrupts(); 		// INT step 7: 	enable interrupts at CPU



		// -------------------------------------------------------------------------------------------
		// Top Camera Interrupt -  NUMIMAGES_Top Hz - Timer4
		// -------------------------------------------------------------------------------------------

		// Calculate pre-scaler based on FPS_Top
		Time1_Top = (((1.0/FPS_Top)*1e9)/12.5);
		N_Top = ceil( (Time1_Top/65535.0) );

		// Select best pre-scaler of 1, 2, 4, 8, 16, 32, 64, or 256 
		if( N_Top == 1 ) {
			N_Top = 1;
			TCKPS_Top = 0;
		}
		else if( N_Top <= 2 ) {
			N_Top = 2;
			TCKPS_Top = 1;
		}
		else if( N_Top <= 4 ) {
			N_Top = 4;
			TCKPS_Top = 2;
		}
		else if( N_Top <= 8 ) {
			N_Top = 8;
			TCKPS_Top = 3;
		}
		else if( N_Top <= 16 ) {
			N_Top = 16;
			TCKPS_Top = 4;
		}
		else if( N_Top <= 32 ) {
			N_Top = 32;
			TCKPS_Top = 5;
		}
		else if( N_Top <= 64 ) {
			N_Top = 64;
			TCKPS_Top = 6;
		}
		else {
			N_Top = 256;
			TCKPS_Top = 7;
		}

		// Calculate period register based on selected prescaler
		PR_Top = (Time1_Top / N_Top) - 1;



		__builtin_disable_interrupts(); 	// INT step 2: disable interrupts at CPU

																			// INT step 3: 	setup TMR2 to call ISR at frequency of 5 kHz
		PR4 = PR_Top; 										// 							set period register to 16,000
		TMR4 = 0;													// 							initialize count to 0
		T4CONbits.TCKPS = TCKPS_Top; 			// 							set prescaler to 1:1
		T4CONbits.ON = 1; 								// 							turn on Timer2
		IPC4bits.T4IP = 5; 								// INT step 4: 	priority 5
		IPC4bits.T4IS = 0; 								// 							subpriority 0
		IFS0bits.T4IF = 0; 								// INT step 5: 	clear interrupt flag
		IEC0bits.T4IE = 1; 								// INT step 6: 	enable interrupt

		__builtin_enable_interrupts(); 		// INT step 7: 	enable interrupts at CPU




		// Convert DELAYTIME to DelayFrames
		//DelayFrames_Side = DELAYTIME*FPS_Side;

		// Turn RecordFlag ON since we have received from master
		RecordFlag = 1;

		// Display properties on LCD
    LCD_Clear();
    LCD_Move(0,0);
		sprintf(message, "%d, %d, %d", NUMIMAGES_Side, FPS_Side, FPS_Top);
		//sprintf(message, "%d, %d, %d", PR2, N_Side, T2CONbits.TCKPS);
    LCD_WriteString(message);                     	

    LCD_Move(1,0);
		sprintf(message, "%d, %f", PULSETIME, DELAYTIME);
		//sprintf(message, "%d, %d, %d", PR4, N_Top, T4CONbits.TCKPS);
    LCD_WriteString(message);
  }

  return 0;
}
int main(void) {

	// Cache on, min flash wait, interrupts on, LED/button init, UART init
  NU32_Startup();
  LCD_Setup();

  char message[MAX_MESSAGE_LENGTH];


	// =============================================================================================
	// Change Notification Digital Input Interrupt from PPOD
	// =============================================================================================
  CNPUEbits.CNPUE17 = 0;  // CN17/RF4 input has no internal pull-up

  oldF = PORTF;           // all pins of port F are inputs, by default

  __builtin_disable_interrupts(); // step 1: disable interrupts
  CNCONbits.ON = 1;               // step 2: configure peripheral: turn on CN
  CNENbits.CNEN17 = 1; 						//         listen to CN17/RF4
  IPC6bits.CNIP = 3;              // step 3: set interrupt priority
  IPC6bits.CNIS = 2;              // step 4: set interrupt subpriority
  IFS1bits.CNIF = 0;              // step 5: clear the interrupt flag
  IEC1bits.CNIE = 1;              // step 6: enable the _CN interrupt
  __builtin_enable_interrupts();  // step 7: CPU enabled for mvec interrupts



	// =============================================================================================
	// PWM and digital output for piezoelectric droplet generator
	// =============================================================================================
  OC1CONbits.OCTSEL = 1;  			 		// Select Timer3 for comparison

  T3CONbits.TCKPS = 0;     					// Timer3 prescaler N=1 (1:1)
  PR3 = 3999;              					// period = (PR3+1) * N * 12.5 ns = 20 kHz
  TMR3 = 0;                					// initial TMR3 count is 0

  OC1CONbits.OCM = 0b110; 					// PWM mode with no fault pin; other OC1CON bits are defaults
  OC1RS = 0;           							// duty cycle = OC1RS/(PR3+1) = 75%
  OC1R = 0;             						// initialize before turning OC1 on; afterward it is read-only

  T3CONbits.ON = 1;        					// turn on Timer3
  OC1CONbits.ON = 1;       					// turn on OC1

	// Set A10/A2 to digital output pins
	TRISAbits.TRISA10 = 0;						// RA10 is an output pin
	TRISAbits.TRISA2 = 0;							// RA2 is an output pin

	//-Vcc: set L1 to HIGH, L2 to LOW, PWMA to >0
	OC1RS = 4000;
	LATAbits.LATA10 = 1;
	LATAbits.LATA2 = 0;



	// =============================================================================================
	// TrackCam ExSync Trigger
	// =============================================================================================

	// Set A3 to digital output pin
	TRISAbits.TRISA3 = 0;						// RA3 is an output pin

	//Set A3 to HIGH
	LATAbits.LATA3 = 1;



	// =============================================================================================
	// Keep program running to look for command from master to start record procedure
	// =============================================================================================
  while(1) {
		// Get message from computer
    NU32_ReadUART1(message, MAX_MESSAGE_LENGTH);

		//Serial message: [NUMIMAGES, FPS, PULSETIME, DELAYTIME]
		sscanf(message, "%d%*c %d%*c %d%*c %f", &NUMIMAGES, &FPS, &PULSETIME, &DELAYTIME);		//%*c reads in comma and ignores it

		// Convert DELAYTIME to DelayFrames
		DelayFrames = DELAYTIME*FPS;

		// Turn RecordFlag ON since we have received from master
		RecordFlag = 1;

		// Display properties on LCD
    LCD_Clear();
    LCD_Move(0,0);
		sprintf(message, "%d, %d", NUMIMAGES, FPS);
    LCD_WriteString(message);                     	

		sprintf(message, "%d, %f", PULSETIME, DELAYTIME);
    LCD_Move(1,0);
    LCD_WriteString(message);
  }

  return 0;
}