最近在做遥控器的adc转换,总是没有调通,没有读数,请大家帮忙看看程序。
#include "stm32f10x_conf.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "adc.h"
#include "nvicconf.h"
#include "uart.h"
//#include "acc.h"
// PORT A
#define GPIO_VRF GPIO_Pin_0
#define GPIO_RSET2 GPIO_Pin_1
#define GPIO_RSET1 GPIO_Pin_2
#define GPIO_VBAT GPIO_Pin_3
#define GPIO_ADC0 GPIO_Pin_4
#define GPIO_ADC1 GPIO_Pin_5
#define GPIO_ADC2 GPIO_Pin_6
#define GPIO_ADC3 GPIO_Pin_7
// CHANNELS
#define NBR_OF_ADC_CHANNELS 9
#define CH_VRF ADC_Channel_0
#define CH_RSET2 ADC_Channel_1
#define CH_RSET1 ADC_Channel_2
#define CH_VBAT ADC_Channel_3
#define CH_ADC0 ADC_Channel_4
#define CH_ADC1 ADC_Channel_5
#define CH_ADC2 ADC_Channel_6
#define CH_ADC3 ADC_Channel_7
#define CH_VREF ADC_Channel_17
#define CH_TEMP ADC_Channel_16
static bool isInit;
volatile uint16_t adcValues[NBR_OF_ADC_CHANNELS * ADC_MEAN_SIZE];
void GPIO_Configuration(void);
xQueueHandle adcQueue;
static void adcDmaInit(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// DMA channel1 configuration
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&adcValues;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = NBR_OF_ADC_CHANNELS * ADC_MEAN_SIZE;
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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
// Enable DMA channel1
DMA_Cmd(DMA1_Channel1, ENABLE);
}
/**
* Decimates the adc samples after oversampling
*/
static void adcDecimate(uint16_t * oversampled, uint16_t * decimated)
{
uint32_t i, j;
uint32_t sum;
// Compute sums and decimate each channel
for (i = 0; i < NBR_OF_ADC_CHANNELS; i++)
{
sum = 0;
for (j = 0; j < ADC_MEAN_SIZE; j++){
sum += oversampled[i + NBR_OF_ADC_CHANNELS * j];
}
// Decimate
decimated[i] = sum / ADC_MEAN_SIZE;
}
}
void GPIO_Configuration(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_VRF | GPIO_RSET2 | GPIO_RSET1 | GPIO_VBAT
| GPIO_ADC0 | GPIO_ADC1 | GPIO_ADC2 | GPIO_ADC3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void adcInit(void)
{
ADC_InitTypeDef ADC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
if(isInit)
return;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Enable GPIOA and ADC1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
//Timer configuration
TIM_TimeBaseStructure.TIM_Period = ADC_TRIG_PERIOD;
TIM_TimeBaseStructure.TIM_Prescaler = ADC_TRIG_PRESCALE;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// TIM2 channel2 configuration in PWM mode
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
// Halt timer 2 during debug halt.
DBGMCU_Config(DBGMCU_TIM2_STOP, ENABLE);
GPIO_Configuration();
adcDmaInit();
// ADC1 configuration
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = NBR_OF_ADC_CHANNELS;
ADC_Init(ADC1, &ADC_InitStructure);
// ADC1 channel sequence
ADC_RegularChannelConfig(ADC1, CH_VRF , 1, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_RSET2, 2, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_RSET1, 3, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_VBAT , 4, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_ADC0 , 5, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_ADC1 , 6, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_ADC2 , 7, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_ADC3 , 8, ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1, CH_VREF , 9, ADC_SampleTime_28Cycles5);
// Enable ADC1
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC | DMA_IT_HT, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
// Calibrate ADC1
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
// Enable ADC1 external trigger
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
ADC_TempSensorVrefintCmd(ENABLE);
// Enable the DMA1 channel1 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_ADC_PRI;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
adcQueue = xQueueCreate(1, NBR_OF_ADC_CHANNELS);
printf("this is in adcInit\r\n");
isInit = true;
}
bool adcTest(void)
{
return isInit;
}
float adcConvertToVoltageFloat(uint16_t v, uint16_t vref)
{
return ((v<<1) / (vref / ADC_INTERNAL_VREF));
}
void adcInterruptHandler(void)
{
portBASE_TYPE xHigherPriorityTaskWoken;
uint16_t *adcBuffer;
printf("\r\n");
if(DMA_GetITStatus(DMA1_IT_HT1))
{
DMA_ClearITPendingBit(DMA1_IT_HT1);
adcBuffer = adcValues;
xQueueSendFromISR(adcQueue, &adcBuffer, &xHigherPriorityTaskWoken);
}
if(DMA_GetITStatus(DMA1_IT_TC1))
{
DMA_ClearITPendingBit(DMA1_IT_TC1);
adcBuffer = adcValues;
xQueueSendFromISR(adcQueue, &adcBuffer, &xHigherPriorityTaskWoken);
}
}
void adcTask(void *param)
{
uint16_t adcRawValues[NBR_OF_ADC_CHANNELS];
uint16_t adcValues[NBR_OF_ADC_CHANNELS];
printf("This is in adcTask\r\n");
while(1)
{
printf("This is in adc while loop1\r\n");
xQueueReceive(adcQueue, adcRawValues, portMAX_DELAY);
printf("This is in adc while loop2\r\n");
adcDecimate(adcRawValues, adcValues); // 10% CPU
// pmBatteryUpdate(&adcValues);
vTaskDelay(1000/portTICK_RATE_MS);
printf("%04x, %04x, %04x, %04x\r\n",adcValues[0],adcValues[1],adcValues[2],adcValues[3] );
}
}