예제 #1
0
int main(void)
{
  volatile unsigned int i;
  //initalizations
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
  P2DIR = 0x00;                         //input port pin 2.0 -> ADC; Pin 2.1 & Pin 2.2 ->select waveform
  P3OUT = 0x40;                             // Set slave reset
  P3DIR |= 0x40;                            //
  P3SEL |= 0x31;                            // P3.0,4,5 USCI_A0 option select
  UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;  // 3-pin, 8-bit SPI master
  UCA0CTL1 |= UCSSEL_3;                     // SMCLK
  UCA0BR0 |= 0x02;                          // /2
  UCA0BR1 = 0;                              //
  UCA0MCTL = 0;                             // No modulation
  UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  P3OUT &= ~0x40;                           // Now with SPI signals initialized,
  P3OUT |= 0x40;                            // reset slave
  ADC_init();

  int count = 0;
  volatile int freq = get_ADC_value();
  while(1)
  {
      if (count == 24)
      {
         freq = get_ADC_value();
         count = 0;
      }
      count++;

      switch(get_waveform_type())
      {
      case SINE:
          output_waveform(freq,sin);
          break;
      case SQUARE:
          output_waveform(freq,square);
          break;
      case TRAINGLE:
          output_waveform(freq,traingle);
          break;
      case SAWTOOTH:
          output_waveform(freq,sawtooth);
          break;
      }


  }
}
예제 #2
0
/**
* \brief Get temperature value from ADC
*
* \return the Celsius temperature
*/
int16_t get_temperature(void) {

/** not used at present   */
#if 0		
	uint8_t	i;
	long ADC_value;
	float Vadc=0.0;
	float degC=0.0;
	adc_enable(&g_adc_inst);
	for(i=0; i<10; i++)get_ADC_value();
	ADC_value=0;
	for(i=0; i<EPD_ADCSampleCount; i++) {
		ADC_value+=get_ADC_value();
	}
	ADC_value=ADC_value/EPD_ADCSampleCount;
	adc_disable(&g_adc_inst);
	Vadc=(EPD_ADCRefVcc/EPD_ADCres)*ADC_value*EPD_TempeScaled;
	degC=(100.0f+EPD_DegCOffset)-(float)(((Vadc-1.199f)*1000.0f)/10.77f);
	return   (int16_t)degC;
	
	/** Configure the PMC to enable the Timer/Counter (TC) module. */
	sysclk_enable_peripheral_clock(&ADC);
	

	uint32_t F_CPU_VAL;
       F_CPU_VAL = F_CPU;
       float temp_result, temp_result_cal;
       volatile float offset;
       double result , result_cal;
	   
	   #define NUM_SAMPLES 1
#if NUM_SAMPLES > 255
       uint16_t i;
#else
       uint8_t i;
#endif
       uint16_t adc_value;

       uint32_t temp_result32 = 0;
       int32_t offset32 = 0;

       ADCSRA = (1 << ADEN); /** Enable ADC */

       /**
       * Analog channel and gain selection
       * The MUX5 bit has to written first followed by a
       * write access to the MUX4:0 bits which triggers the update of the
       *internal buffer.
       */
       ADCSRB = (1 << MUX5);

       /**
       * Select internal 1.6V reference voltage
       * Select temperature sensor
       */
       ADMUX = (1 << REFS1) | (1 << REFS0) | (1 << MUX3) | (1 << MUX0);

       /** Dummy conversion to clear PGA */
       if ((F_CPU_VAL == 16000000UL) || (F_CPU_VAL == 15384600UL)) {
             ADCSRA
                    = (1 <<
                           ADEN) |
                           (1 <<
                           ADSC) |
                           (1 << ADIF) | (1 << ADPS2) | (1 << ADPS0);
       } else if (F_CPU_VAL == 8000000UL) {
             ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIF) | (1 << ADPS2);
       } else if (F_CPU_VAL == 4000000UL) {
             ADCSRA
                    = (1 <<
                           ADEN) |
                           (1 <<
                           ADSC) |
                           (1 << ADIF) | (1 << ADPS1) | (1 << ADPS0);
       } else {
       }

       /** Wait for conversion to be completed */
       do {
       } while ((ADCSRA & (1 << ADIF)) == 0x00);

       /** Sample */
       for (i = 0; i < NUM_SAMPLES; i++) {
             ADCSRA |= (1 << ADEN) | (1 << ADSC) | (1 << ADIF);

             /**
             * ADC Control and Status Register A:
             * ADC Enable
             * ADC Start Conversion
             * Clear ADIF
             * Prescaler = 32 (500kHz) for 16 MHz main clock
             */

             /** Wait for conversion to be completed */
             do {
             } while ((ADCSRA & (1 << ADIF)) == 0x00);

             adc_value = ADC;

             /** Averaging */
             temp_result32 += adc_value;
       }
       temp_result = (float)temp_result32 / NUM_SAMPLES;

       ADCSRA ^= (1 << ADEN); /** Disable ADC for channel change */

       /** Get offset value */
       ADCSRB = 0x00;
       ADMUX = (1 << REFS1) | (1 << REFS0) | (1 << MUX4) | (1 << MUX0);

       for (i = 0; i < NUM_SAMPLES; i++) {
             ADCSRA |= (1 << ADEN) | (1 << ADSC) | (1 << ADIF);

             /** Wait for conversion to be completed */
             do {
             } while ((ADCSRA & (1 << ADIF)) == 0x00);

             adc_value = ADC;

             /** Averaging */
             if (adc_value > 0x1FF) {
                    offset32 -= 0x400 - adc_value;
             } else {
                    offset32 += adc_value;
             }
       }
       offset = (float)offset32 / NUM_SAMPLES;

       ADCSRA &= ~(1 << ADEN); /** Disable ADC */
       
       temp_result -= offset;	     
       result = ((double)temp_result * 1.13) - 272.8;
       
       F_CPU_VAL = F_CPU_VAL; /** Ignore Warnings */
	   
	   /** Configure the PMC to enable the Timer/Counter (TC) module. */
	   sysclk_disable_peripheral_clock(&ADC);
       return (int16_t)result;
	   
#endif
	   
	return   (int16_t)25;
}