The use of the TIMER timer and the timing control program of the STM32f103 digital power acquisition circuit

Publisher:Qinghua2022Latest update time:2017-09-12 Source: eefocusKeywords:STM32f103 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The general purpose timer of STM32 is a 16-bit auto-load counter (CNT) driven by a programmable prescaler (PSC). The general purpose timer of STM32 can be used to measure the pulse length of the input signal (input capture) or generate output waveforms (output comparison and PWM). Using the timer prescaler and the RCC clock controller prescaler, the pulse length and waveform period can be adjusted from a few microseconds to a few milliseconds. Each general purpose timer of STM32 is completely independent and does not share any resources with each other.

The general steps for setting a universal timer can be summarized as follows:

1. Timer clock enable

2. Set the timing parameters

3. Timer working mode initialization

4. Timer interrupt mode enable

5. Enable interrupts and initialize NVIC (this step is only required if interrupts need to be enabled)

6. Enable the timer

7. Write an interrupt handling function

The front-end acquisition module uses the TIM4 timer as the timed transmission of the USART1 serial port with a timing interval of 10ms. The interrupt method is used to enable the DMA channel of USART1 in the interrupt service function, so that USART1 can automatically complete the data transmission task, reduce the CPU workload and greatly reduce the interrupt jump time. At the same time, it is independent of the ADC sampling timing and is not affected by the ADC sampling interval, ensuring the stability of the data interval time.


  1. //General timer interrupt initialization  

  2. //The clock here is selected to be twice that of APB1, and APB1 is 36M  

  3. //arr: automatically reload value.  

  4. //psc: clock pre-division number  

  5. //Timer 4 is used here!  

  6. void TIM4_Int_Init(u16 arr,u16 psc)  

  7. {  

  8.   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  

  9.     NVIC_InitTypeDef NVIC_InitStructure;  

  10.   

  11.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Clock enable  

  12.   

  13.     TIM_TimeBaseStructure.TIM_Period = arr; //Set the value of the auto-reload register period to load the activity at the next update event. Count to 5000 for 500ms  

  14.     TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value used as the TIMx clock frequency divisor 10Khz counting frequency    

  15.     TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Set clock division: TDTS = Tck_tim  

  16.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode  

  17.     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //Initialize the time base unit of TIMx according to the parameters specified in TIM_TimeBaseInitStruct  

  18.    

  19.     TIM_ITConfig( //Enable or disable the specified TIM interrupt  

  20.         TIM4, //TIM4  

  21.         TIM_IT_Update ,  

  22.         ENABLE //Enable  

  23.         );  

  24.     NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4 interrupt  

  25.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preempt priority level 0  

  26.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3  

  27.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled  

  28.     NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct  

  29.   

  30.     TIM_Cmd(TIM4, ENABLE); //Enable TIMx peripherals  

  31.                                

  32. }  




  1. uint8_t HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //Hexadecimal character table  

  2.   

  3. void TIM4_IRQHandler(void) //TIM4 interrupt  

  4. {  

  5.     if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) // Check whether the specified TIM interrupt occurs: TIM interrupt source   

  6.     {  

  7. //Read the data and convert it into the characters to be sent  

  8.         AdcChar[0] = HexTable[(adcValue>>12)&0x0f];  

  9.         AdcChar[1] = HexTable[(adcValue>>8)&0x0f];  

  10.         AdcChar[2] = HexTable[(adcValue>>4)&0x0f];  

  11.         AdcChar[3] = HexTable[(adcValue)&0x0f];  

  12.         // Load data into the serial port send array  

  13.         SendBuff[0] = AdcChar[0];  

  14.         SendBuff[1] = AdcChar[1];  

  15.         SendBuff[2] = AdcChar[2];  

  16.         SendBuff[3] = AdcChar[3];  

  17.         //USB_SendString("Connect to stm32 test the max lenght and more over 22 Byte.");  

  18.         DMA_USART_Enable(DMA1_Channel4);  

  19.     }  

  20.     TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); // Clear TIMx interrupt pending bit: TIM interrupt source   

  21. }  



In order to ensure the stability of the data sampling rate, TIM4 is used here to control the sampling rate. When the TIM4 timing is reached, it immediately enters the interrupt response. In the interrupt function, the array space sampled by the ADC is read and loaded into the USART send data. For details on the ADC sampling configuration, see http://blog.csdn.net/devintt/article/details/46997985

The data message here is sent in binary character form. The communication data message is as follows: (This is the message of the dual-channel ADC, and the message of the single channel takes the first 5 bits)

Message data bits

1

2

3

4

5

6

7

8

9

10

content

P

Data3

Data2

Data1

Data0

Q

Data7

Data6

Data5

Data4

Data significance

ADC1 data identification

ADC1 value hexadecimal character 3rd digit

ADC1 value hexadecimal character second digit

ADC1 value hexadecimal character first digit

ADC1 value hexadecimal character 0th bit

ADC2 data identification

ADC2 value hexadecimal character 3rd digit

ADC2 value hexadecimal character second digit

ADC2 value hexadecimal character first digit

ADC2 value hexadecimal character 0th bit


Note: Data7, Data6, Data5, Data, Data3, Data2, Data1, Data0 are in character format.

Eg: ADC1 data 1024 mV => 0x400

ADC2 data 2048 mV => 0x800

Data message sending: P0400Q0800


Keywords:STM32f103 Reference address:The use of the TIMER timer and the timing control program of the STM32f103 digital power acquisition circuit

Previous article:Design and use of dual ADC in digital power acquisition circuit of STM32f103
Next article:Program for using USART of STM32f103 digital power acquisition circuit and connecting with Bluetooth

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号