default port = GPIO_0

Dependents:   multithread_ADC_timing audio_record_serial_out

Committer:
yutonggu
Date:
Sun Dec 08 05:26:48 2019 +0000
Revision:
1:d415e0b7dc3e
Parent:
0:56e4e76188d9
Set system to readADC at 20KHz

Who changed what in which revision?

UserRevisionLine numberNew 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 1:d415e0b7dc3e 13 while((ADC1->SR & (1 << 8)));
yutonggu 0:56e4e76188d9 14 }
yutonggu 0:56e4e76188d9 15
yutonggu 0:56e4e76188d9 16 uint16_t readADC(){
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 1:d415e0b7dc3e 19 while(!(ADC1->SR & (1 << 6)));
yutonggu 1:d415e0b7dc3e 20 while((ADC1->SR & (1 << 8)));
yutonggu 0:56e4e76188d9 21 return (uint16_t) ADC1->DR & 0xFFFF; /* Read AD converted value */
yutonggu 0:56e4e76188d9 22 }