📄 _main.c
字号:
/* this is the main system _main.c module, all the function is entered here. */
/* specification */
/* the system include the following module: */
/* _main.c
_edit.c
_io.c
_rs485.c */
/* define the include file */
#include <reg52.h>
#include "_main.h"
#include "_io.h"
#include "_rs485.h"
#include "_edit.h"
#include "_predit.h"
#include "_prio.h"
#include "_prrs485.h"
/* Define the module level declarations. */
uchar xdata *g_char_ptr; /* the general pointer to the xdata byte style. */
uint xdata *g_uint_ptr; /* the general pointer to the xdata int style. */
idata uchar g_t2counter; /* T2 overflow times counter */
/* the program begin here. */
/***************************************************************************
NAME: Reset_Watchdog
FUNCTION: This procedure will reste the watchdog.
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void Reset_Watchdog()
{
uchar i;
AD_WDG = 1;
i = 1;
AD_WDG = 0;
i = 1;
}
/***************************************************************************
NAME: Delay_us
FUNCTION: This procedure set the us delay.
INPUTS: delay time value
OUTPUTS: NONE
***************************************************************************/
void Delay_us(uint i)
{
uint m, k;
/* XTALI is 18.432 M Hz. then, TIMER is 0.65us / instruction */
for( m = 0 ; m < i ; m ++ )
{
k = 1; /* 3.25 us */
while ( k > 0 ) /* 5 instruction */
k --;
}
}
/***************************************************************************
NAME: Delay_Set
FUNCTION: This procedure set the delay time
INPUTS: delay time value
OUTPUTS: NONE
***************************************************************************/
void Delay_Set(uint i)
{
uint m, k;
/* XTALI is 18.432 M Hz. then, TIMER is 0.65us / instruction */
for( m = 0 ; m < i ; m ++ )
{
Reset_Watchdog();
k = CN_TIMER; /* 2 ms setting */
while ( k > 0 ) /* 5 instruction */
k --;
}
}
/***************************************************************************
NAME: Timer0_int
FUNCTION: This procedure will handle the Timer0 interrupt.
INPUTS: NONE
OUTPUTS: g_generalbit
***************************************************************************/
void Timer0() interrupt 1 using 1
{
TR0 = 0;
TH0 = g_thvalue;
TL0 = g_tlvalue;
g_generalbit = FALSE;
TR0 = 1; /* start the Timer0. */
}
/***************************************************************************
NAME: Ex0_int
FUNCTION: This procedure will handle the EX0 interrupt.
INPUTS: NONE
OUTPUTS: change the g_onrunflag status.
***************************************************************************/
void Ex0() interrupt 0 using 2
{
uchar d_keyvalue;
uchar xdata *d_char_ptr;
IE0 = 0; /* clear the EX0 interrupt apply_flag. */
EX0 = 0; /* EX0 interrupt disable. */
d_char_ptr = (uchar xdata *)AD_KEYIN; /* point to the key input port. */
d_keyvalue = *d_char_ptr; /* get the key value. */
d_keyvalue = d_keyvalue&0x3F; /* set the high 2 bit as 0. */
if(d_keyvalue==CN_KEYSTOP)
{
g_onrunflag = CN_STOPWORK;
TR2 = 0; /* Timer2 stop. */
g_outsw = CN_MENTON;
d_char_ptr = (uchar xdata *)AD_OUTPUT; /* point to the output port. */
*d_char_ptr = g_outsw; /* the mention voice on. */
g_keypress = TRUE;
TR2 = 1;
}
if(g_onrunflag!=CN_STOPWORK)
{
EX0 = 1; /* EX0 interrupt enable. */
}
}
/***************************************************************************
NAME: Init_Timer2
FUNCTION: This procedure initialize the Timer2
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void Init_Timer2()
{
EA = 0; /* interrupt disable. */
T2CON = 0x00; /* 16bit automatic timer mode. */
RCAP2H = 0x00; /* 43ms hardware timer. */
RCAP2L = 0x00; /* the low byte. */
TH2 = 0x00; /* set the start timer value. */
TL2 = 0x00;
PT2 = 0; /* timer2 low interrupt grade. */
ET2 = 1; /* timer2 inetrrupt enable. */
EA = 1; /* interrupt enable. */
g_t2counter = CN_DELAYSTR;
g_outsw = CN_MENTON;
g_char_ptr = (uchar xdata *)AD_OUTPUT; /* point to the output port. */
*g_char_ptr = g_outsw; /* the mention voice on. */
TR2 = 1; /* Start Timer2. */
}
/***************************************************************************
NAME: Timer2_int
FUNCTION: This procedure will handle the Timer2 interrupt.
INPUTS: NONE
OUTPUTS: ON/OFF the beevoice
***************************************************************************/
void Timer2() interrupt 5 using 1
{
uchar j;
uchar xdata *d_intrptr;
TF2 = 0; /* clear the Timer2 int flag. */
T2IP = 0; /* clear the Timer2 int flag. */
g_t2counter++; /* the next 43ms start. */
if(g_alarmflag==FALSE) /* key mention voice handle. */
{
if(g_t2counter>=CN_DELAYEND)
{
TR2 = 0; /* stop Timer2. */
g_outsw = CN_MENTOFF;
d_intrptr = (uchar xdata *)AD_OUTPUT; /* point to the output port. */
*d_intrptr = g_outsw; /* the bee voice off. */
T2CON = 0x00; /* 16bit automatic timer mode. */
RCAP2H = 0x00; /* 71ms hardware timer. */
RCAP2L = 0x00; /* the low byte. */
TH2 = 0x00; /* set the start timer value. */
TL2 = 0x00;
PT2 = 0; /* timer2 low interrupt grade. */
ET2 = 1; /* timer2 inetrrupt enable. */
TR2 = 0; /* stop Timer2. */
EA = 1;
}
}
else /* the system alarm interrupt handle. */
{
if(g_t2counter>=CN_DELAYEND)
{
if(TRUE==g_lamponflag) /* the lamp is now on. */
{
CN_LAMPOFF; /* the lamp off. */
g_lamponflag = FALSE;
g_outsw = CN_ALARMOFF; /* the alarm off. */
}
else
{
CN_LAMPON; /* the lamp on. */
g_lamponflag = TRUE;
g_outsw = CN_ALARMON; /* the alarm on. */
}
d_intrptr = (uchar xdata *)AD_OUTPUT; /* point to the output port. */
*d_intrptr = g_outsw;
if(g_fin_or_troub==TRUE)
{ g_t2counter = CN_660ms; } /* 660ms delay. */
else
{ g_t2counter = CN_330ms; } /* 330ms delay. */
}
}
}
/***************************************************************************
NAME: Init_System
FUNCTION: This procedure initialize the system
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void Init_System()
{
AD_485TR = CN_RE_EN;/* the MAX487E receive enable. */
Reset_Watchdog();
Delay_Set(CN_20ms);
Init_Io();
Init_Edit();
Init_Timer2();
Init_EX0();
Init_Crystal();
Start_Interface();
Reset_Watchdog();
Delay_Set(CN_1s); /* delay 1s to show the welcome information.*/
Reset_Watchdog();
Init_Crystal();
Reset_Watchdog();
Page0_Show();
Init_Timer0(); /* initialize the Timer0. */
Frontmotor_Movedist(CN_CASTWIRE,CN_MOTFRONT);
g_char_ptr = (uchar xdata *)AD_WFMOTOR;
*g_char_ptr = CN_DBMOTDISA; /* front and back motor disable. */
if(!Cutter_Reset()) /* the cutter sensor damage, stop prcess and alarm on. */
{
Cut_Rst_Failure();
}
else
{
Cutter_Movedist(CN_TESTFORGEAR,CN_CUTCLOSE,CN_CUTACCEL);
if(!Gear_clearence_Get())
{
Cut_Rst_Failure();
}
}
Disable_Timer0();
g_rece_flash = CN_RECE_FON;
}
/***************************************************************************
NAME: De_Verify()
FUNCTION: This procedure verify if the detect system work normally.
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void De_Verify()
{
uchar i;
bit wireflag;
AD_LAMPIN = 1; /* lamp detect enable. */
g_numshow[6] = 0; /* default, the lamp is on. have no wire. */
g_numshow[5] = 0;
for(i=0;i<5;i++)
{
g_numshow[i] = CN_NWHITE;
}
if(AD_LAMPIN==CN_HAVEWIRE)
{
wireflag = TRUE;
for(i=0;i<CN_FILTER;i++)
{
Delay_us(CN_135us);
if(AD_LAMPIN==CN_NOWIRE)
{
wireflag = FALSE;
}
}
if(wireflag==TRUE)
{
CN_LAMPOFF; /* the lamp is off, have wire. */
g_numshow[6] = 1;
g_numshow[5] = 1;
}
}
else
{
wireflag = FALSE;
for(i=0;i<CN_FILTER;i++)
{
Delay_us(CN_135us);
if(AD_LAMPIN==CN_HAVEWIRE)
{
wireflag = TRUE;
}
}
if(wireflag==FALSE)
{
CN_LAMPON; /* the lamp is on, have no wire. */
g_numshow[6] = 0;
g_numshow[5] = 0;
}
}
if(g_pagecnter==2) /* the page 2 and item 5 is the system status position. */
{
Number_Show(5); /* show the check system status. */
}
else
{
Delay_us(CN_650us); /* keep the equal delay with the last module. */
}
}
/***************************************************************************
NAME: Receive_Flash()
FUNCTION: This procedure flash to show receiving in data receive status.
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void Receive_Flash()
{
if(g_rece_flash==CN_RECE_FON)
{
g_rece_flash = CN_RECE_FONOVER;
if(g_systemsta==CN_EDITSTA && g_itemcnter==7)
{
Hzinfo_Show(CN_READY);
}
if(g_systemsta==CN_SELSTA && g_itemcnter==7)
{
Hzinfo_Show(CN_DISAB);
}
}
if(g_rece_flash==CN_RECE_FOFF)
{
g_rece_flash = CN_RECE_FOFFOVER;
if(g_systemsta==CN_EDITSTA && g_itemcnter==7)
{
Hzinfo_Show(CN_DELCLR);
}
if(g_systemsta==CN_SELSTA && g_itemcnter==7)
{
Hzinfo_Show(CN_DISAB);
}
}
}
/*********************************************************************
NAME: main
FUNCTION: The Main contains the main control loop of the system.
The Executive will first initialize the system and then
go into its main processing loop.
INPUTS: NONE
OUTPUTS: NONE
*********************************************************************/
void main ()
{
uchar d_keyget; /* the key value */
Init_System();
for (;;)
{
d_keyget = Get_Key();
Handle_Key(d_keyget);
De_Verify();
Receive_Flash();
Delay_us(CN_10us);
Edit_Refresh();
Reset_Watchdog();
}
}
/**************************************************************************
Edit by:
FRANK 20030910 ShenZhen
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -