Sie sind auf Seite 1von 4

#include <msp430x54x.

h>

//renaming some pins and bits to something meaningful #define AUDIO_PORT_DIR P6DIR #define AUDIO_PORT_OUT P6OUT #define AUDIO_PORT_SEL P6SEL #define AUDIO_OUT_PWR_PIN BIT6 #define MIC_POWER_PIN BIT4 #define MIC_INPUT_PIN BIT5 #define MIC_INPUT_CHAN ADC12INCH_5 #define Num_of_Samples 100 unsigned short set[Num_of_Samples]; unsigned short index = 0; short flag = 0; //flag corresponding to status of the switch (S1 or S2) void ADC_init(void){ //activating the mic //mic power AUDIO_PORT_OUT = AUDIO_OUT_PWR_PIN ; P6DIR |= MIC_POWER_PIN; AUDIO_PORT_OUT |= MIC_POWER_PIN; //mic signal AUDIO_PORT_OUT &= ~MIC_INPUT_PIN; AUDIO_PORT_SEL |= MIC_INPUT_PIN;

//Configure ADC... //ADC12SHT12 selects 16 cyclye of ADCCLK for t_sample //ADC12ON turns ADC on ADC12CTL0 = ADC12ON + ADC12SHT12; //AD12SHP selects the sampling clock to be from the timer. (ADC12SC bit) //ADC12CONSEQ_2: repeated single channel sampling //ADC12SSEL_2 selects the ADCCLK to be drwn from MCLK //ADC12SHS_3 : TimerB to SHI ADC12CTL1 = ADC12SHP + ADC12CONSEQ_2 + ADC12SSEL_2 + ADC12SHS_3; // Select 8-bit resolution 8 bits/sample //Sequence of channels, once //Connect the mic channel to ADC12 channel 0 //Modifiable only when ADC12ENC = 0 //set ADC12MEM0 to rad the mic input channel

//set ADC12 channel0 interrupt enable .. //Enable ADC ADC12CTL0 |= ADC12ENC;

//Configure TimerB to drive the ADC // TimerB trigers the ADC sampling by generating a PWM signal // the SMCLK runs at 16 MHz the PWM is 8192 cyclyes ==> 2048 samples/sec = sampling rate TBCTL = TBSSEL_2; // Use SMCLK as Timer_B source TBR = 0; TBCCR0 = 8191; // Initialize TBCCR0 TBCCR1= 8191 - 100; TBCCTL1 = OUTMOD_7; //the above five lines set the sampling rate to 2000 samples/sec, please do the necessary modifications to set the sampling rate to 8000 samples/sec.

__delay_cycles(200000); }

void AdcStartRead(void) { //stop TimerB

ADC12IFG &= ~(MIC_INPUT_PIN); //clear IFG ADC12IE |= MIC_INPUT_PIN; //Enable interrupt //enable } //sets system clock to 16MHz void setSystemClock() { // Set up XT1 Pins to analog function, and to lowest drive P7SEL |= 0x03; UCSCTL6 |= XCAP_3 ; // Set internal cap values while(SFRIFG1 & OFIFG) { // Check OFIFG fault flag while ( (SFRIFG1 & OFIFG)) // Check OFIFG fault flag { // Clear OSC fault flags UCSCTL7 &= ~(DCOFFG + XT1LFOFFG + XT1HFOFFG + XT2OFFG); SFRIFG1 &= ~OFIFG; // Clear OFIFG fault flag } UCSCTL6 &= ~(XT1DRIVE1_L+XT1DRIVE0); // Reduce the drive strength } // Disable the FLL control loop __bis_SR_register(SCG0); // Set lowest possible DCOx, MODx UCSCTL0 = 0x00; UCSCTL1 = DCORSEL_5; // Select suitable range ADC12

UCSCTL2 = 488 + FLLD_1;

// Set DCO Multiplier | SELM__DCOCLKDIV ;

UCSCTL4 = SELA__XT1CLK | SELS__DCOCLKDIV __bic_SR_register(SCG0);

// Enable the FLL control loop

// Loop until XT1,XT2 & DCO fault flag is cleared do { // Clear XT2,XT1,DCO fault flags UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG); // Clear fault flags SFRIFG1 &= ~OFIFG; }while (SFRIFG1&OFIFG); // Test oscillator fault flag // // Worst-case settling time for the DCO when the DCO range bits have been // changed is n x 32 x 32 x f_FLL_reference. See UCS chapter in 5xx UG // for optimization. // 32 x 32 x / f_FLL_reference (32,768 Hz) = .03125 = t_DCO_settle // t_DCO_settle / (1 / 25 MHz) = 781250 = counts_DCO_settle __delay_cycles(781250); }

void main(void) { int sum = 0; int avg; unsigned short i = 0; // Stop watchdog timer setSystemClock(); ADC_init(); P1DIR |= 0x03; P1OUT = 0x00; P2OUT |= 0x80; P2DIR &= ~0x80; P2REN |= 0x80; P2SEL &= ~0x80; P2IE = 0x80; P2IES &= ~0x80; P2IFG &=~0x80; //enable general interrupts __bis_SR_register(GIE); while(1){ if(flag){ //start ADC do{ sum=0;

for(i = 0; i < 100; i++){ if(set[i] < 127){ set[i] = 254 - set[i]; } sum = set[i] + sum; } avg = sum/100; if( avg > 200 ){ P1OUT = 0x03; } else if( avg > 140 ){ P1OUT = 0x01; } else{ P1OUT = 0x00; } }while(flag); } } } #pragma vector=ADC12_VECTOR __interrupt void ADC12_ISR(void) { set[index] = ADC12MEM0; index++; if(index == Num_of_Samples){ index =0; } //clear ADC interrupt flag } #pragma vector=PORT2_VECTOR __interrupt void Port_2(void){ if(flag){ TBCTL &= ~MC0; //Stop TimerB ADC12CTL0 &= flag = 0; }else{ flag =1; } //clear interrupt flag of Port2 } ~(ADC12ENC + ADC12SC); //stop ADC

Das könnte Ihnen auch gefallen