STM32 triangle wave output

Publisher:PositiveVibesLatest update time:2016-10-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STM32 can also generate triangular waves. Different from the previous generation of sine waves and step waves, it does not need DMA to help draw points to draw the waveform, but can generate triangular waves by itself. Because the triangular wave is very simple, it is just a linear waveform, so it is not a problem for DAC at all.
Next, I will talk about how to output a triangle wave, still based on my own standard project.
1. Modification of the project
1) The timer is needed in the code, so add stm32f10x_tim.c to the STM32F10x_StdPeriod_Driver working group.
2) In addition to the timer, DAC is also needed, so add stm32f10x_dac.c to the STM32F10x_StdPeriod_Driver working group.
3) Open the stm32f10x_conf.h file and include stm32f10x_tim.h and stm32f10x_dac.h, that is, unblock the statements that originally included these files.
4) Create two new files, TriangleWave.c and TriangleWave.h, and save them in the src and inc folders of the BSP folder respectively, and add TriangleWave.c to the BSP of the project.
 
2. Writing of TriangleWave.c and TriangleWave.h programs
The initialization pins are displayed. The two triangular waves are output on the two pins PA4 and PA5 corresponding to CH1 and CH2 of the DAC, so these two pins need to be configured. The code is as follows:

/****************************************************************
Function: TriangleWave_GPIO_Init
Description: Initialize the pin corresponding to the triangle wave
Input: none
return: none
**********************************************************/
static void TriangleWave_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//Initialize the pin clock

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;//Initialize the pins corresponding to CH1 and CH2 of DAC
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//Analog input
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

Next is the configuration of the timer. The code is as follows:

/****************************************************************
Function: TriangleWave_TIM_Init
Description: Timer initialization required for three-foot wave
Input: none
return: none
**************************************************************/
static void TriangleWave_TIM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//Initialize TIM2 clock

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);//Initialize time base structure
TIM_TimeBaseStructure.TIM_Period = 100;//Period value is 100
TIM_TimeBaseStructure.TIM_Prescaler = 0;//No pre-division
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //Clock is not divided
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //Initialize TIM2

TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); //Set the trigger source to update trigger, update cycle once, then trigger a DAC conversion
TIM_Cmd(TIM2, ENABLE); //Turn on the timer
}

Here, the timer is set to have no frequency division, a period of 100, and an increment count. The trigger source is set to an update trigger. In this way, each time the timer is updated, a DAC conversion is triggered.
Next is the configuration of DAC, let’s take a look at the code first:

/****************************************************************
Function: TriangleWave_DAC_Init
Description: DAC initialization required for triangle wave
Input: none
return: none
**********************************************************/
static void TriangleWave_DAC_Init(void)
{
DAC_InitTypeDef DAC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//Initialize DAC clock

DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;//DAC trigger source is TIM2
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;//Output triangle wave
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;//The digital value corresponding to the amplitude of the triangle wave is 2047, that is, 1.65V, and the frequency is about 72M/100/2048/2=175Hz (a little different from the actual output frequency)
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;//DAC output buffer is not applicable
DAC_Init(DAC_Channel_1, &DAC_InitStructure);//Initialize DAC CH1

DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;//The digital value corresponding to the amplitude of the triangle wave is 1023, that is, 0.825V, and the frequency is about 72M/100/1024/2=252Hz (a little different from the actual output frequency)
DAC_Init(DAC_Channel_2, &DAC_InitStructure);//Initialize DAC CH2

DAC_Cmd(DAC_Channel_1, ENABLE);//Turn on DAC CH1
DAC_Cmd(DAC_Channel_2, ENABLE);//Turn on DAC CH2

DAC_SetDualChannelData(DAC_Align_12b_R, 0x00, 0x00);//Set the data of the two DAC channels to right align, and the digital quantity corresponding to the trough is 0,0
}

The key to generating a triangle wave lies in this code. First, set the trigger source of the DAC to TIM2, and set the waveform generated by the DAC to a triangle wave. The DAC can generate a triangle wave by itself without the help of DMA. Next, set the amplitude of the DAC's CH1 to 2047 and the amplitude of CH2 to 1023. You should know that the DAC is 12 bits, and its highest digital quantity is 4095, so the digital quantity corresponding to the triangle wave output by channel 1 will increase from 0 to 2747 as the timer updates, and then decrease from 2047 to 0; the digital quantity corresponding to the three-foot wave output by channel 2 will increase from 0 to 1023 as the timer updates, and then decrease from 1023 to 0. Finally, set the valley value of the DAC to 0. If the reference voltage is 3.3v, then the waveform of channel 1 will increase from 0v to 1.65v, and then decrease from 1.65v to 0v; the waveform of channel 1 will increase from 0 to 0.825v, and then decrease from 0.825 to 0, so that a triangle wave is formed. There is one more thing to say. In the above code, DAC_SetDualChannelData(DAC_Align_12b_R, 0x00, 0x00); is very important. It means to set the data of the two channels of the DAC to be right-aligned, and set the trough of the waveform of the two channels to 0. If we set it like this: DAC_SetDualChannelData(DAC_Align_12b_R, 0xFF, 0xFF), set the digital value of the trough to 0xff, then the waveform will raise the voltage corresponding to 0xff by about 0.2v. I also want to talk about how to calculate the frequency of the output triangle wave. The clock of the timer is 72M, and its period is set to 100. The amplitude of CH1 of the DAC is set to 2047, and the amplitude of CH2 of the DAC is set to 1023. Then we can calculate the frequency of the output two triangle waves:
CH1 output frequency = 72M / 100 / 2048 / 2 = 175.8Hz
CH2 output frequency = 72M /100 / 1024 / 2 = 351.6Hz
 
You also need to write a total function: TriangleWave_Init() to initialize the above code:

/****************************************************************
Function: TriangleWave_Init
Description: Triangle wave initialization
Input: none
return: none
**************************************************************/

void TriangleWave_Init(void)
{
TriangleWave_GPIO_Init();
TriangleWave_TIM_Init();
TriangleWave_DAC_Init();
}

The code of the TriangleWave.h file is posted below:

#ifndef __TRIANGLEWAVE_H__
#define __TRIABGLEWAVE_H__
#include "stm32f10x.h"

void TriangleWave_Init(void);

#endif

3. Writing the main function
The main function is very simple, just calling some initialization functions, the code is as follows:

/****************************************************** ************
Function: main
Description: mainInput
: none
return: none
************************* **********************************/
int main(void)
{
BSP_Init();
TriangleWave_Init() ;
PRINTF("\nmain() is running!\r\n");
while(1)
{
LED1_Toggle();
Delay_ms(1000);
}
}

4. Testing
Use the probe of the oscilloscope to test the pins of PA4 and PA5 respectively, and then check their waveforms.
PA4, that is, the frequency of the waveform output by CH1 of DAC should be around 175.8Hz in theory. The actual waveform is as follows:
STM32 triangle wave output - ziye334 - ziye334's blog
PA5, that is, the frequency of the waveform output by CH2 of DAC should be around 351.6Hz in theory. The actual waveform is as follows:
STM32 triangle wave output - ziye334 - ziye334's blog
 It can be seen from the graph that the actual output triangular wave frequency is a little different from the theoretically calculated frequency. The frequency set in the code above is only a few hundred Hz. If the frequency is increased to above 1KHz, it will be found that the actual output triangular wave frequency is quite different from the theoretically calculated frequency!

Keywords:STM32 Reference address:STM32 triangle wave output

Previous article:STM32 serial port DMA transmission
Next article:STM32 step wave output

Recommended ReadingLatest update time:2024-11-22 22:18

STM32 precise delay function
#include "stm32f10x_systick.h"      //Use SysTick's normal counting mode to manage delays  //Include delay_us, delay_ms    //Fixed the error of calling in the interrupt and causing an infinite loop  //To prevent inaccurate delays, use a do while structure!      static u8 fac_us=0; //us delay multip
[Microcontroller]
STM32 timer generates PWM--breathing light
Note: This project code has been tested on STM32F407ZET6. 1. PWM definition Pulse Width Modulation: Pulse Width Modulation (PWM) 2. Application (1) Control output voltage and current  (2) Brightness of light  (3) Motor control 3. Analysis of PWM generation and key points (1) Analysis of PWM generation (see
[Microcontroller]
Use of STM32 serial port
1. Why use serial port?      The last article I wrote was on January 20th, and it’s June 7th today, so I haven’t updated in half a year. What happened in the past six months? After the Chinese New Year, I went to find a company for internship, and I felt that I had made great progress there. In fact, most people in th
[Microcontroller]
Use of STM32 serial port
Modify the crystal value in the STM32 library function
The default crystal value in the STM32F407 library file is 25MHz. If the external crystal oscillator is 8MHz, the following places need to be modified: 1) Modify the value of HSE_VALUE Will #define HSE_VALUE ((uint32_t)25) /*! Value of the External oscillator in Hz */ Modify to #define HSE_VALUE ((uint32_t)8) /*!
[Microcontroller]
STM32 USB HID custom device bulk transmission
ST (STMicroelectronics) has written a USB peripheral library for the STM32 series processors and provided good reference examples. This article refers to the examples provided by ST to implement USB bulk transmission on the STM32F4 discovery board. The host side is a USB read and write application written on the Linux
[Microcontroller]
stm32 system clock settings
As shown in Figure 1: Clock tree, it can be seen that Stm32 has 5 clock sources. 8M HSI RC: Internal clock. From the diagram, there are two branches. One is used directly as the system clock SYSCLK, and the other is divided by two first, and then multiplied by PLLMUL as the system clock SYSCLK. 4-16M HSE OSC: External
[Microcontroller]
stm32 system clock settings
stm32 uses malloc to apply for memory and free to release memory caused by memory fragmentation test
In this example, stm32c8t6 mdk3.5 and gcc-arm-none-eabi-4_9 are used. EmBitz IDE ARM GCC Compiler is used. It is not recommended to use memory management functions in single-chip microcomputers, but in IoT applications, memory resources are very valuable. The MQTT protocol requires SSL TLS, data signature MD5, RSA, et
[Microcontroller]
Three-phase energy meter solution based on STM32 of CORTEX
background The electric energy meter is an instrument used to measure electric energy, also known as watt-hour meter, fire meter, energy meter, kilowatt-hour meter, which refers to the instrument that measures various electrical quantities. ARM-based solutions have appeared, but the ARM7 TDMI suitable for the a
[Microcontroller]
Three-phase energy meter solution based on STM32 of CORTEX
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号