打印
[STM32WB]

STM32f103c8t6输出三角波频率不对(语言-c#)

[复制链接]
94|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
狗啃模拟|  楼主 | 2024-4-16 17:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
为什么我输出的三角波频率不是1khz

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
void DAC_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    DAC_InitTypeDef DAC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE );//使能dac时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//使能GpioA时钟

    //Gpio4为模拟输出
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

        DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; // 触发源为TIM2 TRGO
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; // 生成三角波
    DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095; // 设置振幅为4095
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; // 禁用输出缓冲
    DAC_Init(DAC_Channel_1, &DAC_InitStructure);

    DAC_Cmd(DAC_Channel_1,ENABLE);//使能DAC通道1
//    DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);//开启DAC通道1
}

void TIM2_waveInit(uint16_t arr, uint16_t psc)
    {

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = arr; // 设定计数器自动重装值
    TIM_TimeBaseStructure.TIM_Prescaler = psc; // 预分频器
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    // 选择更新事件作为触发输出
    TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);

    // 使能定时器2
    TIM_Cmd(TIM2, ENABLE);
    }

     int main(void)
{

TIM2_waveInit(999,71);
DAC_init();   

    while (1)
    {        

    }
}


使用特权

评论回复
沙发
heisexingqisi| | 2024-4-16 22:09 | 只看该作者
可以示波器看看是多少啊,然后调整一下参数看看,直到调整对。

使用特权

评论回复
板凳
elephant00| | 2024-4-17 10:14 | 只看该作者
晶振稳不稳定

使用特权

评论回复
地板
两只袜子| | 2024-4-17 12:00 | 只看该作者
时钟配置有误

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

49

主题

648

帖子

0

粉丝