STM8s103F3p 16位定时器 STM2
2020-02-13 来源:eefocus
/************************************************************************************
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
//16位定时器2的中断方式
//周期为50ms
#include 'stm8s103f.h'
main()
{
//端口初始化
PD_DDR=0x0f;
PD_CR1=0x0f;
PD_CR2=0x00;
PD_ODR=0x0f;//端口D的前4位输出高电平
//定时器初始化
TIM2_IER=0x00;//禁止中断
TIM2_EGR=0x01;//允许产生更新事件
TIM2_PSCR=0x01;//计数器时钟=主时钟/128=2Mhz/2
//相当于技术周期是1us
TIM2_ARRH=0xC3;//设置重装载时候寄存器的高8位
TIM2_ARRL=0x50;//设置重装载时候寄存器的低8位
//0xc350=50000*1us=50ms
TIM2_CNTRH=0xC3;//设定计数器的初始值高8位
TIM2_CNTRL=0x50;//设定计数器的初始值低8位
TIM2_CR1=0x01;//b0=1;允许计数器工作
//b1=0;允许更新
//设置控制器,启动定时器
TIM2_IER=0x01;//允许更新中断
_asm('rim');//允许全局中断
while (1)
{}
}
/************************************************************************************
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
#include 'stm8s103f.h'
typedef void @far (*interrupt_handler_t)(void);
void TIM2_UPD_IRQHandler(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 */
@near @interrupt void TIM2_UPD_IRQHandler(void)
{
TIM2_SR1=0x00;//清楚更新标志
PD_ODR^=0x0f;
}
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, (interrupt_handler_t)TIM2_UPD_IRQHandler}, /* 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, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};