历史上的今天
今天是:2025年03月15日(星期六)
2020年03月15日 | STVD TIM4 8位中断定时器
2020-03-15 来源:eefocus
中断定时:
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 */
};
上一篇:STM8定时器1计数说明
下一篇:STM8S的堆和栈说明
史海拾趣
|
基本原则 质量是关键。没有人会对很差的工作感到满足。当完成高质量的工作时,你会为此而感到骄傲。不管你是否知道,你都会因为你的高质量工作而得到信誉。因此,要想为自己所做的事感到骄傲,就需要建立个人标准,并为达到这一标准而努力奋斗。 ...… 查看全部问答> |
|
我现在在用LPC的ARM的USB模块于PC通讯,我想请教各位高手,如果ARM想发送数据给PC,能主动发送一个中断之类的给PC,让PC接受吗?还是只能PC先发命令呢?… 查看全部问答> |
|
atmega16操作u盘读写模块PB375A例程 在很多工程项目中,电子系统会采集或者生成一些数据需要保存。随着U盘的普及,系统中加入数据保存到U盘已经成为一种不错的选择。该例程就是利用 atmega16的单片机操作PB375A u盘读写模块写入一些数据到U盘中 ...… 查看全部问答> |
|
我写了个今日插件,用CWnd::Create创建的窗体,回调函数用AfxWndProc 在wm5下好用,换到wm6下有问题,有些机器上Create失败,有些机器上虽然成功了 窗体被覆盖后收不到WM_PAINT消息,谁遇到过类似问题吗;后来也改用APIC创建窗体, 但是还要在上 ...… 查看全部问答> |
|
我在DSP开发过程遇到问题,如下: 开发平台:TI DM6446 (ARM+DSP)双核系统 DSP程序运行后用fopen(“opt/motionDetect1227/test.map”,”rb”)函数打开读取二进制文件内容,但fopen()打开失败,不知如何解决?目前猜测是文件路径不对,这个路径是AR ...… 查看全部问答> |




