Analysis and arrangement of Zhilin STM32 program source code 04

Publisher:devilcoreLatest update time:2015-08-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Timer Configuration

1 Overview

   The STM32 timer has many functions and complex configurations. I don't know much about it now, so I'll learn while using it. There are two places where the timer is used on the Zhilin development board. One is the backlight power supply of the LCD, which requires the output of a certain PWM waveform for rectification and power supply; the other is used to control the speaker, which is also the clock pulse output.

   The first choice for the timer is counting or timing. I use timing here. The output mode can be the capture of the timer during input, or the setting of output when the comparison matches (this one is often used).

2. Source program

void TIMx_Config(void) {

 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure; //Time base unit configuration data structure

 TIM_OCInitTypeDef  TIM_OCInitStructure; //Output mode setting data structure

 

   

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2, multiplexed output mode

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //For timing 2 channel 3

 GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 

 TIM_TimeBaseStructure.TIM_Period = CCR3_Val; //40 is written to the auto-load register, 150KHz output.

 TIM_TimeBaseStructure.TIM_Prescaler = 2; //Division factor 3

 TIM_TimeBaseStructure.TIM_ClockDivision = 0; // related to sampling, not used here

 //The characteristics of upward counting are: from 0 to the loaded value, an overflow occurs; then counting starts from 0 again.

 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

 

 

 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //Timer count flips when it matches the comparison channel

 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  // Enable output channel

 TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //Compare the count value of the channel

 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Low level is valid

 TIM_OC3Init(TIM2, &TIM_OCInitStructure);

 

 TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);  //Automatically reload, preload

 TIM_Cmd(TIM2, ENABLE);  

//TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);

}

 

Here only the output of timer 2 channel 3 used by the LCD is listed, and the output frequency is 150kHz. No interrupts are used. How is this frequency obtained? First, TIM2 uses APB1's 36M, and after pre-division, the division factor is (Prescaler+1)=3, and after 3 division, it is 12MHz. The counting cycle is 40, and the count to 40 matches the output compare register, triggering the output flip, and then the counter starts counting again. Therefore, the frequency of the output channel is 12MHz divided by 80, which is 150kHz.

 

2. Serial port configuration

It is not very convenient to input commands by keystrokes. Soon I will have to write a serial communication program to input commands through the serial port and control the development board. It is very convenient to use library functions for serial port configuration.

 

void UART_Config(void){  //Initialize the configuration of the serial port

 

 USART_InitTypeDef USART_InitStructure;

 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;     

 USART1->BRR =0x0271;     

 USART_Cmd(USART1, ENABLE);    

}

Reference address:Analysis and arrangement of Zhilin STM32 program source code 04

Previous article:Getting started with STM32
Next article:Analysis and arrangement of Zhilin STM32 program source code 03

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号