uint16_t analogRead(uint8_t pin) { uint16_t channel,val; uint16_t pinNum = digitalPinToPinNum(pin); switch(pinNum) { case PIN_57: {channel = ADC_CH_0;}break; case PIN_58: {channel = ADC_CH_1;}break; case PIN_59: {channel = ADC_CH_2;}break; case PIN_60: {channel = ADC_CH_3;}break; default: return 0; } while(ADCFIFOLvlGet(ADC_BASE, channel)) { // flush the channel's FIFO if not empty ADCFIFORead(ADC_BASE, channel); } PinTypeADC(pinNum,0xFF); ADCChannelEnable(ADC_BASE, channel); ADCTimerConfig(ADC_BASE,2^17); ADCTimerEnable(ADC_BASE); ADCEnable(ADC_BASE); while(!ADCFIFOLvlGet(ADC_BASE, channel)); val = ADCFIFORead(ADC_BASE, channel) & 0x3FFF; ADCDisable(ADC_BASE); ADCChannelDisable(ADC_BASE, channel); ADCTimerDisable(ADC_BASE); val = val >> 2; return val; }
/* ===================================================================*/ void ADCInit(void) { ADCEnable(); /* Clear ~CS, select ADC */ DelaySomeMs(1); ADCUseInternalClock(); /* Set CLKSEL, ADC uses internal clock */ DelaySomeMs(1); ADCStopConvertByHardware(); /* Clear START, ADC does not convert */ DelaySomeMs(1); ADCPowerUp(); /* Set ~PWDN */ DelaySomeMs(1); // ADCDirectConnect(); /* Use direct connect mode */ ADCResetByHardware(); /* Reset ADC when power on */ DelaySomeMs(1); ADCConfigure(); /* Configure the register of ADC. */ }
void initialize( void ) { CPU_PRESCALE(0); USART_Init(BAUD_RATE); USART_Transmit('\f'); // Send form feed to clear the terminal. USART_SendString("WunderBoard initializing...\r\n"); USART_SendString("\tSetting ADC prescaler and disabling free running mode...\r\n"); SetupADC(ADC_PRESCALER_32, FALSE); USART_SendString("\tEnabling ADC...\r\n"); ADCEnable(); USART_SendString("\tSetting ADC reference to Vcc...\r\n"); ADCSetReference(ADC_REF_VCC); // Configure IO // USART_SendString("\tConfiguring IO...\r\n"); //DDRx corresponds to PORTx/PINx, dependng on direction of data flow -- PORT for output, PIN for input DDRA = 0x00; // Buttons and switches DDRB = 0xE7; // Red enable, green enable and audio out DDRC = 0xff; // Discrete LEDs DDRE = 0x47; // LED Column DDRF = 0x00; // Accelerometer // Disable pullups and set outputs low // PORTA = 0x00; PORTB = 0x01; PORTC = 0x81; PORTE = 0x00; PORTF = 0x00; //Set OC1A to toggle TCCR1A = 0b01000000; // Clk/64 and CTC mode TCCR1B = 0b00001011; OCR1A = 24; USART_SendString("\tSetting SPI\r\n"); //Set the SPI bus appropriately to use the LED array SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); }