高中生
最后登录1970-1-1
在线时间 小时
注册时间2013-5-29
|
20火花
本帖最后由 zpzdd 于 2017-12-11 11:10 编辑
开发环境IAR8.10.1,芯片103VCT6
现象:ADC配置文件已设置只读,原本正常的数据顺序,重新编译后出现错位问题,不管他,过一会编译又正常。
C文件主要内容:
- #define ADC1_DR_Address ((u32)0x40012400+0x4c)
- static uint16_t ADC_ConvertedValue[9];
- /**
- * [ADC1_GPIO_Config description]
- */
- static void ADC1_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable ADC1 and GPIOC clock */
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
- RCC_APB2PeriphClockCmd(GPIO_RCCx(JOYSTICK1_X_PIN), ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = ADC_PINS;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIO_PORTx(JOYSTICK1_X_PIN), &GPIO_InitStructure);
- RCC_APB2PeriphClockCmd(GPIO_RCCx(RS_STICK_PIN), ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_PINx(RS_STICK_PIN) | GPIO_PINx(RD_STICK_PIN);
- GPIO_Init(GPIO_PORTx(RS_STICK_PIN), &GPIO_InitStructure);
- RCC_APB2PeriphClockCmd(GPIO_RCCx(PWR_ADC_PIN), ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_PINx(PWR_ADC_PIN);
- GPIO_Init(GPIO_PORTx(PWR_ADC_PIN), &GPIO_InitStructure);
- }
- void adc_dma_init()
- {
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- /* DMA channel1 configuration */
- DMA_DeInit(DMA1_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADC地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;//内存地址~在这里进行赋值
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = 9;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址固定
- 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);
-
- /* Enable DMA channel1 */
- DMA_Cmd(DMA1_Channel1, ENABLE);
- }
- /* 函数名:ADC1_Mode_Config
- * 描述 :配置ADC1的工作模式为MDA模式
- * 输入 : 无
- * 输出 :无
- * 调用 :内部调用
- */
- static void ADC1_Mode_Config(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- /* ADC1 configuration */
-
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立ADC模式
- ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //禁止扫描模式,扫描模式用于多通道采集
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //开启连续转换模式,即不停地进行ADC转换
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //不使用外部触发转换
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //采集数据右对齐
- ADC_InitStructure.ADC_NbrOfChannel = 9; //要转换的通道数目1
- ADC_Init(ADC1, &ADC_InitStructure);
- /*配置ADC时钟,为PCLK2的8分频,即9Hz*/
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- /*配置ADC1的通道11为55. 5个采样周期,序列为1 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);//PWR_ADC_PIN ADC_SampleTime_239Cycles5
- ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_239Cycles5);//RD_STICK_PIN ADC_SampleTime_55Cycles5
- ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 3, ADC_SampleTime_239Cycles5);//RS_STICK_PIN
- ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 4, ADC_SampleTime_239Cycles5);//JOYSTICK2_X_PIN
- ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 5, ADC_SampleTime_239Cycles5);//JOYSTICK2_Y_PIN
- ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 6, ADC_SampleTime_239Cycles5);//JOYSTICK1_Y_PIN
- ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 7, ADC_SampleTime_239Cycles5);//JOYSTICK1_X_PIN
- ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 8, ADC_SampleTime_239Cycles5);//LD_STICK_PIN
- ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 9, ADC_SampleTime_239Cycles5);//LS_STICK_PIN
- /* Enable ADC1 DMA */
- ADC_DMACmd(ADC1, ENABLE);
-
- /* Enable ADC1 */
- ADC_Cmd(ADC1, ENABLE);
-
- /*复位校准寄存器 */
- ADC_ResetCalibration(ADC1);
- /*等待校准寄存器复位完成 */
- while(ADC_GetResetCalibrationStatus(ADC1));
-
- /* ADC校准 */
- ADC_StartCalibration(ADC1);
- /* 等待校准完成*/
- while(ADC_GetCalibrationStatus(ADC1));
-
- /* 由于没有采用外部触发,所以使用软件触发ADC转换 */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- /*
- * 函数名:ADC1_Init
- * 描述 :无
- * 输入 :无
- * 输出 :无
- * 调用 :外部调用
- */
- void ADC1_Init(void)
- {
- ADC1_GPIO_Config();
- adc_dma_init();
- ADC1_Mode_Config();
- }
- u16 adc_single_channel_get(u8 channel)
- {
- return ADC_ConvertedValue[channel];
- }
- void adc_all_channel_get(u16 data[])
- {
- data[0] = ADC_ConvertedValue[1];
- data[1] = ADC_ConvertedValue[2];
- data[2] = ADC_ConvertedValue[3];
- data[3] = ADC_ConvertedValue[4];
- data[4] = ADC_ConvertedValue[5];
- data[5] = ADC_ConvertedValue[6];
- data[6] = ADC_ConvertedValue[7];
- data[7] = ADC_ConvertedValue[8];
- }
复制代码 头文件:- typedef enum
- {
- PWR_ADC_NUM = 0,
- RD_STICK_NUM = 1,
- RS_STICK_NUM = 2,
- JOYSTICK2_X_NUM = 3,
- JOYSTICK2_Y_NUM = 4,
- JOYSTICK1_Y_NUM = 5,
- JOYSTICK1_X_NUM = 6,
- LD_STICK_NUM = 7,
- LS_STICK_NUM = 8
- }AD_SEQUENCE_NEUM;
- void ADC1_Init(void);
- u16 adc_single_channel_get(u8 channel);
- void adc_all_channel_get(u16 data[]);
复制代码 基本功能读数没任何问题,但是就是通道顺序错位,各位有遇到么?
|
最佳答案
查看完整内容
你先使能dma,再启动adc转换这个问题就没有了
|