STM8S timer TIM4 timing 1kHz output

Publisher:明月昊天Latest update time:2019-12-24 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

insert image description here

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.

Keywords:STM8S Reference address:STM8S timer TIM4 timing 1kHz output

Previous article:STM8L ADC
Next article:STM8L051 buzzer beep--library function version

Recommended ReadingLatest update time:2024-11-16 08:40

【STM8S】 Window watchdog
The .h file is as follows:     #ifndef __WWDG_H #define __WWDG_H #include "stm8s.h" void Delay();  void WWDG_Configuration(void) ;   void Refresh_WWDG_Window(void); #endif The .c file is as follows:   #include "wwdg.h" #include "stm8s_wwdg.h"   #define CounterInit 0x7f #define window      0x77   void Delay() //
[Microcontroller]
PIC16F877A microcontroller (combined use of external interrupt and timer Timer0)
1 Implementation Principle See the previous content of timer 0 and external interrupt 2 Implementation circuit diagram 3 Source code /*----------------Function function:   Application of Timer 0+External Interrupt Function 1: LED0 keeps flashing; Function 2: When no button is pressed, LED1 is off. When a button
[Microcontroller]
PIC16F877A microcontroller (combined use of external interrupt and timer Timer0)
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号