1. Switch the main clock to HSE; (external crystal oscillator is 8MkHz)
2. Divide the main clock frequency by 8, that is, the counter clock period is 1us;
3. Set the timer to interrupt once every 250us;
4. In the interrupt program, determine whether the number of interrupts has reached 2;
5. Toggle the pin level twice. (Logic analyzer detects frequency)
Register version:
unsigned char i=255;
PE_DDR_DDR5=1; //Configure PD0 to output mode
PE_CR1_C15=1; //Configure PD0 to push-pull mode
PE_CR2_C25=0; //Configure PD0 to output a maximum of 10Mhz
if(CLK_CMSR==0xE1) {//If HSI is the main clock source (reset value), switch to HSE
CLK_SWCR_SWIEN=1; //Clock switch interrupt enable
CLK_SWR=0xB4; //HSE is the main clock source
asm("rim"); // Enable global interrupt
}
while(CLK_SWCR_SWBSY) //wait for busy flag to reset
{
i--;
if(i==0) {
CLK_SWCR_SWBSY=0; //If the clock switch is not successful, clear the flag SWBSY to reset the current switching operation
break;
}
}
TIM4_CNTR=0; //Initialize counter value
TIM4_ARR=0xFA; //Automatically reload register value 250, 250us
TIM4_PSCR_PSC=0x03; //Pre-scaling factor is 8, 8M/8=1M->1us
TIM4_EGR_UG=1; //Manually generate an update event to update the prescaler immediately
TIM4_IER_UIE=1; //Update interrupt enable
TIM4_CR1_CEN=1; //Enable counter
while(1);
}
#pragma vector=CLK_SWITCH_vector
__interrupt void CLK_IRQHandler(void){
CLK_SWCR_SWIF=0; //Clear interrupt flag
CLK_SWCR_SWEN=1; //Enable clock switching execution
}
unsigned char t4=0;
#pragma vector=TIM4_OVR_UIF_vector
__interrupt void TIM4_IRQHandler(void){
t4++;
if(t4==2)
{
PE_ODR_ODR5=!PE_ODR_ODR5;// take反
t4=0;
}
TIM4_SR_UIF=0;
}
Library function version:
CLK_DeInit(); //Reset clock related registers to default reset values
GPIO_Init(GPIOE, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
enableInterrupts(); // Enable global interrupts
You are in ErrorStatus;
es = CLK_ClockSwitchConfig(CLK_SWITCHMODE_MANUAL, CLK_SOURCE_HSE, ENABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
//Manual switch; New clock source: HSE; Start switch interrupt; Keep the original clock source on
if (es == SUCCESS) //Whether the switch is successful, SUCCESS or ERROR
{
TIM4_TimeBaseInit(TIM4_PRESCALER_8, 0xFA); //Initialize the time base unit, divide by 8, and automatically reload the register value to 250
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); //Enable TIM4 update interrupt
// TIM4_PrescalerConfig(TIM4_PRESCALER_8, TIM4_PSCRELOADMODE_UPDATE);
TIM4_GenerateEvent(TIM4_EVENTSOURCE_UPDATE); //Manually generate update event
TIM4_Cmd(ENABLE); //Start timer
}
else{
CLK_SYSCLKEmergencyClear(); //Clear the clock switching flag
}
/* Infinite loop */
while (1)
Interrupt service routine:
INTERRUPT_HANDLER(CLK_IRQHandler, 2)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
ITStatus flags_status;
flags_tatus = CLK_GetITStatus(CLK_IT_SWIF); //Detect clock switching interrupt flag
if (flags_tatus == SET) //If the clock switching interrupt flag SWIF is set, SET or RESET
{
CLK_ClearITPendingBit(CLK_IT_SWIF); //Clear clock switching interrupt flag
CLK_ClockSwitchCmd(ENABLE); //Start the clock switching function
};
}
unsigned char i=0;
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
i++;
TIM4_ClearFlag(TIM4_FLAG_UPDATE);
if(i==2){
i=0;
GPIO_WriteReverse(GPIOE, GPIO_PIN_5);
}
}
Frequency output:
It is recommended that the counter should not count too few times and that there should not be too many programs in the interrupt service program. Otherwise, the interrupt service program will not be completed and another interrupt will occur, causing incorrect program execution.
Previous article:STM8L ADC
Next article:STM8L051 buzzer beep--library function version
Recommended ReadingLatest update time:2024-11-16 08:40
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [RISC-V MCU CH32V103 Evaluation] +RTC Usage
- Loongson decided to abandon all American technology and make a truly pure domestic instruction set
- Explosion-proof electromagnetic flowmeter integrated converter
- 【15th Anniversary】Want to meet up? Let's DIY an electronic tool box~You decide the functions
- [2022 Digi-Key Innovation Design Competition] Fully Automatic High-Pressure Steam Sterilization Controller - Unboxing
- The DAC output of the MCU is passed through the DAC0832
- What should I do if the current of the small motor is too large when it starts and the microcontroller is reset?
- MSP430 driver function for LCD1602
- Watch the video to win a JD card | Taixiang test of Shuige cheats
- The motor coil is an inductive load, so the current in the coil will have a certain delay relative to the load voltage on the coil.