• STM32——DAC生成正弦波


    #include <stdio.h>
    #include "math.h"
    
    /*
    int main()//别人的 参考下
    {
        int index=256;
        double out=0;
        double pi=3.1415;
        for (int i = 0; i < index; i++)
        {
            out = ((sin((2*i*pi)/index)+1)*2047);
            printf("the buffer is:%lf",out);
        }
        return 0;
    }
    */
    
    
    /*
    int main(int argc, char const *argv[])//一个完整的周期
    {
        int index=255;
        float pi = 3.1415;
        int Fori=0;
        float outV;
        for (int Fori = 0; Fori <= index; Fori++)
        {
           outV = sin(2*pi/index*Fori)*3300;
           printf("%lf	",outV);
           if(!((Fori+1)%8))
           printf("
    ");
        }
        return 0;
    }*/
    
    
    int main(int argc, char const *argv[])//半个周期
    {
        int index=255;
        float pi = 3.1415;
        int Fori=0;
        float outV;
        for (int Fori = 0; Fori <= index; Fori++)
        {
           outV = sin(pi/index*Fori)*3300;
           printf("%lf	",outV);
           if(!((Fori+1)%8))
           printf("
    ");
        }
        return 0;
    }
    
    

    运行结果——数据:
    半个周期:
    半个周期
    一个周期:
    在这里插入图片描述

    在这里插入图片描述
    程序:就是通过DAC把这些点发出去
    图片上的(sin((2*PI)/256))*X应该是(sin(X2*PI/256)),半个周期同理**

    /*补上代码*/
    
    void Dac1_Init(void)
    {
    	GPIO_InitTypeDef GPIO_InitStructure;
    	DAC_InitTypeDef DAC_InitType;
    
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );	  //使能PORTA通道时钟
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE );	  //使能DAC通道时钟 
    
    	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				 // 端口配置
     	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 		 //模拟输入
     	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     	GPIO_Init(GPIOA, &GPIO_InitStructure);
    	GPIO_SetBits(GPIOA,GPIO_Pin_4)	;//PA.4 输出高
    					
    	DAC_InitType.DAC_Trigger=DAC_Trigger_None;	//不使用触发功能 TEN1=0
    	DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形发生
    	DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值设置
    	DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;	//DAC1输出缓存关闭 BOFF1=1
      DAC_Init(DAC_Channel_1,&DAC_InitType);	 //初始化DAC通道1
    
    	DAC_Cmd(DAC_Channel_1, ENABLE);  //使能DAC1
      
      DAC_SetChannel1Data(DAC_Align_12b_R, 0);  //12位右对齐数据格式设置DAC值
    }
    
    void TIM4_Config(void)
    {
    	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    	NVIC_InitTypeDef				 NVIC_InitTypeStructure;
    	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);	//使能定时器4时钟
    	
    	TIM_TimeBaseStructure.TIM_Period = 1000-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
    	TIM_TimeBaseStructure.TIM_Prescaler =72-1; //设置用来作为TIMx时钟频率除数的预分频值 
    	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
    	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
    	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
    	
    	NVIC_InitTypeStructure.NVIC_IRQChannel=TIM4_IRQn;
    	NVIC_InitTypeStructure.NVIC_IRQChannelPreemptionPriority=3;
    	NVIC_InitTypeStructure.NVIC_IRQChannelSubPriority=3;
    	NVIC_InitTypeStructure.NVIC_IRQChannelCmd=ENABLE;
    	NVIC_Init(&NVIC_InitTypeStructure);
    	
    	TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    	TIM_Cmd(TIM4, ENABLE);  //使能TIM3
    }
    
    void TIM4_IRQHandler(void)
    {
    	if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)==SET)
    	{
    		
    		Vout=sin(PI/255*i)*4095;
    		Vdac=Vout;
    		printf("Vdac = %d, Vout = %lf 
    ",Vdac,Vout);
    		i++;
    		if(i==255)
    			i=0;
    		TIM_ClearFlag(TIM4, TIM_FLAG_Update);
    	}
    }
    
    

    printf("Vdac = %d, Vout = %lf 
    ",Vdac,Vout);
    

    DAC_SetChannel1Data(DAC_Align_12b_R, Vdac);  //12位右对齐数据格式设置DAC值
    

    替换即可

  • 相关阅读:
    SpringMVC 中整合之JSON、XML
    解决Django扩展用户表后新表内增加用户密码存储为明文
    python之路:始解(四)
    python作业:select版本FTP
    python作业:高级FTP程序
    python归档:笔记转化
    python之路:始解(三)
    python之路:始解(二)
    python作业:FTP程序
    python文件操作:pickle模块多次dump后出现的读取问题
  • 原文地址:https://www.cnblogs.com/hhsxy/p/14018409.html
Copyright © 2020-2023  润新知