富士通微电子 ( 上海 ) 有限公司应用笔记 MCU-AN-500004-Z-10 F²MC-8FX 家族 8 位微控制器 MB95200H/210H 系列 8/16 位多功能定时器 应用笔记
变更履历 变更履历 日期作者修正记录 2008-03-20 Levi Zhang V1.0, 第一版 本手册共 40 页 1. 本文档记载的产品信息及规格说明如有变动, 恕不预先通知 如需最新产品信息和 / 或规格说明, 联系富士通销售代表或富士通授权分销商 2. 基于本文档记载信息或示意图的使用引起的对著作权 工业产权或第三方的其他权利的侵害, 富士通不承担任何责任 3. 未经富士通明文批准, 不得对本文档的记载内容进行转让 拷贝 4. 本文档所介绍的产品并不旨在以下用途 : 需要极高可靠性的设备, 诸如航空航天装置 海底中继器 核控制系统或维系生命的医用设施 5. 本文档介绍的部分产品可能是 " 外汇及外贸管理法 " 规定的战略物资 ( 或专门技术 ), 出口该产品或其中部分元件前, 应根据该法获得正式批准 版权 2008 富士通微电子 ( 上海 ) 有限公司版权所有 MCU-AN-500004-Z-10 第 2 页
目录 目录 概要. 5 位多功能定时器.. 6 1 概要 2 8/16 位多功能定时器 2.1 主要特性 6 2.2 框图 6 2.3 寄存器 7 2.3.1 8/16 位多功能定时器 00/01 控制状态寄存器 0 (T00CR0/T01CR0). 7 2.3.2 8/16 位多功能定时器 00/01 控制状态寄存器 1 (T00CR1/T01CR1). 7 2.3.3 8/16 位多功能定时器 00/01 定时器模式控制寄存器 (TMCR0).. 7 2.3.4 8/16 位多功能定时器 00/01 数据寄存器 ch.0 (T00DR/T01DR). 7 2.4 工作模式 8 操作实例 9 3 操作实例 3.1 间隔定时器功能的操作 ( 单次模式 ) 9 3.2 间隔定时器功能的操作 ( 连续模式 ). 11 3.3 间隔定时器功能的操作 ( 自由运行模式 ) 13 3.4 PWM 定时器功能的操作 ( 固定周期模式 ). 16 3.5 PWM 定时器功能的操作 ( 可变周期模式 ). 18 3.6 PWC 定时器功能的使用.. 20 3.7 输入捕捉功能的操作. 23 位多功能定时器的使用注意事项 26 4 8/16 位多功能定时器的使用注意事项 5 更多信息 6 附录 更多信息. 27 附录.. 28 6.1 示意图一览 28 6.2 示例表一览 29 6.3 样本程式. 30 6.3.1 工程名称 : 单次.. 30 6.3.2 工程名称 : 连续.. 31 6.3.3 工程名称 : 自由运行. 33 6.3.4 工程名称 : PWM_Fixed.. 35 6.3.5 工程名称 : PWM_Vari. 36 MCU-AN-500004-Z-10 第 3 页
目录 6.3.6 工程名称 : PWC. 37 6.3.7 工程名称 : 捕捉.. 39 MCU-AN-500004-Z-10 第 4 页
第 1 章概要 1 概要 本手册介绍在不同的模式下如何使用 8/16 位多功能定时器及其特性和实例 MCU-AN-500004-Z-10 第 5 页
第 2 章 8/16 位多功能定时器 2 8/16 位多功能定时器 本章介绍 8/16 位多功能定时器的基本功能 2.1 主要特性 8/16 位多功能定时器由两个 8 位计数器构成 两个计数器可用作两个 8 位定时器, 也可串联后 用作一个 16 位的定时器 8/16 位多功能定时器具有以下功能 : 间隔定时器功能 PWM 定时器功能 PWC 定时器功能 ( 脉宽测定 ) 输入捕捉功能 2.2 框图图 2-1 是 8/16 位多功能定时器的内部框图 图 2-1: 8/16 位多功能定时器框图 MCU-AN-500004-Z-10 第 6 页
第 2 章 8/16 位多功能定时器 2.3 寄存器关于寄存器设定的详情, 见 MB95200H/210H 系列硬件手册的第 14 章 2.3.1 8/16 位多功能定时器 00/01 控制状态寄存器 0 (T00CR0/T01CR0) 该寄存器用于选择定时器的工作状态 选择计数时钟 使能 / 禁止 IF 标志中断 寄存器 T00CR0 和 T01CR0 分别对应定时器 00 和定时器 01 Address bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 Initial value 0F92H T01CR0 IFE C2 C1 C0 F3 F2 F1 F0 00000000 B 0F93H T00CR0 R/W R/W R/W R/W R/W R/W R/W R/W 图 2-2: T00CR0/T01CR0 2.3.2 8/16 位多功能定时器 00/01 控制状态寄存器 1 (T00CR1/T01CR1) 这两只寄存器控制中断标志 定时器输出和定时器运行 寄存器 T00CR1 和 T01CR1 分别对应定时器 00 和 01 Address bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 Initial value 0036H T01CR1 STA HO IE IR BF IF SO OE 00000000 B 0037H T00CR1 R/W R/W R/W R(RM1),W R/WX R(RM1),W R/W R/W 图 2-3: T00CR1/T01CR1 2.3.3 8/16 位多功能定时器 00/01 定时器模式控制寄存器 (TMCR0) 该寄存器选择噪声滤波器的功能 8 位或 16 位工作模式 到定时器 00 的输入信号, 并显示定时器输出值 该寄存器可对应定时器 00 和 01 Address bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 Initial value 0F96H TMCR0 TO1 TO0 IIS MOD FE11 FE10 Fe01 FE00 00000000 B R/WX R/WX R/W R/W R/W R/W R/W R/W 图 2-4: TMCR0 2.3.4 8/16 位多功能定时器 00/01 数据寄存器 ch.0 (T00DR/T01DR) 该寄存器用于写入间隔定时器运行或 PWM 定时器运行时的计数上限值, 并读取 PWC 定时器运行或输入捕捉运行时的计数值 寄存器 T00DR 和 T01DR 分别对应定时器 00 和 01 Address bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 Initial value 0F93H T01DR TDR7 TDR6 TDR5 TDR4 TDR3 TDR2 TDR1 TDR0 00000000 B 0F95H T00DR R/W R/W R/W R/W R/W R/W R/W R/W 图 2-5: T00DR/T01DR R/W : 读 / 写 ( 读值与写值相同 ) R/WX : 只读 ( 可读, 写操作无效 ) R (RM1), W : 读 / 写 ( 读值与写值不同, 读 - 修改 - 写类指令读 "1" ) MCU-AN-500004-Z-10 第 7 页
2.4 工作模式 8/16 位多功能定时器 V1.0 第 2 章 8/16 位多功能定时器 七种模式下的 8/16 位多功能定时器的功能如下所示 : 间隔定时器功能 ( 单次模式 ) 间隔定时器功能 ( 连续模式 ) 间隔定时器功能 ( 自由运行模式 ) PWM 定时器功能 ( 固定周期模式 ) PWM 定时器功能 ( 可变周期模式 ) PWC 定时器功能 输入捕捉功能 MCU-AN-500004-Z-10 第 8 页
第 3 章操作实例 3 操作实例 本章介绍 8/16 位多功能定时器的操作实例 本章例解不同模式下 8/16 位多功能定时器的操作 3.1 间隔定时器功能的操作 ( 单次模式 ) 使用间隔定时器功能 ( 单次模式 ) 时, 定时器启动后, 计数器自 "00H" 计数 计数器值与寄存器设定值匹配时, 定时器输出反转 发出中断请求后, 计数器停止计数 图 3-1 介绍 8 位工作模式下间隔定时器功能的操作 图 3-1: 8 位工作模式下间隔定时器功能的操作 ( 定时器 0) 设置步骤示例下列步骤介绍使用间隔定时器功能 ( 单次模式,8 位 ) 时如何设定 8/16 位多功能定时器 1) 设定模式 (T00CR0: [F3..F0] = 0 0 0 0 ) 2) 设定计数器时钟 (T00CR0: [C2..C0] ) 3) 设定计数器值 (T00DR) 4) 必要时设定中断级 (ILR1) 5) 使能定时器输出 (T00CR1: OE = 1) 6) 启动定时器 (T00CR1: STA = 1) 若使能溢出中断, 溢出标志应该在中断服务程序中被清除 (T00CR1: IF = 0) MCU-AN-500004-Z-10 第 9 页
第 3 章操作实例 下例介绍如何在单次模式下设定 8/16 位多功能定时器 0 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for interval timer (One-shot) function */ void InitCompTimer (void) T00DR = 0xFF; // set the count value TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x00; // interval timer in one-shot mode T00CR1 = 0x01; // disable interrupt, enable output /* main routine */ void main (void) InitComTimer(); T00CR1_STA = 1; // start the timer 关于工程 " 单次 ", 见附录的样本程式 MCU-AN-500004-Z-10 第 10 页
3.2 间隔定时器功能的操作 ( 连续模式 ) 8/16 位多功能定时器 V1.0 第 3 章操作实例 使用间隔定时器功能 ( 连续模式 ) 时, 定时器启动后, 计数器自 "00H" 开始计数 计数器值与寄存器设定值匹配时, 定时器输出反转 发出中断请求后, 计数器返回 "00H", 重新计数 重复操作促使定时器输出方波 图 3-2 是 8 位工作模式下间隔定时器功能的操作 设置步骤示例 图 3-2: 间隔定时器功能的操作框图 ( 连续模式 ) 下列步骤介绍在间隔定时器功能 ( 连续模式 ) 下如何设定 8/16 位多功能定时器 1) 设定模式 (T00CR0: [F3..F0] = 0 0 0 1 ) 2) 设定计数器时钟 (T00CR0 : [C2..C0] ) 3) 设定计数器值 (T00DR) 4) 必要时设定中断级 (ILR1) 5) 使能定时器输出 (T00CR1: OE = 1) 6) 启动定时器 (T00CR1: STA = 1) 若溢出中断使能, 溢出标志应该在中断服务程序中被清除 (T00CR1: IF = 0) 下例介绍如何使用中断设定 8/16 位多功能定时器 0 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for interval timer (continuous) function */ void InitCompTimer (void) MCU-AN-500004-Z-10 第 11 页
第 3 章操作实例 T01DR = 0x01; T00DR = 0xFF; TMCR0 = 0x10; T00CR0 = 0x81; T00CR1 = 0xA1; // set the count value (upper 8 bits) // set the count value (lower 8 bits) // 16-bit, no filtering // interval timer in the continuous mode // enable IF flag interrupt // enable the interrupt, enable the output // start the timer /* enter ISR while the counter value is matches the pre-set value */ interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt T00CR1_IF = 0; // clear the flag // interrupt service routine T00CR1_IE = 1; // enable the interrupt /* main routine */ void main (void) InitCompTimer(); while (1); 注 : 相关的中断向量和中断级应在富士通标准模板工程的 vector.c 模块 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /*--------------------------------------------------------------------*/ /* interrupt level setting */ void InitIrqLevels (void) ILR1 = 0xF3; // IRQ5: 8/16-bit timer ch.0 (lower) interrupt void CompTimer (void); // Prototype #pragma intvect CompTimer 5 // 8/16-bit timer ch.0 (lower) 关于工程 " 连续 ", 见附录的样本程式 MCU-AN-500004-Z-10 第 12 页
第 3 章操作实例 3.3 间隔定时器功能的操作 ( 自由运行模式 ) 使用间隔定时器功能 ( 自由运行模式 ) 时, 计数器自 "00H" 起开始计数 计数器值与寄存器设定值匹配时, 定时起输出反转, 发出中断请求 计数器持续计数, 到 "FFH" 后, 返回到 "00H" 重新计数 重复执行该操作, 定时器可输出方波 设置步骤示例 图 3-3: 间隔定时器功能的操作框图 ( 自由运行模式 ) 下面介绍在间隔定时器功能 ( 自由运行模式 ) 下运行时如何设定 8/16 位多功能定时器 1) 设定模式 (T00CR0: [F3..F0] = 0 0 1 0 ) 2) 设定计数器时钟 (T00CR0: [C2..C0] ) 3) 设定计数器值 (T00DR) 4) 必要时设定中断级 (ILR1) 5) 使能定时器输出 (T00CR1: OE = 1) 6) 启动定时器 (T00CR1: STA = 1) 若使能溢出中断, 溢出标志应该在中断服务程序中被清除 (T00CR1: IF = 0) MCU-AN-500004-Z-10 第 13 页
第 3 章操作实例 下例介绍自由运行模式下如何设定 8/16 位多功能定时器 0 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for interval timer (continuous) function */ void InitCompTimer (void) T01DR = 0x01; // set the count value (upper 8 bits) T00DR = 0xFF; // set the count value (lower 8 bits) TMCR0 = 0x10; // 16-bit, no filtering T00CR0 = 0x82; // interval timer in free run mode // enable IF flag interrupt T00CR1 = 0xA1; // enable the interrupt, enable output // start the timer /* enter ISR while counter value matches the pre-set value */ interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt T00CR1_IF = 0; // clear the flag // interrupt service routine T00CR1_IE = 1; // enable the interrupt /* main routine */ void main (void) InitCompTimer(); while (1); MCU-AN-500004-Z-10 第 14 页
第 3 章操作实例 注 : 相关的中断向量和中断级应在富士通标准模板工程的 vector.c 模块 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /*--------------------------------------------------------------------*/ /* interrupt level setting */ void InitIrqLevels (void) ILR1 = 0xF3; // IRQ5: 8/16-bit timer ch.0 (lower) interrupt void CompTimer (void); // Prototype #pragma intvect CompTimer 5 // 8/16-bit timer ch.0 (lower) 关于工程 " 自由运行 ", 见附录的样本程式 MCU-AN-500004-Z-10 第 15 页
第 3 章操作实例 3.4 PWM 定时器功能的操作 ( 固定周期模式 ) 使用 PWM 定时器功能 ( 固定周期模式 ) 时, 固定周期模式下可产生可变 "H" 脉宽 PWM 信号 8 位工作模式下, 周期固定到 "FFH",16 位工作模式下, 周期固定到 "FFFFH" 所选的计数时钟决定时间 设定相应的寄存器可指定 "H" 脉宽 图 3-4: PWM 定时器功能的操作框图 ( 固定周期模式 ) 设置步骤示例下列步骤介绍如何在 PWM 定时器功能 ( 固定周期模式 ) 模式下设定 8/16 位多功能定时器 1) 设定模式 (T00CR0: [F3..F0] = 0 0 1 1 ) 2) 设定计数器时钟 (T00CR0: [C2..C0]) 3) 设定 T00DR 以设定 H 脉宽 4) 启动定时器 (T00CR1: STA = 1) MCU-AN-500004-Z-10 第 16 页
第 3 章操作实例 下例介绍如何在 PWM 定时器功能 ( 固定周期模式 ) 模式下设定 8/16 位多功能定时器 0 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for the PWM function (fixed cycle) */ void InitCompTimer (void) T00DR = 0x40; // set the PWM timer duty TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x03; T00CR1 = 0x01; // PWM timer function (fixed cycle mode) // enable the output /* main routine */ void main (void) InitCompTimer (); T00CR1_STA = 1; //start the timer While(1); 关于工程 "PWM_Fixed", 见附录的样本程式 MCU-AN-500004-Z-10 第 17 页
第 3 章操作实例 3.5 PWM 定时器功能的操作 ( 可变周期模式 ) 使用 PWM 定时器功能 ( 可变周期模式 ) 时, 基于相应寄存器指定的周期和 "L" 脉宽, 两个 8 位计数器可在任何周期和占空下生成 8 位 PWM 信号 该操作模式下, 因使用两个 8 位计数器, 该多功能定时器不可形成 1 个 16 位计数器 设置步骤示例 图 3-5: PWM 定时器功能的操作框图 ( 可变周期模式 ) 下列步骤介绍如何在 PWM 定时器功能 ( 可变周期模式 ) 下设定 8/16 位多功能定时器 1) 设定模式 (T00CR0: [F3..F0] = 0 1 0 0 ) 2) 设定计数器时钟 (T00CR0: [C2..C0]) 3) 设定计数器时钟 (T01CR0: [C2..C0]) 4) 设 T00DR 以设定 L 脉宽 5) 设 T01DR 以设定周期 6) 使能定时器输出 (T00CR1: OE = 1) 7) 启动定时器 (T00CR1: STA = 1) MCU-AN-500004-Z-10 第 18 页
第 3 章操作实例 下列介绍如何在 PWM 定时器功能 ( 可变周期模式 ) 下设定 8/16 位多功能定时器 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for PWM function (variable cycle) */ void InitCompTimer (void) T01DR = 0x80; // counter timer 01 value for duty T00DR = 0x40; // counter timer 00 value for cycle // duty ratio 50% TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x04; // PWM timer function (variable cycle mode) T01CR0 = 0x04; // choose the same count clock as Timer 00 T00CR1 = 0x01; // enable the output /* main routine */ void main (void) InitCompTimer (); T00CR1_STA = 1; //start the timer while(1); 关于工程 "PWM_Vari", 见附录的样本程式 注 : 使用该功能时, 定时器 00 和定时器 01 须使用同一个计数器时钟 MCU-AN-500004-Z-10 第 19 页
第 3 章操作实例 3.6 PWC 定时器功能的使用 使用 PWC 定时器功能时, 可测量外部输入脉冲的宽度和周期 在该工作模式下运行时, 检测到外部输入信号的计数起始沿后, 计数器自 "00H" 开始计数 ; 检测到计数终止沿后, 计数值传输到寄存器并可产生中断 设置步骤示例 图 3-6: PWC 定时器功能的操作框图 (H 脉宽测定示例 ) 下列步骤介绍如何在 PWC 定时器功能 ("H" 脉冲 ) 下设定 8/16 位多功能定时器 1) 设定定时器输出 (DDR0_D04 = 0; AIDRL_AI4 = 1;) 2) 选择输入口 ( SYSC_EC0SL = 1) 3) 设定模式 (T00CR0: [F3..F0] = 0 1 0 1 ) 4) 设定计数器时钟 (T00CR0 : [C2..C0] ) 5) 必要时设定中断级 (ILR1) 6) 使能定时器输出 (T00CR1: OE = 1) 7) 启动定时器 (T00CR1: STA = 1) 8) 测定结束后读取值若使能中断, 可清零 ISR 的标志 (T00CR1: IR = 0) PWC 定时器具有如下五个测定模式 : T00CR0/T01CR0 [F3..F0] Description 0 1 0 1 "H" pulse = rising to falling 0 1 1 0 L" pulse = falling to rising 0 1 1 1 cycle = rising to rising 1 0 0 0 cycle = falling to falling 1 0 0 1 H" pulse = rising to falling; Cycle = rising to rising 表 3-1: T00CR0/T01CR_F3..F0 和 PWM 定时器功能的关系 MCU-AN-500004-Z-10 第 20 页
第 3 章操作实例 下例介绍如何在 PWC 定时器功能下设定 8/16 位多功能定时器 0 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for the PWC function */ void InitCompTimer (void) SYSC_EC0SL = 1; // P04 as EC0 DDR0_P04 = 0; // P04 input AIDRL = 0x10; // disable AN04 input TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x05; T00CR1 = 0xA0; // PWC timer ("H" pulse = from rising to falling) // enable an interrupt, disable the output // start the timer /* enter ISR while measurement completes or overflows */ interrupt void CompTimer (void) T00CR1_IE = 0; // disable an interrupt if (T00CR1_BF) // measurement data present in data register? uchcycle = T00DR; // read value T00CR1_IR = 0; // clear the flag T00CR1_IE = 1; // enable the interrupt /* main routine */ void main (void) InitCompTimer (); // input signal to be measured while(1); MCU-AN-500004-Z-10 第 21 页
第 3 章操作实例 注 : 相关的中断向量和中断级应在富士通标准模板工程的 vector.c 模块 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /*--------------------------------------------------------------------*/ /* interrupt level setting */ void InitIrqLevels (void) ILR1 = 0xF3; // IRQ5: 8/16-bit timer ch.0 (lower) interrupt void CompTimer (void); // Prototype #pragma intvect CompTimer 5 // 8/16-bit timer ch.0 (lower) 关于工程 "PWC", 见附录的样本程式 MCU-AN-500004-Z-10 第 22 页
第 3 章操作实例 3.7 输入捕捉功能的操作使用输入捕捉功能时, 检测到外部输入信号的边沿后, 计数器值存储在寄存器中 设置步骤示例 图 3-7: 输入捕捉功能的操作框图 下列步骤介绍如何在使用输入捕捉功能 ( 上升, 计数器清零 ) 时设定 8/16 位多功能定时器 1) 设定定时器输入 (DDR0_D04 = 0; AIDRL_AI4 = 1; ) 2) 选择输入口 ( SYSC_EC0SL = 1) 3) 设定模式 (T00CR0: [F3..F0] = 1 1 0 1 ) 4) 设定计数器时钟 (T00CR0: [C2..C0] ) 5) 必要时设定中断级 (ILR1) 6) 使能定时器输出 (T00CR1: OE = 1) 7) 启动定时器 (T00CR1: STA = 1) 8) 测定完成后读取值使能中断后, 清零 ISR 的标志 (T00CR1: IR = 0). 输入捕捉具有如下 6 种捕捉模式 : T00CR0/T01CR0 [F3..F0] Description 1 0 1 0 rising, free-run counter 1 0 1 1 falling, free-run counter 1 1 0 0 both edges, free-run counter 1 1 0 1 rising, counter clear 1 1 1 0 falling, counter clear 1 1 1 1 both edges, counter clear 表 3-2: T00CR0/T01CR_F3..F0 和输入捕捉功能的关系一览 MCU-AN-500004-Z-10 第 23 页
第 3 章操作实例 下例介绍如何在输入捕捉功能运行时设定 8/16 位多功能定时器 0 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /* Date: 20080320 Version: 1V0 Author: Levi */ /*--------------------------------------------------------------------*/ /* initial timer for the input capture function */ void InitCompTimer (void) SYSC_EC0SL = 1; // P04 as EC0 DDR0_P04 = 0; // P04 input AIDRL = 0x10; // disable AN04 input TMCR0 = 0x00; T00CR0 = 0x0D; T00CR1 = 0xA0; // 8-bit, no filtering // Input capture (rising, counter clear) // enable the interrupt, disable the output // start the timer /* Enter ISR while capturing the input */ interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt uchcycle = T00DR; // read value T00CR1_IR = 0; T00CR1_IE = 1; // clear the flag // enable the interrupt /* main routine */ void main (void) InitCompTimer (); // input signal to be captured while(1); MCU-AN-500004-Z-10 第 24 页
第 3 章操作实例 注 : 相关的中断向量和中断级应在富士通标准模板工程的 vector.c 模块 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. */ /* FUJITSU MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY */ /* FOR ANY ERRORS OR ELIGIBILITY FOR ANY PURPOSES. */ /* (C) Fujitsu Microelectronics (Shanghai) Co., LTD. */ /*--------------------------------------------------------------------*/ /* interrupt level setting */ void InitIrqLevels (void) ILR1 = 0xF3; // IRQ5: 8/16-bit timer ch.0 (lower) interrupt void CompTimer (void); // Prototype #pragma intvect CompTimer 5 // 8/16-bit timer ch.0 (lower) 关于工程 " 捕捉 ", 见附录的样本程式 MCU-AN-500004-Z-10 第 25 页
第 4 章 8/16 位多功能定时器的使用注意事项 4 8/16 位多功能定时器的使用注意事项 本章是关于 8/16 多功能定时器的使用注意事项 使用工作模式选择位 (T00CR0/T01CR0:F3, F2, F1, F0) 切换定时器功能时, 首先停止定时器的操作 (T00CR1/T01CR1: STA = 0), 然后清零中断标志 (T00CR1/T01CR1: IF, IR) 中断使能位 (T00CR1/T01CR1: IE, T00CR0/T01CR0: IFE) 和缓冲器满载标志 (T00CR1/T01CR1: BF) PWC 功能运行或使用输入捕捉功能时, 即使定时器激活前 (STA = 0), 中断也可发生 所以, 定时器启动前, 使 8/16 位多功能定时器 00/01 数据寄存器 (T00DR/T01DR) 的值无效 MCU-AN-500004-Z-10 第 26 页
第 5 章更多信息 5 更多信息 关于更多的富士通微电子产品信息, 敬请访问以下网址 : MCU-AN-500004-Z-10 第 27 页
第 6 章附录 6 附录 6.1 示意图一览图 2-1: 8/16 位多功能定时器框图. 6 图 2-2: T00CR0/T01CR0.. 7 图 2-3: T00CR1/T01CR1.. 7 图 2-4: TMCR0.. 7 图 2-5: T00DR/T01DR 7 图 3-1: 8 位工作模式下间隔定时器功能的操作 ( 定时器 0). 9 图 3-2: 间隔定时器功能的操作框图 ( 连续模式 ) 11 图 3-3: 间隔定时器功能的操作框图 ( 自由运行模式 ).. 13 图 3-4: PWM 定时器功能的操作框图 ( 固定周期模式 ) 16 图 3-5: PWM 定时器功能的操作框图 ( 可变周期模式 ) 18 图 3-6: PWC 定时器功能的操作框图 (H 脉宽测定示例 ) 20 图 3-7: 输入捕捉功能的操作框图 23 MCU-AN-500004-Z-10 第 28 页
6.2 示例表一览 8/16 位多功能定时器 V1.0 第 6 章附录 表 3-1: T00CR0/T01CR_F3..F0 和 PWM 定时器功能的关系. 20 表 3-2: T00CR0/T01CR_F3..F0 和输入捕捉功能的关系一览 23 MCU-AN-500004-Z-10 第 29 页
第 6 章附录 6.3 样本程式 6.3.1 工程名称 : 单次间隔定时器功能 ( 单次模式 ) main.c #include "mb95200.h" name: InitCompTimer(); function: initial 定时器 for the interval 定时器 (One-shot) function void InitCompTimer (void) T00DR = 0xFF; // set the count value TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x00; // interval timer in one-shot mode T00CR1 = 0x01; // disable the interrupt, enable the output name: main(); function: main loop void main(void) InitCompTimer(); T00CR1_STA = 1; // start the timer MCU-AN-500004-Z-10 第 30 页
第 6 章附录 6.3.2 工程名称 : 连续间隔定时器功能 ( 连续模式 ) main.c #include "mb95200.h" name: InitCompTimer(); function: initial timer for the interval timer (continuous) function void InitCompTimer (void) T01DR = 0x01; // set the count value (upper 8 bits) T00DR = 0xFF; // set the count value (lower 8 bits) TMCR0 = 0x10; // 16-bit, no filtering T00CR0 = 0x81; // interval timer in continuous mode // enable the IF flag interrupt T00CR1 = 0xA1; // enable the interrupt, enable the output // start the timer name: CompTimer (); function: enter ISR while the counter value matches the pre-set value interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt T00CR1_IF = 0; // clear the flag // // interrupt service routine T00CR1_IE = 1; // enable the interrupt name: main (); function: main loop void main (void) InitCompTimer(); MCU-AN-500004-Z-10 第 31 页
第 6 章附录 InitIrqLevels(); EI(); while (1); vector.c #include "mb95200.h" name: InitIrqLevels (); function: Interrupt level (priority) setting void InitIrqLevels(void) ILR1 = 0xF3; // IRQ4: UART/SIO ch0 // IRQ5: 8/16-bit timer ch.0 (lower) // IRQ6: 8/16-bit timer ch.0 (upper) // IRQ7: LIN-UART (reception) Prototypes interrupt void CompTimer (void); Vector definition #pragma intvect CompTimer 5 // IRQ5: 8/16-bit timer ch.0 (lower) MCU-AN-500004-Z-10 第 32 页
第 6 章附录 6.3.3 工程名称 : 自由运行间隔定时器功能 ( 自由运行模式 ) main.c #include "mb95200.h" name: InitCompTimer(); function: initial timer for the interval timer (Free-run) function void InitCompTimer (void) T01DR = 0x01; // set count value (upper 8 bits) T00DR = 0xFF; // set count value (lower 8 bits) TMCR0 = 0x10; // 16-bit, no filtering T00CR0 = 0x82; // interval timer in a free run mode // enable the IF flag interrupt T00CR1 = 0xA1; // enable the interrupt, enable the output // start the timer name: CompTimer (); function: enter ISR while the counter value matches the pre-set value interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt T00CR1_IF = 0; // clear the flag // // interrupt service routine T00CR1_IE = 1; // enable the interrupt name: main (); function: main loop void main (void) InitCompTimer(); MCU-AN-500004-Z-10 第 33 页
第 6 章附录 InitIrqLevels(); EI(); while (1); vector.c #include "mb95200.h" name: InitIrqLevels (); function: Interrupt level (priority) setting void InitIrqLevels(void) ILR1 = 0xF3; // IRQ4: UART/SIO ch.0 // IRQ5: 8/16-bit timer ch.0 (lower) // IRQ6: 8/16-bit timer ch.0 (upper) // IRQ7: LIN-UART (reception) Prototypes interrupt void CompTimer (void); Vector definition #pragma intvect CompTimer 5 // IRQ5: 8/16-bit timer ch0 (lower) MCU-AN-500004-Z-10 第 34 页
第 6 章附录 6.3.4 工程名称 :PWM_Fixed PWM 定时器功能 ( 固定周期模式 ) main.c #include mb95200.h name: InitCompTimer(); function: initial timer for the PWM (fixed-cycle) function void InitCompTimer (void) T00DR = 0x40; // set the PWM timer duty TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x03; // PWM timer function (fixed cycle mode) T00CR1 = 0x01; // enable the output name: main (); function: main loop void main (void) InitCompTimer(); T00CR1_STA = 1; // start the timer while(1); MCU-AN-500004-Z-10 第 35 页
第 6 章附录 6.3.5 工程名称 :PWM_Vari PWM 定时器功能 ( 可变周期模式 ) main.c #include "mb95200.h" name: InitCompTimer(); function: initial timer for PWM (variable cycle) function void InitCompTimer (void) T01DR = 0x80; // set the PWM timer cycle T00DR = 0x40; // set the PWM timer duty // duty ratio 50% TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x04; // PWM timer function (variable cycle mode) T01CR0 = 0x04; // choose the same count clock as Timer 00 T00CR1 = 0x01; // enable the output name: main (); function: main loop void main (void) InitCompTimer(); T00CR1_STA = 1; // start the timer while(1); MCU-AN-500004-Z-10 第 36 页
第 6 章附录 6.3.6 工程名称 :PWC PWC 定时器功能 main.c #include "mb95200.h" unsigned char uchcycle = 0xFF; name: InitCompTimer(); function: initial timer for the PWC function void InitCompTimer (void) SYSC_EC0SL = 1; // P04 as EC0 DDR0_P04 = 0; // P04 input AIDRL = 0x10; // disable AN04 input TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x05; // PWC timer ("H" pulse = from rising to falling) T00CR1 = 0xA0; // enable the interrupt, disable the output // start the timer name: CompTimer (); function: enter ISR while the counter value matches the pre-set value interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt if (T00CR1_BF) // measurement data present in a data register? uchcycle = T00DR; // read the value T00CR1_IR = 0; // clear the flag T00CR1_IE = 1; // enable the interrupt name: main (); function: main loop MCU-AN-500004-Z-10 第 37 页
void main (void) vector.c InitCompTimer(); InitIrqLevels(); EI(); while (1); #include "mb95200.h" 8/16 位多功能定时器 V1.0 第 6 章附录 // input signal to be measured name: InitIrqLevels (); function: Interrupt level (priority) setting void InitIrqLevels(void) ILR1 = 0xF3; // IRQ4: UART/SIO ch.0 // IRQ5: 8/16-bit timer ch.0 (lower) // IRQ6: 8/16-bit timer ch.0 (upper) // IRQ7: LIN-UART (reception) Prototypes interrupt void CompTimer (void); Vector definition #pragma intvect CompTimer 5 // IRQ5: 8/16-bit timer ch.0 (lower) MCU-AN-500004-Z-10 第 38 页
第 6 章附录 6.3.7 工程名称 : 捕捉输入捕捉功能 main.c #include "mb95200.h" unsigned char uchcycle = 0xFF; name: InitCompTimer(); function: initial timer for the capture input function void InitCompTimer (void) SYSC_EC0SL = 1; // P04 as EC0 DDR0_P04 = 0; // P04 input AIDRL = 0x10; // disable AN04 input TMCR0 = 0x00; // 8-bit, no filtering T00CR0 = 0x0D; // Input capture (rising, counter clear) T00CR1 = 0xA0; // enable the interrupt, disable the output // start the timer name: CompTimer (); function: enter ISR while capturing the input interrupt void CompTimer (void) T00CR1_IE = 0; // disable the interrupt uchcycle = T00DR; // read the value T00CR1_IR = 0; // clear the flag T00CR1_IE = 1; // enable the interrupt name: main (); function: main loop void main (void) MCU-AN-500004-Z-10 第 39 页
第 6 章附录 InitCompTimer(); InitIrqLevels(); EI(); // input signal to be captured while (1); vector.c #include "mb95200.h" name: InitIrqLevels (); function: Interrupt level (priority) setting void InitIrqLevels(void) ILR1 = 0xF3; // IRQ4: UART/SIO ch0 // IRQ5: 8/16-bit timer ch0 (lower) // IRQ6: 8/16-bit timer ch0 (upper) // IRQ7: LIN-UART (reception) Prototypes interrupt void CompTimer (void); Vector definition #pragma intvect CompTimer 5 // IRQ5: 8/16-bit timer ch0 (lower) MCU-AN-500004-Z-10 第 40 页