找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2683|回復(fù): 2
收起左側(cè)

關(guān)于STM32輸出1vpwm dac電壓的程序 求救中,失眠

[復(fù)制鏈接]
回帖獎勵 5 黑幣 回復(fù)本帖可獲得 5 黑幣獎勵! 每人限 1 次
ID:219729 發(fā)表于 2017-7-18 11:19 | 顯示全部樓層 |閱讀模式
有哪位大神有關(guān)于STM32輸出1vpwm dac電壓的程序,這個單片機庫函數(shù)好多,腦袋都大了
回復(fù)

使用道具 舉報

ID:155507 發(fā)表于 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; //觸發(fā)方式改為軟件觸發(fā)

  15.         DAC_InitType.DAC_Trigger=DAC_Trigger_None; //不使用觸發(fā)功能 TEN1=0
  16.         DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形發(fā)生
  17.         DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值設(shè)置
  18.         DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1輸出緩存關(guān)閉 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位右對齊數(shù)據(jù)格式設(shè)置DAC值
  23.         DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
  24. }

  25. //設(shè)置通道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位右對齊數(shù)據(jù)格式設(shè)置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
復(fù)制代碼
回復(fù)

使用道具 舉報

ID:123289 發(fā)表于 2017-7-19 06:41 | 顯示全部樓層
為什么不弄清PWM的原理呢,真正懂了,再多也忽悠不了你了。你應(yīng)當(dāng)先有自己的思路,再去參考別人的。
錯就錯在:你想吃現(xiàn)成飯。
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表