default port = GPIO_0
Dependents: multithread_ADC_timing audio_record_serial_out
fastADC.cpp@0:56e4e76188d9, 2019-12-04 (annotated)
- Committer:
- yutonggu
- Date:
- Wed Dec 04 10:41:22 2019 +0000
- Revision:
- 0:56e4e76188d9
- Child:
- 1:d415e0b7dc3e
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yutonggu | 0:56e4e76188d9 | 1 | #include "fastADC.h" |
yutonggu | 0:56e4e76188d9 | 2 | #include "mbed.h" |
yutonggu | 0:56e4e76188d9 | 3 | |
yutonggu | 0:56e4e76188d9 | 4 | void initADC(){ |
yutonggu | 0:56e4e76188d9 | 5 | GPIOA->MODER |= (0x03 << 16); /* Configure PB0 as ADC.8 input */ |
yutonggu | 0:56e4e76188d9 | 6 | ADC1->CR2 &= 1 << 1; /* non continuous conversion */ |
yutonggu | 0:56e4e76188d9 | 7 | ADC1->SQR5 &= ~(0x1f); /* clear ADC_IN8 */ |
yutonggu | 0:56e4e76188d9 | 8 | ADC1->SQR5 |= 0x08; /* set ADC_IN8 */ |
yutonggu | 0:56e4e76188d9 | 9 | ADC1->CR2 &= ~(1 << 2); /* Make sure to use bank A */ |
yutonggu | 0:56e4e76188d9 | 10 | ADC1->CR2 &= ~(3 << 24); /* set resolution to 12 bits */ |
yutonggu | 0:56e4e76188d9 | 11 | ADC1->CR2 |= 1 << 0; /* turn on ADC */ |
yutonggu | 0:56e4e76188d9 | 12 | while(!(ADC1->SR & (1 << 6))); |
yutonggu | 0:56e4e76188d9 | 13 | } |
yutonggu | 0:56e4e76188d9 | 14 | |
yutonggu | 0:56e4e76188d9 | 15 | uint16_t readADC(){ |
yutonggu | 0:56e4e76188d9 | 16 | while(!(ADC1->SR & (1 << 6))); /* Wait for ADC to be available */ |
yutonggu | 0:56e4e76188d9 | 17 | ADC1->CR2 |= 1 << 30; /* Start new conversion */ |
yutonggu | 0:56e4e76188d9 | 18 | while(!(ADC1->SR & (1 << 1))); /* If conversion has finished */ |
yutonggu | 0:56e4e76188d9 | 19 | return (uint16_t) ADC1->DR & 0xFFFF; /* Read AD converted value */ |
yutonggu | 0:56e4e76188d9 | 20 | } |