You are on page 1of 5

#include <msp430.

h>

/*
* main.c
*/

unsigned tempo_inicial=0, tempo=0;


float medida=0.0;
float tempo_real=0.0;
unsigned char x=0;
void config_P1(void);
void config_clock(void);
void config_P2(void);

int main(void) {
WDTCTL = WDTPW | WDTHOLD;

// Stop watchdog timer

IE1 |= WDTIE;
config_P1();
config_clock();
config_P2();

TA0CCR0=59999;
TA0CCTL1= OUTMOD_7;// RESET SET
TA0CTL= TASSEL1 + ID0 + MC0;// SMCLK/2 = 1MHZ ///MC0 CONTA AT TA0CCR0
TA0CCR1=9;

TA1CTL= TASSEL1 + ID0 + MC1;//conta at 65535


TA1CCTL0= CM0 + CAP+ CCIE;

_BIS_SR(GIE);

while(1);
}

void config_clock(void)
{
WDTCTL = WDTPW | WDTHOLD;

// Stop watchdog timer

WDTCTL = WDTPW | WDTHOLD;


DCOCTL = CALDCO_16MHZ;
BCSCTL1 = CALBC1_16MHZ;
BCSCTL2 = DIVS0 + DIVS1;//2mhz = SMCLK
BCSCTL3 = XCAP0 + XCAP1;
while(BCSCTL3 & LFXT1OF); // sai do loop quando oscilador LFXT1 estiver
estavel
__enable_interrupt();// seta o bit GIE - permite geracao de interrupcoes

void config_P1(void)
{

P1DIR|=BIT6 + BIT0;
P1OUT &=~BIT0;

P1SEL|= BIT6;// PWM P1.6


P1SEL &=~BIT3;//SELECIONA BUTTON P1.3
P1DIR &=~BIT3;// ENTRADA
P1REN |=BIT3;
P1OUT=BIT3;//pull up
P1IE|=BIT3;//HABILITA INTERRUPAO;
P1IES |=BIT3; //BORDA DE DESCIDA;
P1IFG &=~BIT3;//DESABILITA FLAG;

void config_P2(void){

P2REN =BIT0;
P2OUT =BIT0;
P2DIR &=~BIT0;
P2SEL= BIT0;//ENTRADA ECHO

#pragma vector=PORT1_VECTOR
__interrupt void P1_RTI(void){

P1IFG =0;//&=~(BIT0+BIT1+BIT2+BIT3);//Limpa a flag para nao gerar interrupcoes


consecutivas
P1IE &= ~(BIT0+BIT1+BIT2+BIT3);//Desabilita interrupcoes
WDTCTL = WDTPW + WDTTMSEL + WDTCNTCL+ WDTIS1;//Interrup. a cada ~
4ms

#pragma vector=WDT_VECTOR
__interrupt void WDT_RTI(void){

WDTCTL = WDTPW + WDTHOLD; // Para o WDT

if((~P1IN) & BIT3)


{
if(TA0CCR1==0){

TA0CCR1=10;
}
}
P1IFG&=~BIT3;
P1IE = BIT3;
}

#pragma vector=TIMER1_A0_VECTOR
__interrupt void Timer1(void){

if(x==0){

x=1;

tempo_inicial = TA1CCR0;//TEMPO NA BORDA DE SUBIDA


TA1CCTL0= CM1 + CAP + CCIE;// BORDA DE DESCIDA AGORA

}else{

x=0;
if((TA1CCR0 - tempo_inicial) > 0){

tempo = TA1CCR0 - tempo_inicial;

}else{
tempo=(65535 - tempo_inicial)+ TA1CCR0;

}
tempo_real = 0.000001*tempo;//tempo em segundo do sinal em alta

medida = 17000*tempo_real;//17000 j sai em centimetros;


TA1CCTL0 = CM0+ CAP + CCIE;

if(medida>=10 & medida<=20){

P1DIR|=BIT0;
P1OUT|=BIT0;
}else{

P1OUT^=BIT0;
}
}