中断定时:
1.在main.c中写下中断函数
@far @interrupt void TIM4_UPD_OVF_IRQHandler(void)
{
TIM4_SR=0x00;//清楚更新标志
PD_ODR^=0x0f;
}
添加声明:void TIM4_UPD_OVF_IRQHandler(void);
出现错误“#error cpstm8 main.c:40(48) space attribute conflict“
将“@near @interrupt void TIM4_UPD_OVF_IRQHandler(void)”改为“@far@interrupt void TIM4_UPD_OVF_IRQHandler(void)”
2.在stm8_interrupt_vector.c文件中在“extern void _stext();”下面添加声明:extern void TIM4_UPD_OVF_IRQHandler(void);
更改中断向量表为: {0x82, (interrupt_handler_t)TIM4_UPD_OVF_IRQHandler}, /* irq23 */
在中断向量表中若不添加(interrupt_handler_t),将会出现“#error cpstm8 stm8_interrupt_vector.c:48(8+23) invalid pointer initializer”的错误
3,中断函数既可以放在main.c中也可以放在stm8_interrupt_vector.c中。
/********************************************************************************************************************************************************
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
//中断方式的8位定时器
//周期是30.63ms
#include "stm8s103f.h"
main()
{
//IO口的初始化
PD_DDR=0x0f;
PD_CR1=0x0f;
PD_CR2=0x00;
PD_ODR=0x00;//设置pd0、pd1、pd2、pd3的输出为0
//定时器4的初始化
TIM4_IER = 0x00; // 禁止中断
TIM4_EGR = 0x01; // 允许产生更新事件
TIM4_PSCR = 0x07; // 计数器时钟=主时钟/128=2MHZ/128
// 相当于计数器周期为64us
TIM4_ARR = 255; // 设定重装载时的寄存器值,255是最大值
TIM4_CNTR = 255;// 设定计数器的初值
// 定时周期=(ARR+1)*64=30.63mS
TIM4_CR1 = 0x01;// b0 = 1,允许计数器工作
// b1 = 0,允许更新
// 设置控制器,启动定时器
TIM4_IER = 0x01;// 允许更新中断
_asm("rim"); // 允许CPU全局中断
//进入无限循环
while (1)
{
}
}
/********************************************************************************************************************************************************
stm8_interrupt_vector.c
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
#include "stm8s103f.h"
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 */
void TIM4_UPD_OVF_IRQHandler(void);
//函数功能:定时器4的中断服务程序
//输入参数:无
//输出参数:无
//返回值:无
@near @interrupt void TIM4_UPD_OVF_IRQHandler(void)
{
TIM4_SR=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, 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 */
};
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』