174 lines
4.1 KiB
C
174 lines
4.1 KiB
C
|
#include "main.h"
|
|||
|
/* LED<45><44>ʾ */
|
|||
|
uchar ucLed[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ */
|
|||
|
uchar Seg_Buf[8] = {5, 10, 10, 10, 10, 10, 10, 10}; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ֵ
|
|||
|
uchar Seg_Pos; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָʾ
|
|||
|
uchar Seg_Point[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // ijλ<C4B3>Ƿ<EFBFBD><C7B7><EFBFBD>ʾС<CABE><D0A1><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
/* <20><><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD> */
|
|||
|
uchar Uart_Buf[10]; // <20><><EFBFBD>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
uchar Uart_Rx_Index; // <20><><EFBFBD>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
|
|||
|
/* ʱ<>䷽<EFBFBD><E4B7BD> */
|
|||
|
uint time_all_1s;
|
|||
|
uchar Sys_Tick;
|
|||
|
uchar time_100ms;
|
|||
|
/* <20>ж<EFBFBD> */
|
|||
|
bit Uart_Flag;
|
|||
|
bit Wring_Flag;
|
|||
|
bit Led_Blink_Flag;
|
|||
|
/* <20><>ʾ */
|
|||
|
uchar Seg_show_mode; // 0 <20><><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
|
|||
|
/* <20><><EFBFBD><EFBFBD> */
|
|||
|
uint Noise_Value_10x;
|
|||
|
uchar Noise_Para = 65;
|
|||
|
/* <20><><EFBFBD>̴<EFBFBD><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void Key_Proc()
|
|||
|
{
|
|||
|
static uchar Key_Val, Key_Down, Key_Up, Key_Old;
|
|||
|
if (time_all_1s % 10)
|
|||
|
return;
|
|||
|
Key_Val = Key_Read();
|
|||
|
Key_Down = Key_Val & (Key_Old ^ Key_Val);
|
|||
|
Key_Up = ~Key_Val & (Key_Old ^ Key_Val);
|
|||
|
Key_Old = Key_Val;
|
|||
|
if (Key_Down == 12)
|
|||
|
Seg_show_mode = (++Seg_show_mode) % 2;
|
|||
|
if (Seg_show_mode == 1)
|
|||
|
{
|
|||
|
if (Key_Down == 16)
|
|||
|
Noise_Para = (Noise_Para == 90) ? 0 : Noise_Para + 5;
|
|||
|
else if (Key_Down == 17)
|
|||
|
Noise_Para = (Noise_Para == 0) ? 90 : Noise_Para - 5;
|
|||
|
}
|
|||
|
}
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void Seg_Proc()
|
|||
|
{
|
|||
|
if (time_all_1s % 50)
|
|||
|
return;
|
|||
|
Noise_Value_10x = Ad_Read(0x03) * 180 / 51;
|
|||
|
Wring_Flag = Noise_Value_10x > Noise_Para * 10;
|
|||
|
|
|||
|
Seg_Buf[0] = 11; // U
|
|||
|
Seg_Buf[1] = Seg_show_mode + 1;
|
|||
|
switch (Seg_show_mode)
|
|||
|
{
|
|||
|
case 0:
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>ʾ */
|
|||
|
Seg_Point[6] = 1;
|
|||
|
Seg_Buf[2] = Seg_Buf[3] = Seg_Buf[4] = 10;
|
|||
|
Seg_Buf[5] = (Noise_Value_10x / 100 % 10 == 0) ? 10
|
|||
|
: Noise_Value_10x / 100 % 10;
|
|||
|
Seg_Buf[6] = Noise_Value_10x / 10 % 10;
|
|||
|
Seg_Buf[7] = Noise_Value_10x % 10;
|
|||
|
break;
|
|||
|
|
|||
|
case 1:
|
|||
|
/* <20>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
Seg_Point[6] = 0;
|
|||
|
Seg_Buf[2] = Seg_Buf[3] = Seg_Buf[4] = Seg_Buf[5] = 10;
|
|||
|
Seg_Buf[6] = (Noise_Para / 10 % 10 == 0) ? 10
|
|||
|
: Noise_Para / 10 % 10;
|
|||
|
Seg_Buf[7] = Noise_Para % 10;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* LED<45><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void Led_Proc()
|
|||
|
{
|
|||
|
ucLed[0] = (Seg_show_mode == 0);
|
|||
|
ucLed[1] = (Seg_show_mode == 1);
|
|||
|
ucLed[7] = Led_Blink_Flag;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void Uart_Proc()
|
|||
|
{
|
|||
|
if (Uart_Rx_Index == 0)
|
|||
|
// û<>н<EFBFBD><D0BD>յ<EFBFBD><D5B5>κ<EFBFBD><CEBA><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return;
|
|||
|
if (Sys_Tick >= 10)
|
|||
|
{
|
|||
|
Sys_Tick = Uart_Flag = 0;
|
|||
|
if (Seg_show_mode == 0)
|
|||
|
{
|
|||
|
if (Uart_Buf[0] == 'R' && Uart_Buf[1] == 'e' && Uart_Buf[2] == 't' &&
|
|||
|
Uart_Buf[3] == 'u' && Uart_Buf[4] == 'r' && Uart_Buf[5] == 'n')
|
|||
|
printf("Noises:%0.1fdB", (float)Noise_Value_10x / 10.0);
|
|||
|
}
|
|||
|
memset(Uart_Buf, 0, Uart_Rx_Index);
|
|||
|
Uart_Rx_Index = 0;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ʱ<EFBFBD><CAB1>0<EFBFBD>жϳ<D0B6>ʼ<EFBFBD><CABC> */
|
|||
|
void Timer0_Init(void) // 1<><31><EFBFBD><EFBFBD>@12.000MHz
|
|||
|
{
|
|||
|
AUXR &= 0x7F; // <20><>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1>12Tģʽ
|
|||
|
TMOD &= 0xF0; // <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ģʽ
|
|||
|
TL0 = 0x18; // <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ʼֵ
|
|||
|
TH0 = 0xFC; // <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ʼֵ
|
|||
|
TF0 = 0; // <20><><EFBFBD><EFBFBD>TF0<46><30>־
|
|||
|
TR0 = 1; // <20><>ʱ<EFBFBD><CAB1>0<EFBFBD><30>ʼ<EFBFBD><CABC>ʱ
|
|||
|
ET0 = 1;
|
|||
|
EA = 1;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ʱ<EFBFBD><CAB1>0<EFBFBD>жϺ<D0B6><CFBA><EFBFBD> */
|
|||
|
void Timer0_ISR(void) interrupt 1
|
|||
|
{
|
|||
|
if (++time_all_1s == 1000)
|
|||
|
time_all_1s = 0;
|
|||
|
if (++Seg_Pos == 8)
|
|||
|
Seg_Pos = 0;
|
|||
|
if (Uart_Flag)
|
|||
|
Sys_Tick++;
|
|||
|
if (Wring_Flag)
|
|||
|
{
|
|||
|
if (++time_100ms == 100)
|
|||
|
{
|
|||
|
time_100ms = 0;
|
|||
|
Led_Blink_Flag ^= 1;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
time_100ms = 0;
|
|||
|
Led_Blink_Flag = 0;
|
|||
|
}
|
|||
|
Seg_Disp(Seg_Pos, Seg_Buf[Seg_Pos], Seg_Point[Seg_Pos]);
|
|||
|
Led_Disp(Seg_Pos, ucLed[Seg_Pos]);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void Uart_ISR(void) interrupt 4
|
|||
|
{
|
|||
|
if (RI == 1) // <20><><EFBFBD>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
Uart_Flag = 1;
|
|||
|
Sys_Tick = 0;
|
|||
|
Uart_Buf[Uart_Rx_Index] = SBUF;
|
|||
|
Uart_Rx_Index++;
|
|||
|
RI = 0;
|
|||
|
}
|
|||
|
if (Uart_Rx_Index > 10)
|
|||
|
Uart_Rx_Index = 0;
|
|||
|
}
|
|||
|
void main()
|
|||
|
{
|
|||
|
System_Init();
|
|||
|
Timer0_Init();
|
|||
|
Uart1_Init();
|
|||
|
|
|||
|
while (1)
|
|||
|
{
|
|||
|
Key_Proc();
|
|||
|
Seg_Proc();
|
|||
|
Uart_Proc();
|
|||
|
Led_Proc();
|
|||
|
}
|
|||
|
}
|