Analogdaten mit dem mySTM32 Board Light

//---------------------------------------------------------------------------
// Title     : simple ADC Solution, ARM C application in SiSy
//---------------------------------------------------------------------------
// Function  : ...
// Wiring    : ...
//---------------------------------------------------------------------------
// Hardware  : ...
// Clock     : ... MHz
// Language  : ARM C
// Date      : ...
// Version   : ...
// Author    : ...
//---------------------------------------------------------------------------
#include <stddef.h>
#include <stdlib.h>
#include "hardware.h"


void initApplication() {
    // 1. Initialize SysTick
    SysTick_Config(SystemCoreClock/100);
    
    // 2. Configure LED on PB0
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    GPIO_InitTypeDef led;
    led.GPIO_Pin = GPIO_Pin_0;
    led.GPIO_Mode = GPIO_Mode_Out_PP;
    led.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &led);
    
    // 3. Configure ADC channel on PA7
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    GPIO_InitTypeDef adcPin;
    adcPin.GPIO_Pin = GPIO_Pin_7;
    adcPin.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &adcPin);
    
    // 4. Enable ADC1 clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    
    // 5. Configure ADC
    ADC_InitTypeDef ADC_InitStruct;
    ADC_StructInit(&ADC_InitStruct);
    ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_8b;
    ADC_InitStruct.ADC_ExternalTrigConv = 0;
    ADC_Init(ADC1, &ADC_InitStruct);
    
    // 6. Configure ADC channel
    ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 1, ADC_SampleTime_239_5Cycles);
    
    // 7. Enable ADC
    ADC_Cmd(ADC1, ENABLE);

 
    // 8. Start conversion
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

int main(void) {
    SystemInit();
    initApplication();
    
    while(1) {
        waitMs(10);
        uint8_t adcValue = ADC_GetConversionValue(ADC1);
        
        if(adcValue > 100) {
            GPIO_SetBits(GPIOB, GPIO_Pin_0);
        } else {
            GPIO_ResetBits(GPIOB, GPIO_Pin_0);
        }
    }
    
    return 0;
}

void SysTick_Handler(void) {
    // SysTick interrupt handler
}
//------------------------------------------------------------------------

Test

Nutzen sie die Schaltflächen Kompilieren, Linken und Brennen. Stellen Sie die nötigen Verbindungen auf dem Board mit den dafür vorgesehenen Patchkabeln her. Testen Sie die Anwendung.

Weiter mit