Joel Pallent
/
Elec241-adc
c
ADC.c@0:7088bc4c9949, 2018-05-31 (annotated)
- Committer:
- Joelpallent
- Date:
- Thu May 31 19:30:01 2018 +0000
- Revision:
- 0:7088bc4c9949
c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Joelpallent | 0:7088bc4c9949 | 1 | #include "ADC.h" |
Joelpallent | 0:7088bc4c9949 | 2 | |
Joelpallent | 0:7088bc4c9949 | 3 | |
Joelpallent | 0:7088bc4c9949 | 4 | |
Joelpallent | 0:7088bc4c9949 | 5 | void init_ADC(void) |
Joelpallent | 0:7088bc4c9949 | 6 | { |
Joelpallent | 0:7088bc4c9949 | 7 | RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN; //GPIOC clock enable |
Joelpallent | 0:7088bc4c9949 | 8 | ADC_input_port->MODER|=(3u<<(2*ADC_input_pin)); //ADC input pin is analogue mode |
Joelpallent | 0:7088bc4c9949 | 9 | |
Joelpallent | 0:7088bc4c9949 | 10 | RCC->APB2ENR|=RCC_APB2ENR_ADC1EN; //ADC clock enable |
Joelpallent | 0:7088bc4c9949 | 11 | ADC1->SQR1&=~ADC_SQR1_L; //set number of conversions per sequence to 1 |
Joelpallent | 0:7088bc4c9949 | 12 | ADC1->SQR3&=~ADC_SQR3_SQ1; //clear channel select bits |
Joelpallent | 0:7088bc4c9949 | 13 | ADC1->SQR3|=ADC_Channel; //set channel |
Joelpallent | 0:7088bc4c9949 | 14 | ADC1->CR2|=ADC_CR2_ADON; //enable ADC |
Joelpallent | 0:7088bc4c9949 | 15 | |
Joelpallent | 0:7088bc4c9949 | 16 | } |
Joelpallent | 0:7088bc4c9949 | 17 | |
Joelpallent | 0:7088bc4c9949 | 18 | |
Joelpallent | 0:7088bc4c9949 | 19 | unsigned short read_adc(void) |
Joelpallent | 0:7088bc4c9949 | 20 | { |
Joelpallent | 0:7088bc4c9949 | 21 | ADC1->CR2|=ADC_CR2_SWSTART; //start ADC conversion |
Joelpallent | 0:7088bc4c9949 | 22 | while((ADC1->SR&ADC_SR_EOC)==0); //wait for ADC conversion complete |
Joelpallent | 0:7088bc4c9949 | 23 | return ADC1->DR; //return converted value |
Joelpallent | 0:7088bc4c9949 | 24 | } |