找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6588|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

NEC 78K0/KE2 脈沖寬度測(cè)量

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:11255 發(fā)表于 2009-6-15 10:49 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式

更多資料請(qǐng)?jiān)L問bbs.getsoon.com.cn


NEC 78K0/KE2 脈沖寬度測(cè)量 (16位定時(shí)器/計(jì)數(shù)器00)

程序功能:

采用TM00計(jì)數(shù)器,通過按鍵模擬脈沖輸入,測(cè)量脈沖寬度,并從LED顯示出來。
程序運(yùn)行現(xiàn)象:
上電后按K4按鈕模擬脈沖,按下時(shí)間長(zhǎng)度顯示在LED上,LED上面為高位,LED的值每單位為10ms,最多可測(cè)量2.5s寬度脈沖。
初始化步驟:
1.關(guān)定時(shí)器:
TMC00 = 0x0;
2.設(shè)置定時(shí)器的需要時(shí)鐘,預(yù)分頻器設(shè)置:
SetBit(PRM00, 0x02);
3.設(shè)置捕捉比較控制寄存器。
SetBit(CRC00, 0x07);
4.清中斷標(biāo)志,屏蔽中斷。
TMIF000 = 0;
TMMK000 = 1;
5.開定時(shí)器,開始計(jì)數(shù)。
SetBit(TMC00, 0x08);
例程如下:
*使用此程序前請(qǐng)先設(shè)置選項(xiàng)字節(jié)及仿真代碼(minicube2硬件仿真)。
 *使用PM編譯環(huán)境的請(qǐng)先去掉程序中中文注釋部分。

/************************** (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);  // 設(shè)置定時(shí)器時(shí)鐘Fprs/256
 SetBit(CRC00, 0x07);  // 設(shè)置CR000, CR010用作捕捉寄存器。
 TMIF000 = 0;    // 清中斷標(biāo)志位。
 TMMK000 = 1;    // 屏蔽中斷。
 SetBit(TMC00, 0x08);  // 打開定時(shí)器。
 
 //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空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表