找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3178|回复: 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, 下载次数: 58)


分享到:  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 单片机教程网

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