找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5690|回复: 0
打印 上一主题 下一主题
收起左侧

NEC 78K0/KE2 脉冲宽度测量

[复制链接]
跳转到指定楼层
楼主
ID:11255 发表于 2009-6-15 10:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

更多资料请访问bbs.getsoon.com.cn


NEC 78K0/KE2 脉冲宽度测量 (16位定时器/计数器00)

程序功能:

采用TM00计数器,通过按键模拟脉冲输入,测量脉冲宽度,并从LED显示出来。
程序运行现象:
上电后按K4按钮模拟脉冲,按下时间长度显示在LED上,LED上面为高位,LED的值每单位为10ms,最多可测量2.5s宽度脉冲。
初始化步骤:
1.关定时器:
TMC00 = 0x0;
2.设置定时器的需要时钟,预分频器设置:
SetBit(PRM00, 0x02);
3.设置捕捉比较控制寄存器。
SetBit(CRC00, 0x07);
4.清中断标志,屏蔽中断。
TMIF000 = 0;
TMMK000 = 1;
5.开定时器,开始计数。
SetBit(TMC00, 0x08);
例程如下:
*使用此程序前请先设置选项字节及仿真代码(minicube2硬件仿真)。
 *使用PM编译环境的请先去掉程序中中文注释部分。

/************************** (C) COPYRIGHT 2008 GETSOON*************************
* File Name   : main.c
* Author    : Tsinming
* Date First Issued  : 08/08/2008
* Description   : Main program body
*******************************************************************************
* History:
* 08/08/2008: V0.1
******************************************************************************/
#include "ke2demo.h"

/* Variable defintion--------------------------------------------------------*/
sreg u32 pulsevalue, ovfcnt;
sreg u8 inputflg;

/* Functions defintion-------------------------------------------------------*/
void init_cpu(void);
void init_para(void);

/*---------------------------------------------------------------------------*/
void main(void)
{
 IMS = 0xCC;
 IXS = 0x00;
 DI();
 init_cpu();
 EI();
 init_para();
 
 while(1)
{
  inputflg <<= 1;
  inputflg.0 = P0.0;
  if((inputflg & 0x03) == 0x02)
  {
   ovfcnt = 0x0;
   OVF00 = 0;
  }
  
  if(OVF00 == 1)
  {
   OVF00 = 0;
   ovfcnt++;
  }
  
  if((inputflg & 0x03) == 0x01)
  {
   pulsevalue = CR000;
   pulsevalue += (ovfcnt << 16);
   pulsevalue = pulsevalue/310;   
   //1 pulsevalue = 10ms
   P2.3 = ~pulsevalue.7;
   P2.2 = ~pulsevalue.6;
   P2.5 = ~pulsevalue.5;
   P2.4 = ~pulsevalue.4;
   P4.3 = ~pulsevalue.3;
   P4.2 = ~pulsevalue.2;
   P4.1 = ~pulsevalue.1;
   P4.0 = ~pulsevalue.0;
  }
 }
}

/******************************************************************************
* Function Name : init_cpu
* Description  : initialization.
* Input   : None
* Output   : None
* Return   : None
******************************************************************************/
void init_cpu(void)
{
 //clock generator, after reset: OSCCTL = 0x00; MCM = 0x00; MOC = 0x80; OSTS = 0x05; PCC = 0x01;
 ClrBit(PCC, 0xFF);   // CPU Clock = Fxp
 
 //16-bit Timer init
 SetBit(PRM00, 0x02);  // 设置定时器时钟Fprs/256
 SetBit(CRC00, 0x07);  // 设置CR000, CR010用作捕捉寄存器。
 TMIF000 = 0;    // 清中断标志位。
 TMMK000 = 1;    // 屏蔽中断。
 SetBit(TMC00, 0x08);  // 打开定时器。
 
 //Port Initialization, PMX(X=1~7,12,14) after reset: 0xFF
 SetBit(PM7, 0x08);   // P7.3 is input for fulse
 SetBit(PU7, 0x08);
SetBit(PM0, 0x01);   // P00 is input for timer
 SetBit(PU0, 0x01);
 ClrBit(PM2, 0x3C);   // P22~P25 is output for LED
 ClrBit(P2, 0x3C);
 ClrBit(PM4, 0x0F);   // P40~P43 is output for LED
 ClrBit(P4, 0x0F);
 ADPC=0x08;
}

/******************************************************************************
* Function Name : init_para
* Description  : initialization the Variable.
* Input   : None
* Output   : None
* Return   : None
******************************************************************************/
void init_para(void)
{
 pulsevalue = 0x0;
 ovfcnt = 0x0;
 inputflg = 0x0;
}

/************************* (C) COPYRIGHT 2008 GETSOON*************************/

 

/************************** (C) COPYRIGHT 2008 GETSOON*************************
* File Name   : ke2demo.h
* Author    : Tsinming
* Date First Issued  : 08/08/2008
* Description   : Header file for all files
*******************************************************************************
* History:
* 08/08/2008: V0.1
******************************************************************************/
#ifndef _KE2DEMO_H
#define _KE2DEMO_H

#pragma sfr
#pragma di
#pragma ei
#pragma NOP
#pragma HALT
#pragma STOP
#pragma asm

/* Exported types -----------------------------------------------------------*/
typedef unsigned long  u32;
typedef unsigned int  u16;
typedef unsigned char  u8;
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

/* Exported macro -----------------------------------------------------------*/
/* clear IO register bit and set IO register bit */
#define ClrBit(Para, ClrBitMap) Para &= ~ClrBitMap
#define SetBit(Para, SetBitMap) Para |= SetBitMap

/* Exported Variable defintion-----------------------------------------------*/
extern sreg u32 ovfcnt;

/* Exported functions -------------------------------------------------------*/
#endif

/************************* (C) COPYRIGHT 2008 GETSOON*************************/

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表