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.
//General timer interrupt initialization
//The clock here is selected to be twice that of APB1, and APB1 is 36M
//arr: automatically reload value.
//psc: clock pre-division number
//Timer 4 is used here!
void TIM4_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Clock enable
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
TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value used as the TIMx clock frequency divisor 10Khz counting frequency
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Set clock division: TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //Initialize the time base unit of TIMx according to the parameters specified in TIM_TimeBaseInitStruct
TIM_ITConfig( //Enable or disable the specified TIM interrupt
TIM4, //TIM4
TIM_IT_Update ,
ENABLE //Enable
);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preempt priority level 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled
NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct
TIM_Cmd(TIM4, ENABLE); //Enable TIMx peripherals
}
uint8_t HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //Hexadecimal character table
void TIM4_IRQHandler(void) //TIM4 interrupt
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) // Check whether the specified TIM interrupt occurs: TIM interrupt source
{
//Read the data and convert it into the characters to be sent
AdcChar[0] = HexTable[(adcValue>>12)&0x0f];
AdcChar[1] = HexTable[(adcValue>>8)&0x0f];
AdcChar[2] = HexTable[(adcValue>>4)&0x0f];
AdcChar[3] = HexTable[(adcValue)&0x0f];
// Load data into the serial port send array
SendBuff[0] = AdcChar[0];
SendBuff[1] = AdcChar[1];
SendBuff[2] = AdcChar[2];
SendBuff[3] = AdcChar[3];
//USB_SendString("Connect to stm32 test the max lenght and more over 22 Byte.");
DMA_USART_Enable(DMA1_Channel4);
}
TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); // Clear TIMx interrupt pending bit: TIM interrupt source
}
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
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Should the external ADC not use the same power supply as the microcontroller?
- Various filters used in circuits,,,
- EEWORLD University Hall ---- IoT Moment: When Intelligence is No Longer Intelligent
- Renovation of old things + I use old forehead thermometer to realize turning over monitoring
- 30 50-yuan Jingdong cards are waiting for you! Prize-winning quiz | Infineon's one-stop BMS solution
- How to determine the similarity of two signal waveforms
- NO7. Evaluation and Prospect of Rapid lot Kit in Smart Home and Smart Grid Scenarios
- MSP430FR6043 Ultrasonic Sensing Evaluation Module
- 【Me and Yatli】+ Upcoming review
- NE555 timer problem