void buzzer_play(note *notes) { /* Allow buzzer PWM output on P2.7 */ P2SEL |= BIT7; /* 0x000F is the "stop bit" */ while (PITCH(*notes) != 0x000F) { if (PITCH(*notes) == 0) { /* Stop the timer! We are playing a rest */ TA1CTL &= ~MC_3; } else { /* Set PWM frequency */ TA1CCR0 = base_notes[PITCH(*notes)] >> OCTAVE(*notes); /* Start the timer */ TA1CTL |= MC__UP; } /* Delay for DURATION(*notes) milliseconds, use LPM1 because we need SMCLK for tone generation */ timer0_delay(DURATION(*notes), LPM1_bits); /* Advance to the next note */ notes++; } /* Stop buzzer */ buzzer_stop(); }
// ************************************************************************************************* // @fn adc12_single_conversion // @brief Init ADC12. Do single conversion. Turn off ADC12. // @param none // @return none // ************************************************************************************************* uint16_t adc12_single_conversion(uint16_t ref, uint16_t sht, uint16_t channel) { // Initialize the shared reference module REFCTL0 |= REFMSTR + ref + REFON; // Enable internal reference (1.5V or 2.5V) // Initialize ADC12_A ADC12CTL0 = sht + ADC12ON; // Set sample time ADC12CTL1 = ADC12SHP; // Enable sample timer ADC12MCTL0 = ADC12SREF_1 + channel; // ADC input channel ADC12IE = 0x001; // ADC_IFG upon conv result-ADCMEMO // Wait 2 ticks (66us) to allow internal reference to settle timer0_delay(66,LPM3_bits); // Start ADC12 ADC12CTL0 |= ADC12ENC; // Clear data ready flag adc12_data_ready = 0; // Sampling and conversion start ADC12CTL0 |= ADC12SC; // Wait until ADC12 has finished timer0_delay(170, LPM3_bits); uint8_t loops = 0; //We were going away and the watchdog was tripping - this should reduce the instances of that. while (!adc12_data_ready && loops++ < 30) { timer0_delay(66, LPM3_bits); } // Shut down ADC12 ADC12CTL0 &= ~(ADC12ENC | ADC12SC | sht); ADC12CTL0 &= ~ADC12ON; // Shut down reference voltage REFCTL0 &= ~(REFMSTR + ref + REFON); ADC12IE = 0; // Return ADC result return (adc12_result); }
void as_start(uint8_t mode) { /* Initialize SPI interface to acceleration sensor */ AS_SPI_CTL0 |= UCSYNC | UCMST | UCMSB /* SPI master, 8 data bits, MSB first, */ | UCCKPH; /* clock idle low, data output on falling edge */ AS_SPI_CTL1 |= UCSSEL1; /* SMCLK as clock source */ AS_SPI_BR0 = AS_BR_DIVIDER; /* Low byte of division factor for baud rate */ AS_SPI_BR1 = 0x00; /* High byte of division factor for baud rate */ AS_SPI_CTL1 &= ~UCSWRST; /* Start SPI hardware */ /* Initialize interrupt pin for data read out from acceleration sensor */ AS_INT_IES &= ~AS_INT_PIN; /* Interrupt on rising edge */ #ifdef AS_DISCONNECT /* Enable interrupt */ AS_INT_DIR &= ~AS_INT_PIN; /* Switch INT pin to input */ AS_SPI_DIR &= ~AS_SDI_PIN; /* Switch SDI pin to input */ AS_SPI_REN |= AS_SDI_PIN; /* Pulldown on SDI pin */ AS_SPI_SEL |= AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN; /* Port pins to SDO, SDI and SCK function */ AS_CSN_OUT |= AS_CSN_PIN; /* Deselect acceleration sensor */ AS_PWR_OUT |= AS_PWR_PIN; /* Power on active high */ #endif /* Delay of >5ms required between switching on power and configuring sensor */ timer0_delay(10, LPM3_bits); /* Initialize interrupt pin for data read out from acceleration sensor */ AS_INT_IFG &= ~AS_INT_PIN; /* Reset flag */ AS_INT_IE |= AS_INT_PIN; /* Enable interrupt */ /* Reset sensor */ as_write_register(0x04, 0x02); as_write_register(0x04, 0x0A); as_write_register(0x04, 0x04); /* Wait 5 ms before starting sensor output */ timer0_delay(5, LPM3_bits); /* then select modality */ change_mode(mode); }
// delay operations until the master is ready. void init_delay(void) { timer0_delay(100000); }
// ************************************************************************************************* // @fn as_start // @brief Power-up and initialize acceleration sensor // @param none // @return none // ************************************************************************************************* void as_start(uint8_t mode) { volatile uint16_t Counter_u16; uint8_t bConfig, bStatus; // Initialize SPI interface to acceleration sensor AS_SPI_CTL0 |= UCSYNC | UCMST | UCMSB // SPI master, 8 data bits, MSB first, | UCCKPH; // clock idle low, data output on falling edge AS_SPI_CTL1 |= UCSSEL1; // SMCLK as clock source AS_SPI_BR0 = AS_BR_DIVIDER; // Low byte of division factor for baud rate AS_SPI_BR1 = 0x00; // High byte of division factor for baud rate AS_SPI_CTL1 &= ~UCSWRST; // Start SPI hardware // Initialize interrupt pin for data read out from acceleration sensor AS_INT_IES &= ~AS_INT_PIN; // Interrupt on rising edge #ifdef AS_DISCONNECT // Enable interrupt AS_INT_DIR &= ~AS_INT_PIN; // Switch INT pin to input AS_SPI_DIR &= ~AS_SDI_PIN; // Switch SDI pin to input AS_SPI_REN |= AS_SDI_PIN; // Pulldown on SDI pin AS_SPI_SEL |= AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN; // Port pins to SDO, SDI and SCK function AS_CSN_OUT |= AS_CSN_PIN; // Deselect acceleration sensor AS_PWR_OUT |= AS_PWR_PIN; // Power on active high #endif // Delay of >5ms required between switching on power and configuring sensor timer0_delay(10); // Initialize interrupt pin for data read out from acceleration sensor AS_INT_IFG &= ~AS_INT_PIN; // Reset flag AS_INT_IE |= AS_INT_PIN; // Enable interrupt // Reset sensor as_write_register(0x04, 0x02); as_write_register(0x04, 0x0A); as_write_register(0x04, 0x04); // Wait 5 ms before starting sensor output timer0_delay(5); // Configure sensor and start to sample data switch (mode) { case FALL_MODE: if (as_config.range == 2) { bConfig = 0x80; if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x0A; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x0C; } else if (as_config.range == 8) { bConfig = 0x00; if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x0A; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x0C; } // fall time as long as possible 150 msec at 100 Hz write_FFTMR(as_config.MDFFTMR); //threshold for computation write_FFTHR(as_config.FFTHR); break; case MEASUREMENT_MODE: // Configure sensor and start to sample data if (as_config.range == 2) { bConfig = 0x80; if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x02; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x04; } else if (as_config.range == 8) { bConfig = 0x00; if (as_config.sampling == SAMPLING_40_HZ) bConfig |= 0x06; else if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x02; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x04; } break; case ACTIVITY_MODE: // Configure sensor and start to sample data if (as_config.range == 2) { bConfig = 0x80; if (as_config.sampling == SAMPLING_10_HZ) bConfig |= 0x08; } else if (as_config.range == 8) { bConfig = 0x00; if (as_config.sampling == SAMPLING_10_HZ) bConfig |= 0x08; } bConfig |= MDET_EXIT<<5; // fall time as long as possible 150 msec at 100 Hz write_MDTMR(as_config.MDFFTMR); //check if lower than 571 mgrav //write_MDTHR(sAccel.MDTHR); write_MDTHR(as_config.MDTHR); break; default: bConfig = 0x80; break; } // Wait 2 ms before entering modality to settle down timer0_delay(2); //write the configuration as_write_register(ADDR_CTRL, bConfig); // Wait 2 ms before entering modality to settle down timer0_delay(2); }
void change_mode(uint8_t mode) { uint8_t bConfig = 0x00; /* Configure sensor and start to sample data */ switch (mode) { case FALL_MODE: if (as_config.range == 2) { bConfig = 0x80; if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x0A; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x0C; } else if (as_config.range == 8) { bConfig = 0x00; if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x0A; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x0C; } /* fall time as long as possible 150 msec at 100 Hz */ write_FFTMR(as_config.MDFFTMR); /* threshold for computation */ write_FFTHR(as_config.FFTHR); break; case MEASUREMENT_MODE: /* Configure sensor and start to sample data */ if (as_config.range == 2) { bConfig = 0x80; if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x02; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x04; } else if (as_config.range == 8) { bConfig = 0x00; if (as_config.sampling == SAMPLING_40_HZ) bConfig |= 0x06; else if (as_config.sampling == SAMPLING_100_HZ) bConfig |= 0x02; else if (as_config.sampling == SAMPLING_400_HZ) bConfig |= 0x04; } break; case ACTIVITY_MODE: /* Configure sensor and start to sample data */ if (as_config.range == 2) { bConfig = 0x80; if (as_config.sampling == SAMPLING_10_HZ) bConfig |= 0x08; } else if (as_config.range == 8) { bConfig = 0x00; if (as_config.sampling == SAMPLING_10_HZ) bConfig |= 0x08; } bConfig |= MDET_EXIT << 5; /* fall time as long as possible 150 msec at 100 Hz */ write_MDTMR(as_config.MDFFTMR); /* check if lower than 571 mgrav */ /* write_MDTHR(sAccel.MDTHR); */ write_MDTHR(as_config.MDTHR); break; default: bConfig = 0x80; break; } /* Wait 2 ms before entering modality to settle down */ timer0_delay(2, LPM3_bits); /* write the configuration */ as_write_register(ADDR_CTRL, bConfig); /* Wait 2 ms before entering modality to settle down */ timer0_delay(2, LPM3_bits); }