/****************************************************************************/
/* Copyright 2005 MBARI.                                                    */
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */
/* All rights reserved.                                                     */
/****************************************************************************/
#include <16f876A.h>
#include <string.h>
#include "pic_test.h"
//#include "errors.h"

#if DEBUG
#device ICD=TRUE
#fuses XT,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#else
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP
#endif

#use delay(clock=4000000)

#define  RS485_RX_BUFFER_SIZE 64
#define  RS485_USE_EXT_INT    FALSE
// Defines the device's RS485 ID


// Defines the destination's RS485 ID
#define  RS485_DEST_ID        0x01
#include "myrs485.c"
#include <stdlib.h>
#include <stdio.h>

//#include "cmd_io.c"

int8 in_char = 0;
int8 next_in = 0;
int8 next_out = 0;
int8 command[1];
int8 data[1];
#define INTS_PER_SECOND 15     // (20000000/(4*256*256))
int8 RS485_ID=0; 
int32 seconds;      // A running seconds counter
int8 int_count;    // Number of interrupts left before a second has elapsed
int32 ElapsedSeconds;      // A running seconds counter
int8 Elapsed_int_count;    // Number of interrupts left before a second has elapsed



long clockTicks = 0;
#INT_RTCC
rtccCounter()
{
    clockTicks++;
     if(--int_count==0) {           // per second.
      ++seconds;
      if(seconds&0x01)
      output_bit(PIN_C3, LO);
	  else
	    output_bit(PIN_C3, HI); 
	    
      int_count=INTS_PER_SECOND;
    }

}
void startTimer(void)
{
    disable_interrupts(INT_RTCC);
 	seconds=0;
 	int_count=INTS_PER_SECOND;
 	enable_interrupts(INT_RTCC);
 					
}
void getElapsedTime(void)
{
	disable_interrupts(INT_RTCC);
 	ElapsedSeconds=seconds;
 	Elapsed_int_count=INTS_PER_SECOND-int_count;// Number of interrupts since the last second, there are 15 per sec
 	enable_interrupts(INT_RTCC);
 

}
void RS485sendresponse(char* s)
{
   int8 size;

   for(size=0; s[size]!='\0'; ++size);

   rs485_wait_for_bus(FALSE);

   while(!rs485_send_message(RS485_DEST_ID,RS485_ID,size, s))
   {    
    delay_ms(10);
    }
     
}

main()
{
char c;
   
 int8 i,try,cs, send_addr = 0;
 int1 ret=FALSE;
 int8 size;
 int8 message[16];
 
 RS485_ID=  (input(PIN_C1)<<1)|(input(PIN_C0)); 
//RS485_ID=2;
 	
 	strcpy(message,"RS485");
    /* set up the system clock */
    int_count=INTS_PER_SECOND;
   
    set_timer0(0);
      setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT);
   /* enable appropriate interrupts */ 
    rs485_init();
  //  printf("ID=%d", RS485_ID);
    enable_interrupts(INT_RTCC);
  	RS485sendresponse(message);
	    for(;;)
		{
	
		 	if(rs485_get_new_message(data,command,RS485_ID, FALSE))
		 	{
		                                                                        	  
			  switch(command[0])
	         {
	         	
	         	case 'S':
	               strcpy(message,"Start");
	               startTimer();
	               output_bit(PIN_C4, HI);
   				   RS485sendresponse(message);
	               break;

	            case 'C':
	           /*	 strcpy(message,"InCommand");
	             RS485sendresponse(message);
	             */  
	               output_bit(PIN_C4, LO);
				   getElapsedTime();
				 //  RCV_OFF();
				   printf("Command"); 
				   printf("%Ld", ElapsedSeconds);
				   printf("%d",Elapsed_int_count);
	              // RCV_ON();
	               break;

	            default:
	               strcpy(message,"Error");
	  			   RS485sendresponse(message);
	               break;
	         }
			  	 
			}	
	/*	output_bit(PIN_C3, LO);
	    delay_ms(2000);
	    output_bit(PIN_C3, HI); delay_ms(3000);
	  */           
         
		}
        
   
}
