//#define Debug
#define SEND 
/**************************************************************************************************************
 *    DESCRIPTION:   Can Example   
 *    SOURCE:        canmain.c
 *    Date:         10-16-2005 
 *    AUTHOR:        Jianjian Song
 *    Reference:   page 633, Embedded Systems by Steven Barrett and Daniel Pack,
 **************************************************************************************************************/

#include "can.h"

/* Local Variable Declarations */

ulong busclk;

/* Main Routine */
void main ()
{
uchar success_flag;  //indicate if an operation is successful. 0 for failure and 1 for success
uchar potvalue, received_voltage;

#ifndef Debug
// set clock speed to be 24MHz
  Crg.clksel.byte &=~PLLSEL; // disconnect PLL
  Crg.pllctl.byte |=PLLON;  // turn on PLL
  Crg.synr.byte |=SYN1; // set PLL multiplier
  Crg.refdv.byte |=REFDV0; // 
	while(!(Crg.crgflg.byte & LOCK)){
	};  // wait for PLL to lock in
	Crg.clksel.byte |=PLLSEL; // connect PLL
  busclk = oscclk/2;					// Set Bus Freq. = (1/2) * Oscillator Freq.
  (void) InitSCI(BaudRate);				// Initialize SCI module
#endif
	SEI();								// Disable Interrupts
	SetLedindicator();
	CLI();  // enable I bit interrupts

#ifndef Debug    
	(void) printf("\n\r*********************************************\n\r\n\r");	// Prints Message
 	(void) printf("ECE331 Fall 2005\n\r");					// Prompts for User Information
	//	(void) scanf("%s",&temp_string); 										// Gets User Information
   (void) printf("Jianjian Song\n\r");										// Prints Message

  (void) printf("Initializing MSCAN .....\n\r");	// Prints Message	
#endif
  (void) can_initialize(); 
#ifdef SEND
#ifndef Debug
 (void) printf("\n\r***I am the sender***\n\r");
#endif
  potvalue = 0;

#ifndef Debug
  (void) printf("\n\rSend 1st Pot Value = %d%s\n\r",potvalue);	// Prints Message	
#endif    
  success_flag = can_send_message(potvalue);
  potvalue = 1;
#ifndef Debug
  (void) printf("\n\rSend 2nd Pot Value = %d%s\n\r",potvalue);	// Prints Message	
#endif
  success_flag = can_send_message(potvalue);
#endif

#ifndef SEND
#ifndef Debug	
 (void) printf("\n\r***I am the receiver*** waiting to receive a message\n\r");
#endif

	(void) can_receive_message(&received_voltage);
#ifndef Debug
  printf("\n\r1st received votlage = %d%s\n\r",received_voltage);	// Prints Message		
#endif
  
  (void) can_receive_message(&received_voltage);
#ifndef Debug
  printf("\n\r2nd received votlage = %d%s\n\r",received_voltage);	// Prints Message		
#endif
#endif

#ifndef Debug
  printf("\n\rEnd of Can Message Exchange\n\r");	// Prints Message		
#endif
	while(1);
}