找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2193|回复: 2
打印 上一主题 下一主题
收起左侧

关于STM32输出1vpwm dac电压的程序 求救中,失眠

[复制链接]
回帖奖励 5 黑币 回复本帖可获得 5 黑币奖励! 每人限 1 次
跳转到指定楼层
楼主
ID:219729 发表于 2017-7-18 11:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有哪位大神有关于STM32输出1vpwm dac电压的程序,这个单片机库函数好多,脑袋都大了
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:155507 发表于 2017-7-18 15:45 | 只看该作者
如何让STM32的PWM DAC输出幅值在0-5V的
  1. #include "stm32f10x.h"

  2. //DAC通道1输出初始化
  3. void Dac1_Init(void)
  4. {
  5.         GPIO_InitTypeDef GPIO_InitStructure;
  6.         DAC_InitTypeDef DAC_InitType;

  7.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); //使能PORTA通道时钟
  8.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE ); //使能DAC通道时钟

  9.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // 端口配置
  10.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入
  11.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  13.         //GPIO_SetBits(GPIOA,GPIO_Pin_4) ;//PA.4 输出高
  14.         DAC_InitStruct.DAC_Trigger = DAC_Trigger_Software; //触发方式改为软件触发

  15.         DAC_InitType.DAC_Trigger=DAC_Trigger_None; //不使用触发功能 TEN1=0
  16.         DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形发生
  17.         DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值设置
  18.         DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1输出缓存关闭 BOFF1=1
  19.         //DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Enable;
  20.         DAC_Init(DAC_Channel_1,&DAC_InitType); //初始化DAC通道1

  21.         DAC_Cmd(DAC_Channel_1, ENABLE); //使能DAC1
  22.         DAC_SetChannel1Data(DAC_Align_12b_R, 0); //12位右对齐数据格式设置DAC值
  23.         DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
  24. }

  25. //设置通道1输出电压
  26. //vol:0~3300,代表0~3.3V
  27. void Dac1_Set_Vol(u16 vol)
  28. {
  29.         float temp=vol;
  30.         temp/=1000;
  31.         temp=temp*4095/3.3;
  32.         DAC_SetChannel1Data(DAC_Align_12b_R,temp);//12位右对齐数据格式设置DAC值
  33.         DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
  34. }

  35. int main()
  36. {
  37.         Dac1_Init();
  38.         while(1)
  39.         {
  40.                 int NUM=0;
  41.                 Dac1_Set_Vol(3300);
  42.                 DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
  43.                 NUM=DAC_GetDataOutputValue(DAC_Channel_1);       
  44.         }

  45. }

  46. /**
  47. * @brief Inserts a delay time.
  48. * @param nCount: specifies the delay time length.
  49. * @retval None
  50. */
  51. void Delay(__IO uint32_t nCount)
  52. {
  53.         for(; nCount != 0; nCount--);
  54. }

  55. #ifdef USE_FULL_ASSERT

  56. /**
  57. * @brief Reports the name of the source file and the source line number
  58. * where the assert_param error has occurred.
  59. * @param file: pointer to the source file name
  60. * @param line: assert_param error line source number
  61. * @retval None
  62. */
  63. void assert_failed(uint8_t* file, uint32_t line)
  64. {
  65.         /* User can add his own implementation to report the file name and line number,
  66. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  67.         /* Infinite loop */
  68.         while (1)
  69.         {
  70.         }
  71. }

  72. #endif
复制代码
回复

使用道具 举报

板凳
ID:123289 发表于 2017-7-19 06:41 | 只看该作者
为什么不弄清PWM的原理呢,真正懂了,再多也忽悠不了你了。你应当先有自己的思路,再去参考别人的。
错就错在:你想吃现成饭。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表