Ejemplo n.º 1
0
void initIO(void)
{
    //SET OUTPUTS
    
    //Servo channels as outputs
    
    DDRB |= (1 << PIN0) | (1 << PIN1) | (1 << PIN2) | (1 << PIN3) | (1 << PIN4);
    DDRD |= (1 << PIN0) | (1 << PIN1) | (1 << PIN2) | (1 << PIN3) | (1 << PIN4) | (1 << PIN5) | (1 << PIN6);
    
    //LED as ouput
    DDRB |= (1 << PIN6);
    
    //LED on
    PORTB |= (1 << PIN6);
    
    _delay_ms(500);
    
    PORTB &= ~(1 << PIN6);
    
    InitTimer1();
    
    StartTimer1();
    
    cli();  // Disable interrupts
    
    usiTwiSlaveInit(SLAVE_ADDR_ATTINY);	// TWI slave init
    
    sei();  // Re-enable interrupts
}
Ejemplo n.º 2
0
void Init44k22(void)
{
//OSC int 16MHz (62.5ns/inst.)
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;
while(!OSCCONbits.HFIOFS); //wait until ready

//Uart1
ANSELCbits.ANSC7 = 0; //Port C7 select som digital port
ANSELCbits.ANSC6 = 0; //Port C6 select som digital port

//Uart2
ANSELDbits.ANSD7 = 0; //Port D7 select som digital port
ANSELDbits.ANSD6 = 0; //Port D6 select som digital port

//Leds
ANSELAbits.ANSA3 = 0; //Port A3 select som digital port Red
ANSELEbits.ANSE1 = 0; //Port E1 select som digital port Green
ANSELEbits.ANSE2 = 0; //Port E2 select som digital port Blue

//InitTimer0();
InitTimer1();
//InitTimer2();
InitTimer3();

InitUart1();
InitUart2();

Init_IO();
}
Ejemplo n.º 3
0
int main(void)
{
	InitIO();
	init_ADC();
	InitTimer1();
	InitTimer2();
	InitExtInt();
	
    while(1)
    {
        switch(display_select)
		{
			case 0x00:
				MusicOnLed();
			break;
			case 0x01:
				FadeOnLed();
			break;
			case 0x02:
				StrobeOnLed(100, 100, 100);
			break;
			/*
			.
			.	TODO:: Sync up pin change interrupts for the last push button
			.
			*/
			case 0xFF:
				ColorOnLed(1,0,1);
			break;
		}
    }
}
Ejemplo n.º 4
0
void Init26k20(){

    //OSC int 16MHz (62.5ns/inst.)
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;
OSCCONbits.SCS1 = 1;

while(!OSCCONbits.IOFS); //wait until ready

//dioder

ANSEL=0x00; //set ports som digitale
ANSELH=0x00;

//input
//ANSELBbits.ANSB0 = 1; //Port B0 som analog
//ANSELBbits.ANSB1 = 1; //Port B1 som analog
//ANSELBbits.ANSB2 = 1; //Port B2 som analog


//InitTimer0();
InitTimer1();
//InitTimer2();
InitTimer3();

InitUart1();

Init_IO();


}
Ejemplo n.º 5
0
int RCInitReception(int blocking)
{
    InitTimer1();               // Initialize and start timer 1
    StopTimer1();               // Stop timer
    RC_reset = 1;               // Reset the Cypress
    DS_CS_2 = 1;               // Deselect the Cypress
    activeChannelIndex=0;         // 
    rcState = RC_LOWLEVEL_RESET1;
    StartTimer1();               // Start timer
    if (blocking) {
        while (rcState != RC_LOWLEVEL_RUNNING) {
            // Nothing to do but wait...
        }
    }
    return 0;
}
// Start program
int main(void)
{
	// Timer initialize
	InitTimer1();
	InitTimer0();

	// Set LED pin to output
	LED_DDR = (1 << LED_OUT);

	// Enable interrupts
	sei();

	while(1)
	{
		// Sleep Mode
		set_sleep_mode(SLEEP_MODE_IDLE);
		sleep_mode();

		// If command exists ... compare
		if (CmdDone == 1)
		{
			// If matches required command
			if (RC5_cmd_val == RC5_CMD)
			{
				// Sleep disablen
				//sleep_disable();

				// Set output to 1
				LED_PORT |= (1 << LED_OUT);
				// Set CmdMatch to 1 ... no new command will be sampled
				CmdMatch = 1;
				// Set TimerValue to zero
				TimerValue = 0;
				// Start counting
				StartTimer0();
			}

			// Reset all values
			RC5_cmd_val = 0x00;
			CmdBitNumber = 7;
			StartBit = 0;
			CmdDone = 0;
		}
	}
}
Ejemplo n.º 7
0
void CShiftPWM::Start(int ledFrequency, unsigned char maxBrightness){
	// Configure and enable timer1 or timer 2 for a compare and match A interrupt.    

	m_ledFrequency = ledFrequency;
	m_maxBrightness = maxBrightness;

	if(LoadNotTooHigh() ){
		if(m_timer==1){ 
			InitTimer1();
		}
		else if(m_timer==2){
			InitTimer2();
		}
	}
	else{
		Serial.println("Interrupts are disabled because load is too high.");
		cli(); //Disable interrupts
	}
}
Ejemplo n.º 8
0
void InitApp(void) {
    LCD_TRIS = 0;
    //WATCH_DOG
    //WDTCONbits.SWDTEN=1;     // WDT is turned on
    OPTION_REGbits.PSA = 1; //PSA = 1 (Prescaler Assignment bit 1 = Prescaler is assigned to the WDT)
    OPTION_REGbits.PS = 0b111; // PS = 1:128
    //WDTCONbits.WDTPS=0b110; // 16-bit WDT Prescaler 1:2048  SUM = 8s
    //WDTCONbits.WDTPS=0b0100; // 16-bit WDT Prescaler 1:512  SUM = 2s
    //WDTCONbits.WDTPS=0b0011; // 16-bit WDT Prescaler 1:256  SUM = 1.04s
    //WDTCONbits.WDTPS=0b0010; // 16-bit WDT Prescaler 1:128  SUM = 0.52s
    WDTCONbits.WDTPS = 0b0001; // 16-bit WDT Prescaler 1:64  SUM = 0.251s
    /* TODO Initialize User Ports/Peripherals/Project here */

    /* Setup analog functionality and port direction */

    /* Initialize peripherals */

    /* Enable interrupts */


    InitTimer1();
    InitIOC();
}
Ejemplo n.º 9
0
void main() {
    char p1;
    unsigned int Duty;
    char tmp;

    OSCCON = 0b01111100;
    OSCCON2 = 0b10000111;
    OSCTUNE = 0b01000000;
    ADCON0 = 0;
    CCP1CON = 0;
    CCP2CON = 0;
    CCP3CON = 0;
    PWM1_Init( 44000 );
    PWM1_Start();
    x1 = 0;
    xStep = 1;
    TRISB4_bit = 1;
    TRISB5_bit = 1;
    InitTimer1();
    while(1)
    {
            if( PORTB.B4 == 1) // Play Cat Meow
            {
                x1 = 0;
                wavSel = 0;
                T1CON         = 0x01;
                Delay_ms(250);
            }
            if( PORTB.B5 == 1) // Play Dog Bark
            {
                x1 = 0;
                wavSel = 1;
                T1CON         = 0x01;
                Delay_ms(250);
            }
    }
}
Ejemplo n.º 10
0
/********************************* 
	main entry point
*********************************/
int main ( void )
{
    // Init the basic hardware
    InitCnsts();
    InitHardware();

    // Start the main 1Khz timer and PWM timer
    InitTimer1();
    InitTimer2();
    InitPWM();

    // Initialize A2D, 
    InitA2D();

    UART1_Init(XBEE_SPEED);         // for communication and control signals
    UART2_Init(LOGGING_RC_SPEED);   // for spektrum RC satellite receiver

    // Wait for a bit before doing rate gyro bias calibration
    // TODO: test this length of wait
    uint16_t i=0;for(i=0;i<60000;i++){Nop();}

    // turn on the leds until the bias calibration is complete
    led_on(LED_RED);
    led_on(LED_GREEN);

    // Initialize the AHRS
    AHRS_init();

    // Initialize the Controller variables
    Controller_Init();

    // MAIN CONTROL LOOP: Loop forever
    while (1)
    {
        // Gyro propagation
        if(loop.GyroProp){
            loop.GyroProp = 0;
            // Call gyro propagation
            AHRS_GyroProp();
        }

        // Attitude control
        if(loop.AttCtl){
            loop.AttCtl = 0;
            // Call attitude control
            Controller_Update();
        }

        // Accelerometer correction
        if( loop.ReadAccMag ){
            loop.ReadAccMag = 0;
            AHRS_AccMagCorrect( );
        }

        // Send data over modem - runs at ~20Hz
        if(loop.SendSerial){
            loop.SendSerial = 0;

            // Send debug packet
            UART1_SendAHRSpacket();
        }

        // Process Spektrum RC data
        if(loop.ProcessSpektrum){
            loop.ProcessSpektrum = 0;
            UART2_ProcessSpektrumData();
        }

        // Read data from UART RX buffers - 500 Hz
        if(loop.ReadSerial){
            loop.ReadSerial = 0;

            // Read serial data
            //UART2_FlushRX_Spektrum();

        }


        // Toggle Red LED at 1Hz
        if(loop.ToggleLED){
            loop.ToggleLED = 0;
            
            // Toggle LED
            led_toggle(LED_RED);
        }

    } // End while(1)
	
}
Ejemplo n.º 11
0
void main(void)
{
  unsigned char t = 0;
  unsigned int i = 0;
//  unsigned int i;
  
//  unsigned char wdtc = *((__far unsigned char*)0x188);
//  name[2] = wdtc;
  
//  WDTC_WTE = 0;
  
  clear_CPU_operation_detection();
  
  clear_CPU_operation_detection();
  DDR8 = 0x00;
  PDR8 = 0x00;
  DDR8_D87 = 1;
  PDR8_P87 = 1;
  
  DDR0 = 0x00;
  DDR1 = 0x00;
  DDR3 = 0x00;
  DDR4 = 0x00;
  DDR5 = 0x00;
  DDR6 = 0x00;
  DDR7 = 0x00;
  DDR9 = 0x00;
  
  PDR0 = 0x00;
  PDR1 = 0x00;
  PDR3 = 0x00;
  PDR4 = 0x00;
  PDR5 = 0x00;
  PDR6 = 0x00;
  PDR7 = 0x00;
  PDR9 = 0x00;
 
  EIRR = 0;  
  
  ADER = 0;
  
  PWC0_OE1 = 0;
  PWC0_OE2 = 0;   
  PWC1_OE1 = 0;
  PWC1_OE2 = 0;  
  PWC2_OE1 = 0;
  PWC2_OE2 = 0;  
  PWC3_OE1 = 0;
  PWC3_OE2 = 0;  
  
  
  InitTimer1();
  driver_Init();
  
  InitIrqLevels();
  __set_il(7);                /* allow all levels */
  __EI();                     /* globaly enable interrupts */
  
/*
  for (i=0; i<30000; i++)
		clear_CPU_operation_detection();
  for (i=0; i<30000; i++)
		clear_CPU_operation_detection();
  for (i=0; i<30000; i++)
		clear_CPU_operation_detection();
  for (i=0; i<30000; i++)
		clear_CPU_operation_detection();
  for (i=0; i<30000; i++)
		clear_CPU_operation_detection();
//*/		

  
  while(1)
  {
    driver_Update();
    clear_CPU_operation_detection();
    
//    if (t)
//	   	PDR8_P87 = 1;
//	else
//		PDR8_P87 = 0;
//	t=~t;
//	PDR8_P87 = ~PDR8_P87;
  }

}
Ejemplo n.º 12
0
//--------------------------------------------------------------------------
// Module:
//  main
//
///   This function is the main program. It provides initialization of hardware
///   and software components and software. The main loop  portion handles
///   only the Debug task.
///
//--------------------------------------------------------------------------
void main (void)
{
    UINT_16 temp = 0;

	/* Disable all C167 interrupts */
    DISABLE_ALL_INTERRUPTS();

	/* Don't allow any interrupt processing to occur */
    SetMvbReady (FALSE);
    SetStartupSuccessful (FALSE);

    /* Release MVB IPACK hardware */
    ReleaseIpackHw();

    /* Configure Push-Pull digital IOs at P7 to Digital Outputs */
    DO_Init (0xFF);

	/* Initialize the HiDAC outputs */
    InitP8ForDigOuts();
    InitDigitalOutputs();
    InitAnalogOutputs();

	/* Toggle VDrive and CPU watchdogs */
    ToggleVDriveWatchdog();
    ToggleCPUWatchdog();

    /* Set the state of the HIDAC LEDs */
    HIDAC_TEST_LED_ON();
    HIDAC_FAIL_LED_OFF();

    /* Enable SSMR1, SSMR2, VDrive & +5V to Hex LED Display */
    DO_WriteBank (DIGOUT_BANK2, 0x00FF);

	/* Initialize the software to detect a 15 volt failure */
    Init15VoltFailDetect();
    /* This call initializes the 10 ms Timer ISR 
*/
	InitTimer0();
    InitTimer1 ();
	InitTimer7 ();

	ENABLE_ALL_INTERRUPTS();

	/* Allow the VDrive watchdog to toggle and wait some timer before 
	   trying to drive display */
    SetVdriveDisable (FALSE);
	TM_Wait (250);


    /* Display the stored CRC checksum for approximately 1 second at startup */
    DisplayCRConHexLED();

    /* Perform the RAM Test. */
    if (!VerifyRAM())
    {
        HIDAC_FAIL_LED_ON();
        UpdateHidacError (RAM_FAILURE);
    }

	if (!HidacErrorExists())
	{
		/* Perform the ROM Test. */
		if (!VerifyCRCAtStart())
		{
			HIDAC_FAIL_LED_ON();
			UpdateHidacError (ROM_FAILURE);
		}
	}

    /* Init the RS-232 */
    InitSerialCommunications();

    /* Init the analog inputs */
    InitAnalogInputs();

    /* Init the PWM input hardware */
    PwmInInit();

    /* Init the MVB IPACK interface */
    InitMVBIpack();

	/* DAS This call to InitPwmOutputs needs to be after InitMVBIpack. After Sweden made an
	  an upgrade to the IPACK CPLD to rev 2, for some reason, initializing PWM1 prior to initializing 
	  the IPACK causes the IPACK not to initialize properly. Didn't have time to investigate ????? */
	InitPwmOutputs();

	if (!HidacErrorExists())
	{
	    /* stop here if +/- 15VDC supply is low */
		if (Get15VoltBad())
		{
			HexLEDUpdate (0x15);
			HIDAC_FAIL_LED_ON();
			UpdateHidacError (BAD_15V);
		}
	}

#ifdef BURN_IN
	if (!HidacErrorExists())
	{
		BurnInCode();
	}
	else
	{
		while (1);
	}
    ////////////////////////////////////////////////////////////////
    // Code never gets past here if Burn-In code enabled
    // Infinite while (FOREVER) in BurnInCode() or if error detected
    ////////////////////////////////////////////////////////////////
#endif


    HIDAC_TEST_LED_OFF();

    SetStartupSuccessful (TRUE);

	// Setting the argument to FALSE indicates that the baseline CPU loading has
	// already been performed and that the CPU load check, that can be performed via
	// the serial port, will report the CPU loading as a percentage. Setting to TRUE
	// should only be done when capturing the CPU baseline loading and should be returned
	// to FALSE for normal operation
    DebugInit (FALSE);

    if (!HidacErrorExists())
    {
        HexLEDUpdate (0x00);
    }

    /* This is the background loop */
    while (1)
    {
		// Used to measure CPU loading
		DebugUpdateLoadCounter();
        
		/* Code that executes in the main while forever loop. All RealTime critical
			application code is interrupt driven */
        DebugService();

    }

}
Ejemplo n.º 13
0
int main(void){ //main function, execution starts here
	char c = 'g';

    /*
     * PIC pins start as analog inputs to protect any sensitive 
     * externally connected components from unwanted pin output. 
     * To use a pin for digital input and output, we need to 
     * disable the analog functions on a pin by writing 1 to 
     * the corresponding bits of the AD1PCFGL register.
     */
	AD1PCFGL = 0xFFFF; //digital pins

    /* 	Initialize ports */
	LATA  = 0x0000; 				// set latch levels
	TRISA = 0x0000; 				// set IO as outputs
	LATB  = 0x0000; 				// set latch levels
	TRISB = 0x0000; 				// set IO as outputs
	TRISBbits.TRISB6 = 1;
	
    
	//setup internal clock for 80MHz/40MIPS
	//7.37/2=3.685*43=158.455/2=79.2275
	CLKDIVbits.PLLPRE=0; // PLLPRE (N2) 0=/2 
	PLLFBD=41; //pll multiplier (M) = +2
	CLKDIVbits.PLLPOST=0;// PLLPOST (N1) 0=/2
    while(!OSCCONbits.LOCK);//wait for PLL ready
    
    InitTimer1();

	//uart
	//UART can be placed on any RPx pin
	//we need to configure it for RP14/RP15 to use the FTDI usb->serial converter
	//you could also output one (or both) of the two available UARTS to the I/O header
	//assign pin 14 to the UART1 RX input register
	//RX PR14 (input)
	U1RXR_I = 6;
	//assign UART1 TX function to the pin 15 output register
	//TX RP15 (output)
	RP7_O=U1TX_O;

	//InitializeUART1();	
	//setup UART
    U1BRG = 85;//86@80mhz, [email protected]=115200
    U1MODE = 0; //clear mode register
    U1MODEbits.BRGH = 1; //use high percison baud generator
    U1STA = 0;	//clear status register
    U1MODEbits.UARTEN = 1; //enable the UART RX
    IFS0bits.U1RXIF = 0;  //clear the receive flag

    /*
	//setup LEDs
//	SD_TRIS = 0; //set pin direction to output
//	IO1_TRIS = 0;
	LD1_TRIS = 0;
//	SD_O = 1;	//set all pins high (LED on)
	LD1_O = 0;
//	IO1_O=1;
 */
    UART1TX(' ');//first character is discarded
    UART1TX('h');
    UART1TX('e');
    UART1TX('l');
    UART1TX('l');
    UART1TX('o');
    
    
    while(FOREVER){//never ending loop

        
        /*Microchip code*/
        if (timer_expired && Counter == FLASH_RATE )
		{
			LATA = ~LATA;
			LATB = ~LATB;
			Counter = 0;
			timer_expired = 0;
		}

		// or do something else
		Nop();
		Nop();
		Nop();
        //-----------end Microchip code-------------//
        
//        for(counterNum1 = 0;counterNum1<10000;counterNum1++)
//        {
//            counterNum1=counterNum1;
//        } //random delay)
//        LD1_O = 1 - LD1_O;
		if(UART1RXRdy()==1){//check for data waiting
			c=UART1RX();//get the character
			/*if(c=='0'){
				//LATA &=(~0b11100000000);
				LD1_O = 0;
				IO1_O=0;
				SD_O = 0;
			}else if(c=='1'){
				LD1_O = 1;
			}else if(c=='2'){
				IO1_O=1;
			}else if(c=='3'){
				SD_O = 1;
			}*/
			UART1TX(c);//echo the character back
  		}	
          
	}
}
Ejemplo n.º 14
0
int main(int argc, char** argv) {
    
    //Initializations
    InitCLK();
    InitGPIO();
    InitADC();
    //InitSPI();
    InitTimer0();
    InitTimer1();
    InitWatchdog();
    InitUART();             //Initialize UART module

    unsigned char periodicCounter = MIN_PERIOD;
    gpsIndex = 0;

    INTCONbits.GIE = 1;

    ToggleSleepGPS();       //Turn GPS on
    SetupGPS();             //Setup Lat/Long recording

    PORTBbits.PORTB = LATBbits.LATB & 0xDF; //turn red LED off
    PORTBbits.PORTB = LATBbits.LATB | 0x10; //turn green LED on
    __delay_ms(100);
    PORTBbits.PORTB = LATBbits.LATB & 0xEF; //turn green LED off
    
    
    SPI_CS = CS_IDLE;//prerecord
    
    while(1){
        
        //PRE_RECORD FUNCTION
        //InitSPI();            //Start-up SPI again
        //PreRecordMode();
        //SSPCON1bits.SSPEN=0;  // Disable SPI Port
        //PORTCbits.RC5 = 0;    //Set MOSI low
        //SPI_CS = CS_IDLE;//prerecord
        //__delay_ms(5);
        
        SPI_CS = CS_IDLE;
        
        //Check Flags
        if(PORTAbits.RA1)
        {
             //Strobe LED
            PORTBbits.RB0 = 1;
            __delay_ms(100);
            PORTBbits.RB0 = 0;
        }

        if(recordFlag)
        {
          RecordMode();
          recordFlag = 0;
          __delay_ms(500); //trying to get gps to not hangup
        }

        //Not recording, Update the GPS
        if(gpsTimeoutState==0)
            UpdateGPS(); //tell GPS to send an update
        else if(gpsTimeoutState==1)
        {
            ToggleSleepGPS();
            gpsTimeoutState = 2;
        }
        else if(gpsTimeoutState==2)
        {
            ToggleSleepGPS();
            gpsTimeoutState = 3;
        }
        else if(gpsTimeoutState==3)
        {
            __delay_ms(1000);
            gpsTimeoutState = 0;
        }
        else
        {
            gpsTimeoutState = 0;
        }

        if(gpsInvalidFlag) //turn on red LED if invalid message
        {
            PORTBbits.PORTB = LATBbits.LATB | 0x20; //turn red LED on
            PORTBbits.PORTB = LATBbits.LATB & 0xEF; //turn green LED off
            __delay_ms(250);
            PORTBbits.PORTB = LATBbits.LATB & 0xDF; //turn red LED off
            if(periodicCounter < MAX_PERIOD)
                periodicCounter++;
        }
        else //turn on green LED if valid message
        {
            PORTBbits.PORTB = LATBbits.LATB | 0x10; //turn green LED on
            PORTBbits.PORTB = LATBbits.LATB & 0xDF; //turn red LED off
            __delay_ms(250);
            PORTBbits.PORTB = LATBbits.LATB & 0xEF; //turn green LED off
            periodicCounter = MIN_PERIOD;
        }
        
        if(!recordFlag)
        {
            if(PORTAbits.RA1) //check strobe
                GoToSleep(MIN_PERIOD);
            else if(gpsInvalidFlag)
                GoToSleep(periodicCounter);
            else
            {
                ToggleSleepGPS();       //Turn GPS off
                Hibernate();
                ToggleSleepGPS();       //Turn GPS on
            }
        }
        

    }

    return (EXIT_SUCCESS);
}
Ejemplo n.º 15
0
/*-----------------------------------------------------------------------------
*  process received bus telegrams
*/
static void ProcessBus(void) {

   uint8_t     ret;
   TBusMsgType msgType;
   uint8_t     i;
   uint8_t     *p;
   bool        msgForMe = false;
   uint8_t     flags;

   uint8_t         old_osccal;
   static uint8_t  sOsccal = 0;
   uint16_t        startCnt;
   uint16_t        stopCnt;
   uint16_t        clocks;
   uint16_t        diff;
   uint16_t        seqLen;
   static int      sCount = 0;
   static uint16_t sMinDiff = 0xffff;
   static uint8_t  sMinOsccal = 0;
   uint8_t         osccal_corr;
   
   ret = BusCheck();

   if (ret == BUS_MSG_OK) {
      msgType = spRxBusMsg->type; 
      switch (msgType) {  
         case eBusDevReqReboot:
         case eBusDevReqInfo:
         case eBusDevReqSetAddr:
         case eBusDevReqEepromRead:
         case eBusDevReqEepromWrite:
         case eBusDevReqDoClockCalib:
            if (spRxBusMsg->msg.devBus.receiverAddr == MY_ADDR) {
               msgForMe = true;
            }
            break;
         default:
            break;
      }
      if (msgForMe == false) {
         return;
      }

      switch (msgType) {  
         case eBusDevReqReboot:
            /* reset controller with watchdog */    
            /* set watchdog timeout to shortest value (14 ms) */                     
            cli();
            wdt_enable(WDTO_15MS);
            /* wait for reset */
            while (1);
            break;   
         case eBusDevReqInfo:
            sTxBusMsg.type = eBusDevRespInfo;  
            sTxBusMsg.senderAddr = MY_ADDR; 
            sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr;
            sTxBusMsg.msg.devBus.x.devResp.info.devType = eBusDevTypeSw8Cal;
            strncpy((char *)(sTxBusMsg.msg.devBus.x.devResp.info.version),
               version, BUS_DEV_INFO_VERSION_LEN); 
            sTxBusMsg.msg.devBus.x.devResp.info.version[BUS_DEV_INFO_VERSION_LEN - 1] = '\0';
            BusSend(&sTxBusMsg);  
            break;
         case eBusDevReqSetAddr:
            sTxBusMsg.senderAddr = MY_ADDR; 
            sTxBusMsg.type = eBusDevRespSetAddr;  
            sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr;
            p = &(spRxBusMsg->msg.devBus.x.devReq.setAddr.addr);
            eeprom_write_byte((uint8_t *)MODUL_ADDRESS, *p);
            BusSend(&sTxBusMsg);  
            break;
         case eBusDevReqEepromRead:
            sTxBusMsg.senderAddr = MY_ADDR; 
            sTxBusMsg.type = eBusDevRespEepromRead;
            sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr;
            sTxBusMsg.msg.devBus.x.devResp.readEeprom.data = 
               eeprom_read_byte((const uint8_t *)spRxBusMsg->msg.devBus.x.devReq.readEeprom.addr);
            BusSend(&sTxBusMsg);  
            break;
         case eBusDevReqEepromWrite:
            sTxBusMsg.senderAddr = MY_ADDR; 
            sTxBusMsg.type = eBusDevRespEepromWrite;
            sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr;
            p = &(spRxBusMsg->msg.devBus.x.devReq.writeEeprom.data);
            eeprom_write_byte((uint8_t *)spRxBusMsg->msg.devBus.x.devReq.readEeprom.addr, *p);
            BusSend(&sTxBusMsg);  
            break;
         case eBusDevReqDoClockCalib:
            if (spRxBusMsg->msg.devBus.x.devReq.doClockCalib.command == eBusDoClockCalibInit) {
                sCount = 0;
                sOsccal = 0;
                sMinDiff = 0xffff;
            } else if (sCount > MAX_CAL_TEL) {
                sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateError;
                sTxBusMsg.senderAddr = MY_ADDR; 
                sTxBusMsg.type = eBusDevRespDoClockCalib;
                sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr;
                BusSend(&sTxBusMsg);
                break;
            }

            flags = DISABLE_INT;

            BUS_TRANSCEIVER_POWER_UP;
            PORTB = 0;

            /* 8 bytes 0x00: 8 * (1 start bit + 8 data bits) + 7 stop bits  */
            seqLen = 8 * 1000000 * 9 / 9600 + 7 * 1000000 * 1 / 9600; /* = 8229 us */
            old_osccal = OSCCAL;

            ExitComm();
            InitTimer1();

            TCCR1B = (1 << ICES1) | TIMER1_PRESCALER;

            OSCCAL = sOsccal;
            NOP_10;
            
            for (i = 0; i < 8; i++) {
                startCnt = Synchronize();
                stopCnt = ClkMeasure();
                
                clocks = stopCnt - startCnt;
                if (clocks > seqLen) {
                    diff = clocks - seqLen;
                } else {
                    diff = seqLen - clocks;
                }
                if (diff < sMinDiff) {
                    sMinDiff = diff;
                    sMinOsccal = OSCCAL;
                }
                OSCCAL++;
                NOP_4;
            }

            BUS_TRANSCEIVER_POWER_DOWN;

            InitTimer1();
            InitComm();
            sOsccal = OSCCAL;
            OSCCAL = old_osccal;

            RESTORE_INT(flags);

            if (sCount < MAX_CAL_TEL) { 
                sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateContiune;
                sCount++;
            } else {
                sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateSuccess;
                /* save the osccal correction value to eeprom */
                osccal_corr = eeprom_read_byte((const uint8_t *)OSCCAL_CORR);
                osccal_corr += sMinOsccal - old_osccal;
                eeprom_write_byte((uint8_t *)OSCCAL_CORR, osccal_corr);
                OSCCAL = sMinOsccal;
                NOP_10;
            }
            sTxBusMsg.senderAddr = MY_ADDR; 
            sTxBusMsg.type = eBusDevRespDoClockCalib;
            sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr;
            BusSend(&sTxBusMsg);  
            break;
         default:
            break;
      }   
   }
}