|
最开始定义AD是在函数里的局部变量 如下: void AD_Single(unsigned char channel) { ADC_InitTypeDef ADC_InitStructure; ...... }结果 一直得不到EOC 后来改成全局变量 在main函数里定义 再用extern ADC_InitTypeDef ADC_InitStructure; 结果EOC FLAG就产生了 ,我晕 ,只要一换回去 就又失败了 用调试看了下汇编 图片上传 这是ok的全局变量
这是失败的局部变量
|
微信公众号
手机版
不要在A函数处修改结构体,却去B函数初始化外设。
1. 正常运行
extern ADC_InitTypeDef ADC_InitStructure;
void AD_Single(unsigned char channel)
{
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1, channel , ADC_SampleTime_13_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_WaitModeCmd(ADC1, ENABLE);
ADC_AutoPowerOffCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
}
2.只中断一次,就不再中断的情况
void AD_Single(unsigned char channel)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1, channel , ADC_SampleTime_13_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_WaitModeCmd(ADC1, ENABLE);
ADC_AutoPowerOffCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
}
初始化和配置都是在一个作用域的
1. 正常运行
extern ADC_InitTypeDef ADC_InitStructure;
void AD_Single(unsigned char channel)
{
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1, channel , ADC_SampleTime_13_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_WaitModeCmd(ADC1, ENABLE);
ADC_AutoPowerOffCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
}
2.只中断一次,就不再中断的情况
void AD_Single(unsigned char channel)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1, channel , ADC_SampleTime_13_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_WaitModeCmd(ADC1, ENABLE);
ADC_AutoPowerOffCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
}
这是定义的全局变量
这是我的工程文件 adc配置放在ad.c里面
这是定义成局部变量
这是AD采集等待EOC标志位
局部变量的作用域作用于函数体范围.
哥们 你没懂我意思 我是说 我定义成全局变量 ADC初始化成功 ,改成局部变量就失败 ,调试的时候 两者的内容是一模一样 区别就在汇编里面 但是就是没找到原因
这里的函数,传递一个ADC的通道进来。
ADC_DeInit(ADC1); 这句里头的 ADC1,从哪里来的?这是个实参吧?
之前没有初始化过,这里却执行 清初始化(DeInit)。
ADC1是STM32库函数里定义的,那个传输来的通道是多余变量 我没用它
void AD_Single(unsigned char channel)
{
ADC_DeInit(ADC1);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits*/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //¾«¶È
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//ENABLE; //µ¥Ò»Ä£Ê½
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//Èí¼þ´¥·¢
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ÓÒ¶ÔÆë
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
这是我的函数 原先是在void AD_Single(unsigned char channel)里面定义的
ADC_InitTypeDef ADC_InitStructure;结果中断了一次 就不再中断了
现在放在函数外面定义成全局变量 就能正常了 不知道为什么我都是在函数里面赋值 太TMD郁闷了