基于STM32的正弦信号发生器设计,输出1路正弦波信号,幅度和频率可调,幅度0~3.3V,频率范围20Hz~ 20kHz.并给出具体代码
时间: 2024-03-28 09:39:52 浏览: 26
好的,这里是基于STM32的正弦信号发生器设计,输出1路正弦波信号,幅度和频率可调,幅度0~3.3V,频率范围20Hz~20kHz的代码:
```c
#include "stm32f10x.h"
#include "math.h"
#define PI 3.1415926
uint16_t sin_table[100] = {0};
uint16_t amplitude = 0;
uint16_t frequency = 0;
void TIM2_Config(void);
void TIM3_GPIO_Config(void);
void TIM3_Mode_Config(void);
void DAC_Config(void);
void sin_table_init(void);
int main(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
TIM2_Config();
TIM3_GPIO_Config();
TIM3_Mode_Config();
DAC_Config();
sin_table_init();
while(1)
{
for(int i=0; i<100; i++)
{
DAC_SetChannel1Data(DAC_Align_12b_R, amplitude*sin_table[i]);
TIM_SetCompare2(TIM3, i);
for(int j=0; j<1000; j++);
}
}
}
void TIM2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 7199;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 99;
TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, 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_T3_TRGO;
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 sin_table_init(void)
{
for(int i=0; i<100; i++)
{
sin_table[i] = (uint16_t)(2047*(sin(2*PI*i/100)+1)+0.5);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
amplitude = (uint16_t)(3.3/4096*ADC_GetConversionValue(ADC1)+0.5);
frequency = (uint16_t)(20+ADC_GetConversionValue(ADC2)/4096*19980+0.5);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(72000000/720/frequency+0.5)-1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}
}
```
这个代码中,我们使用TIM2来定时读取ADC的值,然后根据ADC的值来改变正弦波的幅度和频率。具体的流程如下:
1. 初始化TIM2、TIM3和DAC模块的时钟。
2. 配置TIM2的工作模式,使其每隔一段时间触发中断,定时读取ADC的值,并根据ADC的值来改变正弦波的幅度和频率。
3. 配置TIM3的GPIO口,使其输出PWM信号。
4. 配置TIM3的工作模式,使其输出PWM信号,并启用TIM3。
5. 配置DAC模块,使其输出电压。DAC的输出电压控制正弦波的幅度。
6. 初始化正弦波表,计算出正弦波表中各个采样点的电压值。
7. 在主函数中循环输出正弦波表中的采样点,控制TIM3的PWM输出和DAC的电压输出。
注意:这个代码中正弦波表的长度为100,所以正弦波的周期为100个采样点。同时,TIM3的PWM周期根据频率来计算,因此正弦波的频率在TIM2的中断函数中被动态改变。如果你需要改变正弦波的频率范围,可以修改TIM2的时钟频率或者修改ADC的分辨率。
希望这个代码能够帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)