当前位置:网站首页>STM32 outputs 1PPS with adjustable phase
STM32 outputs 1PPS with adjustable phase
2022-07-05 08:10:00 【Nanbolwan】
When doing projects such as crystal oscillator punctuality, it is often necessary to generate a phase adjustable 1PPS, I recently encountered such a problem . utilize STM32 Timer of 1S, And then in TIM_IT_UPDATE Output when interrupt 1PPS The rising edge theory is ok , But it is inconvenient to move the phase , Modifying the count value of the timer is a method , But this will introduce a lot of systematic error .
utilize STM32 The output of the timer compares the mode output 1PPS And achieving the purpose of phase adjustment is the best method I think of at present , In fact, the principle of this mechanism is very simple .STM32 There are several registers in the timer of which we often use , They are :
- Prescaler register : Configuring it will divide the input main frequency of the timer . Suppose the dominant frequency of my system is 100M, The reference frequency of my timer is APB1 That is to say 50M, If I write 4, Then divide the frequency of the timer into 10M.
- Preload registers : This register will load the value of this register into the count register when the count register overflows .
- Count register : According to the example value of pre frequency division above , This register every 100ns The register value will be added until it reaches the preload value, and then an overflow interrupt will be generated .
- CCRx: When configured to compare output ,CCRx When the value in the register is equal to the value in the count register TIM_IT_CCRx interrupt .
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Set interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* The clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// initialization TIM3
TIM_TimeBaseStructure.TIM_Period = 1000000-1; // Set the value of the auto reload register cycle for the next update event load activity
TIM_TimeBaseStructure.TIM_Prescaler =72-1; //72 frequency division ,TIM3 = 1Mhz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //1 frequency division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Upcount mode
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// initialization TIM3 Channel2 PWM Pattern
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 1000000/2; //TIM3->CCR1 The initial value is 500000
TIM_OC1Init(TIM3, &TIM_OCInitStructure); // according to T The specified parameter initializes the peripheral TIM4 OC1
TIM_ITConfig(TIM3, TIM_IT_CC1,ENABLE);
TIM_SetCounter(TIM3,0);
The above configuration is the count register of the timer. The count value is from 0 Count to 999999, Because the clock frequency is just right 1M, So it happens to be every 1S The count value is cleared once . We set it to count to 5000000 A comparison output interrupt is generated . This cycle produces a 1PPS. We can adjust TIM3->CCR1 To adjust 1PPS The aspect of , The adjustment step is 1us.
void TIM3_IRQHandler(void) //TIM3 interrupt
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) // Check the specified TIM Whether the interruption occurs or not :TIM Interrupt source
{
// Production here PPS the front
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 ); // eliminate TIMx Interrupt pending bit of :TIM Interrupt source
}
}
边栏推荐
- Soem EtherCAT source code analysis I (data type definition)
- Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
- Volatile of C language
- Programming knowledge -- assembly knowledge
- [tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
- UEFI development learning 2 - running ovmf in QEMU
- Semiconductor devices (I) PN junction
- Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
- Define in and define out
- 研究發現,跨境電商客服系統都有這五點功能!
猜你喜欢
Summary of SIM card circuit knowledge
Talk about the circuit use of TVs tube
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
Design a clock frequency division circuit that can be switched arbitrarily
Improve lighting C program
Solutions to compilation warnings in Quartus II
Connection mode - bridge and net
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Stablq of linked list
随机推荐
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Tailq of linked list
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Design a clock frequency division circuit that can be switched arbitrarily
Baiwen 7-day smart home learning experience of Internet of things
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Take you to understand the working principle of lithium battery protection board
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Summary of SIM card circuit knowledge
Explication de la procédure stockée pour SQL Server
Programming knowledge -- assembly knowledge
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
NTC thermistor application - temperature measurement
Live555 push RTSP audio and video stream summary (I) cross compilation
Some tips for using source insight (solve the problem of selecting all)
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Adaptive filter
How to migrate the device data accessed by the RTSP of the easycvr platform to easynvr?