给我一个基于stm32f103的两一路ADC采样并通过一路DAC输出的keil程序
时间: 2024-04-30 17:21:51 浏览: 90
下面是一个基于STM32F103的双路ADC采样并通过一路DAC输出的Keil程序示例,你可以参考一下:
```c
#include "stm32f10x.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
__IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
__IO uint16_t ADC2ConvertedValue = 0, ADC2ConvertedVoltage = 0;
void ADC1_GPIO_Config(void);
void ADC2_GPIO_Config(void);
void ADC1_Mode_Config(void);
void ADC2_Mode_Config(void);
void DAC_Config(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
ADC1_GPIO_Config();
ADC2_GPIO_Config();
ADC1_Mode_Config();
ADC2_Mode_Config();
DAC_Config();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
ADC1ConvertedVoltage = (ADC1ConvertedValue * 3300) / 0xFFF;
ADC_SoftwareStartConvCmd(ADC2, ENABLE);
while (!ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC));
ADC2ConvertedValue = ADC_GetConversionValue(ADC2);
ADC2ConvertedVoltage = (ADC2ConvertedValue * 3300) / 0xFFF;
DAC_SetChannel1Data(DAC_Align_12b_R, ADC1ConvertedValue);
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
Delay(0xFFFFF);
}
}
void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC1_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void ADC2_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC2, ENABLE);
ADC_ResetCalibration(ADC2);
while (ADC_GetResetCalibrationStatus(ADC2));
ADC_StartCalibration(ADC2);
while (ADC_GetCalibrationStatus(ADC2));
}
void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
}
void Delay(__IO uint32_t nCount)
{
while (nCount--)
{
}
}
```
需要注意的是,这个程序只是一个示例,你需要根据实际的硬件连接情况进行调整和修改。同时,如果你使用的是不同型号或者不同版本的STM32芯片,也需要修改一些配置项。
阅读全文