久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3732|回復: 2
打印 上一主題 下一主題
收起左側

這個PCF8591的stm32程序如何輸出“正弦波_方波_鋸齒波”?請教!

[復制鏈接]
跳轉到指定樓層
樓主
ID:82098 發表于 2018-6-5 15:36 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
請教!這個程序如何輸出“正弦波_方波_鋸齒波”?


單片機源程序如下:
  1. /******************** (C) COPYRIGHT 2013 CEPARK多功能開發學習板********************
  2. * 文件名  :main.c
  3. * 描述    :CPU通過控制PCF8591輸出不同模擬值,使LED燈漸暗漸明。
  4. * 實驗平臺:CEPARK多功能開發學習板
  5. * 庫版本  :ST3.5.0

  6. * 硬件連接:------------------------
  7. * 單片機IO |  PB10 - JP68 - SCL           | AD/DA轉換模塊
  8.                                                 |  PB11 - JP68 - SDA           |
  9. *           ------------------------
  10. * 硬件連接:JP66短接,用于AD顯示
  11. * 硬件連接:J3上端連接到核心板的J7(3.3V)
  12. **********************************************************************************/

  13. #include "stm32f10x.h"
  14. #include "PCF8591.h"
  15. #include "delay.h"
  16. #include "iic.h"


  17. /*
  18. * 函數名:main
  19. * 描述  : 主函數
  20. * 輸入  :無
  21. * 輸出  : 無
  22. */
  23. int main(void)
  24. {                              
  25.         u8 i;
  26.        
  27.        
  28.         /* 設置系統時鐘為72M */
  29.           SystemInit();
  30.        
  31.           PCF8591_Init();                        //IIC初始化
  32.                                                                                   
  33.         while(1)
  34.         {
  35.                 for(i=255;i>0;i--)
  36.                 {
  37.                         pcf8591_da_write(i);       
  38.                         delay_ms(10);
  39.                 }
  40.                
  41.                 delay_ms(500);
  42.                
  43.                 for(i=0;i<255;i++)
  44.                   {
  45.                             pcf8591_da_write(i);
  46.                             delay_ms(10);
  47.                   }
  48.         }
  49. }

復制代碼

所有資料51hei提供下載:
PCF8591 DA實驗.rar (268.27 KB, 下載次數: 59)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:155507 發表于 2018-6-5 22:20 | 只看該作者
DA芯片先輸出高電平,延時一下,再輸出低電平,再延時一下,如此反復就是方波。
三角波、正弦波一個道理。


  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f4_discovery.h"
  3. #include <stdio.h>

  4. /* Private typedef -----------------------------------------------------------*/
  5. GPIO_InitTypeDef  GPIO_InitStructure;
  6. USART_InitTypeDef USART_InitStructure;
  7. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  8. TIM_OCInitTypeDef  TIM_OCInitStructure;
  9. /* Private define ------------------------------------------------------------*/
  10. // This is the magic number used to increment the phase accumulator
  11. #define R 107374182
  12. /* Private macro -------------------------------------------------------------*/
  13. /* Private variables ---------------------------------------------------------*/
  14. uint16_t sinetable[]  = {
  15.         127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  16.         242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  17.         221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  18.         76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  19.         33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124
  20. };
  21. uint16_t pulse_width = 128;
  22. uint32_t phase_accumulator = 0;
  23. uint8_t angle = 0;
  24. /* Private function prototypes -----------------------------------------------*/
  25. void TIM_Config(void);
  26. void PWM_Config(int period);
  27. void PWM_SetDC(uint16_t channel,uint16_t dutycycle);
  28. void Delay(__IO uint32_t nCount);
  29. void LED_Config(void);
  30. void INTTIM_Config(void);
  31. /* Private functions ---------------------------------------------------------*/



  32. void TIM2_IRQHandler(void)
  33. {
  34.         if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  35.         {
  36.                 GPIO_SetBits(GPIOD, GPIO_Pin_13);
  37.                 PWM_SetDC(1,pulse_width);
  38.                 // Calculate a new pulse width
  39.                 phase_accumulator+=R;
  40.                 angle=(uint8_t)(phase_accumulator>>24);
  41.                 pulse_width = sinetable[angle];
  42.                 TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  43.                 GPIO_ResetBits(GPIOD, GPIO_Pin_13);
  44.         }
  45. }
  46. /**
  47. * @brief  Main program
  48. * @param  None
  49. * @retval None
  50. */
  51. int main(void)
  52. {

  53.         /* TIM Configuration */
  54.         TIM_Config();
  55.         /* LED Configuration */
  56.         LED_Config();
  57.         /* PWM Configuration */
  58.         PWM_Config(256);
  59.         /* Sampling rate timer */
  60.         INTTIM_Config();

  61.         PWM_SetDC(1,128);
  62.         PWM_SetDC(2,128);
  63.         PWM_SetDC(3,128);
  64.         PWM_SetDC(4,128);
  65.         while (1)
  66.         {
  67.                 // Wait for sampling clock to overflow
  68.                 if (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) != RESET)
  69.                 {
  70.                        
  71.                 }
  72.         }
  73. }

  74. /**
  75. * @brief  Configure the TIM3 Ouput Channels.
  76. * @param  None
  77. * @retval None
  78. */
  79. void TIM_Config(void)
  80. {
  81.         GPIO_InitTypeDef GPIO_InitStructure;

  82.         /* TIM3 clock enable */
  83.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  84.         /* GPIOC and GPIOB clock enable */
  85.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOB, ENABLE);

  86.         /* GPIOC Configuration: TIM3 CH1 (PC6) and TIM3 CH2 (PC7) */
  87.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
  88.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  89.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  90.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  91.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  92.         GPIO_Init(GPIOC, &GPIO_InitStructure);

  93.         /* GPIOB Configuration:  TIM3 CH3 (PB0) and TIM3 CH4 (PB1) */
  94.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  95.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  96.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  97.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  98.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  99.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  100.         /* Connect TIM3 pins to AF2 */  
  101.         GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
  102.         GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
  103.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3);
  104.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM3);
  105. }

  106. void PWM_Config(int period)
  107. {
  108.         uint16_t PrescalerValue = 0;
  109.         /* -----------------------------------------------------------------------
  110.         TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles.
  111.        
  112.         In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1),
  113.         since APB1 prescaler is different from 1.   
  114.         TIM3CLK = 2 * PCLK1  
  115.         PCLK1 = HCLK / 4
  116.         => TIM3CLK = HCLK / 2 = SystemCoreClock /2
  117.                
  118.         To get TIM3 counter clock at 28 MHz, the prescaler is computed as follows:
  119.         Prescaler = (TIM3CLK / TIM3 counter clock) - 1
  120.         Prescaler = ((SystemCoreClock /2) /28 MHz) - 1
  121.                                                                                        
  122.         To get TIM3 output clock at 30 KHz, the period (ARR)) is computed as follows:
  123.         ARR = (TIM3 counter clock / TIM3 output clock) - 1
  124.                 = 665

  125.         Note:
  126.         SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
  127.         Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  128.         function to update SystemCoreClock variable value. Otherwise, any configuration
  129.         based on this variable will be incorrect.   
  130. ----------------------------------------------------------------------- */  

  131.         /* No prescale, run at maximum frequency */
  132.         PrescalerValue = 0;

  133.         /* Time base configuration */
  134.         TIM_TimeBaseStructure.TIM_Period = period;
  135.         TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  136.         TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  137.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  138.         TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  139.         /* PWM1 Mode configuration: Channel1 */
  140.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  141.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  142.         TIM_OCInitStructure.TIM_Pulse = 0;
  143.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  144.         TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  145.         TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

  146.         /* PWM1 Mode configuration: Channel2 */
  147.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  148.         TIM_OCInitStructure.TIM_Pulse = 0;

  149.         TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  150.         TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

  151.         /* PWM1 Mode configuration: Channel3 */
  152.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  153.         TIM_OCInitStructure.TIM_Pulse = 0;

  154.         TIM_OC3Init(TIM3, &TIM_OCInitStructure);

  155.         TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

  156.         /* PWM1 Mode configuration: Channel4 */
  157.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  158.         TIM_OCInitStructure.TIM_Pulse = 0;

  159.         TIM_OC4Init(TIM3, &TIM_OCInitStructure);

  160.         TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

  161.         TIM_ARRPreloadConfig(TIM3, ENABLE);

  162.         /* TIM3 enable counter */
  163.         TIM_Cmd(TIM3, ENABLE);
  164. }

  165. void PWM_SetDC(uint16_t channel,uint16_t dutycycle)
  166. {
  167.         if (channel == 1)
  168.         {
  169.                 TIM3->CCR1 = dutycycle;
  170.         }
  171.         else if (channel == 2)
  172.         {
  173.                 TIM3->CCR2 = dutycycle;
  174.         }
  175.         else if (channel == 3)
  176.         {
  177.                 TIM3->CCR3 = dutycycle;
  178.         }
  179.         else
  180.         {
  181.                 TIM3->CCR4 = dutycycle;
  182.         }
  183. }

  184. void LED_Config(void)
  185. {
  186.         /* GPIOD Periph clock enable */
  187.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  188.         /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  189.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  190.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  191.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  192.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  193.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  194.         GPIO_Init(GPIOD, &GPIO_InitStructure);

  195. }

  196. void INTTIM_Config(void)
  197. {
  198.         NVIC_InitTypeDef NVIC_InitStructure;
  199.         /* Enable the TIM2 gloabal Interrupt */
  200.         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  201.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  202.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  203.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  204.         NVIC_Init(&NVIC_InitStructure);

  205.         /* TIM2 clock enable */
  206.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  207.         /* Time base configuration */
  208.         TIM_TimeBaseStructure.TIM_Period = 10 - 1;  // 1 MHz down to 100 KHz
  209.         TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1;
  210.         TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  211.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  212.         TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  213.         /* TIM IT enable */
  214.         TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  215.         /* TIM2 enable counter */
  216.         TIM_Cmd(TIM2, ENABLE);
  217. }

  218. /**
  219. * @brief  Delay Function.
  220. * @param  nCount:specifies the Delay time length.
  221. * @retval None
  222. */
  223. void Delay(__IO uint32_t nCount)
  224. {
  225.         while(nCount--)
  226.         {
  227.         }
  228. }

復制代碼
回復

使用道具 舉報

板凳
ID:780116 發表于 2020-6-28 11:34 | 只看該作者
請問這是用的stm32那個系列的芯片
回復

使用道具 舉報

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

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品久久视频 | 亚洲欧美精品在线观看 | 色永久 | 亚洲精品欧洲 | 日本成人中文字幕在线观看 | 欧美黄在线观看 | 亚洲国产欧美国产综合一区 | 亚洲精品一区在线观看 | 国产精品国产三级国产播12软件 | 国产精品久久久久久吹潮 | 亚洲国产成人精品久久久国产成人一区 | 国产美女自拍视频 | 久久久精品一区二区 | 正在播放国产精品 | 国内精品视频在线观看 | 免费看91| 久久免费视频1 | 国产精品视频一区二区三区不卡 | 黄视频国产| 亚洲国产高清在线观看 | 国产香蕉视频 | 四虎影院免费在线播放 | 九色在线观看 | 成人精品一区二区三区中文字幕 | 久久午夜剧场 | 久久久久久91香蕉国产 | 一级欧美一级日韩片免费观看 | 一区二区视频 | 91在线区| 成人一区二区三区在线观看 | 91免费看片| 中文精品视频 | 这里只有精品999 | 日韩欧美国产不卡 | 国产一区二区成人 | 日韩中文字幕一区二区 | 亚洲国产成人久久综合一区,久久久国产99 | 蜜桃av人人夜夜澡人人爽 | www久久| 国产一级在线 | 手机看片1 |