回覆列表
  • 1 # 一個殺豬的詩人

    while (1) { EA = 0; // Disable interrupts printf("\f"); for(i = 0; i < ANALOG_INPUTS; i++) { // The 10-bit ADC value is averaged across INT_DEC measurements. // The result is then stored in RESULT, and is right-justified // The measured voltage applied to the port pins is then: // // Vref (mV) // measurement (mV) = --------------- * Result (bits) // (2^10)-1 (bits) measurement = RESULT[i] * 2430 / 1023; printf("P0.%bu voltage: %4ld mV\n",PIN_TABLE[i],measurement); } EA = 1; // Re-enable interrupts Timer0_wait(20); // Wait before displaying new values }void Port_Init (void){ P0SKIP = 0xCF; // Skip all analog pins XBR0 = 0x01; // UART0 TX and RX pins enabled XBR1 = 0x40; // Enable crossbar and weak pull-ups P0MDOUT |= 0x10; // Enable TX0 as a push-pull output P0MDIN &= ~0xCF; // Set desired pins as analog inputs}void ADC0_Init (void){ ADC0CN = 0x02; // ADC0 disabled, normal tracking, // conversion triggered on TMR2 overflow REF0CN = 0x03; // Enable internal VREF AMX0P = PIN_TABLE[0]; // ADC0 initial positive input = P0.1 AMX0N = 0x11; // ADC0 negative input = GND // i.e., single ended mode ADC0CF = ((SYSCLK/3000000)-1)<<3; // Set SAR clock to 3MHz ADC0CF |= 0x00; // Right-justify results EIE1 |= 0x08; // Enable ADC0 EOC interrupt AD0EN = 1; // Enable ADC0}void ADC0_ISR (void) interrupt 10{ static unsigned int_dec = INT_DEC; // Integrate/decimate counter // A new result is posted when // int_dec is 0 // Integrate accumulator for the ADC samples from input pins static long accumulator[ANALOG_INPUTS] = 0x00000000; unsigned char i; // Loop counterAD0INT = 0; // Clear ADC conversion complete // overflowaccumulator[AMUX_INPUT] += ADC0; // Read the ADC value and add it to the // running total // Reset sample counter <int_dec> and <AMUX_INPUT> if the final input was // just read if(AMUX_INPUT == (ANALOG_INPUTS - 1)) { int_dec--; // Update decimation counter // when the last of the analog inputs // is sampled if (int_dec == 0) // If zero, then post the averaged { // results int_dec = INT_DEC; // Reset counter // Copy each averaged ADC0 value into the RESULT array for(i = 0; i < ANALOG_INPUTS; i++) { // Copy averaged values into RESULT RESULT[i] = accumulator[i] / int_dec; // Reset accumulators accumulator[i] = 0x00000000; } } AMUX_INPUT = 0; // Reset input index back to P0.1 } // Otherwise, increment the AMUX channel counter else { AMUX_INPUT++; // Step to the next analog mux input }}

  • 中秋節和大豐收的關聯?
  • 2011希望杯官網?