×
单片机 > 单片机程序设计 > 详情

stm32f10x的AD配置

发布时间:2020-06-16 发布时间:
|
#include "stm32f10x_lib.h"

#include"stdio.h"


#define ADC1_DR_Address    ((u32)0x4001244C)
int AD_value;
static unsigned long ticks;
unsigned char Clock1s;



ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
vu16 ADC_ConvertedValue;
ErrorStatus HSEStartUpStatus;
   

void RCC_Configuration(void);
void GPIO_Configuration(void);

void USART_Configuration1(void);
void SetupClock (void);
int fputc(int ch, FILE *f); 

int main(void)
{

 
  RCC_Configuration();


 
  GPIO_Configuration();
  
  
  USART_Configuration1();
 
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
 
 
  DMA_Cmd(DMA1_Channel1, ENABLE);
    
 
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);

 
  ADC_DMACmd(ADC1, ENABLE);
 
 
  ADC_Cmd(ADC1, ENABLE);

    
  ADC_ResetCalibration(ADC1);
 
  while(ADC_GetResetCalibrationStatus(ADC1));

 
  ADC_StartCalibration(ADC1);
 
  while(ADC_GetCalibrationStatus(ADC1));
    
 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

 while(1)
  {
  AD_value=ADC_GetConversionValue(ADC1);
   if (ticks++ >= 900000)
      {                 
     ticks=0;
     Clock1s=1;
      } 
  }
}


void RCC_Configuration(void)
{
 
  RCC_DeInit();
 
  RCC_HSEConfig(RCC_HSE_ON);
 
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
   
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
   
    RCC_PCLK2Config(RCC_HCLK_Div1);
   
    RCC_PCLK1Config(RCC_HCLK_Div2);
   
    RCC_ADCCLKConfig(RCC_PCLK2_Div4);
   
    FLASH_SetLatency(FLASH_Latency_2);
   
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
   
    RCC_PLLCmd(ENABLE);
   
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
   
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
   
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
}


 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
}


void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

 


void USART_Configuration1(void)
{
  USART_InitTypeDef USART_InitStructure;
  USART_ClockInitTypeDef  USART_ClockInitStructure;

 
  USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

USART_ClockInit(USART1, &USART_ClockInitStructure);

USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;


USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);
 
  USART_Cmd(USART1, ENABLE);
}
 
 

int fputc(int ch, FILE *f)
{
#ifdef DBG_ITM

  if (DEMCR & TRCENA) {
    while (ITM_Port32(0) == 0);
    ITM_Port8(0) = ch;
  }
#else 

  USART_SendData(USART1, (unsigned char) ch);
  while (!(USART1->SR & USART_FLAG_TXE));
#endif 
  return (ch);
}

关键字:stm32f10x  AD配置

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈AVR中定时器几种工作模式