📄 计数器程序.c
字号:
void Init(void)
{
Delay_100ms(5);
Wdog_Enable;
}
/*
*************************************************
* 蜂鸣器发声子程序
*************************************************
*/
void Speake(uchar ucTime) /* 发声延时 */
{
P_SPEAK = 0;
DelayMs(ucTime);
P_SPEAK = 1;
}
/*
*************************************************
* - LED显示数据译码子程序 -
* Data : 2004-3-5
*************************************************
*/
uchar DataToLed(uchar Value)
{
uchar ucLedData;
switch(Value)
{
case '0':
ucLedData = S7LED_0;
break;
case '1':
ucLedData = S7LED_1;
break;
case '2':
ucLedData = S7LED_2;
break;
case '3':
ucLedData = S7LED_3;
break;
case '4':
ucLedData = S7LED_4;
break;
case '5':
ucLedData = S7LED_5;
break;
case '6':
ucLedData = S7LED_6;
break;
case '7':
ucLedData = S7LED_7;
break;
case '8':
ucLedData = S7LED_8;
break;
case '9':
ucLedData = S7LED_9;
break;
default:
ucLedData = S7LED_OFF;
break;
}
return ucLedData;
}
/*
*************************************************
* - 显示数据译码子程序 -
* Data : 2004-10-19
*************************************************
*/
void Display(void)
{
Led1_Control = Count_Data % 10;
P0 = DataToLed(Led1_Control);
P_A1 = 0;
DelayMs(1);
P_A1 = 1;
Led2_Control = ((Count_Data % 100) - (Count_Data % 10)) / 10;
P0 = DataToLed(Led2_Control);
P_A2 = 0;
DelayMs(1);
P_A2 = 1;
Led3_Control = ((Count_Data % 1000) - (Count_Data % 100)) / 100;
P0 = DataToLed(Led3_Control);
P_A3 = 0;
DelayMs(1);
P_A3 = 1;
Led4_Control = ((Count_Data % 10000) - (Count_Data % 1000)) / 1000;
P0 = DataToLed(Led4_Control);
P_A4 = 0;
DelayMs(1);
P_A4 = 1;
Led5_Control = (Count_Data - (Count_Data % 10000)) / 10000;
P0 = DataToLed(Led5_Control);
P_A5 = 0;
DelayMs(1);
P_A5 = 1;
}
/*
*************************************************
* - 扫描按键 -
* Return : 键值,无键则返回V_K_NULL
* Data : 2004-2-20
*************************************************
*/
uchar GetKey(void)
{
uint usTimes;
uchar ucKeyDown;
Wdog_Enable;
P1 = P1 | 0xf0;
DelayMs(1);
ucKeyDown = P1 & 0xf0; /* 读入P1.4 P1.5 P1.6 P1.7,并屏蔽其它位 */
switch(ucKeyDown)
{
case 0x70:
DelayMs(100);
if(0 == P_KEY1)
{
return (V_K1);
}
case 0xb0:
DelayMs(100);
if(0 == P_KEY2)
{
return (V_K2);
}
case 0xd0:
DelayMs(100);
if(0 == P_KEY3)
{
return (V_K3);
}
case 0xe0:
DelayMs(100);
if(0 == P_KEY4)
{
return (V_K4);
}
default:
return (V_K_NULL); /* 无键按下 */
break;
}
Wdog_Enable;
}
/*
*************************************************
* - 处理按键 -
* Input : 键值
* Data : 2004-2-20
*************************************************
*/
void ProcessKey(uchar ucKey)
{
uchar ucLed1;
uchar ucLed2;
uchar ucLed3;
uchar ucLed4;
uchar ucLed5;
ucLed1 = Led1_Control;
ucLed2 = Led2_Control;
ucLed3 = Led3_Control;
ucLed4 = Led4_Control;
ucLed5 = Led5_Control;
ES = 0;
EX1 = 0;
switch (ucKey)
{
case V_K1: /* clear */
Count_Data = 0;
Display();
P_SWITCH = 1;
Speake(30);
while(0 == P_KEY1)
{
Wdog_Enable;
}
break;
case V_K2: /* set */
Set_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Save_Flag();
Count_Data = (ucLed5 * 10000) + (ucLed4 * 1000) + (ucLed3 * 100) + (ucLed2 * 10) + ucLed1;
Display();
Speake(30);
while(0 == P_KEY2)
{
Wdog_Enable;
}
break;
case V_K3: /* move */
switch(Dis_Bit)
{
case 1:
Led1_Control = 0;
Led2_Control = ucLed2;
Led3_Control = ucLed3;
Led4_Control = ucLed4;
Led5_Control = ucLed5;
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
Dis_Bit = 2;
break;
case 2:
Led2_Control = 0;
Led1_Control = ucLed1;
Led3_Control = ucLed3;
Led4_Control = ucLed4;
Led5_Control = ucLed5;
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
Dis_Bit = 3;
break;
case 3:
Led3_Control = 0;
Led2_Control = ucLed2;
Led1_Control = ucLed1;
Led4_Control = ucLed4;
Led5_Control = ucLed5;
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
Dis_Bit = 4;
break;
case 4:
Led4_Control = 0;
Led2_Control = ucLed2;
Led3_Control = ucLed3;
Led1_Control = ucLed1;
Led5_Control = ucLed5;
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
Dis_Bit = 5;
break;
case 5:
Led5_Control = 0;
Led2_Control = ucLed2;
Led3_Control = ucLed3;
Led4_Control = ucLed4;
Led1_Control = ucLed1;
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
Dis_Bit = 1;
break;
}
Speake(30);
while(0 == P_KEY3)
{
Wdog_Enable;
}
break;
case V_K4: /* add */
switch(Dis_Bit)
{
case 2:
if(Led1_Control >= 9)
{
Led1_Control = 0;
}
else
{
Led1_Control = Led1_Control + 1;
}
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
break;
case 3:
if(Led2_Control >= 9)
{
Led2_Control = 0;
}
else
{
Led2_Control = Led2_Control + 1;
}
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
break;
case 4:
if(Led3_Control >= 9)
{
Led3_Control = 0;
}
else
{
Led3_Control = Led3_Control + 1;
}
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
break;
case 5:
if(Led4_Control >= 9)
{
Led4_Control = 0;
}
else
{
Led4_Control = Led4_Control + 1;
}
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
Dis_Bit = 5;
break;
case 1:
if(Led5_Control >= 9)
{
Led5_Control = 0;
}
else
{
Led5_Control = Led5_Control + 1;
}
Count_Data = (Led5_Control * 10000) + (Led4_Control * 1000) + (Led3_Control * 100) + (Led2_Control * 10) + Led1_Control;
Display();
break;
}
Speake(30);
while(0 == P_KEY4)
{
Wdog_Enable;
}
break;
default:
break;
}
EX1 = 1;
ES = 1;
}
/*
*************************************************
* - 串口发送命令 -
* Input: Command: 命令字
* num :发送个数
* Data : 2004-2-21
*************************************************
*/
void Sent_Ack(void)
{
uint usTimes;
uchar ucDataH,ucDataM,ucDataL;
uchar ucNum, aucSendBuf[6];
TI = 0;
ucNum = 0;
ucDataH = (Count_Data - Count_Data % 10000) / 10000;
ucDataM = ((Count_Data % 10000) -(Count_Data % 100)) / 100;
ucDataL = Count_Data % 100;
aucSendBuf[0] = SER_ID;
aucSendBuf[1] = COUNT;
aucSendBuf[2] = ucDataH;
aucSendBuf[3] = ucDataM;
aucSendBuf[4] = ucDataL;
aucSendBuf[5] = SER_END;
while (ucNum < 4)
{
SBUF = aucSendBuf[ucNum];
usTimes = 5000;
while ((0 == TI)&&(0 != usTimes))
{
usTimes--;
Wdog_Enable;
}
if (0 == usTimes)
break;
TI = 0;
ucNum++;
}
}
/*
*************************************************
* - 处理串口消息 -
* Input: *Buff: 输入缓冲
* Data : 2004-2-20
*************************************************
*/
void Serial_ProcessMsg(uchar *Buff)
{
uchar ucCom;
uint ucData;
ucCom = *(Buff + 1);
ucData = (*(Buff + 2) * 10000) + (*(Buff + 3) * 100) + *(Buff + 4);
switch(ucCom)
{
case DELETE:
ProcessKey(V_K1);
break;
case SET:
Set_Data = ucData;
ProcessKey(V_K2);
break;
case MOVE:
ProcessKey(V_K3);
break;
case ADD:
ProcessKey(V_K4);
break;
case COUNT:
Sent_Ack();
default:
break;
}
}
/*
*************************************************
* - 串口中断例程 -
* Data : 2004-2-20
*************************************************
*/
void serial(void) interrupt 4
{
uint usTimes;
uchar ucNum, aucSerialBuf[6];
RI = 0;
ucNum = 0;
aucSerialBuf[ucNum] = SBUF;
if (aucSerialBuf[ucNum] == 0x7e)
{
while (ucNum < 5)
{
usTimes = 0x1000;
while ((0 == RI)&&(0 != usTimes))
{
usTimes--;
Wdog_Enable;
}
if (0 == usTimes)
break;
RI = 0;
ucNum++;
aucSerialBuf[ucNum] = SBUF;
}
}
RI = 0;
if (ucNum == 5)
{
ES = 0;
if((aucSerialBuf[0] == SER_ID) && (aucSerialBuf[5] == SER_END))
{
Serial_ProcessMsg(aucSerialBuf);
}
ES = 1;
}
Wdog_Enable;
}
/*
*************************************************
* - 计数器0中断例程 -
* Data : 2004-10-19
*************************************************
*/
void count(void) interrupt 2
{
DelayMs(2);
if(P_CON == 0)
{
Count_Data = Count_Data + 1;
}
Display();
if(Count_Data == Set_Data)
{
P_SWITCH = 0;
Speake(200);
}
Wdog_Enable;
}
/*
*************************************************
* 保存当前设定
*************************************************
*/
void SaveBackPar(void)
{
P0_Status = P0;
P1_Status = P1;
P2_Status = P2;
SerMem_Write(&F_Status, STATUS, 1);
SerMem_Write(&P0_Status, P0ROM, 1);
SerMem_Write(&P1_Status, P1ROM, 1);
SerMem_Write(&P2_Status, P2ROM, 1);
SerMem_Write(&Par_Change, PAR_FLAG, 1);
Wdog_Enable;
}
/*
*************************************************
* 调用上次保存
*************************************************
*/
void LoadBackPar(void)
{
uchar ucLen[6];
SerMem_Read(&ucLen, P0ROM, 6);
P0_Status = ucLen[0];
P1_Status = ucLen[1];
P2_Status = ucLen[2];
F_Status = ucLen[3];
Volume_Ram = ucLen[4];
Par_Change = ucLen[5];
Wdog_Enable;
}
/*
*************************************************
* 保存外部RAM参数设置
*************************************************
*/
void Save_Flag(void)
{
uchar ucFlags;
uchar ucFlag[3];
ucFlags = 0x55;
SerMem_Write(&ucFlags, A_FLAG1, 1);
ucFlags = 0xaa;
SerMem_Write(&ucFlags, A_FLAG2, 1);
ucFlags = 0x55;
SerMem_Write(&ucFlags, A_FLAG3, 1);
ucFlags = 0xaa;
SerMem_Write(&ucFlags, A_FLAG4, 1);
ucFlags = 0x55;
SerMem_Write(&ucFlags, A_FLAG5, 1);
ucFlags = 0xaa;
SerMem_Write(&ucFlags, A_FLAG6, 1);
ucFlag[2] = (Set_Data - (Set_Data % 10000)) / 10000;
ucFlag[1] = ((Set_Data % 10000) - (Set_Data % 100)) / 100;
ucFlag[0] = Set_Data % 100;
SerMem_Write(ucFlag, A_SETDATA, 3);
}
/*
*************************************************
* 读出保存参数设置
*************************************************
*/
void Flag_LoadCode(void)
{
uchar ucFlag[3];
SerMem_Read(ucFlag, A_SETDATA, 3);
Set_Data = (ucFlag[2] * 10000) + (ucFlag[1] * 100) + ucFlag[0];
Wdog_Enable;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -