1、定时器概述
STM8S提供三种类型的 TIM 定时器:高级控制型(TIM1)、通用型(TIM2/TIM3/TIM5)和基本型定
时器(TIM4/TIM6)。它们虽有不同功能但都基于共同的架构。此共同的架构使得采用各个定时器
来设计应用变得非常容易与方便(相同的寄存器映射,相同的基本功能)。
2、现在来介绍高级定时器1的配置和应用。
代码如下:
main.c
-
/* MAIN.C file
-
*
-
* Copyright (c) 2002-2005 STMicroelectronics
-
*/
-
#include "stm8s.h"
-
#include "stm8s003f3p.h"
-
#include "stm8s_conf.h"
-
-
void GPIOInit(void);
-
void Time1_Init(void);
-
-
-
void main(void)
-
{
-
-
CLK_HSECmd(DISABLE); //关闭外部高速振荡器
-
CLK_HSICmd(ENABLE); //启用内部高速时钟
-
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //设置 内部 时钟16M 为主时钟(默认是8分配2MHz)
-
GPIOInit();
-
Time1_Init();
-
while (1){
-
-
}
-
}
-
-
void GPIOInit(void)
-
{
-
GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
-
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
-
-
}
-
-
void Time1_Init(void)
-
{
-
TIM1_DeInit(); //恢复为默认值
-
TIM1_TimeBaseInit(0x0000, TIM1_COUNTERMODE_UP, 0x3e80, 0x00); //不分频16MHz 计数值16000 1ms中断一次
-
TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE); //手册里有说明,不设置的话就不会产生中断
-
TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE); //配置更新中断
-
TIM1_Cmd(ENABLE); //使能定时器
-
enableInterrupts(); //开启总中断
-
}
-
-
-
@far @interrupt void Timer4_interrupt_handler_t(void) //1ms定时中断
-
{
-
static unsigned int count = 0;
-
static unsigned char falg = 0;
-
-
TIM1_ClearITPendingBit(TIM1_IT_UPDATE); //TIM1_SR = 0,清除中断标志
-
-
if(count<1000){
-
-
GPIO_WriteHigh(GPIOC, GPIO_PIN_4);
-
GPIO_WriteLow(GPIOC, GPIO_PIN_3);
-
}
-
else if(count>=1000)
-
{
-
-
GPIO_WriteHigh(GPIOC, GPIO_PIN_3);
-
GPIO_WriteLow(GPIOC, GPIO_PIN_4);
-
}
-
-
if(count >=2000)count =0;
-
count++;
-
}
然后还有一个很重要的地方,就是在stm8_interrupt_vector.c中注册中断服务函数。
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 */
-
extern @far @interrupt void Timer4_interrupt_handler_t(void);
-
struct interrupt_vector const _vectab[] = {
-
{0x82, (interrupt_handler_t)_stext}, /* reset */
-
{0x82, NonHandledInterrupt}, /* trap */
-
{0x82, NonHandledInterrupt}, /* TLI */
-
{0x82, NonHandledInterrupt}, /* AWU */
-
{0x82, NonHandledInterrupt}, /* CLK */
-
{0x82, NonHandledInterrupt}, /* EXTI0 Port A */
-
{0x82, NonHandledInterrupt}, /* EXTI1 Port B */
-
{0x82, NonHandledInterrupt}, /* EXTI2 Port C */
-
{0x82, NonHandledInterrupt}, /* EXTI3 Port D */
-
{0x82, NonHandledInterrupt}, /* EXTI4 Port E */
-
{0x82, NonHandledInterrupt}, /* CAN RX */
-
{0x82, NonHandledInterrupt}, /* CAN TX */
-
{0x82, NonHandledInterrupt}, /* SPI (End of transfer) */
-
{0x82, (interrupt_handler_t)Timer4_interrupt_handler_t}, /* TIM1 update/ overflow/ underflow/trigger/ break */
-
{0x82, NonHandledInterrupt}, /* TIM1 capture/ compare */
-
{0x82, NonHandledInterrupt}, /* TIM2 update/ overflow */
-
{0x82, NonHandledInterrupt}, /* TIM2 capture/ compare */
-
{0x82, NonHandledInterrupt}, /* TIM3 update/ overflow */
-
{0x82, NonHandledInterrupt}, /* TIM3 capture/ compare */
-
{0x82, NonHandledInterrupt}, /* UART1 Tx complete */
-
{0x82, NonHandledInterrupt}, /* UART1 Receive register DATA FULL */
-
{0x82, NonHandledInterrupt}, /* I2C interrupt */
-
{0x82, NonHandledInterrupt}, /* UART2/3 Tx complete */
-
{0x82, NonHandledInterrupt}, /* UART2/3 Receive register DATA FULL */
-
{0x82, NonHandledInterrupt}, /* ADC1 end of conversion/ analog watchdog interrupt */
-
{0x82, NonHandledInterrupt}, /* TIM4 update/ overflow */
-
{0x82, NonHandledInterrupt}, /* Flash EOP/WR_PG_DIS */
-
{0x82, NonHandledInterrupt}, /* irq25 */
-
{0x82, NonHandledInterrupt}, /* irq26 */
-
{0x82, NonHandledInterrupt}, /* irq27 */
-
{0x82, NonHandledInterrupt}, /* irq28 */
-
{0x82, NonHandledInterrupt}, /* irq29 */
-
};
通过上述的简单设置就可以使用高级定时器1了。
现象是一个LED以1s的间隔在亮灭间转换。
阅读(6622) | 评论(1) | 转发(0) |