#include #include "common.h" const word onecycle = 30;// 1/32768 word diff; byte compareDone; word distance; word cnt; #pragma vector = TIMER0_B0_VECTOR __interrupt void Timer_B (void) { __no_operation(); if(TB0IV==TB0IV_TBIFG) //Check if Timer1 overflow caused the interrupt { TB0CTL&=~(TBIFG); //Reset overflow flag } else //If Capture module caused the interrupt { diff=TB0CCR1; //Save the value of Timer1 at the time of first key press TB0CCTL1&=~(CCIFG); //Reset Capture Flag compareDone = 0x02; TB0CTL&=~(TBIFG); //Reset overflow flag } } /* ///////////////////////////////////////////////////////////////////////////////////////////////////////// /////// // Function : M A I N R O U T I N E // Programmer : musa melih çulha Start Date : // Modified by : yyyyyyyyy Last Edit : // Description : // History : // ///////////////////////////////////////////////////////////////////////////////////////////////////////// /////// */ void main(void) { word mesafe = 0; byte reset; word negEdge; word posEdge; WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer //Configuring Trigger pin as output P1.0 P1DIR |= BIT2; P1OUT &= ~BIT2; //Configuring echo pin as input P1.7 P2DIR &= ~BIT0; P2SEL0 |= BIT0; //1 P2SEL1 &= ~BIT0;//0 P2OUT &= ~BIT0; // Set output to low __delay_cycles(18000); // low for 18 ms // P2REN |= BIT0; // SYSCFG3 = 0x02; PM5CTL0 &= ~LOCKLPM5; SYSCFG3 = 0x02; P1DIR |= BIT7; // P1.0 and P1.6 pins output the rest are input TB0CCTL0 = CCIE; // CCR0 interrupt enabled TB0CCR0 = 10500; // for 10ms // timer freq = (1/Xtal)*TA0CCR0 second // TB0CTL = TBSSEL_2 | MC_1 | TBCLR; // SMCLK, upmode, clear TAR TB0CCTL1 |= CM_2+CCIS__CCIA+SCS+CAP+CCIE; //Setup Timer1 with synchronous capture on both edges, connected to Vcc //and generate interrupt on capture TB0CTL |= TBSSEL_2+MC_1+TBIE; //Setup Timer1 to run on SMCLK, UP mode and generate interrupt on overflow diff = 0; compareDone = 0; distance = 0; cnt = 0; __enable_interrupt(); //Enable maskable interrupts __bis_SR_register( GIE); //Trigger pin high for more than 20us P1OUT |= BIT2; __delay_cycles(20); P1OUT &= ~BIT2; __no_operation(); for(;;) { P1OUT |= BIT2; __delay_cycles(20); P1OUT &= ~BIT2; __enable_interrupt(); //Enable maskable interrupts __bis_SR_register( GIE); __delay_cycles(100000); __no_operation(); } while(1) { //Perform calculation to find distance in cm. __no_operation(); if(compareDone == 0x02) { __disable_interrupt(); TB0CTL = TBCLR; diff = negEdge - posEdge; distance = (onecycle * diff) / 58; mesafe = distance; //After calculation restart the operation compareDone = 0x00; __enable_interrupt(); P1OUT |= BIT2; __delay_cycles(20); P1OUT &= ~BIT2; TB0CTL = TBSSEL__ACLK | MC__CONTINUOUS; reset = 0; } __delay_cycles(800000); reset ++; //If calculation fails restart the operation again if(reset == 10) { __disable_interrupt(); TB0CTL = TBCLR; compareDone = 0x00; __enable_interrupt(); P1OUT |= BIT2; __delay_cycles(20); P1OUT &= ~BIT2; TB0CTL = TBSSEL__ACLK | MC__CONTINUOUS; reset = 0x00; } } }