有人帮我看看代码吗,问题在adc1和adc2处

2019-03-23 19:24发布

#include "stm32f10x_lib.h"
#include "main.h"
#include "platform_config.h"
#include "main.h"
#include <stdio.h>
static vu32 TimingDelay = 0;  
ADC_InitTypeDef  ADC_InitStructure;
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
u16 adc1(void);
u16 adc2(void);
int main(void)
{
int AD_value;
u8 i,j;
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
SysTick_Config();
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 = 2;
ADC_Init(ADC1, &ADC_InitStructure);
while (1)
{
  AD_value = adc1();
//      ADC_SoftwareStartConvCmd(ADC1, DISABLE);
  ADC_Cmd(ADC1,  DISABLE);
  j=AD_value%256;
  i=AD_value/256;
  USART_SendData(USART1, i);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  USART_SendData(USART1, j);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  Delay(200);
  AD_value = adc2();
//      ADC_SoftwareStartConvCmd(ADC1, DISABLE);
  ADC_Cmd(ADC1,  DISABLE);
  j=AD_value%256;
  i=AD_value/256;
  i=i|0xf0;
  USART_SendData(USART1, i);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  USART_SendData(USART1, j);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  Delay(200);
}
}
u16 adc1(void)
{
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_13Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
return(ADC_GetConversionValue(ADC1));
}
u16 adc2(void)
{
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
return(ADC_GetConversionValue(ADC1));
}
void RCC_Configuration(void)
{
RCC_DeInit();  //将外设 RCC寄存器重设为缺省值      V
RCC_HSEConfig(RCC_HSE_ON);   //#define RCC_HSE_ON    ((u32)0x00010000)   V
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待HSE起振   该函数将等待直到 HSE 就绪,或者在超时的情况下退出   V
if(HSEStartUpStatus == SUCCESS)
{
  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);  //使能或者失能预取指缓存
  FLASH_SetLatency(FLASH_Latency_2);  //设置代码延时值      2个延时周期
  RCC_HCLKConfig(RCC_SYSCLK_Div1);     //设置 AHB 时钟(HCLK)  即设置AHB时钟     V
  RCC_PCLK2Config(RCC_HCLK_Div1);    //设置高速 AHB 时钟(PCLK2) 即设置APB2时钟    V
  RCC_PCLK1Config(RCC_HCLK_Div2);    //设置低速 AHB 时钟(PCLK1) 即设置APB1时钟    V
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);    //设置 ADC 时钟
  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);  //设置 PLL 时钟源及倍频系数      V
  RCC_PLLCmd(ENABLE);    //使能或者失能 PLL                 V
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)  //检查指定的 RCC 标志位设置与否    V
  {    }
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  //设置系统时钟          V
  while(RCC_GetSYSCLKSource() != 0x08)               // V
  { }
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE); //使能或者失能 APB2 外设时钟     V
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIO_LED, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
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 SysTick_Config(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);    //设置 SysTick时钟源
SysTick_SetReload(720000);
SysTick_ITConfig(ENABLE);
}
void Delay(u32 nCount)
{
TimingDelay = nCount;
SysTick_CounterCmd(SysTick_Counter_Enable); //使能或者失能 SysTick 计数器
while(TimingDelay != 0)
{}
SysTick_CounterCmd(SysTick_Counter_Disable);
SysTick_CounterCmd(SysTick_Counter_Clear);
}
void Decrement_TimingDelay(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void USART_Configuration(void)
{
USART_InitStructure.USART_BaudRate = 9600;
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_Tx| USART_Mode_Rx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。