stm32f103zet6 adc采集方波三角波正弦波 标准库代码
时间: 2023-08-12 11:04:34 浏览: 143
STM32分辨标准波形(三角波、方波、正弦波等)代码
以下是基于STM32F103ZET6芯片,使用标准库的ADC采集方波、三角波、正弦波的代码示例:
```c
#include "stm32f10x.h"
#include <math.h>
#define ADC1_DR_Address ((uint32_t)0x4001244C)
void RCC_Configuration(void);
void GPIO_Configuration(void);
void ADC_Configuration(void);
void DMA_Configuration(void);
void TIM_Configuration(void);
void DAC_Configuration(void);
void Waveform_Generation(void);
uint16_t ADC_ConvertedValue[3];
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
DMA_Configuration();
TIM_Configuration();
DAC_Configuration();
Waveform_Generation();
while(1)
{
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1);
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 = 3;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 3;
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);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 719;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_Cmd(TIM1, ENABLE);
}
void DAC_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
}
void Waveform_Generation(void)
{
uint16_t i;
while(1)
{
for(i = 0; i < 360; i++)
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(2048 + 2047 * sin(i * 3.1415926 / 180.0)));
TIM_SetCompare1(TIM1, i);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
}
for(i = 360; i > 0; i--)
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(2048 + 2047 * sin(i * 3.1415926 / 180.0)));
TIM_SetCompare1(TIM1, i);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
}
for(i = 0; i < 360; i++)
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(i * 12));
TIM_SetCompare1(TIM1, i);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
}
for(i = 360; i > 0; i--)
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(i * 12));
TIM_SetCompare1(TIM1, i);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
}
for(i = 0; i < 360; i++)
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(i * 11));
TIM_SetCompare1(TIM1, i);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
}
for(i = 360; i > 0; i--)
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(i * 11));
TIM_SetCompare1(TIM1, i);
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
}
}
}
```
这段代码使用了ADC采集模拟信号,并通过DMA传输到内存中,再由DAC输出到模拟输出端口。其中,TIM1用于生成PWM波形,通过改变TIM1的占空比来改变输出信号的幅值。同时,程序中还使用了sine函数来生成正弦波。需要注意的是,这里使用了DMA传输数据,因此需要在main函数的while(1)中加入一个空循环,以让程序不退出。
阅读全文