更多資料請訪問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*************************/ |