STM32F030 重启采样不对
本人用STM32F030做一个检测电路..不断电将程序烧进去,采样正确. 但是断电重启AD采样就不对了,一直显示同一个数字,复位后瞬间能正常采样,但之后采样值保持不变,即使电压改变也保持不变..处理器有一个脚设置500Hz的脉宽输出一直是正常的..数值显示我是通过CP2102转USB传到电脑的,1秒上传1次..请问这是怎么回事??刚接触这类单片机..有很多不懂,请大神指点一下!
你采集的电压大概多少V? 晓枫VS枯叶 发表于 2014-12-23 20:23
你采集的电压大概多少V?
几百毫伏 几百mv的话,你这数据就完全不对了! 晓枫VS枯叶 发表于 2014-12-23 20:37
几百mv的话,你这数据就完全不对了!
是的,所以不知道问题出在哪里..现在有时复位能正确采样,有时不行.. joseya 发表于 2014-12-23 21:11
是的,所以不知道问题出在哪里..现在有时复位能正确采样,有时不行..
请问,如果处理器供电电压手动增大直到电压达到3.3V,会不会对处理器的工作有影响? 重器后程序开始运行的地址对吗 本帖最后由 奔跑小蜗牛 于 2014-12-24 16:51 编辑
测几百mv输出2965那也不对额。。。AD采样电路发一下,还有AD数据处理程序发一下 有可能是你定义的变量没有初始化 奔跑小蜗牛 发表于 2014-12-24 12:20
测几百mv输出2965那也不对额。。。AD采样电路发一下,还有AD数据处理程序发一下 ...
我是通过DMA中断来处理采样的
#include "adc.h"
/*该头文件对GPIO、ADC、DMA进行了初始化处理*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address 0x40012440
#define N 10
__IO uint16_t RegularConvData_Tab;
__IO uint16_t adc_IT;
uint16_t num=0;
uint16_t adc_avg;
void DMA_INT_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ADC1_DMA_Init(void)
{
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
/* ADC1 DeInit */
ADC_DeInit(ADC1); //将外设ADC1的全部寄存器重设为缺省值
/* EnableGPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* Configure PA.01as analog input */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ; //无上拉下拉
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* DMA1 Channel1 Config *///配置DMA
DMA_DeInit(DMA1_Channel1); //DMA通道Channel1重设为缺省值
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize =10; //
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; //循环模式开启,Buffer写满后 自动回到初始地址开始写
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStruct);
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
// /* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStruct); //初始ADC配置
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits*/
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_Init(ADC1, &ADC_InitStruct);
/* Convert the ADC1 Vrefwith 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_55_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //等待完成校准
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1); //软件触发ADC转换
}
void filter(void) //50次DMA1中断处理一次
{
static uint32_tsum = 0;
static uint8_t count;
num++;
if(num>=50)
{
num=0;
for (count=0;count<N;count++)
{
sum += RegularConvData_Tab;
}
adc_IT=sum/N;
sum=0;
}
}
void Get_adcavg(void) //5ms更新并判断一次数据
{
adc_avg=adc_IT*3.3*1000/4096;
}
//以下是DMA中断处理
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)
{
DMA_ClearITPendingBit(DMA1_IT_TC1);
DMA_Cmd(DMA1_Channel1, DISABLE);
filter();
DMA_Cmd(DMA1_Channel1, ENABLE);
}
}
以下是主函数
#include "stm32f0xx.h"
#include "adc.h"
#include "systick.h"
#include "led.h"
#include "uart.h"
externuint16_tadc_avg;
int main(void)
{
int adc;
USART_Configuration();
Systick_Init();
ADC1_DMA_Init();
LED_Init();
DMA_INT_Config();
while(1)
{
Delay(1000);
Get_adcavg();
adc=adc_avg;
printf("voltage= %d V\r\n",adc);
}
}
页:
[1]
2