有人帮我看看代码吗,问题在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);
} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
7条回答
lr2131
1楼-- · 2019-03-24 01:30
/ 实在是不想调代码了
jensenhero
2楼-- · 2019-03-24 03:14
关注adc1和adc2
dontium
3楼-- · 2019-03-24 03:27
 精彩回答 2  元偷偷看……
jensenhero
4楼-- · 2019-03-24 05:07
关于第一个问题config的问题我不知道是配置成
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_13Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
还是
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_13Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 2, ADC_SampleTime_13Cycles5);
因为涉及到关闭后的再次打开,所以不清楚怎么弄
关于第二个问题,我这两天已经加了while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)),但是没有效果

这是我发的另外一个帖子,看了你就明白我大概想做的东西了
jensenhero
5楼-- · 2019-03-24 09:49
http://bbs.eeworld.com.cn/thread-362144-1-1.html,sorry 忘记复制粘贴了
dontium
6楼-- · 2019-03-24 10:37
 精彩回答 2  元偷偷看……

一周热门 更多>