打印

能不能求各位大神帮我看看这个程序,ad,da转换,实现不了,,,,初学者,毕设很头大

[复制链接]
337|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
张さん|  楼主 | 2019-11-24 19:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
adc


adc。c
#include "adc.h"
#include "stm32f4xx.h"
#include "usart.h"
#define ADC1_DR_Address    ((u32)0x4001204C)

vu16 ADC_ConvertedValue [1600];

//初始化DMA
//外设基地址Par
//内存基地址Mar
//传输数据大小Buff
void adc_DMA_Config(void)
{
        DMA_InitTypeDef   DMA_InitStructure;        

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//使能DMA时钟
DMA_InitStructure.DMA_Channel = DMA_Channel_0;  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 1600;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Word;  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);
}                                                                                                                           
void Adc_Init(void)
  { ADC_CommonInitTypeDef ADC_CommonInitStructure;        
        ADC_InitTypeDef       ADC_InitStructure;

   GPIO_InitTypeDef      GPIO_InitStructure;
        

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);        //使能ADC1通道时钟
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* ADC Common Init **********************************************************/
         
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //>84/4=21MHZ(F407ADC在2.4-3.6V供电电压下最大速率36M,稳定速度为30M);
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //失能DMA_MODE
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;// 设置是否打开扫描模式。这里我们设置单次转换所以不打开扫描模式,值为 DISABLE模数转换工作在单通道模式
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在连续转换模式
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //转换由软件而不是外部触发启动
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 0x1;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channe6 configuration *************************************/
  ADC_RegularChannelConfig(ADC1,ADC_Channel_6,1,ADC_SampleTime_3Cycles);// >84/4=21MHZ(F407ADC在2.4-3.6V供电电压下最大速率36M,稳定速度为30M)采样时间为    最大采样频率    为5个ADC时钟周期3加12.5加乘频率
/* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); //使能ADC1的 DMA


  /* Enable ADC3 DMA */

ADC_DMACmd( ADC1, ENABLE);

  /* Enable ADC3 */
  ADC_Cmd(ADC1, ENABLE);

}

        
         

void TIM4_Configuration(void)
{
           TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);   
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = 41;           /*TIM_Period 设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在 0x0000 和0xFFFF 之间。  */
  TIM_TimeBaseStructure.TIM_Prescaler  =44;        /*TIM_Prescaler设置了用来作为 TIMx 时钟频率除数的预分频值。它的取值必须在 0x0000 和0xFFFF 之间。  */
  TIM_TimeBaseStructure.TIM_ClockDivision =0x0;     /*TIM_ClockDivision 设置了时钟分割。*/
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;          /*TIM_CounterMode 选择了计数器模式。TIM_CounterMode_Up: TIM 向上计数模式 */
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  /* Clear TIM2 update pending flag*/
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);

    /* Enable TIM2 Update interrupt */
     TIM_ITConfig(TIM4, TIM_IT_Update,ENABLE );


    /* TIM2 disable counter */
    TIM_Cmd(TIM4, DISABLE);   
}  
void TIM4_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority =0x01;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
void NVIC_Configuration(void)
{

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//NVIC中断分组2:2位抢占优先级,2位响应优先级
}


dac。c


#define DAC_DHR12R1_DACC1DHR   ((uint16_t)0x0FFF)
#include "stm32f4xx.h"
#include "usart.h"
#include "dac.h"
#include "stm32f4xx_dac.h"
vu16  DAC_VALUE[1600];



void GPIO_Configuration(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        
        
        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DAC_TIM_config()
{
TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0x002c;           
TIM_TimeBaseStructure.TIM_Prescaler=0x0029;        
TIM_TimeBaseStructure.TIM_ClockDivision=0x0;     
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;  
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
TIM_Cmd(TIM2, ENABLE);
}
void DAC_config(void)
{
DAC_InitTypeDef            DAC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
DAC_InitStructure.DAC_Trigger=DAC_Trigger_T2_TRGO ;
        DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);

DAC_DMACmd(DAC_Channel_1,  ENABLE);
}

void DAC_DMA_config()
{
DMA_InitTypeDef       DMA_InitStructure;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_InitStructure.DMA_Channel =DMA_Channel_7;  
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_DACC1DHR;
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)&DAC_VALUE;
DMA_InitStructure.DMA_DIR  = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = 1600;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
  }











void Sin_DAC_Config(void)

{
        GPIO_Configuration();
        DAC_TIM_config();
        DAC_config();
        
        DAC_DMA_config();
}        




//#endif

/**
  * @}
  */

/**
  * @}
  */
main
#include "stm32f4xx.h"



#include "usart.h"
#include "adc.h"
#include "dac.h"

extern uint16_t ADC_ConvertedValue[1600];
extern uint16_t DAC_VALUE[1600];
vu16 ADC_TimeOutFlag = 0;//TIM2_ADC中断标志位

void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {

  }
}


int main(void)
{
float DA_value_1;
        vu16 i;
        Delay (0x1ffffff);                     //延时函数初始化
        NVIC_Configuration();        
        TIM4_Configuration();

        TIM4_NVIC_Configuration();
        
     adc_DMA_Config();
         Adc_Init() ;                 //选择ADC1通道6         
        Sin_DAC_Config();
         
        TIM_Cmd(TIM6, ENABLE);

        while(1)
        {
                if(ADC_TimeOutFlag==1)
                {printf("\r\n****************************************************************\r\n");
                        ADC_TimeOutFlag=0;
                        ADC_SoftwareStartConv(ADC1);
                for(i=0;i<1600;i++)
                {
                        DAC_VALUE = ADC_ConvertedValue ;
                        DA_value_1 = DAC_DHR12R1_DACC1DHR;
        
                        printf("The current DA1 value = %4.5fV \r\n", DA_value_1);
                Delay(0x1ffffff);
                }        
        }                                                                                            
}        
}
void TIM (void)
{
           if(TIM_GetITStatus(TIM4, TIM_IT_Update) ==SET)//判断更新中断是否发生
        { TIM_ClearITPendingBit(TIM4 ,TIM_FLAG_Update);
                //清楚更新中断标志
           ADC_TimeOutFlag=1;

        }
}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

448

主题

464

帖子

1

粉丝