NEC 78K0/KE2 間隔定時器 使用例程 (16位定時器/計數(shù)器00)
程序功能:
采用TM00產(chǎn)生1s中斷。
程序運行現(xiàn)象:
板上共8個發(fā)光二極管(LED0 ~ LED7),每次中斷(1s),LED循環(huán)依次亮滅。
初始化步驟:
1.關(guān)定時器:
TMC00 = 0x0;
2.設(shè)置定時器的需要時鐘,預(yù)分頻器設(shè)置:
SetBit(PRM00, 0x02);
3.設(shè)置比較值:
CR000 = 0x7A11.
4.清中斷標志,開中斷。
TMIF000 = 0;
TMMK000 = 0;
5.開定時器,開始計數(shù)。
SetBit(TMC00, 0x0C);
例程如下:
*使用此程序前請先設(shè)置選項字節(jié)及仿真代碼(minicube2硬件仿真)。
*使用PM編譯環(huán)境的請先去掉程序中中文注釋部分。
/*********************** (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 u8 LedLData;
/* Functions defintion--------------------------------------------------*/
void init_cpu(void);
void init_para(void);
void ledflash(void);
/*----------------------------------------------------------------------------*/
void main(void)
{
IMS = 0xCC;
IXS = 0x00;
DI();
init_cpu();
EI();
init_para();
while(1)
{
ledflash();
}
}
/*************************************************************************
* Function Name : ledflash
* Description : LED driver function.
* Input : None
* Output : None
* Return : None
*************************************************************************/
void ledflash(void)
{
P2.3 = LedLData.7;
P2.2 = LedLData.6;
P2.5 = LedLData.5;
P2.4 = LedLData.4;
P4.3 = LedLData.3;
P4.2 = LedLData.2;
P4.1 = LedLData.1;
P4.0 = LedLData.0;
}
/*************************************************************************
* Function Name : init_cpu
* Description : initialization .
* Input : None
* Output : None
* Return : None
**************************************************************************/
void init_cpu(void)
{
ClrBit(PCC, 0xFF); // CPU Clock = Fxp
CR000 = 0x7A11; //設(shè)置比較值
SetBit(PRM00, 0x02); //設(shè)置定時器的需要時鐘,預(yù)分頻器設(shè)置Fprs/256
TMIF000 = 0; //清中斷標志
TMMK000 = 0; //開中斷
SetBit(TMC00, 0x0C); //開定時器,開始計數(shù)
//Port Initialization, PMX(X=1~7,12,14) after reset: 0xFF
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)
{
LedLData = 0xFE;
}
/*********************** (C) COPYRIGHT 2008 GETSOON*************************/
上海格州電子提供NEC全系列8位,16位,32位,高性價比,ALL FLASH單片機,同時提供解決方案.
公司網(wǎng)址: http://www.getsoon.com.cn
聯(lián)系電話: 021-66303207-6041
專業(yè)的NEC單片機技術(shù)社區(qū): http://www.getsoon.com.cn/bbs/
/************************** (C) COPYRIGHT 2008 GETSOON**************************
* File Name : int.c
* Author : Tsinming
* Date First Issued : 08/08/2008
* Description : interrupt program body
*******************************************************************************
* History:
* 08/08/2008: V0.1
******************************************************************************/
#pragma interrupt INTTM000 timer16int
#include "ke2demo.h"
/* Variable defintion---------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/******************************************************************************
* Function Name : timer16int
* Description : interrupt of 16-bit Timer.
* Input : None
* Output : None
* Return : None
******************************************************************************/
void timer16int(void)
{
LedLData = (LedLData >> 7) | (LedLData << 1);
}
/************************* (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;
/* 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 u8 LedLData;
/* Exported functions ------------------------------------------------------- */
#endif
/************************* (C) COPYRIGHT 2008 GETSOON**************************/
上海格州電子提供NEC全系列8位,16位,32位,高性價比,ALL FLASH單片機,同時提供解決方案.
公司網(wǎng)址: http://www.getsoon.com.cn
聯(lián)系電話: 021-66303207-6041
專業(yè)的NEC單片機技術(shù)社區(qū): http://www.getsoon.com.cn/bbs/
歡迎光臨 (http://www.raoushi.com/bbs/) | Powered by Discuz! X3.1 |