diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-02-08 19:25:29 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-02-08 19:25:29 +0100 |
commit | f875ab2a70afe023c8457cdd8814e146814d2b7e (patch) | |
tree | 39d894020f92f838c1098ad49d167b27ded29dbb | |
parent | e16e1fe32f61eac5f741d181facda712bf74cc69 (diff) |
worky analog
-rw-r--r-- | src/main.c | 57 |
1 files changed, 44 insertions, 13 deletions
@@ -24,7 +24,9 @@ const unsigned short leds[] = {PINOUT_LED_1, PINOUT_LED_2, PINOUT_LED_3, PINOUT_ */ void shieldConfig() { // enable clock for I/O ports A and B - RCC->AHBENR |= RCC_AHBENR_GPIOBEN; + RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; + // enable clock for the ADC peripheral + RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clear mode register configuration bits GPIOB->MODER &= ~((0b11 << (PINOUT_LED_1 * 2)) |\ @@ -32,6 +34,9 @@ void shieldConfig() { (0b11 << (PINOUT_LED_3 * 2)) |\ (0b11 << (PINOUT_LED_4 * 2)) |\ (0b11 << (PINOUT_BTN * 2))); + GPIOA->MODER &= ~((0b11 << (PINOUT_DISP_CLK * 2)) |\ + (0b11 << (PINOUT_DISP_DIO * 2)) |\ + (0b11 << (PINOUT_POT * 2))); // set output mode register configuration bits // 0b00 -> input mode (reset state) @@ -43,10 +48,23 @@ void shieldConfig() { (0b01 << (PINOUT_LED_3 * 2)) |\ (0b01 << (PINOUT_LED_4 * 2)) |\ (0b00 << (PINOUT_BTN * 2)); + GPIOA->MODER |= (0b01 << (PINOUT_DISP_CLK * 2)) |\ + (0b01 << (PINOUT_DISP_DIO * 2)) |\ + (0b11 << (PINOUT_POT * 2)); // pull-up resistor for button GPIOB->PUPDR &= ~(0b11 << (PINOUT_BTN * 2)); GPIOB->PUPDR |= (0b01 << (PINOUT_BTN * 2)); + + // calirate ADC1 + ADC1->CR |= ADC_CR_ADCAL; + while ((ADC1->CR & ADC_CR_ADCAL)); + + // enable ADC V_REFINT + ADC->CCR |= ADC_CCR_VREFEN; + + // enable ADC1 + ADC1->CR |= ADC_CR_ADEN; } /* @@ -68,13 +86,21 @@ int buttonRead() { return (GPIOB->IDR & (1 << PINOUT_BTN)) > 0; } -void delay() -{ - int i; - for (i = 0; i < 50000; i++) - { - asm("nop"); - } +/* + * Read potmeter value as analog value between 0 and 2^12 - 1 + * TODO: increase measurement sensitivity + */ +unsigned int potRead() { + ADC1->CHSELR = (1 << PINOUT_POT); + ADC1->CR |= ADC_CR_ADSTART; + while (ADC1->CR & ADC_CR_ADSTART); + uint16_t result = ADC1->DR; + ADC1->CR |= ADC_CR_ADSTP; + return result; +} + +void delay(int amount) { + for (int i = 0; i < 500 * amount; i++) asm("nop"); } int mod(int a, int b) { @@ -82,20 +108,25 @@ int mod(int a, int b) { return m < 0 ? (b < 0) ? m - b : m + b : m; } -int main() -{ +int main() { shieldConfig(); uint8_t reverse = 0; uint8_t led = 0; - while (1) - { + + while (1) { + for(int j = 0; j < 4; j++) ledWrite(j, 0); + ledWrite(potRead(), 1); + + continue; + reverse = buttonRead(); for(int j = 0; j < 4; j++) ledWrite(j, 0); led = mod(led + ((2 * reverse) - 1), 4); ledWrite(led, 1); - delay(); + // ledWrite(3, (ADC1->ISR & ADC_ISR_ADRDY) == 0 ? 0 : 1); + delay(100 + 300 * potRead()); } } |