This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can't use DAC1 registers

Hello,

I'm not able to use DAC1 and DAC2 registers separately, everytime I build my project I get a warning message saying that "cmsis_iar.h" is obsolete so I can't download and debug because DAC1 gives me an error.

If I don't use the TIM2 triggers for the DAC the program works fine.

This is my program:

#include "header.h"
#include <math.h>

#define N 100
#define PI 3.14159

short int LUT[N];
short int LUT2[N];

void sin_gen(float ampiezza)
{
  float Vsin;
  for(int i=0; i<N; i++){
    Vsin = 1.5 + ampiezza*sin(2*PI*i/N);
    LUT[i] = (short int)(Vsin*4095.0/3.0);
  }
}

void abilitazione_periferiche()
{
  RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_ADC12EN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_DACEN;
  
  GPIOA->MODER |= GPIO_MODER_MODER2 | GPIO_MODER_MODER4;
}

void disabilitazione_periferiche()
{
  DAC->CR &= ~DAC_CR_EN1;
  ADC1->CR |= ADC_CR_ADDIS;
  TIM2->CR1 &= ~TIM_CR1_CEN;
}

void setup_ADC()
{
  ADC1->CR &= ~ADC_CR_ADVREGEN_1;
  ADC1->CR |= ADC_CR_ADVREGEN_0;
  for(int i=0; i<1000; i++);
  
  ADC1_2->CCR |= ADC12_CCR_CKMODE_0;
  
  ADC1->CR |= ADC_CR_ADCAL;
  while(ADC1->CR & ADC_CR_ADCAL);
  
  ADC1->CR |= ADC_CR_ADEN;
  while(!(ADC1->ISR & ADC_ISR_ADRD));
  
  ADC1->CFGR &= ~ADC_CFGR_CONT;
  //ADC1->CFGR |= ADC_CFGR_EXTEN_0;
  //ADC1->CFGR |= (11<<6);
  ADC1->SQR1 |= (3<<6);
  ADC1->SQR1 &= ~ADC_SQR1_L;
  ADC1->SMPR1 |= ADC_SMPR1_SMP3;
}

void setup_DAC()
{
  DAC->CR |= DAC_CR_EN1;
  //DAC->CR |= DAC_CR_TEN1;
  //DAC->CR |= DAC_CR_TSEL1_2;
}

void setup_TIM2()
{
  TIM2->ARR = 36000000;
  TIM2->CR2 |= TIM_CR2_MMS2_1;
}

void main()
{
  sin_gen(1.3);
  
  abilitazione_periferiche();
  
  setup_DAC();
  setup_ADC();
  setup_TIM2();
  
  TIM2->CNT = 0;
  TIM2->CR1 |= TIM_CR1_CEN;
  
  //for(int i=0; i<1000; i++);
  //ADC1->CR |= ADC_CR_ADSTART;
  
  for(int i=0; i<N; i++){
    DAC->DHR12R1 = LUT[i];
    while(!(TIM2->SR & TIM_SR_UIF));
    TIM2->SR &= ~TIM_SR_UIF;
    for(int j=0; j<1000; j++);
    
    ADC1->CR |= ADC_CR_ADSTART;
    while(!(ADC1->ISR & ADC_ISR_EOC));
    
    LUT2[i] = ADC1->DR;
  }
  
  disabilitazione_periferiche();
  
  while(1);
}