stm32f4 dac如何产生三角波

[复制链接]
7421|12
 楼主| asun1388 发表于 2013-7-17 00:55 | 显示全部楼层 |阅读模式
本帖最后由 asun1388 于 2013-7-17 07:55 编辑

stm32f407 dac如何产生三角波,使用定时器

dac代码如下
  1. void DAC_Configuration(void)
  2. {
  3.         DAC_InitTypeDef        DAC_InitStructure;
  4.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC , ENABLE);
  5.         DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;        //选择定时器2作外部触发源
  6.           DAC_InitStructure.DAC_WaveGeneration =DAC_Wave_Triangle;   //产生三角波
  7.         DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047; //三角波的高为2047  最高可以为4095                 
  8.           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;           //无输出缓冲 提高驱动能力可以打开缓冲
  9.           DAC_Init(DAC_Channel_1, &DAC_InitStructure);

  10. DAC_DMACmd(DAC_Channel_1, DISABLE);                //不使用DMA

  11.         DAC_Cmd(DAC_Channel_1, ENABLE);

  12. }
dac gpio口配置如下
  1. void GPIO_Configuration(void)
  2. {
  3.           GPIO_InitTypeDef GPIO_InitStructure;

  4.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  5.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  6.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                        
  7.           GPIO_Init(GPIOA , &GPIO_InitStructure);
  8. }
tim 配置如下
  1. void TIM_Configuration(void)
  2. {
  3.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  4.         TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  5.      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
  6.     TIM_TimeBaseStructure.TIM_Period = 168;         
  7.     TIM_TimeBaseStructure.TIM_Prescaler = 0x0;      
  8.     TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  9.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  10.     TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);     
  11.       
  12.     TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); //使用更新事件作为触发输出
  13.         
  14.            TIM_Cmd(TIM2, ENABLE);
  15. }
如何和定时器联系起来,求解。

评论

楼主解决没,求一份代码1723614701@qq.com谢谢  发表于 2018-7-23 16:41
IJK 发表于 2013-7-17 09:47 | 显示全部楼层
印象里DAC可以由某个timer触发,可以看看是不是timer2
IJK 发表于 2013-7-17 09:50 | 显示全部楼层
看了一眼,timer2、4~8都可以的。另外,可以看看Figure 119. General-purpose timer block diagram
airwill 发表于 2013-7-17 16:37 | 显示全部楼层
DAC 有自动生成三角波的功能. 根本不用软件干预
cjhk 发表于 2013-7-17 18:53 | 显示全部楼层
不是很了解这一块   楼主  帮你顶一个   需要仔细钻研钻研   顶一个
乌月明星稀 发表于 2013-7-21 13:31 | 显示全部楼层
你GPIO口配置成模拟输入的,这个你可以看参考手册
a360013139 发表于 2013-7-25 19:53 | 显示全部楼层
顶一个···mark
swallow_sw 发表于 2016-1-20 14:37 | 显示全部楼层
DAC 有自动生成三角波的功能. 根本不用软件干预,这种方式是可以产生三角波,但是频率太小了,不大实用
swallow_sw 发表于 2016-1-21 18:41 | 显示全部楼层
void Dac1_Init(void)
{  
  GPIO_InitTypeDef GPIO_InitStructure;
        DAC_InitTypeDef DAC_InitType;
       
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
          
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

        DAC_InitType.DAC_Trigger=DAC_Trigger_T6_TRGO;       
        DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_Triangle;
        DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_TriangleAmplitude_4095;
        DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;       
  DAC_Init(DAC_Channel_1,&DAC_InitType);       
       
        DAC_Cmd(DAC_Channel_1, ENABLE);  
  DAC_SetChannel1Data(DAC_Align_12b_R, 30);
}

别的不修改就ok了,已测试

swallow_sw 发表于 2016-1-21 18:42 | 显示全部楼层
swallow_sw 发表于 2016-1-21 18:41
void Dac1_Init(void)
{  
  GPIO_InitTypeDef GPIO_InitStructure;

  DAC_InitType.DAC_Trigger=DAC_Trigger_T2_TRGO;  
用TIM2一样的
jizhendong1994 发表于 2017-1-11 19:10 | 显示全部楼层
swallow_sw 发表于 2016-1-21 18:41
void Dac1_Init(void)
{  
  GPIO_InitTypeDef GPIO_InitStructure;

请问主函数该怎样写呀??只要求在pa4上输出三角波。
gejigeji521 发表于 2017-1-11 23:22 | 显示全部楼层
DAC应该可以生成基本的常用波。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

45

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部