STM32f105rct6:配置AD,配置DMA 两路AD取值通道:
配置AD:
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_StructInit(&ADC_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//软件启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2; // 通道数目
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
//ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_55Cycles5);
//ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_55Cycles5);
//ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 4, ADC_SampleTime_55Cycles5);
// 假设你定义channle1 的rank=1,channle2 的rank=2,
//那么对应你在DMA缓存空间的变量数组AdcDMA[0] 就i是channle1的转换结果,
//AdcDMA[1]就是通道2的转换结果。
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while (ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC2 calibration */
while (ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC1软件开始转换
}
配置DMA:
芯片里的寄存器基地址:
//#define SPI1_DR_Address 0x4001300C
//#define SPI2_DR_Address 0x4000380C
//#define ADC1_DR_Address 0x4001244C
//#define USART1_DR_Base 0x40013804
//#define USART2_DR_Base 0x40004404
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_StructInit(&DMA_InitStructure);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//DMA外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)g_stuKEY.KeyAD;//DMA内存基地址 数组:g_stuKEY.KeyAD[2]
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向
DMA_InitStructure.DMA_BufferSize = 2;// 2//DMA通道的DMA缓存的大小
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; //16 bit
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);
//DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
}
直接使用数组里的数据,已经通过DMA 转化存放好
u16 GetVotFromAD(g_stuKEY.KeyAD[0])
{
//u16 int_part,float_part;
u16 temp;
temp = u16AD;
//把采集到的数据转换成电压值
temp = (u16)((((u32)temp) * 3300) >> 12);
//int_part = temp/1000;
//float_part = temp%1000;
return temp;
}
上一篇:STM32 FSMC机制flash拓展
下一篇:STM CAN filter配置纪要
推荐阅读最新更新时间:2024-11-12 10:29
- 热门资源推荐
- 热门放大器推荐