default port = GPIO_0
Dependents: multithread_ADC_timing audio_record_serial_out
Diff: fastADC.cpp
- Revision:
- 0:56e4e76188d9
- Child:
- 1:d415e0b7dc3e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/fastADC.cpp Wed Dec 04 10:41:22 2019 +0000 @@ -0,0 +1,20 @@ +#include "fastADC.h" +#include "mbed.h" + +void initADC(){ + GPIOA->MODER |= (0x03 << 16); /* Configure PB0 as ADC.8 input */ + ADC1->CR2 &= 1 << 1; /* non continuous conversion */ + ADC1->SQR5 &= ~(0x1f); /* clear ADC_IN8 */ + ADC1->SQR5 |= 0x08; /* set ADC_IN8 */ + ADC1->CR2 &= ~(1 << 2); /* Make sure to use bank A */ + ADC1->CR2 &= ~(3 << 24); /* set resolution to 12 bits */ + ADC1->CR2 |= 1 << 0; /* turn on ADC */ + while(!(ADC1->SR & (1 << 6))); +} + +uint16_t readADC(){ + while(!(ADC1->SR & (1 << 6))); /* Wait for ADC to be available */ + ADC1->CR2 |= 1 << 30; /* Start new conversion */ + while(!(ADC1->SR & (1 << 1))); /* If conversion has finished */ + return (uint16_t) ADC1->DR & 0xFFFF; /* Read AD converted value */ +} \ No newline at end of file