#include "adc.h"
__IO uint16_t ADC_ConvertedValue[4]={0};
static void Rheostat_ADC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*=====================í¨μà4======================*/
// ê1Äü GPIO ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
// ÅäÖÃ IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
//2»éÏà-2»ÏÂà-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*=====================í¨μà5======================*/
// ê1Äü GPIO ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
// ÅäÖÃ IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
//2»éÏà-2»ÏÂà-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*=====================í¨μà6=======================*/
// ê1Äü GPIO ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
// ÅäÖÃ IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
//2»éÏà-2»ÏÂà-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*=====================í¨μà7=======================*/
// ê1Äü GPIO ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
// ÅäÖÃ IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
//2»éÏà-2»ÏÂà-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static void Rheostat_ADC_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
// ------------------DMA Init ½á11ìå2Îêy 3õê¼»ˉ--------------------------
// ADC1ê1óÃDMA2£¬êy¾Yá÷0£¬í¨μà0£¬Õa¸öêÇêÖ2á1춨ËàμÄ
// ¿aÆôDMAê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// íaéè»ùÖ·Îa£oADC êy¾Y¼Ä′æÆ÷μØÖ·
DMA_InitStructure.DMA_PeripheralBaseAddr = ((u32)ADC1+0x4c);
// ′æ′¢Æ÷μØÖ·£¬êμ¼êéϾíêÇò»¸öÄú2¿SRAMμıäá¿
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)ADC_ConvertedValue;
// êy¾Y′«êä·½ÏòÎaíaéèμ½′æ′¢Æ÷
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
// »o3åÇø′óD¡Îa£¬Ö¸ò»′Î′«êäμÄêy¾Yá¿
DMA_InitStructure.DMA_BufferSize = 4;
// íaéè¼Ä′æÆ÷Ö»óDò»¸ö£¬μØÖ·2»óÃμYÔö
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// ′æ′¢Æ÷μØÖ·1춨
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
// // íaéèêy¾Y′óD¡Îa°ë×Ö£¬¼′ὸö×Ö½ú
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
// ′æ′¢Æ÷êy¾Y′óD¡ò2Îa°ë×Ö£¬¸úíaéèêy¾Y′óD¡Ïàí¬
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
// Ñ-»·′«êäÄ£ê½
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
// DMA ′«êäí¨μàóÅÏ輶Îa¸ß£¬μ±ê1óÃò»¸öDMAí¨μàê±£¬óÅÏ輶éèÖÃ2»ó°Ïì
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
// ½ûÖ1DMA FIFO £¬ê1óÃÖ±á¬Ä£ê½
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
// FIFO ′óD¡£¬FIFOģ꽽ûÖ1ê±£¬Õa¸ö2»óÃÅäÖÃ
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
// Ñ¡Ôñ DMA í¨μ࣬í¨μà′æÔúóúá÷ÖD
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
//3õê¼»ˉDMAá÷£¬á÷Ïàμ±óúò»¸ö′óμÄ1üμ࣬1üμààïÃæóDoü¶àí¨μà
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
// ê1ÄüDMAá÷
DMA_Cmd(DMA2_Stream0, ENABLE);
// ¿aÆôADCê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
// -------------------ADC Common ½á11ìå 2Îêy 3õê¼»ˉ------------------------
// ¶àá¢ADCÄ£ê½
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
// ê±ÖóÎafpclk x·ÖÆμ
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
// ½ûÖ1DMAÖ±½ó·ÃÎêÄ£ê½
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
// 2éÑùê±¼ä¼ä¸ô
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
// -------------------ADC Init ½á11ìå 2Îêy 3õê¼»ˉ--------------------------
ADC_StructInit(&ADC_InitStructure);
// ADC ·Ö±æÂê
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
// é¨Ãèģ꽣¬¶àí¨μà2é¼ˉDèòa
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
// á¬Dø×a»»
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
//½ûÖ1ía2¿±ßÑØ′¥·¢
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
//ía2¿′¥·¢í¨μ࣬±¾ày×óê1óÃèí¼t′¥·¢£¬′ËÖμËæ±ã¸3Öμ¼′¿é
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
//êy¾Yóò¶ÔÆë
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//×a»»í¨μà 1¸ö
ADC_InitStructure.ADC_NbrOfConversion = 4;
ADC_Init(ADC1, &ADC_InitStructure);
//---------------------------------------------------------------------------
// ÅäÖà ADC í¨μà×a»»Ë3Dòoí2éÑùê±¼äÖüÆú
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_28Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_28Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_28Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_28Cycles);
// ê1ÄüDMAÇëÇó after last transfer (Single-ADC mode)
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
// ê1ÄüADC DMA
ADC_DMACmd(ADC1, ENABLE);
// ê1ÄüADC
ADC_Cmd(ADC1, ENABLE);
//¿aê¼adc×a»»£¬èí¼t′¥·¢
ADC_SoftwareStartConv(ADC1);
}
void Rheostat_Init(void)
{
Rheostat_ADC_GPIO_Config();
Rheostat_ADC_Mode_Config();
}
void ADC_balance_Init(void)
{
// ADC×a»»μÄμçÑ1Öμí¨1yMDA·½ê½′«μ½SRAM
extern __IO uint16_t ADC_ConvertedValue[4];
// ¾Ö2¿±äá¿£¬óÃóú±£′æ×a»»¼ÆËãoóμÄμçÑ1Öμ
float ADC_ConvertedValueLocal[4]={0};
Rheostat_Init(); //3õê¼»ˉADC
ADC_ConvertedValueLocal[0] =(float) ADC_ConvertedValue[0]/4096*(float)3.3;
ADC_ConvertedValueLocal[1] =(float) ADC_ConvertedValue[1]/4096*(float)3.3;
ADC_ConvertedValueLocal[2] =(float) ADC_ConvertedValue[2]/4096*(float)3.3;
ADC_ConvertedValueLocal[3] =(float) ADC_ConvertedValue[3]/4096*(float)3.3;
printf("
PA4 value = %f V
",ADC_ConvertedValueLocal[0]);
printf("
PA5 value = %f V
",ADC_ConvertedValueLocal[1]);
printf("
PA6 value = %f V
",ADC_ConvertedValueLocal[2]);
printf("
PA7 value = %f V
",ADC_ConvertedValueLocal[3]);
}
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "motor.h"
#include "IIC.h"
#include "move.h"
#include "adc.h"
#include "balance.h"
#include "move.h"
#include "sg.h"
int main(void)
{
delay_init(168); //3õê¼»ˉÑóê±oˉêy
SG_PWM_Init(500-1,84-1);
Motor_Init();
Motor_PWM_Init(500-1,84-1);
uart_init(115200); //′®¿ú3õê¼»ˉ2¨ìØÂêÎa115200
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
I2C_GPIO_Config(); //3õê¼»ˉÄ£ÄaI2C¶Ë¿ú
Init_MPU6050(); //3õê¼»ˉMpu6050
uart_init(115200); //′®¿ú3õê¼»ˉ2¨ìØÂêÎa115200
while(1)
{
//MPU6050_balance_Init();
ADC_balance_Init();
delay_ms(1000);
}
}
一周热门 更多>