1. Register development method
1) Taking timer TIM4 as an example, first initialize the relevant registers of TIM4 and enable the interrupt of TIM4. Note that in the development environment of STVD+COSMIC, the statement _asm("rim") is used to enable the general interrupt;
void TIM4_init(void) //TIM4 init
{
TIM4_CR1=0x80;
TIM4_PSCR=0x07; //clock div : 128 (T=8us)
TIM4_ARR=125; //interrupt per 1ms
TIM4_IER=0x01; //enable update interrupt
_asm("rim"); //enable EA
TIM4_EGR=0x01; //update case
TIM4_CR1|=0x01; //enable count
}
2) Next, write the interrupt service routine of TIM4, which can be written directly in main.c. Note that @far @interrupt must be added before the function. The name of the interrupt service function can be chosen by yourself, such as TIM4_Interrupt here;
@far @interrupt void TIM4_Interrupt(void)
{
TIM4_SR&=0xfe;
cnt_num4++;
if(cnt_num4>10)
{
cnt_num4=0;
flag_adc=1;
}
}
3) Next, declare the interrupt function in the stm8_interrupt_vector.c file, and modify the corresponding interrupt service function name in the interrupt vector table to the name we defined, such as TIM4_Interrupt is the 23rd interrupt vector;
extern void _stext(); /* startup routine */
extern @far @interrupt void TIM4_Interrupt(void);
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, TIM4_Interrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
2. Library Function Development Method
1) Taking external interrupt as an example, it can be triggered by a button. First, initialize the corresponding pin, turn on external interrupt and general interrupt, and turn on general interrupt using statement _asm("rim");
void main(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
GPIO_Init(GPIOA,GPIO_PIN_3,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_IN_PU_IT);
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOA,EXTI_SENSITIVITY_FALL_ONLY);
_asm("rim");
/* Infinite loop */
while (1)
{
;
}
}
2) Next, find the interrupt service routine corresponding to the interrupt vector in the stm8s_it.c file and add the code in it;
/**
* @brief External Interrupt PORTA Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
GPIO_WriteReverse(GPIOA,GPIO_PIN_3);
}
Previous article:STM8 Pitfall Avoidance—PB4 and PB5
Next article:Solution to insufficient space on STM8
- Popular Resources
- Popular amplifiers
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
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
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- MSP430 Learning Core-Clock
- 【Silicon Labs BG22-EK4108A Bluetooth Development Review】 II. Progress and Obstacles
- [NUCLEO-L552ZE Review] + Serial Printing Help
- Power supply learning sharing
- Notes and lessons about IAR installation
- [Open Source] Bluetooth module BT401 full set of information based on Bluetooth headset chip development ultra-low cost
- Microcontroller Programming Examples
- Please advise, the voltage of the 5V circuit drops after adding load
- Configuration example of ModbusTCP to Profinet gateway connecting to tightening shaft drive
- What books should Linux newbies read?