Skip to main content
abdelkadermeftah62
Associate
December 10, 2014
Question

sawtooth

  • December 10, 2014
  • 3 replies
  • 809 views
Posted on December 10, 2014 at 17:26

how i can generate a wave sawtooth ? please i need somone help me 

#poorly-framed-question
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    December 10, 2014
    Posted on December 10, 2014 at 17:43

    Can you use the DAC on your non-specific STM32 to output a pattern buffer in a loop?

    A blind example from a prior thread

    // STM32F4-Discovery Dual DAC 6.25 KHz Sine - sourcer32@gmail.com
    #include ''stm32f4_discovery.h''
    /**************************************************************************/
    const uint16_t Sine12bit[32] = {
    2047, 2447, 2831, 3185,
    3498, 3750, 3939, 4056,
    4095, 4056, 3939, 3750,
    3495, 3185, 2831, 2447,
    2047, 1647, 1263, 909,
    599, 344, 155, 38,
    0, 38, 155, 344,
    599, 909, 1263, 1647};
    uint16_t SineTable[64];
    /**************************************************************************/
    void RCC_Config(void)
    {
    /* DMA1 clock and GPIOA clock enable (to be used with DAC) */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE);
    /* DAC and TIM6 Peripheral clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC | RCC_APB1Periph_TIM6, ENABLE);
    }
    /**************************************************************************/
    void GPIO_Config(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    }
    /**************************************************************************/
    void TIM6_Config(void)
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    int Period;
    Period = ((SystemCoreClock / 2) / 200000); // 200 KHz timebase, 6.25 KHz Sine
    /* Time base configuration */
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = Period - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
    /* TIM6 TRGO selection */
    TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
    /* TIM6 enable counter */
    TIM_Cmd(TIM6, ENABLE);
    }
    /**************************************************************************/
    #define DAC_DHR12RD_ADDRESS 0x40007428 // Right Align 12-bit Dual
    void DAC_Config(void)
    {
    DAC_InitTypeDef DAC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    /* DAC channel 1 and 2 Configuration */
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
    DAC_Init(DAC_Channel_1, &DAC_InitStructure);
    DAC_Init(DAC_Channel_2, &DAC_InitStructure);
    /* DMA1_Stream5 channel7 configuration **************************************/
    DMA_DeInit(DMA1_Stream5);
    DMA_InitStructure.DMA_Channel = DMA_Channel_7;
    DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_ADDRESS;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&SineTable[0];
    DMA_InitStructure.DMA_BufferSize = 32; // 16-bit x 64 / 2
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; // 32-bit 16x2
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
    DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    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(DMA1_Stream5, &DMA_InitStructure);
    /* Enable DMA1_Stream5 */
    DMA_Cmd(DMA1_Stream5, ENABLE);
    /* Enable DAC Channel 1 and 2 */
    DAC_Cmd(DAC_Channel_1, ENABLE);
    DAC_Cmd(DAC_Channel_2, ENABLE);
    /* Enable DMA for DAC Channel1 */
    DAC_DMACmd(DAC_Channel_1, ENABLE);
    }
    /**************************************************************************/
    int main(void)
    {
    int i, j;
    j = 0;
    for(i=0; i<32; i++)
    {
    SineTable[j++] = Sine12bit[i];
    SineTable[j++] = Sine12bit[(i + 8) % 32]; // 90 degree phase shift on Channel 2
    }
    RCC_Config();
    GPIO_Config();
    TIM6_Config();
    DAC_Config();
    while(1); // Do not exit
    }

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    abdelkadermeftah62
    Associate
    December 10, 2014
    Posted on December 10, 2014 at 17:54

    it's oblige to work with DMA ? 

    Tesla DeLorean
    Guru
    December 10, 2014
    Posted on December 10, 2014 at 17:59

    it's oblige to work with DMA ?

    No, but combining the DAC+TIM+DMA allows for controlled and accurate waveforms and periodicity.

    The DAC also has other modes, you could review those also.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..