본문 바로가기

컴퓨터/STM8

STM8 timer interrupt using TIM4

[main.c]

#include "stm8s.h"


uint32_t t=0;


@far @interrupt void TIM4_UPD_OVF_IRQHandler(void)

{

if(++t==1000) t=0;

if(t==0) GPIO_WriteReverse(GPIOD,GPIO_PIN_0);

TIM4_ClearITPendingBit(TIM4_IT_UPDATE);

}


void main() 

{

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //16MHz Setting


GPIO_DeInit(GPIOD);

GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_FAST);


CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER4 , ENABLE); 

TIM4_DeInit();

TIM4_TimeBaseInit(TIM4_PRESCALER_64, 250); //TimerClock = 16000000 / 64 /250 = 1000Hz

TIM4_ClearFlag(TIM4_FLAG_UPDATE);

TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);

enableInterrupts(); // global interrupt enable

TIM4_Cmd(ENABLE);  //Start Timer 4

GPIO_WriteReverse(GPIOD,GPIO_PIN_0);



    do {

    } while(1);   // Endless loop

}


#ifdef USE_FULL_ASSERT


/**

  * @brief  Reports the name of the source file and the source line number

  *   where the assert_param error has occurred.

  * @param file: pointer to the source file name

  * @param line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,

     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */

  while (1)

  {

  }

}

#endif







[stm8_interrupt_vector.c]

/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices

 * Copyright (c) 2007 STMicroelectronics

 */


typedef void @far (*interrupt_handler_t)(void);


struct interrupt_vector {

unsigned char interrupt_instruction;

interrupt_handler_t interrupt_handler;

};


@far @interrupt void NonHandledInterrupt (void)

{

/* in order to detect unexpected events during development, 

  it is recommended to set a breakpoint on the following instruction

*/

return;

}


extern void _stext();     /* startup routine */


@far @interrupt void TIM4_UPD_OVF_IRQHandler(void);


struct interrupt_vector const _vectab[] = {

{0x82, (interrupt_handler_t)_stext}, /* reset */

{0x82, NonHandledInterrupt}, /* trap  */

{0x82, NonHandledInterrupt}, /* irq0  */

{0x82, NonHandledInterrupt}, /* irq1  */

{0x82, NonHandledInterrupt}, /* irq2  */

{0x82, NonHandledInterrupt}, /* irq3  */

{0x82, NonHandledInterrupt}, /* irq4  */

{0x82, NonHandledInterrupt}, /* irq5  */

{0x82, NonHandledInterrupt}, /* irq6  */

{0x82, NonHandledInterrupt}, /* irq7  */

{0x82, NonHandledInterrupt}, /* irq8  */

{0x82, NonHandledInterrupt}, /* irq9  */

{0x82, NonHandledInterrupt}, /* irq10 */

{0x82, NonHandledInterrupt}, /* irq11 */

{0x82, NonHandledInterrupt}, /* irq12 */

{0x82, NonHandledInterrupt}, /* irq13 */

{0x82, NonHandledInterrupt}, /* irq14 */

{0x82, NonHandledInterrupt}, /* irq15 */

{0x82, NonHandledInterrupt}, /* irq16 */

{0x82, NonHandledInterrupt}, /* irq17 */

{0x82, NonHandledInterrupt}, /* irq18 */

{0x82, NonHandledInterrupt}, /* irq19 */

{0x82, NonHandledInterrupt}, /* irq20 */

{0x82, NonHandledInterrupt}, /* irq21 */

{0x82, NonHandledInterrupt}, /* irq22 */

{0x82, (interrupt_handler_t)TIM4_UPD_OVF_IRQHandler}, /* irq23 */

{0x82, NonHandledInterrupt}, /* irq24 */

{0x82, NonHandledInterrupt}, /* irq25 */

{0x82, NonHandledInterrupt}, /* irq26 */

{0x82, NonHandledInterrupt}, /* irq27 */

{0x82, NonHandledInterrupt}, /* irq28 */

{0x82, NonHandledInterrupt}, /* irq29 */

};



'컴퓨터 > STM8' 카테고리의 다른 글

STC1000 WR-032 (STM8S003F3P6)modified firmware  (0) 2016.08.12