/* p7_1.c: A to D conversion of channel 1 * This program converts the analog input from channel 1. * Channel 1 is connected to input from PA1. * Clock prescaler is left at 0 (divided by 2) and Sampling * time is also left at default of 3 cycles. * Software trigger is used. The bit 8 of the conversion * result is used to turn on/off the LD2. For the full * scale of the reference voltage, the LD2 should be on * for 8 times. * * This program was tested with Keil uVision v5.23 with DFP v2.0.0. */ #include "stm32f0xx.h" int main(void) { int result; /* set up pin PA5 for LED */ RCC->AHBENR |= 0x00020000; /* enable GPIOA clock */ GPIOA->MODER &= ~0x00000C00; /* clear pin mode */ GPIOA->MODER |= 0x00000400; /* set pin to output mode */ /* set up pin PA1 for analog input */ GPIOA->MODER |= 0xC; /* PA1 analog */ /* setup ADC1 */ RCC->APB2ENR |= 0x00000200; /* enable ADC1 clock */ ADC1->CR = 0; /* SW trigger */ ADC1->CFGR1 = 0; ADC1->CFGR2 = 0; ADC1->CHSELR = 1 << 1; ADC1->CR |= 1; /* enable ADC1 */ while (1) { ADC1->CR |= 0x00000004; /* start a conversion */ while (!(ADC1->ISR & 0x00000002)) { } /* wait for conv complete */ result = ADC1->DR; /* read conversion result */ if (result & 0x100) GPIOA->BSRR = 0x00000020; /* turn on LED */ else GPIOA->BSRR = 0x00200000; /* turn off LED */ } }