Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*ADC*/
- /* Includes */
- #include <stddef.h>
- #include "stm32f10x.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_adc.h"
- void initGPIO(void);
- void initADC(void);
- void calibrateADC(void);
- int main(void)
- {
- //Inicijalizacija
- uint32_t ain;
- initGPIO();
- initADC();
- calibrateADC(); //kalibracija ADC-a
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- //Ciklicko ponavljanje
- while (1)
- {
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
- ain = ADC_GetConversionValue(ADC1); //ucitava podatak s adc1
- if((ain >= 0) && (ain < 0xfff/3)) {
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
- }
- else if((ain >= 0xfff/3) && (ain < 2 * 0xfff/3)) {
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_RESET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
- }
- else {
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_RESET);
- }
- }
- }
- void initGPIO(void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
- GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- void initADC(void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- ADC_InitTypeDef ADC_InitStruct;
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
- //PA0 kao AIN0
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; //za analogni ulaz
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- //ADC1 konfiguracija
- ADC_StructInit(&ADC_InitStruct);
- ADC_InitStruct.ADC_Mode = ADC_Mode_Independent; //neovisni su jedan od drugom
- ADC_InitStruct.ADC_ScanConvMode = DISABLE;
- ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; //poravnavanje u desno kod pretvorbe u 12 bita
- ADC_InitStruct.ADC_NbrOfChannel = 1; //koliki je broj kanala
- ADC_Init(ADC1, &ADC_InitStruct);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
- ADC_Cmd(ADC1, ENABLE);
- }
- void calibrateADC(void) {
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement