📄 timer_rev1_7.c
字号:
// module title: timer_rev1_7.c
// revision: 1.7
// date: 6/1/2004
#include <..\atmel\at89s53.h>
#include <absacc.h>
#include <dom12a.h>
//#define XTAL12MHz
//#define XTAL22_1184MHz
#define XTAL24MHz
#define ACTIVATE 1
#define DEACTIVATE 0
#define Flashing_ON 1
#define Flashing_OFF 0
#define Out_Port XBYTE[0x2000]
#define PanelSW XBYTE[0x2002]
//#define debug
//External Idefinition
extern char output_buf;
sbit Flashing = P1^3; //uP running status
sbit ctrlBuzz = P1^5;
/************************************************
* Local Data : Create software timer *
************************************************/
unsigned int data tmr0, tmr1, tmr2;
unsigned int data Flash_CNT = 250; // flashing at 2Hz
uchar data keySTAGE[8]; //public variables, keys pressed if >= 6
uchar data idxKey;
uchar data durationBuzzON;
/************************************************
* Local Data : Create Bit Addressable Data *
*************************************************/
bit Timer0State =0;
bit Timer1State =0;
bit Timer2State =0;
bit stFlashing = 0;
bit keyDetectEnable = 1;
/************************************
* Local Function Prototype *
************************************/
void Enable_Timer(char timer_name, unsigned int timer_count);
void Disable_Timer(char timer_name);
void Init_Sys_Timer(void);
void Timer_ISR(void);
void msDelay(unsigned int timer_count);
void flashMODE_update(unsigned char modeFlashing, unsigned int Flash_count);
uchar Turn1LEDon(uchar turnon, uchar ledIndex);
uchar STimerStillCounting(uchar idxStimer);
void Mask_Key_Detect(uchar flagClear_nDisable, uchar keyMask);
//**************************************************************************
void Enable_Timer(char timer_name, unsigned int timer_count)
{
EA=0; // Disable 8051 Interrupt
switch (timer_name)
{ case 0 : tmr0 = timer_count;
Timer0State=ACTIVATE;
break;
case 1 : tmr1 = timer_count;
Timer1State=ACTIVATE;
break;
case 2 : tmr2 = timer_count;
Timer2State=ACTIVATE;
break;
}
EA=1; // Enable 8051 Interrupt
}
//**************************************************************************
void Disable_Timer (char timer_name)
{
EA=0; /* Disable 8051 Interrupt */
switch (timer_name)
{ case 0 : Timer0State=DEACTIVATE;
break;
case 1 : Timer1State=DEACTIVATE;
break;
case 2 : Timer2State=DEACTIVATE;
break;
}
EA=1; // Enable 8051 Interrupt
} // end func.
//***********************************************************************************
void Init_Sys_Timer (void)
{
uchar i;
TMOD = 1; // TIMER MODE
#ifdef XTAL12MHz
TH0 = -1000/256; // 1 msec with 12 MHz clock
TL0 = -1000%256;
#endif
#ifdef XTAL22_1184MHz
TH0 = -1843/256; // 1 msec with 12 MHz clock
TL0 = -1843%256;
#endif
#ifdef XTAL24MHz
TH0 = -2000/256; // 1 msec with 12 MHz clock
TL0 = -2000%256;
#endif
tmr0 = tmr1 = tmr2 = 0;
ET0 = 1; // Enable HW Timer0's Interrupt
Enable_Timer(0, Flash_CNT); // Enable Flashing of the running LED
EA = 1; // Enable 8051 Interrupt
TR0 = 1; // Start HW Timer0 to count
keyDetectEnable = 1;
for (i=0; i<8; i++)
keySTAGE[i] = 0;
idxKey = 0;
} // end func.
//***********************************************************************************
void msDelay(unsigned int timer_count)
{
Enable_Timer(2, timer_count); // delay for sometime specified by timer_cnt
while( Timer2State );
Disable_Timer(2);
} // end func.
//**********************************************************************************
uchar code keyMask[8] = {0x01,0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
void Timer_ISR (void) interrupt 1 using 1
{
uchar keyStatus;
#ifdef XTAL12MHz
TH0 = -1000/256; /* 1 msec with 12 MHz clock */
TL0 = -1000%256; /* */
#endif
#ifdef XTAL22_1184MHz
TH0 = -1843/256; // 1 msec with 12 MHz clock
TL0 = -1843%256;
#endif
#ifdef XTAL24MHz
TH0 = -2000/256; // 1 msec with 12 MHz clock
TL0 = -2000%256;
#endif
ET0 = 1; /* Enable HW Timer0's Interrupt */
if (Timer0State)
if ( tmr0 )
tmr0--;
else {
tmr0 = Flash_CNT;
if ( Flashing )
Flashing = 0;
else
Flashing = 1;
} // end else
if (Timer1State) {
if (tmr1)
tmr1--;
else
Timer1State = DEACTIVATE;
} // end
if (Timer2State) {
if (tmr2)
tmr2--;
else
Timer2State = DEACTIVATE;
} // end if
//The following detects totally 8 keys pressed/released status
//Sn-4 Sn-3 Sn-2 Sn-1 Sn(current)
// 0 1 2 3 4 keySTAGE[idxKey]
// 1 0 0 1 1 sensing port of the key
// |
// |--> key pressed confirmed
// with time interval 12ms
if (keyDetectEnable) {
if ( keySTAGE[idxKey] < 4 ) {
keyStatus = keyMask[idxKey] & PanelSW;
switch ( keySTAGE[idxKey] ) {
case 0:if (!keyStatus )
keySTAGE[idxKey] = 1;
break;
case 1:if ( !keyStatus )
keySTAGE[idxKey] = 2;
else
keySTAGE[idxKey] = 0;
break;
case 2:if ( keyStatus )
keySTAGE[idxKey] = 3;
break;
case 3:if ( keyStatus ) {
if ( durationBuzzON < 100 ) {
durationBuzzON = 100;
Out_Port = output_buf |= 0x20; //buzz on
} // end if
keySTAGE[idxKey] = 4;
} // end if
break;
default: keySTAGE[idxKey] = 0;
break;
} // end switch
} // end if
if ( ++idxKey > 7 ) // TOTALLY 6 Keys
idxKey = 0;
} // end if
if ( durationBuzzON ) {
if ( --durationBuzzON)
Out_Port = output_buf &= 0xdf; // BUZZ OFF
} // end if
} // end isr
//************************************************************************************
void flashMODE_update(unsigned char modeFlashing, unsigned int Flash_count)
{// Apply on LED : ledTesting only
// modeFlashing = 1: Flashing at freq. def. by Flash_count
// 0: No flashing, ON/OFF def. by Flash_count
if (modeFlashing) { // Flashing with <Flash count
Disable_Timer(0);
Flash_CNT = Flash_count;
Enable_Timer(0, Flash_CNT);
} else { // not Flashing, LED ON/OFF determines by Flash_count
Disable_Timer(0); // 1 = on, 0 = off
if ( Flash_count ) {
stFlashing = 1;
//valCtrlIO &= 0xfe; //Mask to on
} else {
stFlashing = 0;
//valCtrlIO |= 0x01; //Mask to off
} // end else
} // end else
} // end func.
//*************************************************************************
/*
uchar Turn1LEDon(uchar turnon, uchar ledIndex)
{ // A15=1, A14=0, ctrlIO = PBYTE[0x01]
// LE_374 = A15 & !A14 & A1.q & !WR;
uchar mask = 1 << ledIndex; //led0 => 0x01,... led3 = 0x08
//uchar temp;
if ( ledIndex > 7 )
return ( 0x01 ); //out of range error
if ( turnon ) // turns on the LED by drive low
valCtrlIO &= ~mask;
else
valCtrlIO |= mask; // turns off the LED
A14 = 0; A15 = 1;
ctrlIO = valCtrlIO;
return (0);
} // end func.
*/
//*********************************************************************
uchar STimerStillCounting(uchar idxStimer)
{ //stmr0,stmr1,stmr2;
switch(idxStimer) {
case 0: return ( (uchar) Timer0State);
break;
case 1: return ( (uchar) Timer1State);
break;
case 2: return ( (uchar) Timer2State);
break;
} // end switch
return ( False );
} // end func.
//*****************************************************************
void Mask_Key_Detect(uchar flagClear_nDisable, uchar keyMask)
{
uchar i, keystageval;
uchar mask = 0x01;
if (flagClear_nDisable)
keystageval = 0x00; // clear keystage to start detect
else
keystageval = 0x05; // disable detect
for (i=0; i<8; i++) {
if ( keyMask & mask )
keySTAGE[i] = keystageval;
mask <<= 1;
} // end for
} // end func.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -