AnsweredAssumed Answered

STM32F303VC DAC signal generation and period measure using EXTI interrupt

Question asked by Antonio Santoro on May 12, 2018

I'm trying to generate a square wave signal using the DAC channel 2 (PA5) with a 1.627 ms period and then measuring that by catching the rising edge trigger in an EXTI4 interrupt on the PF4 pin. The problem is that I have to use the DAC channel 1 to see the EXTI4 triggers and the measured period is not what I expect to be.
This is the complete program, I'm working using IAR Embedded Workbench 8.22:

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

#define N 100
//#define tau 1.38*pow(10,-8)


int j=0, flag=0;
float periodi[N];

void setup(){
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOFEN;
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN | RCC_APB1ENR_DACEN;

GPIOA->MODER |= GPIO_MODER_MODER5;
GPIOF->MODER |= GPIO_MODER_MODER4;

NVIC->ISER[0] |= (1<<10 | 1<<29);
SYSCFG->EXTICR[2] |= SYSCFG_EXTIRCR_EXTI4_PF;
EXTI->IMR |= EXTI_IMR_MR4;
EXTI->RTSR |= EXTI_RTSR_TR4;
}

void setupTIM2(){
TIM2->PSC = 71;
TIM2->CR1 |= TIM_CR1_CEN;
TIM2->CNT = 0;
}

// 65536 : 0.0009*PSC = 65536 : 0.8135 ==> PSC = 0.8135 / 0.0009 ==> PSC = 904

// 65536 : 0.8228 = ARR : 0.8135 ==> ARR = (65536 / 0.8228) * 0.8135 = 64795

void setupTIM3(){
TIM3->PSC = 903;
TIM3->ARR = 64795;
TIM3->DIER |= TIM_DIER_UIE;
TIM3->CR1 |= TIM_CR1_CEN;
TIM3->CNT = 0;
}


void TIM3_IRQHandler(){
TIM3->SR &= ~TIM_SR_UIF;
//printf("TIM3\n");
if(flag==0){
DAC->DHR12R1 = 0;
flag = 1;
for(int i=0; i<1000; i++);
}
else{
DAC->DHR12R1 = 4095;
flag = 0;
for(int i=0; i<1000; i++);
}

}

void EXTI4_IRQHandler(){
EXTI->PR |= EXTI_PR_PR4;
//printf("EXTI4\n");
if(j<N){
periodi[j] = TIM2->CNT*tau;
j++;
TIM2->CNT = 0;

}
else{
TIM2->CR1 &= ~TIM_CR1_CEN;
TIM3->CR1 &= ~TIM_CR1_CEN;
DAC->CR &= ~DAC_CR_EN1;
}
}

void setupDAC(){
DAC->CR |= DAC_CR_EN1;
}

void main(){
setup();
setupDAC();
setupTIM2();
setupTIM3();

while(1);
}

Outcomes