Advertisement
Domy131097

[LV7] Mikroračunalni sustavi - ADC

May 19th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. /*ADC*/
  2.  
  3. /* Includes */
  4. #include <stddef.h>
  5. #include "stm32f10x.h"
  6. #include "stm32f10x_rcc.h"
  7. #include "stm32f10x_gpio.h"
  8. #include "stm32f10x_adc.h"
  9.  
  10. void initGPIO(void);
  11. void initADC(void);
  12. void calibrateADC(void);
  13.  
  14. int main(void)
  15. {
  16.     //Inicijalizacija
  17.     uint32_t ain;
  18.  
  19.     initGPIO();
  20.     initADC();
  21.     calibrateADC(); //kalibracija ADC-a
  22.  
  23.     ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  24.     //Ciklicko ponavljanje
  25.     while (1)
  26.     {
  27.         while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  28.         ain = ADC_GetConversionValue(ADC1); //ucitava podatak s adc1
  29.  
  30.         if((ain >= 0) && (ain < 0xfff/3)) {
  31.             GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
  32.             GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
  33.             GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
  34.         }
  35.         else if((ain >= 0xfff/3) &&  (ain < 2 * 0xfff/3)) {
  36.             GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  37.             GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_RESET);
  38.             GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
  39.         }
  40.         else {
  41.             GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  42.             GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
  43.             GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_RESET);
  44.         }
  45.     }
  46. }
  47.  
  48. void initGPIO(void) {
  49.     GPIO_InitTypeDef GPIO_InitStruct;
  50.  
  51.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  52.  
  53.     GPIO_StructInit(&GPIO_InitStruct);
  54.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
  55.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  56.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  57.     GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  58.     GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
  59.     GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
  60.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  61. }
  62.  
  63. void initADC(void) {
  64.     GPIO_InitTypeDef GPIO_InitStruct;
  65.     ADC_InitTypeDef ADC_InitStruct;
  66.  
  67.     RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  68.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
  69.  
  70.     //PA0 kao AIN0
  71.     GPIO_StructInit(&GPIO_InitStruct);
  72.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  73.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; //za analogni ulaz
  74.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  75.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  76.  
  77.     //ADC1 konfiguracija
  78.     ADC_StructInit(&ADC_InitStruct);
  79.     ADC_InitStruct.ADC_Mode = ADC_Mode_Independent; //neovisni su jedan od drugom
  80.     ADC_InitStruct.ADC_ScanConvMode = DISABLE;
  81.     ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
  82.     ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  83.     ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; //poravnavanje u desno kod pretvorbe u 12 bita
  84.     ADC_InitStruct.ADC_NbrOfChannel = 1; //koliki je broj kanala
  85.  
  86.     ADC_Init(ADC1, &ADC_InitStruct);
  87.  
  88.     ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
  89.  
  90.     ADC_Cmd(ADC1, ENABLE);
  91.  
  92. }
  93.  
  94. void calibrateADC(void) {
  95.     while(ADC_GetResetCalibrationStatus(ADC1));
  96.     ADC_StartCalibration(ADC1);
  97.     while(ADC_GetCalibrationStatus(ADC1));
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement